Epoch/Unix Timestamp Converter

date-timeThe Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight GMT).

This explains how date & time values are actually stored on computers: using an integer value representing the number of seconds since 01/01/1970 00:00:00 GMT. Note that a negative value would represent a date prior to 01/01/1970.

For instance, the current date & time is:

Human Readable Date to Epoch Date Converter


Your first challenge is to write a script, using a programming language of your choice, to convert a date entered in a human readable format e.g. DD/MM/YYYY HH:MM:SS as an input and output the date using the Epoch/Unix Timestamp.

To do so you will need to calculate the number of seconds elapsed between the date given and 01/01/1970 00:00:00.

To simplify your calculations you can make the following assumptions:

  • There are 365.25 days in a year.
  • There are 30.44 days in a month (on average).
  • There are 24 hours in a day.
  • There are 60 minutes in an hour.
  • There are 60 seconds in a minute.

You can test you script with today’s date and compare the output of your program with the Epoch date that appears at the top of this blog post.

Epoch Date to Human Readable Date Converter


Your second challenge is to work out and output an actual date in the DD/MM/YYYY HH:MM:SS format from an Epoch timestamp.

Testing


Here are some key dates for you to test your scripts:

Epoch/Unix Timestamp Date Event
410270400 1 January 1983 ARPANET adopted TCP/IP on January 1, 1983, and from there researchers began to assemble the “network of networks” that became the modern Internet. The online world then took on a more recognizable form in 1990, when computer scientist Tim Berners-Lee invented the World Wide Web.
479736000 15 March 1985 The first domain name registered was Symbolics.com. It was registered March 15, 1985, to Symbolics Inc., a computer systems company in Cambridge, Massachussetts.
773409600 05 July 1994 Amazon was founded in the garage of Bezos’ rented home in Bellevue, Washington. In July 1995, the company began service as an online bookstore.
904910400 4 September 1998 The search engine Google was invented by computer scientists Larry Page and Sergey Brin. It was named after a googol (the name for the number 1 followed by 100 zeros) found in the book “Mathematics and the Imagination” by Edward Kasner and James Newman.
1075896000 4 February 2004 The social network Facebook was launched on February 4, 2004, by Mark Zuckerberg, along with fellow Harvard College students and roommates Eduardo Saverin, Andrew McCollum, Dustin Moskovitz and Chris Hughes

Using the time library


Note that, in Python, the time library already includes a few functions to manipulate Unix Epoch timestamps. You can for example use this library to convert a Unix Epoch Timestamp into a human readable format, using the following code:

import time
timestamp = 1547281745
datetime = time.strftime('%A, %Y-%m-%d %H:%M:%S', time.localtime(timestamp))
print(datetime)
unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 4.6 / 5. Vote count: 18

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!