Tutorials on dates and times in Ruby _ruby topics

Source: Internet
Author: User
Tags current time diff time zones local time time and date

The time class is used in Ruby to represent dates and times. It is based on the system date and time provided by the OS. The class may not represent a date before or after 1970 or 2038 years.

This tutorial will familiarize you with all the important concepts of date and time.
Create the current date and time

The following 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 June 12:02:39-0700 2008 Current
Time:mon June 02 12:02:39-0700 2008

Get the Date & Time component

We can use the time object to get components of various dates and times. Take a look at the following example:

#!/usr/bin/ruby-w Time
 
=
 
component of Time.new # time
puts ' current time: ' + time.inspect
puts time.year  # = Year of > Date
puts time.month  # => Date month (1 to)
puts Time.day   # => Day (1 to) puts Time.wda of one months
Y  # => The week of the week (0 is Sunday)
puts Time.yday  # => 365: Day of the year
puts Time.hour  # => 23:24-hour system C16/>puts time.min   # =>
puts Time.sec   # =>-puts
# time.usec  999999: microseconds
Puts Time.zone  # => "UTC": Time zone 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 functions can be used to format dates in a standard format, 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 instance gets all the 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

This will produce the following results:

[2, 6, 2008, 1, 154, False, "MST"]

The array can be uploaded to the TIME.UTC or time.local function to obtain a different format for 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 June 12:15:36 UTC 2008

Here's how to get time, the number of seconds since the era (platform-dependent):

# returns the number of seconds since the era
= Time.now.to_i 
 
# Converts the number of seconds to a time object
time.at (time)
 
returns the number of seconds since the epoch, containing the subtle time
= Time.now.to_f

Time zones and daylight savings

You can use the time object to get all the information about the timezone and daylight savings, as follows:

Time = Time.new
 
# Here is the explanation
time.zone    # => "UTC": Returns the timezone
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 savings)
TIME.UTC?    # => true: if the UTC time Zone
Time.localtime # is converted to the local time zone
time.gmtime   # converts back to UTC
time.getlocal  # Returns a new time object in the region
TIME.GETUTC   # Returns a new time object in UTC

Format Time and date

There are a number of ways to format dates and times. The following example demonstrates some of these:

#!/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 Formatting Instructions

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

Time algorithm

You can use the time to do some simple arithmetic, as follows:

now = Time.now      # Current time
puts today
 
past = now-10     # 10 seconds ago. Time-number =>
Time puts past
 
future =    Now + # 10 seconds after the start. Time + number => time
puts future
 
diff = future-now   # => time-time => 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

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.