Ruby中处理时间的一些基本操作
程序员文章站
2022-04-28 22:50:07
获取当前日期和时间:
以下是一个简单的例子,以获得当前的日期和时间:
#!/usr/bin/ruby -w
time1 = time.new...
获取当前日期和时间:
以下是一个简单的例子,以获得当前的日期和时间:
#!/usr/bin/ruby -w time1 = time.new puts "current time : " + time1.inspect # time.now is a synonym: time2 = time.now puts "current time : " + time2.inspect
这将产生以下结果:
current time : mon jun 02 12:02:39 -0700 2008 current time : mon jun 02 12:02:39 -0700 2008
获取组件的日期和时间:
我们可以用 time 对象来获取日期和时间的各个组成部分。下面的例子显示相同的:
#!/usr/bin/ruby -w time = time.new # components of a time puts "current time : " + time.inspect puts time.year # => year of the date puts time.month # => month of the date (1 to 12) puts time.day # => day of the date (1 to 31 ) puts time.wday # => 0: day of week: 0 is sunday puts time.yday # => 365: day of year puts time.hour # => 23: 24-hour clock puts time.min # => 59 puts time.sec # => 59 puts time.usec # => 999999: microseconds puts time.zone # => "utc": timezone name
这将产生以下结果:
current time : mon jun 02 12:03:08 -0700 2008 2008 6 2 1 154 12 3 8 247476 utc
time.utc,time.gm和time.local函数:
可用于这两个函数以标准格式来格式化日期如下:
# july 8, 2008 time.local(2008, 7, 8) # july 8, 2008, 09:10am, local time time.local(2008, 7, 8, 9, 10) # july 8, 2008, 09:10 utc time.utc(2008, 7, 8, 9, 10) # july 8, 2008, 09:10:11 gmt (same as utc) time.gm(2008, 7, 8, 9, 10, 11)
下面的例子,在一个数组来获取所有组件按以下格式:
[sec,min,hour,day,month,year,wday,yday,isdst,zone]
尝试以下操作:
#!/usr/bin/ruby -w time = time.new values = time.to_a p values
这将产生以下结果:
[26, 10, 12, 2, 6, 2008, 1, 154, false, "mst"]
这个数组可以传递给time.utc或time.local函数得到不同的日期格式如下:
#!/usr/bin/ruby -w time = time.new values = time.to_a puts time.utc(*values)
这将产生以下结果:
mon jun 02 12:15:36 utc 2008
跟随着的方式来获得内部表示(依赖于平台)历元以来的秒数时间:
# returns number of seconds since epoch time = time.now.to_i # convert number of seconds into time object. time.at(time) # returns second since epoch which includes microseconds time = time.now.to_f
时区和夏令时:
可以使用一个 time 对象来获取相关的所有信息的时区和夏令如下:
time = time.new # here is the interpretation time.zone # => "utc": return the timezone time.utc_offset # => 0: utc is 0 seconds offset from utc time.zone # => "pst" (or whatever your timezone is) time.isdst # => false: if utc does not have dst. time.utc? # => true: if t is in utc time zone time.localtime # convert to local timezone. time.gmtime # convert back to utc. time.getlocal # return a new time object in local zone time.getutc # return a new time object in utc
格式化时间和日期:
有各种各样的方式来格式化日期和时间。下面是一个例子,说明几个:
#!/usr/bin/ruby -w time = time.new puts time.to_s puts time.ctime puts time.localtime puts time.strftime("%y-%m-%d %h:%m:%s")
这将产生以下结果:
mon jun 02 12:35:19 -0700 2008 mon jun 2 12:35:19 2008 mon jun 02 12:35:19 -0700 2008 2008-06-02 12:35:19
时间算术:
可以在时间上做简单的算术如下:
now = time.now # current time puts now past = now - 10 # 10 seconds ago. time - number => time puts past future = now + 10 # 10 seconds from now time + number => time puts future diff = future - now # => 10 time - time => number of seconds puts diff
这将产生以下结果:
thu aug 01 20:57:05 -0700 2013 thu aug 01 20:56:55 -0700 2013 thu aug 01 20:57:15 -0700 2013 10.0