English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Time The class in Ruby used to represent dates and times. It is based on the system date and time provided by the operating system. This class may not be able to represent 197Or 0 years before 2038 The date after the year.
This tutorial will familiarize you with all the important concepts of dates and times.
The following is a simple example of getting the current date and time:
#!/usr/bin/ruby -w # -*- coding: UTF-8 -*- time1 = Time.new puts "Current time : " + time1.inspect # Same as Time.now function time2 = Time.now puts "Current time : " + time2.inspect
The output result of the above example is as follows:
Current time : 2015-09-17 15:23:14 +0800 Current time : 2015-09-17 15:23:14 +0800
We can use Time Objects to get various date and time components. See the following examples:
#!/usr/bin/ruby -w # -*- coding: UTF-8 -*- time = Time.new # Components of Time puts "Current time : " + time.inspect puts time.year # => the year of the date puts time.month # => the month of the date (1 to 12) puts time.day # => the day of the month (1 to 31) puts time.wday # => a week of the week (0 is Sunday) puts time.yday # => 365: the day of the year puts time.hour # => 23:24 Hour system puts time.min # => 59 puts time.sec # => 59 puts time.usec # => 999999: microseconds puts time.zone # => "UTC": the name of the time zone
The output result of the above example is as follows:
Current time : 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 date formats, as shown below:
# 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 (the same as UTC) Time.gm(2008, 7, 8, 9, 10, 11)
# The following example retrieves all 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 output result of the above example is as follows:
[39, 25, 15, 17, 9, 2015, 4, 260, false, "CST"]
The array can be passed to Time.utc or Time.local Functions to get different date formats are as follows:
#!/usr/bin/ruby -w time = Time.new values = time.to_a puts Time.utc(*values)
The output result of the above example is as follows:
2015-09-17 15:26:09 UTC
The following is the way to get time, the number of seconds since the epoch (platform related):
# Returns the number of seconds since the epoch time = Time.now.to_i # Converts the number of seconds to a Time object Time.at(time) # Returns the number of seconds since the epoch, including microseconds time = Time.now.to_f
to get all the information about time zones and daylight saving time, as shown below: Time You can use
time = Time.new # Here is the explanation time.zone # => "UTC": Returns the time zone time.utc_offset # => 0: UTC is relative to UTC with a 0 second offset time.zone # => "PST" (or other time zone) time.isdst # => false: If UTC does not have DST (Daylight Saving Time) time.utc? # => true: If in UTC time zone time.localtime # Converts to local time zone time.gmtime # Converts back to UTC time.getlocal # Returns a new Time object in the local time zone time.getutc # Returns a new Time object in UTC
There are many ways to format dates and times. The following examples demonstrate a part 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 output result of the above example is as follows:
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 following table lists the commands and methods Time.strftime Used together.
Command | Description |
---|---|
%a | Abbreviated name of the day of the week (e.g., Sun). |
%A | Full name of the day of the week (e.g., Sunday). |
%b | Abbreviated name of the month (e.g., Jan). |
%B | Full name of the month (e.g., January). |
%c | Preferred local date and time representation. |
%d | Day number in a month (01 to 31) |
%H | Hour number in a day,24 Timekeeping system (00 to 23) |
%I | Hour number in a day,12 Timekeeping system (01 to 12) |
%j | Day number in the year (001 to 366) |
%m | Month number in the year (01 to 12) |
%M | Minute in the hour (00 to 59) |
%p | Meridian indicator (AM or PM). |
%S | Second in the minute (00 or 60). |
%U | Week number in the current year, starting from the first Sunday (as the first day of the first week) (00 to 53) |
%W | Week number in the current year, starting from the first Monday (as the first day of the first week) (00 to 53) |
%w | Which day of the week in a week (Sunday is 0, 0 to 6) |
%x | Preferred representation with only date, without time. |
%X | Preferred representation with only time, without date. |
%y | Year without century representation (00 to 99) |
%Y | Year with century. |
%Z | Time zone name. |
%% | % character. |
You can perform some simple arithmetic with time, as shown below:
now = Time.now # Current time puts now past = now - 10 # 10 seconds before. Time - number => Time puts past future = now + 10 # Start from now 10 seconds later. Time + number => Time puts future diff = future - now # => 10 Time - Time => seconds puts diff
The output result of the above example is as follows:
2015-09-17 15:27:08 +0800 2015-09-17 15:26:58 +0800 2015-09-17 15:27:18 +0800 10.0