The date and time is important for an application which is used in different timezones. For example, consider what happens if a user from India enters some data with its own local timestamp at 5:30 a.m and a user from U.S.A wants to see the data. If the timezone is not changed then time which is entered is in the future for the user in U.S.A.
Before getting into actual coding to handle date and time, let us understand the time terminology:
- UTC - The reference time.
- Standard Time - The local time without a daylight saving time offset. For example, in India, standard time(IST) is UTC+5:00.
- Daylight Saving Time(DST) - The local time with a daylight saving time offset. This offset is typically one hour, but not always. It is typically used in most countries away from the equator. In India, daylight saving time is UTC+05:30
Ruby provides two special Libraries to handle time and date:
Time: It is usually sufficient for everyday programming needs because the class uses the time provided by the operating system. It may not be able to display dates before the year 1970 and after the year 2038 and is based on library time.
Date: It consists of two classes
There are many solutions to managing the timezones and one of them is to store the time in a standard format like UTC and converting it to the user's time-zone.
The code given below can display all possible date-time formats: