方法是:首先取得客户端的时区,之后得到服务器的本地时区,之后取得两个时区的差值,计算出相应的时间。
输入:服务器时间
输出:相应的客户端对应的时间
上代码:
#时间同步方法
#clint_timezone:client timezone
def formate_date_to_client_time_zone(clint_timezone,time)
diff=(clint_timezone-get_server_time_zone)
unless(clint_timezone.nil?)
time=time+diff*3600
end
return time
end
#获得本地时区
#return the server timezone
def get_server_time_zone
return Time.now.gmtoff/3600
end
Ruby:string to date Or date to string
#string to date
str="2009-02-02"
date=Date.strptime(str,"%Y-%m-%d")
#date to string:<br />date=Date.new(2009,2,2)
str=date.strftime("%Y-%m-%d")
昨天尝试在Spring中使用脚本语言,比如jruby,发现非常不方便,最主要的一点就是ruby class必须继承某一个java的接口,而其他bean调用时也是调用这个接口类型的对象。jruby的动态性基本丧失了。唉!
JRuby wiki上列出了性能优化的四条建议:
1、调优编译器,JRuby早就弃暗投明跟随XRuby走上了编译这条牛B的道路,将Ruby Script编译成字节码,因此这个环节是断断不能忽略的。
两种编译方式:
AOT模式:直接生成class文件,脱了Ruby这层皮,咱就是人见人“爱”的java了。
JIT模式:充分利用成熟的jit技术,咱不全脱,朦胧美才是真的美。默认从0.9.9版本开始就是开启的,关闭的话(要我说还不如全脱)
jruby -J-Djruby.jit.enabled=false
2、关闭ObjectSpace
ObjectSpace是Ruby用来操作所有运行时对象的模块,这个功能相当牛x。这个的实现在c ruby里是比较容易的,但是对于JRuby代价就比较昂贵了,其实就大部分情况下你基本用不到这个东东,那么最好就是关闭它,JRuby提供了
jruby -J-Djruby.objectspace.enabled=false选项来关闭它。
3、开启线程池
我们知道,在c ruby中的线程是绿色的轻量级线程,因此运行时就动不动开个百来十个“线程”跑一跑充下款爷;然而在JRuby中,线程的实现那可是实打实的本地线程(也就是Ruby线程与java线程一比一),你这么动不动上百个线程那不慢才怪了。因此JRuby提供了线程池选项,运行时尽可能地满足你的要求开线程,但是当短命的Ruby线程重复创建的时候,这些线程将被复用,这在大多数情况下能提高性能表现,特别是在每次调用都启动一个线程的情况下。不过具体效果还是要测试的实际数据说话。
jruby -J-Djruby.thread.pooling=true
4、使用Java “server”模式虚拟机,地球淫都知道
jruby -J-server myscript.rb
5、尽量使用最新的jdk,在我的测试中,jdk6跑jruby是效率最高的。
前几天写的一个ruby爬虫,专抓指定网站的Email
require 'net/http'
module EmailSpider
EMAIL=/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/i
#the fullest uri like this
#http://www.sina.com.cn:80/abc?query='123'&page='http://eee.sss.vv:8080'
URL=/http:\/\/\w+(\.\w+)*([:]?\d*)?[\/]?[0-9a-zA-Z\/=\?%#\-\&_~\`@\[\]\':\+!\.]*/i
NON_HTML_URL=/(\.dtd)|(\.css)|(\.js)|(\.pdf)|(\.png)|(\.jpg)|(\.ps)|(\(.*\)[;]?[ ]*)$/i
GOOGLE_URL=/http:\/\/\w+\.google\./i
DOMAIN=/http:\/\/\w+(\.\w+)*([:]?\d*)?[\/]?/i
class EmailSet
attr_accessor :emails,:url
def initialize(emails,url)
@emails,@url=emails,url
end
end
def self.query_by_url(url)
url+="/" if (not url=~/[\/]$/)and(not url=~/(\.htm.?)|(\.jsp)|(\.asp)|(\.php)$/i)
url.strip!
begin
if url=~URL
content=Net::HTTP.get(URI.parse(url.strip));
content.gsub!(/<.?frame .*src="(.+)".*>/i) do |match|
sub_url=match.scan(/src=[\\'](.+)[\\']/i)
if sub_url=~URL and not url=~GOOGLE_URL
query_by_url sub_url
else
query_by_url "#{url}#{sub_url}"
end
end
return content
end
rescue
#puts "missing the page of #{url}"
return ""
end
end
def self.find_email_from_html(html)
emails=[]
html.gsub(EMAIL){|match| emails<<match}
return emails
end
def self.find_url_from_html(html,url=nil)
url+="/" if (not url=~/\/$/)and(not url=~/htm.?/i)
urls=[]
html.gsub(URL) do |match|
match=match.split("\"")[0]
if(not (match=~NON_HTML_URL or match=~GOOGLE_URL))
urls<<match
end
end
html.scan(/<a .*href=[\\']([^<>]+)[\\'][ ]*>/i) do |matchs|
matchs.each do |match|
if (not (match=~URL or match=~NON_HTML_URL or match=~/mailto:/i or match=~/news:/i or match=~GOOGLE_URL))
if match=~/^[\/]/
urls<<"#{url[DOMAIN]}#{match.gsub(/^[\/]/,"")}"
else
urls<<"#{url}#{match}"
end
end
end
end
return urls
end
def self.find_email_in_depth(urls_in_depth,depth,page_num,crawled_urls,max_depth,max_page,&block)
return if urls_in_depth.size<=0
#puts "the depth is #{depth} and the page num is #{urls_in_depth.size}"
crawled_urls_in_this_depth=[]
urls_in_next_depth=[]
urls_in_depth.uniq!
urls_in_depth.each do |url|
if page_num<max_page
unless crawled_urls.include?(url)
html=query_by_url(url)
block.call(find_email_from_html(html).uniq,url)
page_num+=1
crawled_urls_in_this_depth<<url
urls_in_next_depth+=find_url_from_html(html,url)
#puts "the #{url} url_num is #{page_num} and the next depth urls size is #{urls_in_next_depth.size}"
else
urls_in_depth.delete(url)
next
end
else
return
end
end
depth+=1
crawled_urls_in_this_depth+=urls_in_depth
if depth<=max_depth
find_email_in_depth((urls_in_next_depth).uniq,depth,page_num,crawled_urls,max_depth,max_page){|em,url|block.call(em,url)}
else
return
end
end
def self.find_email_online(start_url,max_depth=0,max_pages=10)
result_set=[]
crawled_urls=[]
start_page=query_by_url(start_url)
crawled_urls<<start_url
result_set<<EmailSet.new(find_email_from_html(start_page),start_url)
find_email_in_depth(find_url_from_html(start_page,start_url).uniq,1,2,crawled_urls,max_depth,max_pages) do |em,url|
result_set<<EmailSet.new(em,url) if em.size>0
end if max_depth>0
#print the result
result_set.each do |es|
puts "#{es.emails.join(",")},#{es.url}" if es.emails.size>0
end
end
end
begin
#start_url='http://www-cs-students.stanford.edu/~dbyang/'
start_url='http://www-cs.stanford.edu/People'
EmailSpider.find_email_online(start_url,2,1000)
puts "----------------------------------------"
EmailSpider.find_email_online(start_url,3,1000)
puts "----------------------------------------"
EmailSpider.find_email_online(start_url,4,1000)
puts "----------------------------------------"
EmailSpider.find_email_online(start_url,5,1000)
puts "----------------------------------------"
end
递归删除文件目录:
require 'pathname'
dir=Pathname.new("/home/poole/")
dir.rmtree
#or
require 'fileutils'
FileUtils.rm_r("/home/poole")