May 12, 2021 Ruby
2. Create the current date and time
3. Get the Date and Time component
4. Time.utc, Time.gm, and Time.local functions
The Time class is used in Ruby to represent dates and times. I t is based on the system date and time provided by the operating system. This class may not represent dates before 1970 or after 2038.
This tutorial will familiarize you with all the important concepts of date and time.
Here's a simple example of getting the current date and time:
This results in the following:
Current Time : Mon Jun 02 12:02:39 -0700 2008 Current Time : Mon Jun 02 12:02:39 -0700 2008
We can use time objects to get components of various dates and times. Take a look at the following example:
#!/usr/bin/ruby -w # -*- coding: UTF-8 -*- time = Time.new # Time 的组件 puts "当前时间 : " + time.inspect puts time.year # => 日期的年份 puts time.month # => 日期的月份(1 到 12) puts time.day # => 一个月中的第几天(1 到 31) puts time.wday # => 一周中的星期几(0 是星期日) puts time.yday # => 365:一年中的第几天 puts time.hour # => 23:24 小时制 puts time.min # => 59 puts time.sec # => 59 puts time.usec # => 999999:微秒 puts time.zone # => "UTC":时区名称
This results in the following:
当前时间 : 2015-09-17 15:24:44 +0800 2015 9 17 4 260 15 24 44 921519 CST
These functions can be used to format standard format dates, as follows:
# July 8, 2008 Time.local(2008, 7, 8) # July 8, 2008, 09:10am,本地时间 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 (与 UTC 相同) Time.gm(2008, 7, 8, 9, 10, 11)
The following example gets all the components in the array:
[sec,min,hour,day,month,year,wday,yday,isdst,zone]
Try the following example:
#!/usr/bin/ruby -w time = Time.new values = time.to_a p values
The above instance runs the output as:
[39, 25, 15, 17, 9, 2015, 4, 260, false, "CST"]
The array can be passed to the Time.utc or Time.local function to get the different formats of the date, as follows:
#!/usr/bin/ruby -w time = Time.new values = time.to_a puts Time.utc(*values)
The above instance runs the output as:
2015-09-17 15:26:09 UTC
Here's how to get time, the number of seconds since the era (platform-related):
# 返回从纪元以来的秒数 time = Time.now.to_i # 把秒数转换为 Time 对象 Time.at(time) # 返回从纪元以来的秒数,包含微妙 time = Time.now.to_f
You can use the Time object to get all the information about time zones and daylight saving time, as follows:
time = Time.new # 这里是解释 time.zone # => "UTC":返回时区 time.utc_offset # => 0:UTC 是相对于 UTC 的 0 秒偏移 time.zone # => "PST"(或其他时区) time.isdst # => false:如果 UTC 没有 DST(夏令时) time.utc? # => true:如果在 UTC 时区 time.localtime # 转换为本地时区 time.gmtime # 转换回 UTC time.getlocal # 返回本地区中的一个新的 Time 对象 time.getutc # 返回 UTC 中的一个新的 Time 对象
There are several ways to format dates and times. The following example demonstrates some of them:
#!/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")
The above instance runs the output as:
2015-09-17 15:26:42 +0800 Thu Sep 17 15:26:42 2015 2015-09-17 15:26:42 +0800 2015-09-17 15:26:42
The instructions listed in the following table are used with the method Time.strftime.
指令 | 描述 |
---|---|
%a | 星期几名称的缩写(比如 Sun)。 |
%A | 星期几名称的全称(比如 Sunday)。 |
%b | 月份名称的缩写(比如 Jan)。 |
%B | 月份名称的全称(比如 January)。 |
%c | 优选的本地日期和时间表示法。 |
%d | 一个月中的第几天(01 到 31)。 |
%H | 一天中的第几小时,24 小时制(00 到 23)。 |
%I | 一天中的第几小时,12 小时制(01 到 12)。 |
%j | 一年中的第几天(001 到 366)。 |
%m | 一年中的第几月(01 到 12)。 |
%M | 小时中的第几分钟(00 到 59)。 |
%p | 子午线指示(AM 或 PM)。 |
%S | 分钟中的第几秒(00 或 60)。 |
%U | 当前年中的周数,从第一个星期日(作为第一周的第一天)开始(00 到 53)。 |
%W | 当前年中的周数,从第一个星期一(作为第一周的第一天)开始(00 到 53)。 |
%w | 一星期中的第几天(Sunday 是 0,0 到 6)。 |
%x | 只有日期没有时间的优先表示法。 |
%X | 只有时间没有日期的优先表示法。 |
%y | 不带世纪的年份表示(00 到 99)。 |
%Y | 带有世纪的年份。 |
%Z | 时区名称。 |
%% | % 字符。 |
You can spend time doing some simple arithmetic, as follows:
now = Time.now # 当前时间 puts now past = now - 10 # 10 秒之前。Time - number => Time puts past future = now + 10 # 从现在开始 10 秒之后。Time + number => Time puts future diff = future - now # => 10 Time - Time => 秒数 puts diff
This results in the following:
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