Ruby Date Time

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Ruby Date & Time

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:

  1. require 'date'   
  2.   
  3. puts Date.new(2017,4,3)            
  4. puts Date.jd(2451877)               
  5. puts Date.ordinal(2017,3)         
  6. puts Date.commercial(2017,5,6)     
  7. puts Date.parse('2017-02-03')    
  8. puts Date.strptime('03-02-2017''%d-%m-%Y')                           
  9. puts Time.new(2017,10,8).to_date     

Output:

Ruby date time 1

The Date object has various methods as shown in the below example.

Example:

  1. require 'date'   
  2.   
  3. d = Date.parse('4th Mar 2017')   
  4.                                
  5. puts d.year                        
  6. puts d.mon                         
  7. puts d.mday                        
  8. puts  d.wday                        
  9. puts d += 1                        
  10. puts d.strftime('%a %d %b %Y')    

Output:

Ruby date time 2


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:

  1. require 'date'   
  2. puts DateTime.new(2017,3,4,5,6,7)   

Output:

Ruby date time 3

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:

  1. require 'date'   
  2.   
  3. d = DateTime.parse('4th Mar 2017 02:37:05+05:40')   
  4.                        
  5. puts d.hour                 
  6. puts d.min                 
  7. puts d.sec                  
  8. puts d.offset               
  9. puts d.zone                 
  10. puts d += Rational('1.0')   
  11.                        
  12. puts d = d.new_offset('+05:00')   
  13.                       
  14. puts d.strftime('%I:%M:%S %p')   
  15.                      
  16. puts d > DateTime.new(2000)   

Output:

Ruby date time 4

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.


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:

  1. puts Time.new          
  2. puts Time.new(2017, 3)       
  3. puts Time.new(2017, 3, 4)   
  4. puts Time.new(2017, 3, 4, 6, 5, 5, "+05:00")   

Output:

Ruby date time 5

Time with gm, utc and local functions

Instead of using current system setting, you can also use GMT, local and UTC timezones.

Example:

  1. puts Time.local(2017, 2, 5)    
  2.   
  3. puts Time.local(2017, 2, 5, 4, 30)     
  4.   
  5. puts Time.utc(2017, 2, 5, 4, 30)    
  6.   
  7. puts Time.gm(2017, 2, 5, 4, 30, 36) 

Output:

Ruby date time 6

Working with an instance of time

After creating an instance of time, we can work on that time in following ways.

Example:

Output:

Ruby date time 7

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:

  1. time = Time.new   
  2.   
  3. puts time.zone         
  4. puts time.utc_offset   
  5. puts time.zone        
  6. puts time.isdst       
  7. puts time.utc?      
  8. puts time.localtime    
  9. puts time.gmtime      
  10. puts time.getlocal    
  11. puts time.getutc   

Output:

Ruby date time 8