Get the current date and time:
The following is a simple example to get the current date and time:
#!/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:
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:
#!/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)
puts Time.wday # => 0:day of week:0 is Sunday
puts Time.yday # => 3 65:day of year
puts Time.hour # => 23:24-hour clock puts
time.min # =>
-puts 19/># =>
puts Time.usec # => 999999:microseconds puts Time.zone # => "UTC": TimeZone Name
This will produce the following results:
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:
# 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:10 UTC
TIME.UTC (2008, 7, 8, 9, ten)
# 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:
[Sec,min,hour,day,month,year,wday,yday,isdst,zone]
Try these actions:
#!/usr/bin/ruby-w time
= time.new
values = time.to_a
p values
This will produce the following results:
[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:
#!/usr/bin/ruby-w time
= time.new
values = time.to_a
puts TIME.UTC (*values)
This will produce the following results:
Mon June 12:15:36 UTC 2008
followed by the way to get the number of seconds since the internal representation (platform dependent) calendar:
# 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:
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:
#!/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:
Mon June 12:35:19-0700 2008
Mon June 2 12:35:19 2008
Mon June 12:35:19-0700 2008
2008-06-02 12:35:19
Time Arithmetic:
Can do simple sums in time as follows:
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
PU TS Future
diff = future-now # => time-time => number of seconds
puts diff
This will produce the following results:
Thu Aug 20:57:05-0700 2013
Thu Aug-20:56:55-0700 2013 Thu
Aug-20:57:15-0700 2013
10.0