A Date and Time Processing tutorial in Ruby and a date Processing tutorial in ruby

Source: Internet
Author: User

A tutorial on working with dates and times in Ruby

The Time class is used in Ruby to represent dates and times. It 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 important concepts of date and time.
Create the current date and time

Here is a simple example of getting 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 Jun 02 12:02:39 -0700 2008
Current Time: Mon Jun 02 12:02:39 -0700 2008
Get Date & Time component

We can use Time objects to get various date and time components. Consider the following example:
#! / usr / bin / ruby -w
 
time = Time.new
 
# Time components
puts "Current Time:" + time.inspect
puts time.year # => year of date
puts time.month # => month of the date (1 to 12)
puts time.day # => day of the month (1 to 31)
puts time.wday # => day of the week (0 is Sunday)
puts time.yday # => 365: day of the year
puts time.hour # => 23: 24-hour clock
puts time.min # => 59
puts time.sec # => 59
puts time.usec # => 999999: microseconds
puts time.zone # => "UTC": time zone name
This will produce the following results:
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

These functions can be used to format dates in the standard format as follows:
# July 8, 2008
Time.local (2008, 7, 8)
# July 8, 2008, 09:10 am, 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 (same as UTC)
Time.gm (2008, 7, 8, 9, 10, 11)
The following example gets all components in an 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
This will produce the following results:
[26, 10, 12, 2, 6, 2008, 1, 154, false, "MST"]
This 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)
This will produce the following results:
Mon Jun 02 12:15:36 UTC 2008
Here's how to get the time, in seconds since the epoch (platform-dependent):
# Returns the number of seconds since the epoch
time = Time.now.to_i
 
# Convert seconds to Time
Time.at (time)
 
# Returns the number of seconds since the epoch, including subtle
time = Time.now.to_f
Time zone and daylight saving time

You can use the Time object to get all the information about the time zone and daylight saving time as follows:
time = Time.new
 
# Here is the explanation
time.zone # => "UTC": returns the time zone
time.utc_offset # => 0: UTC is a 0 second offset from UTC
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 # convert to local time zone
time.gmtime # convert back to UTC
time.getlocal # returns a new Time object in this locale
time.getutc # returns a new Time object in UTC
Format time and date

There are multiple ways to format the date and time. The following examples demonstrate 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")
This will produce the following results:
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 format instruction

The instructions listed in the following table are used with the method Time.strftime.
Time algorithm

You can use time to do some simple arithmetic as follows:
now = Time.now # current time
puts now
 
past = now-10 # 10 seconds ago. Time-number => Time
puts past
 
future = now + 10 # After 10 seconds from now. Time + number => Time
puts future
 
diff = future-now # => 10 Time-Time => seconds
puts diff
This will produce the following results:
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.