Time-formats
-
HTTP header format. HTTP headers use a particular format, involving abbreviated English names of the days of the week and the months, a comma (after the first of these), day before month before year, 24-hour:minute:second, and GMT. Examples:
Thu, 01 Dec 1994 16:00:00 GMT Thu, 26 Jan 2006 15:01:16 GMT Tue, 12 Jan 2010 13:48:00 GMT
Note that this is different from what is output by Python's
time.asctime()
function. -
The struct_time object. Python represents time conveniently as a
struct_time
object, defined at [http://docs.python.org/library/time.html#time.struct_time][] as "an object with a named tuple interface: values can be accessed by index and by attribute name" (accessed 20120523). Here is what the first of the three date-time strings above looks like composed as astruct_time
object:time.struct_time(tm_year=1994, tm_mon=12, tm_mday=1, tm_hour=16, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1)
-
ISO 8601 standard format. The International Organization for Standardization (ISO) prescribes a standard format for date and time:
YYYY-MM-DDThh:mm:ss
. See [the ISO's informal description of the standard][]. (The 2004 revision of the actual specification, which costs money to download, is currently available at [this site][].) -
Compact sortable format. For appending to the names of output files when I need to generate multiple versions, and for use as a sortable but human-readable date-time string, I use a format
YYYMMDD_hhmmss
. The three dates initially listed above are shown in this format here:19941201_160000 20060126_150116 20100112_134800
I haven't seen this given a standard name in the Python docs, so I call it "compact sortable" format; it is more compact than the ISO 8601 standard format but still easy to read. In cases where it is impossible for there to be more than one output file generated in the same clock minute, I omit the two digits representing seconds.
Time-format conversions
- We can use
time.strptime
andtime.strftime
to convert to and from astruct_time
object using a kind of formatting syntax using %, reminiscent of the more primitive of the string-formatting syntaxes, and similar to that used in C'ssprintf()
function. Details are provided in the [Python docs cited above][http://docs.python.org/library/time.html#time.struct_time]. -
Examples:
-
HTTP header format and a
struct_time
object-
generate HTTP header format from a
struct_time
object:time.strftime('%a, %d %b %Y %H:%M:%S GMT', time_struct)
-
generate a
struct_time
object from HTML header time format:time.strptime(http_header_time, '%a, %d %b %Y %H:%M:%S GMT')
-
as an identity, deconstruct and regenerate the original HTML header time format:
time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.strptime(http_header_time, '%a, %d %b %Y %H:%M:%S GMT'))
-
-
ISO 8601 format and a
struct_time
object-
generate ISO 8601 format from a
struct_time
object:time.strftime('%Y-%m-%dT%H:%M:%S', time_struct)
-
generate a
struct_time
object from ISO 8601 format:time.strptime(iso8601, '%Y-%m-%dT%H:%M:%S')
-
as an identity, deconstruct and regenerate the original ISO 8601 string:
time.strftime('%Y-%m-%dT%H:%M:%S', time.strptime(iso8601, '%Y-%m-%dT%H:%M:%S'))
-
-
produce compact sortable format from HTML header time format
strftime("%Y%m%d_%H%M%S", time.strptime(http_header_time, '%a, %d %b %Y %H:%M:%S GMT'))
-
-
The following brief functions can be used
def make_struct_time(http_header_time): '''Input HTML header-type time string and output struct_time''' return time.strptime(http_header_time, '%a, %d %b %Y %H:%M:%S GMT') def make_http_time_string(time_struct): '''Input struct_time and output HTTP header-type time string''' return time.strftime('%a, %d %b %Y %H:%M:%S GMT', time_struct) def make_iso8601_time_string(time_struct): '''Input struct_time and output an ISO 8601 time string''' return time.strftime('%Y-%m-%dT%H:%M:%S', time_struct) def make_sortable_time_string(time_struct): '''Input struct_time and output a "compact sortable" time string''' return time.strftime('%Y%m%d_%H%M%S', time_struct)
Current time
- The following generates our current, local time as a
struct_time
object:time.localtime()
[end]