Dates and times have been traditionally tricky for computers to handle, because of the complexities of the Gregorian calendar, let alone taking into account the existence of non-western cultural calendars. The solution was to find something standard, common to every calendar, and easy to manipulate. The second was chosen as the standard unit of time in programming, and in the UNIX world a fixed standard time was named, the UNIX epoch, being 00:00am 1st January, 1970. (The year UNIX was first released). Other operating systems use a different epoch. The reason for this, is that practically every application of arithmetic to times or dates involves breaking down the date or time in question to a value in seconds, and then converting it back. Both conversion to and from seconds, and finally into a nice human readable representation are common practice in programming, tedious, and thus already solved by someone else in the form of the time module.
Whilst there are many functions available in the time modules, three are of primary importance, ignoring the complexities of locales and timezones. Obviously the first thing we need to do is to import the module...
Python 2.4.3 (#1, Oct 2 2006, 21:50:13) [GCC 3.4.6 (Gentoo 3.4.6-r1, ssp-3.4.5-1.0, pie-8.7.9)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import time >>>
time.time()
returns the number of seconds since
the epoch, where the epoch is system dependant. On some systems, a
floating point number is returned, in which the non-integer portion
of the number represents sub-second precision.time.localtime([seconds])
converts 'seconds' if
given, otherwise the value returned by time.time(), into a
tuple of 9 values, broken down as follows. Note that the tuple
returned can be accessed using dot notation and the attribute
name, as well as by index subscription.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() description |
6 | tm_wday | range [0,6], Monday is 0 |
7 | tm_yday | range [1,366] |
8 | tm_isdst | 0, 1 or -1; see below |
time.strftime(format[, t])
returns a string that
formats the tuple given in 't', or the tuple returned by
time.localtime() if 't' is not given, as per the string
'format'. 't', if given, must be a tuple matching the structure
of one returned by time.localtime(). The 'format' string will
be returned as is, except that '%' specifiers will be replaced
as indicated by the following table.Directive | Meaning |
---|---|
%a | Locale's abbreviated weekday name. |
%A | Locale's full weekday name. |
%b | Locale's abbreviated month name. |
%B | Locale's full month name. |
%c | Locale's appropriate date and time representation. |
%d | Day of the month as a decimal number [01,31]. |
%H | Hour (24-hour clock) as a decimal number [00,23]. |
%I | Hour (12-hour clock) as a decimal number [01,12]. |
%j | Day of the year as a decimal number [001,366]. |
%m | Month as a decimal number [01,12]. |
%M | Minute as a decimal number [00,59]. |
%p | Locale's equivalent of either AM or PM. |
%S | Second as a decimal number [00,61]. |
%U | Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. |
%w | Weekday as a decimal number [0(Sunday),6]. |
%W | Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. |
%x | Locale's appropriate date representation. |
%X | Locale's appropriate time representation. |
%y | Year without century as a decimal number [00,99]. |
%Y | Year with century as a decimal number. |
%Z | Time zone name (no characters if no time zone exists). |
%% | A literal "%" character. |