Ruby has Mainly three classes related to date and time in its documentation.
- Date
- DateTime
- Time
Date
Ruby date provides two classes, Date and DateTime.
To understand the concept of date, first we need to understand some terms.
- Calendar date: The calendar date is a particular day within a calendar month within a year.
- Ordinal date: The ordinal date is a particular day of a calendar year identified by its ordinal number.
- Week date: The week date is a day identified by calendar week and day numbers. The first calendar week of the year is the one which includes first Thursday of that year.
- Julian day number: The julian day number is in elapsed day since noon on January 1, 4713 BCE.
- Modified julian day number: The modified julian day number is in elapsed day since midnight on November 17, 1858 CE.
The Date object is created with ::new, ::parse, ::today, ::jd, ::strptime, etc. All date objects are immutable, hence they can’t modify themselves.
Example:
require 'date'
puts Date.new(2017,4,3)
puts Date.jd(2451877)
puts Date.ordinal(2017,3)
puts Date.commercial(2017,5,6)
puts Date.parse('2017-02-03')
puts Date.strptime('03-02-2017', '%d-%m-%Y')
puts Time.new(2017,10,8).to_date
Output:

The Date object has various methods as shown in the below example.
Example:
require 'date'
d = Date.parse('4th Mar 2017')
puts d.year
puts d.mon
puts d.mday
puts d.wday
puts d += 1
puts d.strftime('%a %d %b %Y')
Output:

DateTime
Ruby DateTime is a subclass of Date. It easily handles date, hour, minute, second and offset.
The DateTime object created with DateTime.new, DateTime.ordinal, DateTime.parse, DateTime.jd, DateTime.commercial, DateTime.now, etc.
Example:
require 'date'
puts DateTime.new(2017,3,4,5,6,7)
Output:

The last element of day, minute, second or hour can be fractional.
The DateTime object has various methods as shown in the below example.
Example:
- require 'date'
-
- d = DateTime.parse('4th Mar 2017 02:37:05+05:40')
-
- puts d.hour
- puts d.min
- puts d.sec
- puts d.offset
- puts d.zone
- puts d += Rational('1.0')
-
- puts d = d.new_offset('+05:00')
-
- puts d.strftime('%I:%M:%S %p')
-
- puts d > DateTime.new(2000)
Output:

Time
Time class is an abstraction of dates and times. It is stored internally as the number of seconds since Epoch time. The Time class treats GMT (Grenwich Mean Time) and UTC (Coordinated Universal Time) equivalent.
Times may appear equal but on comparison they may be different as all times may have fraction.
Time implementation uses a signed 63 bit integer, Bignum or Rational. Time works slower when integer is used.
AD
Creating a new Time Instance
A new Time instance can be created with ::new. This will use your current system’s time. Parts of time like year, month, day, hour, minute, etc can also be passed.
While creating a new time instance, you need to pass at least a year. If only year is passed, then time will default to January 1 of that year at 00:00:00 with current system time zone.
Example:
puts Time.new
puts Time.new(2017, 3)
puts Time.new(2017, 3, 4)
puts Time.new(2017, 3, 4, 6, 5, 5, "+05:00")
Output:

Time with gm, utc and local functions
Instead of using current system setting, you can also use GMT, local and UTC timezones.
Example:
- puts Time.local(2017, 2, 5)
-
- puts Time.local(2017, 2, 5, 4, 30)
-
- puts Time.utc(2017, 2, 5, 4, 30)
-
- puts Time.gm(2017, 2, 5, 4, 30, 36)
Output:

Working with an instance of time
After creating an instance of time, we can work on that time in following ways.
Example:
- t = Time.new(1991, 07, 5, 9, 15, 33, "+09:00")
- puts t.friday? #=> false
- puts t.year #=> 1993
- puts t.dst? #=> false
- puts t + (60*60*24*365) #=> 1994-02-24 12:00:00 +0900
- puts t.to_i #=> 730522800
-
- t1 = Time.new(2017)
- t2 = Time.new(2015)
-
- puts t1 == t2 #=> false
- puts t1 == t1 #=> true
- puts t1 < t2 #=> true
- puts t1 > t2 #=> false
-
- puts Time.new(2010,10,31).between?(t1, t2) #=> true
Output:

Timezones and daylight savings time
A Time object can be used to get all the information related to timezones. All the information will be displayed respective to current time of our system.
Example:
time = Time.new
puts time.zone
puts time.utc_offset
puts time.zone
puts time.isdst
puts time.utc?
puts time.localtime
puts time.gmtime
puts time.getlocal
puts time.getutc
Output:

Leave a Reply