time/datetime/calendar

import time
import calendar
import datetime

UNIX time

in (time.timegm(0)):

t  = time.gmtime(0)
print t
print calendar.timegm(t)
print

out (time.struct_time):

time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)
0

in (datetime.datetime):

t = datetime.datetime(1970, 1, 1, 0, 0, 0, 0)
print t
print t.timetuple()
print t.utctimetuple()
print calendar.timegm(t.timetuple())
print calendar.timegm(t.utctimetuple())
print

out (time.timetuple, time.utctimetuple, calendar.timegm):

1970-01-01 00:00:00
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=-1)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)
0
0

in (datetime.datetime w/ None):

t = datetime.datetime(1970, 1, 1, 0, 0, 0, 0, None)
print t
print t.timetuple()
print t.utctimetuple()
print calendar.timegm(t.timetuple())
print calendar.timegm(t.utctimetuple())
print

out (time.timetuple, time.utctimetuple, calendar.timegm):

1970-01-01 00:00:00
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=-1)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)
0
0

in (datetime.datetime.strptime):

t = datetime.datetime.strptime("1970-01-01 00:00:00", "%Y-%m-%d %H:%M:%S")
print t
print t.timetuple()
print t.utctimetuple()
print calendar.timegm(t.timetuple())
print calendar.timegm(t.utctimetuple())

out (time.timetuple, time.utctimetuple, calendar.timegm):

1970-01-01 00:00:00
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=-1)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)
0
0

Unix time/MJD/ISO8601

#!/usr/bin/env python

import time
import calendar
import datetime

_MJD_TO_UNIX_TIME =  -3506716800.
_UNIX_TIME_TO_MJD = -_MJD_TO_UNIX_TIME

def mjd_to_unix_time(mjd):
    return mjd * 24. * 3600. + _MJD_TO_UNIX_TIME

def unix_time_to_mjd(unix_time):
    return (unix_time + _UNIX_TIME_TO_MJD) / 24. / 3600.

def unix_time_to_iso8601(unix_time):
    # 2011-12-09T23:40:00Z
    return time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(unix_time))

def unix_time_to_datetime(unix_time):
    # 2011-12-09 23:40:00
    return time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(unix_time))

def unix_time_to_str(unix_time):
    # 20111209T234000Z
    return time.strftime("%Y%m%dT%H%M%SZ", time.gmtime(unix_time))

def mjd_to_iso8601(mjd):
    return unix_time_to_iso8601(mjd_to_unix_time(mjd))

def mjd_to_datetime(mjd):
    return unix_time_to_datetime(mjd_to_unix_time(mjd))

def mjd_to_str(mjd):
    return unix_time_to_str(mjd_to_unix_time(mjd))

#00:00 November 17, 1858, Wednesday
t = datetime.datetime(1858, 11, 17, 0, 0, 0, 0, None)
print "epoch of MJD"
print t
print t.utctimetuple()
print "UNIX:", calendar.timegm(t.utctimetuple())
print


mjd = 53736.
print "MJD:", mjd

print "# MJD -> UNIX"
unix_time = mjd_to_unix_time(mjd)
print "UNIX:", unix_time
print time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(unix_time))
print 

print "# UNIX -> MJD"
mjd = unix_time_to_mjd(unix_time)
print "MJD:", mjd
print

print mjd_to_iso8601(mjd)
print mjd_to_datetime(mjd)
print mjd_to_str(mjd)

epoch of MJD
1858-11-17 00:00:00
time.struct_time(tm_year=1858, tm_mon=11, tm_mday=17, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=321, tm_isdst=0)
UNIX: -3506716800

MJD: 53736.0
# MJD -> UNIX
UNIX: 1136073600.0
2006-01-01 00:00:00

# UNIX -> MJD
MJD: 53736.0

2006-01-01T00:00:00Z
2006-01-01 00:00:00
20060101T000000Z