Ruby对时间的处理函数
1.当前时间
2.生成指定时间
mktime使用当前时区
Time.gm(同义方法是Time.utc)方法基本上和上面的mktime用法相同,但它使用的是GMT或UTC时区
t8 =Time.gm(2001,3,15,21,30,15)
t9 = Time.utc(2001,3,15,21,30,15)
3.时间数组化
to_a可以把时间相关一信息转化成一个数组,数组中存放的信息格式如下:
seconds,
minutes,
hours,
day,
month,
year,
day of week (0..6),
day of year (1..366),
daylight saving (true or false),
and time zone (as a string)
示例:
t0 = Time.local(0,15,3,20,11,1979,2,324,false,"GMT-8:00")
t1 = Time.gm(*Time.now.to_a)
4.使用数字秒数创建时间,其中这个整数代表从1970年开始到当前的秒数,
epoch =Time.at(0)
newmil = Time.at(978307200) # Happy New Millennium! (1 Jan2001)
now =Time.now
sec =now.to_i
5. 日期的格式化
获取本日是星期几:
now = Time.now + 1
day_num = now.wday
day_abbr = now. strftime("%a")
day_full = now. strftime("%A")
获取一年中本日所在的周:
t1 = Time.local(2002,5,1)
1 = Date.new(2002,5,1)
week1a = t1. strftime("%U").to_i
week1b = t1. strftime("%W").to_i
week1c =d1.cweek
获取当前时区:
z1 =Time.gm(2000,11,10,22,5,0).zone
z2 = Time.local(2000,11,10,22,5,0).zone # "中国标准时间"
格式化时间
t = Time.now
puts t. strftime("%H:%M:%S")
puts t. strftime("%H:%M")
puts (t + 30). strftime("%H:%M") #22:08 (加了30秒)
puts t. strftime("%I:%M%p")
strftime方法中可用的格式化符号:
=====================================================
%a
%A
%U
%W
%H
%M
%S
%I
%p
%b
%B
%c
%d
%j
%m
%w
%x
%Y
%y
%Y
%Z
%%