Skip to content

Instantly share code, notes, and snippets.

@AndyDeNike
Last active September 1, 2019 09:34
Show Gist options
  • Save AndyDeNike/8b3588403793883f634be185cb1dc9e0 to your computer and use it in GitHub Desktop.
Save AndyDeNike/8b3588403793883f634be185cb1dc9e0 to your computer and use it in GitHub Desktop.

Seconds since the beginning of time

What if there was a way to find out how many seconds have passed since the very beginning of existence? Many have said "Nobody knows that" but they aren't taking Unix time into consideration.

Known as the 'epoch', the Unix operating system was created by the gods of Unix at a single datetime point of 00:00:00 Thursday, 1 January 1970 (UTC).

How can we capture this origin point of all Unixistence? We can use the datetime classmethod known as 'datetime.fromtimestamp(timestamp)' where 'timestamp' is any second since the epoch. If we do:

epoch = datetime.utcfromtimestamp(0) we will get the exact point in datetime where it all began!

Create a function that will take one parameter that represents any datetime object. Within body of function, create a time delta object from this parameter and the epoch; determine how many seconds have passed since the beginning of time!

Note: Round the final value by 2 decimal!

# main.py
def epoch_time_seconds(any_dt):
pass
# tests.py
def test_seconds_since_this_exercise_was_created():
test_date = datetime(2019, 8, 30, 12, 26, 4, 60260)
assert(epoch_time_seconds(test_date)) == 1567167964.06
def test_seconds_since_y2k():
test_date = datetime(2000, 1, 1, 0, 0, 0, 0)
assert(epoch_time_seconds(test_date)) == 946684800.0
def test_random_time():
test_date = datetime(1988, 5, 31, 0, 0, 0, 0)
assert(epoch_time_seconds(test_date)) == 581040000.0
# solution.py
from datetime import datetime
def epoch_time_seconds(any_dt):
epoch = datetime.utcfromtimestamp(0)
return round((any_dt - epoch).total_seconds(), 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment