Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{time} --- |
| 2 | Time access and conversions.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | \declaremodule{builtin}{time} |
| 4 | |
| 5 | \modulesynopsis{Time access and conversions.} |
| 6 | |
Fred Drake | 2cfc835 | 1998-04-03 06:12:21 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 8 | This module provides various time-related functions. |
Guido van Rossum | bd851cd | 1994-08-23 13:26:22 +0000 | [diff] [blame] | 9 | It is always available. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 10 | |
| 11 | An explanation of some terminology and conventions is in order. |
| 12 | |
| 13 | \begin{itemize} |
| 14 | |
| 15 | \item |
Fred Drake | eb4ed15 | 1998-04-11 04:52:15 +0000 | [diff] [blame] | 16 | The \dfn{epoch}\index{epoch} is the point where the time starts. On |
| 17 | January 1st of that year, at 0 hours, the ``time since the epoch'' is |
| 18 | zero. For \UNIX{}, the epoch is 1970. To find out what the epoch is, |
Fred Drake | abc8cc6 | 1999-05-26 16:15:17 +0000 | [diff] [blame] | 19 | look at \code{gmtime(0)}. |
Guido van Rossum | 929bd0e | 1998-06-09 21:25:41 +0000 | [diff] [blame] | 20 | |
| 21 | \item |
Fred Drake | 0ad55fb | 1998-12-08 19:59:36 +0000 | [diff] [blame] | 22 | The functions in this module do not handle dates and times before the |
Guido van Rossum | 929bd0e | 1998-06-09 21:25:41 +0000 | [diff] [blame] | 23 | epoch or far in the future. The cut-off point in the future is |
Fred Drake | abc8cc6 | 1999-05-26 16:15:17 +0000 | [diff] [blame] | 24 | determined by the C library; for \UNIX{}, it is typically in |
| 25 | 2038\index{Year 2038}. |
Guido van Rossum | 929bd0e | 1998-06-09 21:25:41 +0000 | [diff] [blame] | 26 | |
| 27 | \item |
Fred Drake | abc8cc6 | 1999-05-26 16:15:17 +0000 | [diff] [blame] | 28 | \strong{Year 2000 (Y2K) issues}:\index{Year 2000}\index{Y2K} Python |
| 29 | depends on the platform's C library, which generally doesn't have year |
| 30 | 2000 issues, since all dates and times are represented internally as |
| 31 | seconds since the epoch. Functions accepting a time tuple (see below) |
| 32 | generally require a 4-digit year. For backward compatibility, 2-digit |
| 33 | years are supported if the module variable \code{accept2dyear} is a |
| 34 | non-zero integer; this variable is initialized to \code{1} unless the |
Fred Drake | 0ad55fb | 1998-12-08 19:59:36 +0000 | [diff] [blame] | 35 | environment variable \envvar{PYTHONY2K} is set to a non-empty string, |
| 36 | in which case it is initialized to \code{0}. Thus, you can set |
Fred Drake | d8a41e6 | 1999-02-19 17:54:10 +0000 | [diff] [blame] | 37 | \envvar{PYTHONY2K} to a non-empty string in the environment to require 4-digit |
Fred Drake | 0ad55fb | 1998-12-08 19:59:36 +0000 | [diff] [blame] | 38 | years for all year input. When 2-digit years are accepted, they are |
| 39 | converted according to the \POSIX{} or X/Open standard: values 69-99 |
| 40 | are mapped to 1969-1999, and values 0--68 are mapped to 2000--2068. |
| 41 | Values 100--1899 are always illegal. Note that this is new as of |
| 42 | Python 1.5.2(a2); earlier versions, up to Python 1.5.1 and 1.5.2a1, |
Fred Drake | abc8cc6 | 1999-05-26 16:15:17 +0000 | [diff] [blame] | 43 | would add 1900 to year values below 1900. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 44 | |
| 45 | \item |
Fred Drake | abc8cc6 | 1999-05-26 16:15:17 +0000 | [diff] [blame] | 46 | UTC\index{UTC} is Coordinated Universal Time\index{Coordinated |
| 47 | Universal Time} (formerly known as Greenwich Mean |
| 48 | Time,\index{Greenwich Mean Time} or GMT). The acronym UTC is not a |
| 49 | mistake but a compromise between English and French. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 50 | |
| 51 | \item |
Fred Drake | abc8cc6 | 1999-05-26 16:15:17 +0000 | [diff] [blame] | 52 | DST is Daylight Saving Time,\index{Daylight Saving Time} an adjustment |
| 53 | of the timezone by (usually) one hour during part of the year. DST |
| 54 | rules are magic (determined by local law) and can change from year to |
| 55 | year. The C library has a table containing the local rules (often it |
| 56 | is read from a system file for flexibility) and is the only source of |
| 57 | True Wisdom in this respect. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 58 | |
| 59 | \item |
| 60 | The precision of the various real-time functions may be less than |
| 61 | suggested by the units in which their value or argument is expressed. |
Fred Drake | 094579e | 1996-12-13 22:09:52 +0000 | [diff] [blame] | 62 | E.g.\ on most \UNIX{} systems, the clock ``ticks'' only 50 or 100 times a |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 63 | 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] | 64 | |
Guido van Rossum | 8cf2db4 | 1996-07-30 18:32:04 +0000 | [diff] [blame] | 65 | \item |
Fred Drake | 2cfc835 | 1998-04-03 06:12:21 +0000 | [diff] [blame] | 66 | On the other hand, the precision of \function{time()} and |
| 67 | \function{sleep()} is better than their \UNIX{} equivalents: times are |
| 68 | expressed as floating point numbers, \function{time()} returns the |
| 69 | most accurate time available (using \UNIX{} \cfunction{gettimeofday()} |
| 70 | where available), and \function{sleep()} will accept a time with a |
| 71 | nonzero fraction (\UNIX{} \cfunction{select()} is used to implement |
| 72 | this, where available). |
Guido van Rossum | 21be147 | 1996-12-12 17:59:37 +0000 | [diff] [blame] | 73 | |
| 74 | \item |
Guido van Rossum | 929bd0e | 1998-06-09 21:25:41 +0000 | [diff] [blame] | 75 | The time tuple as returned by \function{gmtime()}, |
| 76 | \function{localtime()}, and \function{strptime()}, and accepted by |
Fred Drake | 6ffa445 | 1999-01-19 19:35:18 +0000 | [diff] [blame] | 77 | \function{asctime()}, \function{mktime()} and \function{strftime()}, |
| 78 | is a tuple of 9 integers: year (e.g.\ 1993), month (1--12), day |
| 79 | (1--31), hour (0--23), minute (0--59), second (0--59), weekday (0--6, |
| 80 | monday is 0), Julian day (1--366) and daylight savings flag (-1, 0 or |
Fred Drake | abc8cc6 | 1999-05-26 16:15:17 +0000 | [diff] [blame] | 81 | 1). Note that unlike the C structure, the month value is a range |
Fred Drake | 6ffa445 | 1999-01-19 19:35:18 +0000 | [diff] [blame] | 82 | of 1-12, not 0-11. A year value will be handled as descibed under |
| 83 | ``Year 2000 (Y2K) issues'' above. A \code{-1} argument as daylight |
| 84 | savings flag, passed to \function{mktime()} will usually result in the |
| 85 | correct daylight savings state to be filled in. |
Guido van Rossum | 8cf2db4 | 1996-07-30 18:32:04 +0000 | [diff] [blame] | 86 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 87 | \end{itemize} |
| 88 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 89 | The module defines the following functions and data items: |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 90 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 91 | |
Fred Drake | 6ffa445 | 1999-01-19 19:35:18 +0000 | [diff] [blame] | 92 | \begin{datadesc}{accept2dyear} |
| 93 | Boolean value indicating whether two-digit year values will be |
| 94 | accepted. This is true by default, but will be set to false if the |
| 95 | environment variable \envvar{PYTHONY2K} has been set to a non-empty |
| 96 | string. It may also be modified at run time. |
| 97 | \end{datadesc} |
| 98 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 99 | \begin{datadesc}{altzone} |
| 100 | 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] | 101 | meridian, if one is defined. Negative if the local DST timezone is |
| 102 | east of the 0th meridian (as in Western Europe, including the UK). |
| 103 | Only use this if \code{daylight} is nonzero. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 104 | \end{datadesc} |
| 105 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 106 | \begin{funcdesc}{asctime}{tuple} |
Fred Drake | 0ad55fb | 1998-12-08 19:59:36 +0000 | [diff] [blame] | 107 | Convert a tuple representing a time as returned by \function{gmtime()} |
| 108 | or \function{localtime()} to a 24-character string of the following form: |
Fred Drake | abc8cc6 | 1999-05-26 16:15:17 +0000 | [diff] [blame] | 109 | \code{'Sun Jun 20 23:21:05 1993'}. Note: unlike the C function of |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 110 | the same name, there is no trailing newline. |
| 111 | \end{funcdesc} |
| 112 | |
Guido van Rossum | bd851cd | 1994-08-23 13:26:22 +0000 | [diff] [blame] | 113 | \begin{funcdesc}{clock}{} |
| 114 | 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] | 115 | seconds. The precision, and in fact the very definiton of the meaning |
Fred Drake | abc8cc6 | 1999-05-26 16:15:17 +0000 | [diff] [blame] | 116 | of ``CPU time''\index{CPU time}, depends on that of the C function |
Fred Drake | 2cfc835 | 1998-04-03 06:12:21 +0000 | [diff] [blame] | 117 | of the same name, but in any case, this is the function to use for |
| 118 | benchmarking\index{benchmarking} Python or timing algorithms. |
Guido van Rossum | bd851cd | 1994-08-23 13:26:22 +0000 | [diff] [blame] | 119 | \end{funcdesc} |
| 120 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 121 | \begin{funcdesc}{ctime}{secs} |
| 122 | Convert a time expressed in seconds since the epoch to a string |
Fred Drake | 2cfc835 | 1998-04-03 06:12:21 +0000 | [diff] [blame] | 123 | representing local time. \code{ctime(\var{secs})} is equivalent to |
| 124 | \code{asctime(localtime(\var{secs}))}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 125 | \end{funcdesc} |
| 126 | |
| 127 | \begin{datadesc}{daylight} |
| 128 | Nonzero if a DST timezone is defined. |
| 129 | \end{datadesc} |
| 130 | |
| 131 | \begin{funcdesc}{gmtime}{secs} |
Guido van Rossum | 8cf2db4 | 1996-07-30 18:32:04 +0000 | [diff] [blame] | 132 | Convert a time expressed in seconds since the epoch to a time tuple |
| 133 | in UTC in which the dst flag is always zero. Fractions of a second are |
Guido van Rossum | 929bd0e | 1998-06-09 21:25:41 +0000 | [diff] [blame] | 134 | ignored. See above for a description of the tuple lay-out. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 135 | \end{funcdesc} |
| 136 | |
| 137 | \begin{funcdesc}{localtime}{secs} |
Fred Drake | 2cfc835 | 1998-04-03 06:12:21 +0000 | [diff] [blame] | 138 | Like \function{gmtime()} but converts to local time. The dst flag is |
| 139 | set to \code{1} when DST applies to the given time. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 140 | \end{funcdesc} |
| 141 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 142 | \begin{funcdesc}{mktime}{tuple} |
Fred Drake | 0ad55fb | 1998-12-08 19:59:36 +0000 | [diff] [blame] | 143 | This is the inverse function of \function{localtime()}. Its argument |
| 144 | is the full 9-tuple (since the dst flag is needed --- pass \code{-1} |
| 145 | as the dst flag if it is unknown) which expresses the time in |
| 146 | \emph{local} time, not UTC. It returns a floating point number, for |
| 147 | compatibility with \function{time()}. If the input value cannot be |
| 148 | represented as a valid time, \exception{OverflowError} is raised. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 149 | \end{funcdesc} |
| 150 | |
| 151 | \begin{funcdesc}{sleep}{secs} |
| 152 | Suspend execution for the given number of seconds. The argument may |
| 153 | be a floating point number to indicate a more precise sleep time. |
| 154 | \end{funcdesc} |
| 155 | |
Guido van Rossum | 26ee809 | 1995-09-13 17:37:49 +0000 | [diff] [blame] | 156 | \begin{funcdesc}{strftime}{format, tuple} |
Fred Drake | 0ad55fb | 1998-12-08 19:59:36 +0000 | [diff] [blame] | 157 | Convert a tuple representing a time as returned by \function{gmtime()} |
| 158 | or \function{localtime()} to a string as specified by the \var{format} |
| 159 | argument. \var{format} must be a string. |
Guido van Rossum | 8cf2db4 | 1996-07-30 18:32:04 +0000 | [diff] [blame] | 160 | |
Fred Drake | 0ad55fb | 1998-12-08 19:59:36 +0000 | [diff] [blame] | 161 | The following directives can be embedded in the \var{format} string. |
| 162 | They are shown without the optional field width and precision |
| 163 | specification, and are replaced by the indicated characters in the |
| 164 | \function{strftime()} result: |
Guido van Rossum | 8cf2db4 | 1996-07-30 18:32:04 +0000 | [diff] [blame] | 165 | |
Fred Drake | ee60191 | 1998-04-11 20:53:03 +0000 | [diff] [blame] | 166 | \begin{tableii}{c|p{24em}}{code}{Directive}{Meaning} |
Fred Drake | 2cfc835 | 1998-04-03 06:12:21 +0000 | [diff] [blame] | 167 | \lineii{\%a}{Locale's abbreviated weekday name.} |
| 168 | \lineii{\%A}{Locale's full weekday name.} |
| 169 | \lineii{\%b}{Locale's abbreviated month name.} |
| 170 | \lineii{\%B}{Locale's full month name.} |
| 171 | \lineii{\%c}{Locale's appropriate date and time representation.} |
| 172 | \lineii{\%d}{Day of the month as a decimal number [01,31].} |
| 173 | \lineii{\%H}{Hour (24-hour clock) as a decimal number [00,23].} |
| 174 | \lineii{\%I}{Hour (12-hour clock) as a decimal number [01,12].} |
| 175 | \lineii{\%j}{Day of the year as a decimal number [001,366].} |
| 176 | \lineii{\%m}{Month as a decimal number [01,12].} |
| 177 | \lineii{\%M}{Minute as a decimal number [00,59].} |
| 178 | \lineii{\%p}{Locale's equivalent of either AM or PM.} |
| 179 | \lineii{\%S}{Second as a decimal number [00,61].} |
| 180 | \lineii{\%U}{Week number of the year (Sunday as the first day of the |
| 181 | week) as a decimal number [00,53]. All days in a new year |
| 182 | preceding the first Sunday are considered to be in week 0.} |
| 183 | \lineii{\%w}{Weekday as a decimal number [0(Sunday),6].} |
| 184 | \lineii{\%W}{Week number of the year (Monday as the first day of the |
| 185 | week) as a decimal number [00,53]. All days in a new year |
| 186 | preceding the first Sunday are considered to be in week 0.} |
| 187 | \lineii{\%x}{Locale's appropriate date representation.} |
| 188 | \lineii{\%X}{Locale's appropriate time representation.} |
| 189 | \lineii{\%y}{Year without century as a decimal number [00,99].} |
| 190 | \lineii{\%Y}{Year with century as a decimal number.} |
| 191 | \lineii{\%Z}{Time zone name (or by no characters if no time zone exists).} |
| 192 | \lineii{\%\%}{\%} |
Fred Drake | 094579e | 1996-12-13 22:09:52 +0000 | [diff] [blame] | 193 | \end{tableii} |
Guido van Rossum | 8cf2db4 | 1996-07-30 18:32:04 +0000 | [diff] [blame] | 194 | |
Fred Drake | 094579e | 1996-12-13 22:09:52 +0000 | [diff] [blame] | 195 | Additional directives may be supported on certain platforms, but |
Fred Drake | abc8cc6 | 1999-05-26 16:15:17 +0000 | [diff] [blame] | 196 | 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] | 197 | |
Fred Drake | 094579e | 1996-12-13 22:09:52 +0000 | [diff] [blame] | 198 | On some platforms, an optional field width and precision |
Fred Drake | 0ad55fb | 1998-12-08 19:59:36 +0000 | [diff] [blame] | 199 | specification can immediately follow the initial \character{\%} of a |
Fred Drake | 094579e | 1996-12-13 22:09:52 +0000 | [diff] [blame] | 200 | directive in the following order; this is also not portable. |
Fred Drake | 2cfc835 | 1998-04-03 06:12:21 +0000 | [diff] [blame] | 201 | The field width is normally 2 except for \code{\%j} where it is 3. |
Guido van Rossum | 26ee809 | 1995-09-13 17:37:49 +0000 | [diff] [blame] | 202 | \end{funcdesc} |
| 203 | |
Guido van Rossum | 5d23758 | 1998-06-09 16:30:56 +0000 | [diff] [blame] | 204 | \begin{funcdesc}{strptime}{string\optional{, format}} |
| 205 | Parse a string representing a time according to a format. The return |
Fred Drake | 0ad55fb | 1998-12-08 19:59:36 +0000 | [diff] [blame] | 206 | value is a tuple as returned by \function{gmtime()} or |
| 207 | \function{localtime()}. The \var{format} parameter uses the same |
| 208 | directives as those used by \function{strftime()}; it defaults to |
| 209 | \code{"\%a \%b \%d \%H:\%M:\%S \%Y"} which matches the formatting |
| 210 | returned by \function{ctime()}. The same platform caveats apply; see |
| 211 | the local \UNIX{} documentation for restrictions or additional |
| 212 | supported directives. If \var{string} cannot be parsed according to |
| 213 | \var{format}, \exception{ValueError} is raised. This function may not |
| 214 | be defined on all platforms. |
Guido van Rossum | 5d23758 | 1998-06-09 16:30:56 +0000 | [diff] [blame] | 215 | \end{funcdesc} |
| 216 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 217 | \begin{funcdesc}{time}{} |
| 218 | Return the time as a floating point number expressed in seconds since |
| 219 | the epoch, in UTC. Note that even though the time is always returned |
| 220 | 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] | 221 | precision than 1 second. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 222 | \end{funcdesc} |
| 223 | |
| 224 | \begin{datadesc}{timezone} |
| 225 | The offset of the local (non-DST) timezone, in seconds west of the 0th |
| 226 | meridian (i.e. negative in most of Western Europe, positive in the US, |
| 227 | zero in the UK). |
| 228 | \end{datadesc} |
| 229 | |
| 230 | \begin{datadesc}{tzname} |
| 231 | A tuple of two strings: the first is the name of the local non-DST |
| 232 | timezone, the second is the name of the local DST timezone. If no DST |
| 233 | timezone is defined, the second string should not be used. |
| 234 | \end{datadesc} |
Guido van Rossum | 8cf2db4 | 1996-07-30 18:32:04 +0000 | [diff] [blame] | 235 | |