intro to understanding timestamps and datetime in context of PfDA
Also two modules available in EPD among other places:
Wonderful tutorial on converting among various time formats: Date and Time Representation in Python
# http://docs.python.org/2/library/datetime.html
# http://docs.python.org/2/library/time.html
import datetime
import time
import pytz
import dateutil
Basic task: get current local datetime -- notice no timezone info -- datetime.now() is "timezone naive"
t = datetime.datetime.now()
print t
print 'hour :', t.hour
print 'minute:', t.minute
print 'second:', t.second
print 'microsecond:', t.microsecond
print 'tzinfo:', t.tzinfo
2013-03-07 09:52:11.204124 hour : 9 minute: 52 second: 11 microsecond: 204124 tzinfo: None
Similarly -- can get UTC http://en.wikipedia.org/wiki/Coordinated_Universal_Time
t = datetime.datetime.utcnow()
print t
print 'hour :', t.hour
print 'minute:', t.minute
print 'second:', t.second
print 'microsecond:', t.microsecond
print 'tzinfo:', t.tzinfo
2013-03-07 17:52:11.213013 hour : 17 minute: 52 second: 11 microsecond: 213013 tzinfo: None
I've used pytz for time zones...I think dateutil has similar functionality but not sure
pytz.utc
<UTC>
# I'm assuming for our purposes here that we are in Pacific time
pacific_tz = pytz.timezone('US/Pacific')
# make tz-aware datetimes
t = pacific_tz.localize(datetime.datetime.now())
t_utc = pytz.utc.localize(datetime.datetime.utcnow())
print t
print 'hour :', t.hour
print 'minute:', t.minute
print 'second:', t.second
print 'microsecond:', t.microsecond
print 'tzinfo:', t.tzinfo
2013-03-07 09:56:26.462637-08:00 hour : 9 minute: 56 second: 26 microsecond: 462637 tzinfo: US/Pacific
# example of using astimezone to represent in different tz
t.astimezone(pytz.utc)
datetime.datetime(2013, 3, 7, 17, 56, 26, 462637, tzinfo=<UTC>)
t, t_utc, t_utc-t, (t_utc-t).total_seconds()
(datetime.datetime(2013, 3, 7, 9, 56, 26, 462637, tzinfo=<DstTzInfo 'US/Pacific' PST-1 day, 16:00:00 STD>), datetime.datetime(2013, 3, 7, 17, 56, 26, 462866, tzinfo=<UTC>), datetime.timedelta(0, 0, 229), 0.000229)
# http://pymotw.com/2/time/index.html
time.ctime()
'Thu Mar 7 09:52:11 2013'
# number of seconds since Jan 1, 1970 UTC
# http://www.epochconverter.com/
time.time()
1362679117.391256
# use of fromtimestamp to convert Unix Epoch time to datetime.datetime
datetime.datetime.fromtimestamp(time.time())
datetime.datetime(2013, 3, 7, 10, 0, 28, 730465)
(datetime.datetime.now() - datetime.datetime.fromtimestamp(time.time())).total_seconds()
-8e-06
datetime.datetime(1970,1,1,0,0,0)
datetime.datetime(1970, 1, 1, 0, 0)
datetime.datetime.fromtimestamp(0.0)
datetime.datetime(1969, 12, 31, 16, 0)
# show that if we are Pacific time as local time -- that 0 epoch time UTC
pacific_tz.localize(datetime.datetime.fromtimestamp(0.0)).astimezone(pytz.utc)
datetime.datetime(1970, 1, 1, 0, 0, tzinfo=<UTC>)
# http://www.seehuhn.de/pages/pdate
# coverting datetime to epoch time
t=datetime.datetime.now()
timestamp_now = time.mktime(t.timetuple())+1e-6*t.microsecond
pacific_tz.localize(datetime.datetime.fromtimestamp(timestamp_now))
datetime.datetime(2013, 3, 7, 10, 2, 22, 784013, tzinfo=<DstTzInfo 'US/Pacific' PST-1 day, 16:00:00 STD>)
import dateutil
# does tzlocal work?
t = datetime.datetime.now(dateutil.tz.tzlocal())
t.tzname()
'PST'
t.astimezone(pytz.utc)
datetime.datetime(2013, 3, 7, 17, 52, 11, 463657, tzinfo=<UTC>)