This article mainly introduces some of the basic operations of ruby processing time, mainly using Ruby in the powerful timing module, the need for friends can refer to the
Get the current date and time:
The following is a simple example to get the current date and time:
?
1 2 3 4 5 6 7 8 9 |
#!/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 |
This will produce the following results:
?
1 2 |
Current Time:mon June 12:02:39-0700 2008 current Time:mon June 02 12:02:39-0700 2008 |
Get the date and time of the component:
We can use the time object to get the various parts of the date and times. The following example shows the same:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14-15 16 |
#!/usr/bin/ruby-w time = time.new # components ' A time puts ' current time: + Time.inspect puts Time.year # => Year of the date puts Time.month # => month of the date (1 to) puts Time.day # => Day of the date (1 to) put S Time.wday # => 0:day of week:0 is Sunday puts Time.yday # => 365:day the year puts Time.hour # => 23:24-hour Clock puts Time.min # => puts Time.sec # => puts Time.usec # => 999999:microseconds puts Time.zone # => ; ' UTC ': TimeZone name |
This will produce the following results:
?
1 2 3 4 5 6 7 8 9 10 11 |
Current Time:mon June 12:03:08-0700 2008 2008 6 2 1 154 3 8 247476 UTC |
TIME.UTC,TIME.GM and Time.local functions:
These two functions can be used in a standard format to format the date as follows:
?
1 2 3 4 5 6 7 8 |
# July 8, 2008 Time.local (2008, 7, 8) # July 8, 2008, 09:10am, local time Time.local (2008, 7, 8, 9, ten) # July 8, 2008, 09 : UTC TIME.UTC (2008, 7, 8, 9,) # July 8, 2008, 09:10:11 GMT (same as UTC) TIME.GM (2008, 7, 8, 9, 10, 11) |
The following example, in an array to get all the components in the following format:
?
1 |
[Sec,min,hour,day,month,year,wday,yday,isdst,zone] |
Try these actions:
?
1 2 3 4 5 6 |
#!/usr/bin/ruby-w time = time.new values = Time.to_a p values |
This will produce the following results:
?
1 |
[2, 6, 2008, 1, 154, False, "MST"] |
This array can be passed to the TIME.UTC or time.local function to get different date formats as follows:
?
1 2 3 4 5 6 |
#!/usr/bin/ruby-w time = time.new values = Time.to_a puts TIME.UTC (*values) |
This will produce the following results:
?
1 |
Mon June 12:15:36 UTC 2008 |
followed by the way to get the number of seconds since the internal representation (platform dependent) calendar:
?
1 2 3 4 5 6 7 8 |
# Returns number of seconds since epoch time = Time.now.to_i # Convert number of seconds to time object. Time.at (Time) # Returns second since epoch which includes microseconds time = Time.now.to_f |
TimeZone and daylight Saving time:
You can use a time object to get all of the relevant information about the timezone and daylight as follows:
?
1 2 3 4 5 6 7 8 9 10 11-12 |
Time = time.new # are 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 in UTC time zone Time.localtime # Convert to local timezone. Time.gmtime # Convert back to UTC. Time.getlocal # Return a new Time object in the local zone TIME.GETUTC # Return a new Time object in UTC |
Format Time and Date:
There are a variety of ways to format dates and times. Here is an example that illustrates several:
?
1 2 3 4 5 6 7 |
#!/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 ") |
This will produce the following results:
?
1 2 3 4 |
Mon June 12:35:19-0700 2008 Mon June 2 12:35:19 2008 Mon June 02 12:35:19-0700 2008 2008-06-02 12:35:19 |
Time Arithmetic:
Can do simple sums in time as follows:
?
1 2 3 4 5 6 7 8 9 10 11 |
now = time.now # "Current" puts now past = now-10 # seconds ago. Time-number => time puts past future = Now + ten seconds from now time + number => time puts future diff = Future-now # => Time-time => number of seconds puts diff |
This will produce the following results:
?
1 2 3 4 |
Thu Aug 20:57:05-0700 2013 Thu Aug-20:56:55-0700 2013 Thu Aug 01 20:57:15-0700 2013 10.0 |