Some basic operations on processing time in Ruby
This article mainly introduces some basic operations for processing Time in Ruby. It mainly utilizes the powerful Time module in Ruby. For more information, see
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 produces the following results:
?
1 2 |
Current Time: Mon Jun 02 12:02:39-0700 2008 Current Time: Mon Jun 02 12:02:39-0700 2008 |
Get the date and time of the component:
We can use the Time object to obtain the various components of the date and Time. 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 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 # = & gt; 999999: microseconds Puts time. zone # => "UTC": timezone name |
This produces the following results:
?
1 2 3 4 5 6 7 8 9 10 11 |
Current Time: Mon Jun 02 12:03:08-0700 2008 2008 6 2 1 154 12 3 8 247476 UTC |
Time. utc, Time. gm, and Time. local functions:
The two functions can be used to format the date in the standard format as follows:
?
1 2 3 4 5 6 7 8 |
# July 8, 2008 Time. local (2008, 7, 8) # July 8, 2008, am, local time Time. local (2008, 7, 8, 9, 10) # July 8, 2008, 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) |
The following example shows how to obtain all components in an array in the following format:
?
1 |
[Sec, min, hour, day, month, year, wday, yday, isdst, zone] |
Perform the following operations:
?
1 2 3 4 5 6 |
#! /Usr/bin/ruby-w Time = Time. new Values = time. to_a P values |
This produces the following results:
?
1 |
[26, 10, 12, 2, 6, 2008, 1,154, false, "MST"] |
This array can be passed to the Time. utc or Time. local functions to obtain different date formats:
?
1 2 3 4 5 6 |
#! /Usr/bin/ruby-w Time = Time. new Values = time. to_a Puts Time. utc (* values) |
This produces the following results:
?
1 |
Mon Jun 02 12:15:36 UTC 2008 |
The time in seconds since the internal representation (depending on the platform) is obtained in the following way:
?
1 2 3 4 5 6 7 8 |
# 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 effecdes microseconds Time = Time. now. to_f |
Time zone and Daylight Saving Time:
You can use a Time object to obtain the Time zone and summer order of all related information as follows:
?
1 2 3 4 5 6 7 8 9 10 11 12 |
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 |
Format time and date:
There are various ways to format the date and time. The following is an example to illustrate the following:
?
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 produces the following results:
?
1 2 3 4 |
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 |
Time arithmetic:
You can perform simple arithmetic operations on time as follows:
?
1 2 3 4 5 6 7 8 9 10 11 |
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 |
This produces the following results:
?
1 2 3 4 |
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 |