Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 1 | \section{Built-in Module \sectcode{time}} |
| 2 | |
| 3 | \bimodindex{time} |
| 4 | This module provides various time-related functions. |
Guido van Rossum | bd851cd | 1994-08-23 13:26:22 +0000 | [diff] [blame] | 5 | It is always available. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 6 | |
| 7 | An explanation of some terminology and conventions is in order. |
| 8 | |
| 9 | \begin{itemize} |
| 10 | |
| 11 | \item |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 12 | The ``epoch'' is the point where the time starts. On January 1st of that |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 13 | year, at 0 hours, the ``time since the epoch'' is zero. For UNIX, the |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 14 | epoch is 1970. To find out what the epoch is, look at \code{gmtime(0)}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 15 | |
| 16 | \item |
| 17 | UTC is Coordinated Universal Time (formerly known as Greenwich Mean |
| 18 | Time). The acronym UTC is not a mistake but a compromise between |
| 19 | English and French. |
| 20 | |
| 21 | \item |
| 22 | DST is Daylight Saving Time, an adjustment of the timezone by |
| 23 | (usually) one hour during part of the year. DST rules are magic |
| 24 | (determined by local law) and can change from year to year. The C |
| 25 | library has a table containing the local rules (often it is read from |
| 26 | a system file for flexibility) and is the only source of True Wisdom |
| 27 | in this respect. |
| 28 | |
| 29 | \item |
| 30 | The precision of the various real-time functions may be less than |
| 31 | suggested by the units in which their value or argument is expressed. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 32 | E.g.\ on most UNIX systems, the clock ``ticks'' only 50 or 100 times a |
| 33 | second, and on the Mac, times are only accurate to whole seconds. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 34 | |
Guido van Rossum | 8cf2db4 | 1996-07-30 18:32:04 +0000 | [diff] [blame] | 35 | \item |
Guido van Rossum | 21be147 | 1996-12-12 17:59:37 +0000 | [diff] [blame^] | 36 | On the other hand, the precision of \code{time()} and \code{sleep()} |
| 37 | is better than their UNIX equivalents: times are expressed as floating |
| 38 | point numbers, \code{time()} returns the most accurate time available |
| 39 | (using UNIX \code{gettimeofday()} where available), and \code{sleep()} |
| 40 | will accept a time with a nonzero fraction (UNIX \code{select()} is |
| 41 | used to implement this, where available). |
| 42 | |
| 43 | \item |
Guido van Rossum | 8cf2db4 | 1996-07-30 18:32:04 +0000 | [diff] [blame] | 44 | The time tuple as returned by \code{gmtime()} and \code{localtime()}, |
| 45 | or as accpted by \code{mktime()} is a tuple of 9 |
| 46 | integers: year (e.g.\ 1993), month (1--12), day (1--31), hour |
| 47 | (0--23), minute (0--59), second (0--59), weekday (0--6, monday is 0), |
| 48 | Julian day (1--366) and daylight savings flag (-1, 0 or 1). |
| 49 | Note that unlike the C structure, the month value is a range of 1-12, not |
| 50 | 0-11. A year value of $<$ 100 will typically be silently converted to |
| 51 | 1900 $+$ year value. A -1 argument as daylight savings flag, passed to |
| 52 | \code{mktime()} will usually result in the correct daylight savings |
| 53 | state to be filled in. |
| 54 | |
| 55 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 56 | \end{itemize} |
| 57 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 58 | The module defines the following functions and data items: |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 59 | |
| 60 | \renewcommand{\indexsubitem}{(in module time)} |
| 61 | |
| 62 | \begin{datadesc}{altzone} |
| 63 | The offset of the local DST timezone, in seconds west of the 0th |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 64 | meridian, if one is defined. Negative if the local DST timezone is |
| 65 | east of the 0th meridian (as in Western Europe, including the UK). |
| 66 | Only use this if \code{daylight} is nonzero. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 67 | \end{datadesc} |
| 68 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 69 | \begin{funcdesc}{asctime}{tuple} |
| 70 | Convert a tuple representing a time as returned by \code{gmtime()} or |
| 71 | \code{localtime()} to a 24-character string of the following form: |
| 72 | \code{'Sun Jun 20 23:21:05 1993'}. Note: unlike the C function of |
| 73 | the same name, there is no trailing newline. |
| 74 | \end{funcdesc} |
| 75 | |
Guido van Rossum | bd851cd | 1994-08-23 13:26:22 +0000 | [diff] [blame] | 76 | \begin{funcdesc}{clock}{} |
| 77 | Return the current CPU time as a floating point number expressed in |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 78 | seconds. The precision, and in fact the very definiton of the meaning |
Guido van Rossum | 21be147 | 1996-12-12 17:59:37 +0000 | [diff] [blame^] | 79 | of ``CPU time'', depends on that of the C function of the same name, |
| 80 | but in any case, this is the function to use for benchmarking Python |
| 81 | or timing algorithms. |
Guido van Rossum | bd851cd | 1994-08-23 13:26:22 +0000 | [diff] [blame] | 82 | \end{funcdesc} |
| 83 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 84 | \begin{funcdesc}{ctime}{secs} |
| 85 | Convert a time expressed in seconds since the epoch to a string |
| 86 | representing local time. \code{ctime(t)} is equivalent to |
| 87 | \code{asctime(localtime(t))}. |
| 88 | \end{funcdesc} |
| 89 | |
| 90 | \begin{datadesc}{daylight} |
| 91 | Nonzero if a DST timezone is defined. |
| 92 | \end{datadesc} |
| 93 | |
| 94 | \begin{funcdesc}{gmtime}{secs} |
Guido van Rossum | 8cf2db4 | 1996-07-30 18:32:04 +0000 | [diff] [blame] | 95 | Convert a time expressed in seconds since the epoch to a time tuple |
| 96 | in UTC in which the dst flag is always zero. Fractions of a second are |
| 97 | ignored. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 98 | \end{funcdesc} |
| 99 | |
| 100 | \begin{funcdesc}{localtime}{secs} |
| 101 | Like \code{gmtime} but converts to local time. The dst flag is set |
| 102 | to 1 when DST applies to the given time. |
| 103 | \end{funcdesc} |
| 104 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 105 | \begin{funcdesc}{mktime}{tuple} |
| 106 | This is the inverse function of \code{localtime}. Its argument is the |
Guido van Rossum | 8cf2db4 | 1996-07-30 18:32:04 +0000 | [diff] [blame] | 107 | full 9-tuple (since the dst flag is needed --- pass -1 as the dst flag if |
| 108 | it is unknown) which expresses the time |
| 109 | in \em{local} time, not UTC. It returns a floating |
Guido van Rossum | 036eae6 | 1996-06-26 19:25:12 +0000 | [diff] [blame] | 110 | point number, for compatibility with \code{time.time()}. If the input |
| 111 | value can't be represented as a valid time, OverflowError is raised. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 112 | \end{funcdesc} |
| 113 | |
| 114 | \begin{funcdesc}{sleep}{secs} |
| 115 | Suspend execution for the given number of seconds. The argument may |
| 116 | be a floating point number to indicate a more precise sleep time. |
| 117 | \end{funcdesc} |
| 118 | |
Guido van Rossum | 26ee809 | 1995-09-13 17:37:49 +0000 | [diff] [blame] | 119 | \begin{funcdesc}{strftime}{format, tuple} |
| 120 | Convert a tuple representing a time as returned by \code{gmtime()} or |
| 121 | \code{localtime()} to a string as specified by the format argument. |
Guido van Rossum | 8cf2db4 | 1996-07-30 18:32:04 +0000 | [diff] [blame] | 122 | |
| 123 | The following directives, shown without the optional field width and |
| 124 | precision specification, are replaced by the indicated characters: |
| 125 | |
| 126 | \begin{tabular}{lp{25em}} |
| 127 | \%a & Locale's abbreviated weekday name. \\ |
| 128 | \%A & Locale's full weekday name. \\ |
| 129 | \%b & Locale's abbreviated month name. \\ |
| 130 | \%B & Locale's full month name. \\ |
| 131 | \%c & Locale's appropriate date and time representation. \\ |
| 132 | \%d & Day of the month as a decimal number [01,31]. \\ |
Guido van Rossum | 8cf2db4 | 1996-07-30 18:32:04 +0000 | [diff] [blame] | 133 | \%H & Hour (24-hour clock) as a decimal number [00,23]. \\ |
| 134 | \%I & Hour (12-hour clock) as a decimal number [01,12]. \\ |
| 135 | \%j & Day of the year as a decimal number [001,366]. \\ |
| 136 | \%m & Month as a decimal number [01,12]. \\ |
| 137 | \%M & Minute as a decimal number [00,59]. \\ |
Guido van Rossum | 8cf2db4 | 1996-07-30 18:32:04 +0000 | [diff] [blame] | 138 | \%p & Locale's equivalent of either AM or PM. \\ |
| 139 | \%S & Second as a decimal number [00,61]. \\ |
Guido van Rossum | 8cf2db4 | 1996-07-30 18:32:04 +0000 | [diff] [blame] | 140 | \%U & Week number of the year (Sunday as the first day of the |
| 141 | week) as a decimal number [00,53]. All days in a new |
| 142 | year preceding the first Sunday are considered to be in |
| 143 | week 0. \\ |
| 144 | \%w & Weekday as a decimal number [0(Sunday),6]. \\ |
| 145 | \%W & Week number of the year (Monday as the first day of the |
| 146 | week) as a decimal number [00,53]. All days in a new |
| 147 | year preceding the first Sunday are considered to be in |
| 148 | week 0. \\ |
| 149 | \%x & Locale's appropriate date representation. \\ |
| 150 | \%X & Locale's appropriate time representation. \\ |
| 151 | \%y & Year without century as a decimal number [00,99]. \\ |
| 152 | \%Y & Year with century as a decimal number. \\ |
| 153 | \%Z & Time zone name (or by no characters if no time zone |
| 154 | exists). \\ |
| 155 | \%\% & \% \\ |
| 156 | \end{tabular} |
| 157 | |
Guido van Rossum | 21be147 | 1996-12-12 17:59:37 +0000 | [diff] [blame^] | 158 | Additional directives may be supported on certain platforms, but |
| 159 | only the ones listed here have a meaning standardized by ANSI C. |
Guido van Rossum | 8cf2db4 | 1996-07-30 18:32:04 +0000 | [diff] [blame] | 160 | |
Guido van Rossum | 21be147 | 1996-12-12 17:59:37 +0000 | [diff] [blame^] | 161 | On some platforms, an optional field width and precision |
| 162 | specification can immediately follow the initial \% of a |
| 163 | directive in the following order; this is also not portable. |
| 164 | The field width is normally 2 except for \%j where it is 3. |
Guido van Rossum | 8cf2db4 | 1996-07-30 18:32:04 +0000 | [diff] [blame] | 165 | |
Guido van Rossum | 26ee809 | 1995-09-13 17:37:49 +0000 | [diff] [blame] | 166 | \end{funcdesc} |
| 167 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 168 | \begin{funcdesc}{time}{} |
| 169 | Return the time as a floating point number expressed in seconds since |
| 170 | the epoch, in UTC. Note that even though the time is always returned |
| 171 | as a floating point number, not all systems provide time with a better |
Guido van Rossum | bd851cd | 1994-08-23 13:26:22 +0000 | [diff] [blame] | 172 | precision than 1 second. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 173 | \end{funcdesc} |
| 174 | |
| 175 | \begin{datadesc}{timezone} |
| 176 | The offset of the local (non-DST) timezone, in seconds west of the 0th |
| 177 | meridian (i.e. negative in most of Western Europe, positive in the US, |
| 178 | zero in the UK). |
| 179 | \end{datadesc} |
| 180 | |
| 181 | \begin{datadesc}{tzname} |
| 182 | A tuple of two strings: the first is the name of the local non-DST |
| 183 | timezone, the second is the name of the local DST timezone. If no DST |
| 184 | timezone is defined, the second string should not be used. |
| 185 | \end{datadesc} |
Guido van Rossum | 8cf2db4 | 1996-07-30 18:32:04 +0000 | [diff] [blame] | 186 | |