Epoch time calculations

Description:

This sample code snippet is an illustration of extraction and time conversion between desired epochs. This code may not be the most efficent way solve epoch calculations, however it it believed that for proof-of-concept purposes this code is sufficient.

Author: Abe Lopez

Written in Python 2.7

Libraries required: delorean, datetime

Input: time

Output: display of various epoch times in milliseconds

Version: 0.0.1


In [9]:
import datetime
import delorean
from delorean import parse

In [10]:
# Set custom and local datetime you want to convert to millisecond sine 2004 epoch. 
custom_datetime = "2017/October/05 12:30:00 +1000"

In [11]:
# Time in milliSeconds between now and unix time (1970)
now = datetime.datetime.utcnow()
now_unix_epoch = int((delorean.Delorean(now, timezone="UTC").epoch)*1000)
local_time_now = delorean.Delorean(now, timezone="UTC").shift("Australia/Brisbane").datetime
local_time_str = local_time_now.strftime("%A, %d %B %Y %I:%M:%S.%f %p") # http://strftime.org/

In [12]:
# Time in milliSeconds between 2004 epoch and unix epoch (1970)
Y2004_1970_milli = int((delorean.Delorean(datetime.datetime(2004, 1, 1,0,0), timezone="UTC").epoch)*1000)

In [13]:
# Time in milliseconds between now and 2004 Epoch
time_since_2004_ms = now_unix_epoch - Y2004_1970_milli

In [14]:
# Custom date and time
parse_custom_datetime = parse(custom_datetime).shift("UTC")
#parse_custom_datetime.datetime
custom_unix_epoch = int((delorean.Delorean(parse_custom_datetime.datetime, timezone="UTC").epoch)*1000)
# Time in milliseconds between custom date and time and 2004 Epoch
custom_time_since_2004_ms = custom_unix_epoch - Y2004_1970_milli

In [15]:
print 'Current local datetime is: {}'.format(local_time_str)
print 'Current unix time equivalent is: {} milliseconds'.format(now_unix_epoch)
print 'Time between now and 2004 epoch is: {} milliseconds'.format(time_since_2004_ms)
print 'Time between custom date: *{}* and 2004 epoch is: *{}* milliseconds'.format(custom_datetime,custom_time_since_2004_ms)


Current local datetime is: Thursday, 05 October 2017 07:58:25.315710 AM
Current unix time equivalent is: 1507154305315 milliseconds
Time between now and 2004 epoch is: 434239105315 milliseconds
Time between custom date: *2017/October/05 12:30:00 +1000* and 2004 epoch is: *434255400000* milliseconds

In [ ]: