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