Convert Epoch Time with Python

Convert Unix Epoch with Python

I’m slowly improving my Python skills, mostly by Googling and combining multiple answers to code a solution to my systems administration tasks. Today I decided to write a simpe converter that takes Epoch Time as a parameter and returns you the time and date it corresponds to.

datetime and timezone Modules in Python

Most of the functionality is done using the fromtimestamp function of the datetime module. But because I also like seeing time in UTC, I decided to use timezone module as well.

epoch.py script

#!/Library/Frameworks/Python.framework/Versions/3.6/bin/python3

import sys
from datetime import datetime, timezone

if len(sys.argv)>1:
  print ("This is the Epoch time: ", sys.argv[1])

  try:
      timestamp = int(sys.argv[1])

      if timestamp>0:
        timedate = datetime.fromtimestamp(timestamp)
        timedate_utc = datetime.fromtimestamp(timestamp, timezone.utc)

        print ("Time/date: ", format(timedate))
        print ("Time/date in UTC: ", format(timedate_utc))
  except ValueError:
        print ("Timestamp should be a positive integer, please.")
else:
  print ("Usage: epoch.py <EPOCH-TIMESTAMP>")

FIXME: I’ll revisit this to re-publish script directly from GitHub.

Here’s how you can use the script:

greys@maverick:~/proj/python $ ./epoch.py 1566672346
 This is the Epoch time:  1566672346
 Time/date:  2019-08-24 19:45:46
 Time/date in UTC:  2019-08-24 18:45:46+00:00

I implemented basic checks:

  • script won’t run if no command line parameters are passed
  • an error message will be shown if command line parameter isn’t a number (and therefore can’t be a timestamp)

Do you see anything that should be changed or can be improved? Let me know!

See Also




Epoch Time

unix epoch

I’ve spoken about Unix epoch before, so this is a follow up post with the next few things about epoch time that I find particularly interesting.

What is Unix Epoch time?

Unix Epoch time is the number of seconds elapsed since 00:00:00 UTC, Thursday, 1 January 1970.

It’s an interesting concept, because the point in time is specific time we can easily recognise and process – it’s a specific time on a certain day way back in 1970. But the Epoch time is not a point in time, but a large counter – specifically, a number of seconds elapsed since the beginning on Unix time.

Does Epoch Time differ with timezones?

This is one of the most interesting things about Unix time. Even though it’s based on a specific timezone (UTC), the Unix time itself is not timezone specific. So one server referring to a specific Unix time will be referring to exactly the same point in time as another server in a different timezone – for both of them the number of seconds elapsed since UTC will be the same.

It is a great question though, because Unix Epoch time is commonly converted back to human-friendly time and date stamps, which at that point become timezone specific. So if you decode Epoch time value on two servers in different timezones, you will end up with a different human-friendly timestamp.

Will there be an end of Epoch Time?

Apparently, current representation of Epoch time means some 32-bit implementations will have a problem similar to Y2K, only a bit later: sometime on January 19th, 2038. On that day, the time counter will reach its maximum value for a 32-bit signed number, meaning next valid value will have to be 0 – meaning both the end and the start of the Unix Epoch time.

This 2038 problem sounds like such a fascinating issue that I’ll have a separate post on it soon!

See Aso




Unix Epoch

unix epoch.jpg
Unix Epoch started on January 1, 1970

Unix Epoch is the system for describing time in Unix and Unix like systems. It starts on 00:00:00 January 1st; 1970.

Unix time is the number of seconds that have elapsed since the start of the Unix Epoch. This Unix Epoch time is an ever-incrementing counter which goes up every second and shows the number of seconds elapsed since 00:00:00 UTC on January 1, 1970. Such large numbers are pretty useless in their raw form for the tasks of confirming current time and date, but they’re perfect for measuring between two points in time – it’s easier and quicker to find difference between two Unix timestamps that calculate the same between two human-readable timestamps.

Times and dates you see and work with on your Linux desktop or server are all derived from Unix time: your operating system confirms the Unix Time first and then uses standard library procedures to convert it into commonly used formats we can actually read.

How To Find Out Unix Time

You can get current Unix time using the date command:

$ date +%s
1547311301

Without any parameters, date command will show you a more readable current tkme:

$ date
Sat Jan 12 16:41:43 UTC 2019

If you are sporting the recent bash 5.0 release, you don’t even need the date command: just use the EPOCHSECONDS variable like this:

$ echo $EPOCHSECONDS
1547318486

See Also