blob: f63ecb06b14215e3235f96d8843e366e112ad37a [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{time} ---
2 Time access and conversions.}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{builtin}{time}
4
5\modulesynopsis{Time access and conversions.}
6
Fred Drake2cfc8351998-04-03 06:12:21 +00007
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00008This module provides various time-related functions.
Guido van Rossumbd851cd1994-08-23 13:26:22 +00009It is always available.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000010
11An explanation of some terminology and conventions is in order.
12
13\begin{itemize}
14
15\item
Fred Drakeeb4ed151998-04-11 04:52:15 +000016The \dfn{epoch}\index{epoch} is the point where the time starts. On
17January 1st of that year, at 0 hours, the ``time since the epoch'' is
18zero. For \UNIX{}, the epoch is 1970. To find out what the epoch is,
Guido van Rossum929bd0e1998-06-09 21:25:41 +000019look at \code{gmtime(0)}.%
20\index{epoch}
21
22\item
23The functions in this module don't handle dates and times before the
24epoch or far in the future. The cut-off point in the future is
25determined by the C library; for \UNIX{}, it is typically in 2038.%
26\index{Year 2038}
27
28\item
29Year 2000 (Y2K) issues: Python depends on the platform's C library,
30which generally doesn't have year 2000 issues, since all dates and
31times are represented internally as seconds since the epoch.%
32\index{Year 2000}%
33\index{Y2K}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000034
35\item
36UTC is Coordinated Universal Time (formerly known as Greenwich Mean
37Time). The acronym UTC is not a mistake but a compromise between
Fred Drakeeb4ed151998-04-11 04:52:15 +000038English and French.%
39\index{UTC}%
40\index{Coordinated Universal Time}%
41\index{Greenwich Mean Time}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000042
43\item
44DST is Daylight Saving Time, an adjustment of the timezone by
45(usually) one hour during part of the year. DST rules are magic
Fred Drake2cfc8351998-04-03 06:12:21 +000046(determined by local law) and can change from year to year. The \C{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000047library has a table containing the local rules (often it is read from
48a system file for flexibility) and is the only source of True Wisdom
Fred Drakeeb4ed151998-04-11 04:52:15 +000049in this respect.%
50\index{Daylight Saving Time}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000051
52\item
53The precision of the various real-time functions may be less than
54suggested by the units in which their value or argument is expressed.
Fred Drake094579e1996-12-13 22:09:52 +000055E.g.\ on most \UNIX{} systems, the clock ``ticks'' only 50 or 100 times a
Guido van Rossum470be141995-03-17 16:07:09 +000056second, and on the Mac, times are only accurate to whole seconds.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000057
Guido van Rossum8cf2db41996-07-30 18:32:04 +000058\item
Fred Drake2cfc8351998-04-03 06:12:21 +000059On the other hand, the precision of \function{time()} and
60\function{sleep()} is better than their \UNIX{} equivalents: times are
61expressed as floating point numbers, \function{time()} returns the
62most accurate time available (using \UNIX{} \cfunction{gettimeofday()}
63where available), and \function{sleep()} will accept a time with a
64nonzero fraction (\UNIX{} \cfunction{select()} is used to implement
65this, where available).
Guido van Rossum21be1471996-12-12 17:59:37 +000066
67\item
Guido van Rossum929bd0e1998-06-09 21:25:41 +000068The time tuple as returned by \function{gmtime()},
69\function{localtime()}, and \function{strptime()}, and accepted by
70\function{asctime()}, \function{mktime()} and \function{strftime()}, is a
Fred Drake2cfc8351998-04-03 06:12:21 +000071tuple of 9 integers: year (e.g.\ 1993), month (1--12), day (1--31),
72hour (0--23), minute (0--59), second (0--59), weekday (0--6, monday is
730), Julian day (1--366) and daylight savings flag (-1, 0 or 1).
74Note that unlike the \C{} structure, the month value is a range of 1-12, not
Guido van Rossumf259efe1997-11-25 01:00:40 +0000750-11. A year value less than 100 will typically be silently converted to
Fred Drake2cfc8351998-04-03 06:12:21 +0000761900 plus the year value. A \code{-1} argument as daylight savings
77flag, passed to \function{mktime()} will usually result in the correct
78daylight savings state to be filled in.
Guido van Rossum8cf2db41996-07-30 18:32:04 +000079
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000080\end{itemize}
81
Guido van Rossum470be141995-03-17 16:07:09 +000082The module defines the following functions and data items:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000083
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000084
85\begin{datadesc}{altzone}
86The offset of the local DST timezone, in seconds west of the 0th
Guido van Rossum470be141995-03-17 16:07:09 +000087meridian, if one is defined. Negative if the local DST timezone is
88east of the 0th meridian (as in Western Europe, including the UK).
89Only use this if \code{daylight} is nonzero.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000090\end{datadesc}
91
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000092\begin{funcdesc}{asctime}{tuple}
93Convert a tuple representing a time as returned by \code{gmtime()} or
94\code{localtime()} to a 24-character string of the following form:
95\code{'Sun Jun 20 23:21:05 1993'}. Note: unlike the C function of
96the same name, there is no trailing newline.
97\end{funcdesc}
98
Guido van Rossumbd851cd1994-08-23 13:26:22 +000099\begin{funcdesc}{clock}{}
100Return the current CPU time as a floating point number expressed in
Guido van Rossum470be141995-03-17 16:07:09 +0000101seconds. The precision, and in fact the very definiton of the meaning
Fred Drake2cfc8351998-04-03 06:12:21 +0000102of ``CPU time''\index{CPU time}, depends on that of the \C{} function
103of the same name, but in any case, this is the function to use for
104benchmarking\index{benchmarking} Python or timing algorithms.
Guido van Rossumbd851cd1994-08-23 13:26:22 +0000105\end{funcdesc}
106
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000107\begin{funcdesc}{ctime}{secs}
108Convert a time expressed in seconds since the epoch to a string
Fred Drake2cfc8351998-04-03 06:12:21 +0000109representing local time. \code{ctime(\var{secs})} is equivalent to
110\code{asctime(localtime(\var{secs}))}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000111\end{funcdesc}
112
113\begin{datadesc}{daylight}
114Nonzero if a DST timezone is defined.
115\end{datadesc}
116
117\begin{funcdesc}{gmtime}{secs}
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000118Convert a time expressed in seconds since the epoch to a time tuple
119in UTC in which the dst flag is always zero. Fractions of a second are
Guido van Rossum929bd0e1998-06-09 21:25:41 +0000120ignored. See above for a description of the tuple lay-out.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000121\end{funcdesc}
122
123\begin{funcdesc}{localtime}{secs}
Fred Drake2cfc8351998-04-03 06:12:21 +0000124Like \function{gmtime()} but converts to local time. The dst flag is
125set to \code{1} when DST applies to the given time.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000126\end{funcdesc}
127
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000128\begin{funcdesc}{mktime}{tuple}
129This is the inverse function of \code{localtime}. Its argument is the
Fred Drake2cfc8351998-04-03 06:12:21 +0000130full 9-tuple (since the dst flag is needed --- pass \code{-1} as the
131dst flag if it is unknown) which expresses the time
Fred Drake094579e1996-12-13 22:09:52 +0000132in \emph{local} time, not UTC. It returns a floating
Fred Drake2cfc8351998-04-03 06:12:21 +0000133point number, for compatibility with \function{time()}. If the input
134value cannot be represented as a valid time, \exception{OverflowError}
135is raised.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000136\end{funcdesc}
137
138\begin{funcdesc}{sleep}{secs}
139Suspend execution for the given number of seconds. The argument may
140be a floating point number to indicate a more precise sleep time.
141\end{funcdesc}
142
Guido van Rossum26ee8091995-09-13 17:37:49 +0000143\begin{funcdesc}{strftime}{format, tuple}
144Convert a tuple representing a time as returned by \code{gmtime()} or
145\code{localtime()} to a string as specified by the format argument.
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000146
Fred Drake094579e1996-12-13 22:09:52 +0000147The following directives, shown without the optional field width and
148precision specification, are replaced by the indicated characters:
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000149
Fred Drakeee601911998-04-11 20:53:03 +0000150\begin{tableii}{c|p{24em}}{code}{Directive}{Meaning}
Fred Drake2cfc8351998-04-03 06:12:21 +0000151 \lineii{\%a}{Locale's abbreviated weekday name.}
152 \lineii{\%A}{Locale's full weekday name.}
153 \lineii{\%b}{Locale's abbreviated month name.}
154 \lineii{\%B}{Locale's full month name.}
155 \lineii{\%c}{Locale's appropriate date and time representation.}
156 \lineii{\%d}{Day of the month as a decimal number [01,31].}
157 \lineii{\%H}{Hour (24-hour clock) as a decimal number [00,23].}
158 \lineii{\%I}{Hour (12-hour clock) as a decimal number [01,12].}
159 \lineii{\%j}{Day of the year as a decimal number [001,366].}
160 \lineii{\%m}{Month as a decimal number [01,12].}
161 \lineii{\%M}{Minute as a decimal number [00,59].}
162 \lineii{\%p}{Locale's equivalent of either AM or PM.}
163 \lineii{\%S}{Second as a decimal number [00,61].}
164 \lineii{\%U}{Week number of the year (Sunday as the first day of the
165 week) as a decimal number [00,53]. All days in a new year
166 preceding the first Sunday are considered to be in week 0.}
167 \lineii{\%w}{Weekday as a decimal number [0(Sunday),6].}
168 \lineii{\%W}{Week number of the year (Monday as the first day of the
169 week) as a decimal number [00,53]. All days in a new year
170 preceding the first Sunday are considered to be in week 0.}
171 \lineii{\%x}{Locale's appropriate date representation.}
172 \lineii{\%X}{Locale's appropriate time representation.}
173 \lineii{\%y}{Year without century as a decimal number [00,99].}
174 \lineii{\%Y}{Year with century as a decimal number.}
175 \lineii{\%Z}{Time zone name (or by no characters if no time zone exists).}
176 \lineii{\%\%}{\%}
Fred Drake094579e1996-12-13 22:09:52 +0000177\end{tableii}
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000178
Fred Drake094579e1996-12-13 22:09:52 +0000179Additional directives may be supported on certain platforms, but
180only the ones listed here have a meaning standardized by ANSI C.
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000181
Fred Drake094579e1996-12-13 22:09:52 +0000182On some platforms, an optional field width and precision
Fred Drake2cfc8351998-04-03 06:12:21 +0000183specification can immediately follow the initial \code{\%} of a
Fred Drake094579e1996-12-13 22:09:52 +0000184directive in the following order; this is also not portable.
Fred Drake2cfc8351998-04-03 06:12:21 +0000185The field width is normally 2 except for \code{\%j} where it is 3.
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000186
Guido van Rossum26ee8091995-09-13 17:37:49 +0000187\end{funcdesc}
188
Guido van Rossum5d237581998-06-09 16:30:56 +0000189\begin{funcdesc}{strptime}{string\optional{, format}}
190Parse a string representing a time according to a format. The return
191value is a tuple as returned by \code{gmtime()} or \code{localtime()}.
192The format uses the same directives as those used by
193\code{strftime()}; it defaults to \code{"\%a \%b \%d \%H:\%M:\%S \%Y"}
194which matches the formatting returned by \code{ctime()}. The same
195platform caveats apply; see the local Unix documentation for
196restrictions or additional supported directives. This function may
197not be defined on all platforms.
198
199\end{funcdesc}
200
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000201\begin{funcdesc}{time}{}
202Return the time as a floating point number expressed in seconds since
203the epoch, in UTC. Note that even though the time is always returned
204as a floating point number, not all systems provide time with a better
Guido van Rossumbd851cd1994-08-23 13:26:22 +0000205precision than 1 second.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000206\end{funcdesc}
207
208\begin{datadesc}{timezone}
209The offset of the local (non-DST) timezone, in seconds west of the 0th
210meridian (i.e. negative in most of Western Europe, positive in the US,
211zero in the UK).
212\end{datadesc}
213
214\begin{datadesc}{tzname}
215A tuple of two strings: the first is the name of the local non-DST
216timezone, the second is the name of the local DST timezone. If no DST
217timezone is defined, the second string should not be used.
218\end{datadesc}
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000219