About me

说啥呢 欢迎欢迎,热烈欢迎。。

写了一个时间转换的类,简单的时间转换

August 18, 2013

起因:在社区看到一篇问台资企业的帖子,说到了十万小时的编码时间,一下就震惊了,然后想知道十万小时是多少天或是多少月,发现没有时间转化的。就自己写了个类,没敢直接改string和datetime。

#encoding: utf-8
class Inittime
def initialize (data = "10000h" )
if data.to_s.scan(/^\d+[a-zA-Z]$|^\d+\p{Han}$/u).empty?
puts "格式错误,请重新输入"
else
@inittime = data
@@unit = @inittime.scan(/[a-zA-Z]$|\p{Han}$/u)[0].to_s
end
end

def to_year
case @@unit
when "y" , "Y" , "年"
puts (@inittime.scan(/^\d+/u)[0].to_i).to_s + "年"
when "m" , "月"
puts (@inittime.scan(/^\d+/u)[0].to_i/12).to_s + "年"
when "d" , "D" , "日" , "天"
puts (@inittime.scan(/^\d+/u)[0].to_i/12/30).to_s + "年"
when "h" , "H" , "时"
puts (@inittime.scan(/^\d+/u)[0].to_i/12/30/24).to_s + "年"
when "M" , "分"
puts (@inittime.scan(/^\d+/u)[0].to_i/12/30/24/60).to_s + "年"
when "s" , "S" , "秒"
puts (@inittime.scan(/^\d+/u)[0].to_i/12/30/24/60/60).to_s + "年"
else
puts "转化年份出错"
end
end

def to_month
case @@unit
when "y" , "Y" , "年"
(@inittime.scan(/^\d+/u)[0].to_i*12).to_s + "月"
when "m" , "月"
(@inittime.scan(/^\d+/u)[0].to_i).to_s + "月"
when "d" , "D" , "日" , "天"
(@inittime.scan(/^\d+/u)[0].to_i/30).to_s + "月"
when "h" , "H" , "时"
(@inittime.scan(/^\d+/u)[0].to_i/30/24).to_s + "月"
when "M" , "分"
(@inittime.scan(/^\d+/u)[0].to_i/30/24/60).to_s + "月"
when "s" , "S" , "秒"
(@inittime.scan(/^\d+/u)[0].to_i/30/24/60/60).to_s + "月"
else
"转化月份出错"
end
end

def to_day
case @@unit
when "y" , "Y" , "年"
(@inittime.scan(/^\d+/u)[0].to_i*12*30).to_s + "天"
when "m" , "月"
(@inittime.scan(/^\d+/u)[0].to_i*30).to_s + "天"
when "d" , "D" , "日" , "天"
(@inittime.scan(/^\d+/u)[0].to_i).to_s + "天"
when "h" , "H" , "时"
(@inittime.scan(/^\d+/u)[0].to_i/24).to_s + "天"
when "M" , "分"
(@inittime.scan(/^\d+/u)[0].to_i/24/60).to_s + "天"
when "s" , "S" , "秒"
(@inittime.scan(/^\d+/u)[0].to_i/24/60/60).to_s + "天"
else
"转化日子出错"
end
end

def to_hour
case @@unit
when "y" , "Y" , "年"
(@inittime.scan(/^\d+/u)[0].to_i*12*30*24).to_s + "时"
when "m" , "月"
(@inittime.scan(/^\d+/u)[0].to_i*30*24).to_s + "时"
when "d" , "D" , "日" , "天"
(@inittime.scan(/^\d+/u)[0].to_i*24).to_s + "时"
when "h" , "H" , "时"
(@inittime.scan(/^\d+/u)[0].to_i).to_s + "时"
when "M" , "分"
(@inittime.scan(/^\d+/u)[0].to_i/60).to_s + "时"
when "s" , "S" , "秒"
(@inittime.scan(/^\d+/u)[0].to_i/60/60).to_s + "时"
else
"转化小时出错"
end
end

def to_minute
case @@unit
when "y" , "Y" , "年"
(@inittime.scan(/^\d+/u)[0].to_i*12*30*24*60).to_s + "分"
when "m" , "月"
(@inittime.scan(/^\d+/u)[0].to_i*30*24*60).to_s + "分"
when "d" , "D" , "日" , "天"
(@inittime.scan(/^\d+/u)[0].to_i*24*60).to_s + "分"
when "h" , "H" , "时"
(@inittime.scan(/^\d+/u)[0].to_i*60).to_s + "分"
when "M" , "分"
(@inittime.scan(/^\d+/u)[0].to_i).to_s + "分"
when "s" , "S" , "秒"
(@inittime.scan(/^\d+/u)[0].to_i/60).to_s + "分"
else
"转化分钟出错"
end
end

def to_second
case @@unit
when "y" , "Y" , "年"
(@inittime.scan(/^\d+/u)[0].to_i*12*30*24*60*60).to_s + "秒"
when "m" , "月"
(@inittime.scan(/^\d+/u)[0].to_i*30*24*60*60).to_s + "秒"
when "d" , "D" , "日" , "天"
(@inittime.scan(/^\d+/u)[0].to_i*24*60*60).to_s + "秒"
when "h" , "H" , "时"
(@inittime.scan(/^\d+/u)[0].to_i*60*60).to_s + "秒"
when "M" , "分"
(@inittime.scan(/^\d+/u)[0].to_i*60).to_s + "秒"
when "s" , "S" , "秒"
(@inittime.scan(/^\d+/u)[0].to_i).to_s + "秒"
else
"转化秒数出错"
end
end

end

t = Inittime.new("101564h")
puts t.to_month
puts t.to_day
puts t.to_minute
puts t.to_hour
puts t.to_year