Links
Parsing
- Python strptime() and timezones?
tuple_time = time.strptime(date_string, "%Y%m%dT%H:%M:%S")
Formatting
- datetime_obj.ctime()
gives you the standard
Thu Jun 20 00:00 2009
style. - datetime_obj.strftime(fmt)
- Look up fmt here - strftime() and strptime() Behavior
time.time()
- Note: To measure wall clock time, prefer timeit.default_timer() instead of time.time()
- Return the time as a floating point number expressed in seconds since the epoch, in UTC. Note that even though the time is always returned as a floating point number, not all systems provide time with a better precision than 1 second. While this function normally returns non-decreasing values, it can return a lower value than a previous call if the system clock has been set back between the two calls.
time.ctime([secs])
- Convert a time expressed in seconds since the epoch to a string representing local time. If secs is not provided or None, the current time as returned by time() is used. ctime(secs) is equivalent to asctime(localtime(secs)). Locale information is not used by ctime().
Epoch Time
-
To epoch time from datetime
time.mktime(date_time_obj.timetuple())
-
If you have time in epoch time (i.e. a number), then convert it to datetime like this.
# epoch_secs is in epoch time datetime.datetime(*time.localtime(epoch_secs)[:6])
-
The time value as returned by gmtime(), localtime() and strptime() and accepted by asctime(), mktime() and strftime() may be considered as a sequence of 9 integers. The return values of gmtime(), localtime() and strptime() also offer attribute names for individual fields.
Index Attribute Values 0 tm_year (for example, 1993) 1 tm_mon range [1,12] 2 tm_mday range [1,31] 3 tm_hour range [0,23] 4 tm_min range [0,59] 5 tm_sec range [0,61]; see (1) in strftime() 6 tm_wday range [0,6], Monday is 0 7 tm_yday range [1,366] 8 tm_isdst 0, 1 or -1; see below Note that unlike the C structure, the month value is a range of 1-12, not 0-11. A year value will be handled as described under “Year 2000 (Y2K) issues” above. A -1 argument as the daylight savings flag, passed to mktime() will usually result in the correct daylight savings state to be filled in.
When a tuple with an incorrect length is passed to a function expecting a struct_time, or having elements of the wrong type, a TypeError is raised.
-
Use the following functions to convert between time representations:
From To Use seconds since epoch in UTC struct_time gmtime() seconds since epoch in local struct_time localtime() struct_time seconds since epoch in UTC calendar.timegm() struct_time seconds since epoch in time mktime()
Timestamps
import datetime, calendar # Get current UTC timestamp def timestamp_utcnow(): utc_dt = datetime.datetime.utcnow() return int(calendar.timegm(utc_dt.utctimetuple()))
Translating between timezones
Translate to Eastern Timezone
import calendar import datetime import pytz import time now = datetime.datetime.now() # First, translate to UTC. now = datetime.datetime.utcfromtimestamp(time.mktime(now.timetuple())) # Next, stick in the tzinfo to mark it as UTC (pytz requires it.) now = now.replace(tzinfo=pytz.timezone("UTC")) # Finally, translate to the target timezone. now = now.astimezone(pytz.timezone("US/Eastern")) def parse_utc_timestamp(utc_timestamp): date_obj = datetime.datetime.utcfromtimestamp(utc_timestamp) # This is needed for any .astimezone() calls done later. date_obj = date_obj.replace(tzinfo=pytz.timezone("UTC")) return date_obj def local_to_utc(local_date_obj): # Translate to UTC. We use time.mktime(). # The parallel function, calendar.timegm(), takes it's # input in UTC and not local. utc_timestamp = time.mktime(local_date_obj.timetuple()) return parse_utc_timestamp(utc_timestamp) def utc_to_eastern(date_obj): date_obj = date_obj.astimezone(pytz.timezone("US/Eastern")) return date_obj def utc_to_pacific(date_obj): date_obj = date_obj.astimezone(pytz.timezone("US/Pacific")) return date_obj print "Eastern stuff" now = datetime.datetime.now() print "local =", now now_utc = local_to_utc(now) print "local -> utc =", now_utc print "utcnow =", datetime.datetime.utcnow() now_eastern = utc_to_eastern(now_utc) print now_eastern