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