Ruby Date & Time
Ruby has Mainly three classes related to date and time in its documentation.
Ruby date provides two classes, Date and DateTime.
To understand the concept of date, first we need to understand some terms.
The Date object is created with ::new, ::parse, ::today, ::jd, ::strptime, etc. All date objects are immutable, hence they can't modify themselves.
The Date object has various methods as shown in the below example.
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.
The last element of day, minute, second or hour can be fractional.
The DateTime object has various methods as shown in the below example.
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.
Time with gm, utc and local functions
Instead of using current system setting, you can also use GMT, local and UTC timezones.
Working with an instance of time
After creating an instance of time, we can work on that time in following ways.
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.