Convert time to Unix time

Summary

Unix time, representing seconds elapsed since January 1, 1970, 00:00:00 UTC, offers a standardized method for comparing timestamps across disparate systems and applications. When faced with varying date formats, such as Oracle DATE types or Unix date strings, converting them to a common Unix timestamp streamlines comparison logic. In Oracle, this conversion involves a calculation using TO_NUMBER and TO_DATE against the epoch. For Unix date strings, the date --date="YYYY-MM-DD HH:MM:SS" +%s command provides the timestamp, while stat -c %Y filename can retrieve a file's last modification time in the same format. This standardization simplifies cross-system time analysis.

Unix time is defined as the number of seconds that have elapsed since midnight Coordinated Universal Time (UTC), 1 January 1970,not counting leap seconds. It is used widely in Unix-like and many other operating systems and file formats. It is neither a linear representation of time nor a true representation of UTC. Unix time may be checked on some Unix systems by typing date +%s on the command line.

Sometimes it's a bit difficult to compare times stored in different applications or systems with different formats. For example, if we want to compare a DATE in Oracle with a date string in Unix, we need to convert them into a common time type so that they can be compares easily. The common way is we can convert them to UNIX timestamp and then compare which one is larger.

Now we will show how to convert time to UNIX timestamp.

In Oracle, if we have a DATE type, we can convert it into UNIX timestamp using following.

TO_NUMBER(date-TO_DATE(''01-Jan-1970 00:00:00', 'DD-Mon-YYYY HH24:MI:SS'))*24*60*60

In UNIX, if we have date string like "2013-01-19 23:02:01", then we can convert it into UNIX timestamp using

date --date="2013-01-19 23:02:01" +%s

If you want to get the last modified time of a file in UNIX timestamp format, you can use:

stat -c %Y filename

Hope this can help you when you want to compare time from different applications or systems.

UNIX TIME ORACLE DATE CONVERSION TIMESTAMP

  RELATED

  COMMENTS

0

No comment for this article.