Unix/Epoch Timestamp Conversions and Usages (What ?, Why ?, How ?)

What is the Unix timestamp?

Unix timestamp is nothing but the number of seconds/milliseconds elapsed since 1970 Jan 1st to any particular instant of time.

For example, the timestamp of 1970 Jan 1st, 1 am is 3600(because the time difference from 1970 Jan 1st, 1am to 1970 Jan 1st is 3600 seconds).

---------------------------- About 32-bit and 64-bit systems for timestamps ----------------------------

Few systems still store timestamps as a signed 32-bit integer, those services will cause problems from January 19, 2038. This problem is known as the Year 2038 problem Or Y2038. Storing the timestamp as a 64-bit integer will be more future-proof for the long-term support.

Timestamps can be represented in multiple forms: seconds, milliseconds, and nano-seconds.

  • Seconds timestamp - This timestamp represents the count of seconds. 
  • Milli-seconds timestamp - This timestamp represents the count of milliseconds.
  • Nano-seconds timestamp - This timestamp represents the count of nano-seconds.

Why Unix timestamp is needed?

Unix/Epoch timestamp gives a unique representation of the date-time around worldwide. It always represents the date-time at GMT (Greenwich Mean Time Zone) or UTC, then the date-time of different timezone will be obtained by adding or subtracting the time difference between the timezone and GMT.

While working with date-time using timestamps in the coding language and storing them in the database would be the best approach as they will be generic for all the timezones. Date-time string signifies only one specific timezone, so having a timestamp will be very easy to scale your application to serve users worldwide.

The other alternative for timestamps to use in programming is ISO-String, but even the Iso-strings are generic for all the timezones. The conversion of the date string to a different timezone is not as simple as converting the timestamp.

For Example, If the timestamp value is 1577923200 then,
  • At GMT, the time is Thursday, January 2nd 2020, 00:00:00 GMT+00:00
  • At India/Kolkata, the time is Thursday, January 2nd 2020, 05:30:00 GMT+05:30
  • At America/New_York, the time is Wednesday, January 1st 2020, 19:00:00 GMT-5:00

How Unix timestamp conversion is done?

The epoch difference between the given date and the date 1970 Jan 1st will give the timestamp value.
From the obtained timestamp value, you should add or subtract the offset value of your timezone from the timestamp to get the exact timestamp for the given date.

One of the best tools to do all types of timestamp conversions is Epoch Converter .


Comments