blob: caafd501d3f8df806dc142a41570e0d4e499eb37 [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Built-in Module \module{time}}
Fred Drakeb91e9341998-07-23 17:59:49 +00002\declaremodule{builtin}{time}
3
4\modulesynopsis{Time access and conversions.}
5
Fred Drake2cfc8351998-04-03 06:12:21 +00006
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00007This module provides various time-related functions.
Guido van Rossumbd851cd1994-08-23 13:26:22 +00008It is always available.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00009
10An explanation of some terminology and conventions is in order.
11
12\begin{itemize}
13
14\item
Fred Drakeeb4ed151998-04-11 04:52:15 +000015The \dfn{epoch}\index{epoch} is the point where the time starts. On
16January 1st of that year, at 0 hours, the ``time since the epoch'' is
17zero. For \UNIX{}, the epoch is 1970. To find out what the epoch is,
Guido van Rossum929bd0e1998-06-09 21:25:41 +000018look at \code{gmtime(0)}.%
19\index{epoch}
20
21\item
22The functions in this module don't handle dates and times before the
23epoch or far in the future. The cut-off point in the future is
24determined by the C library; for \UNIX{}, it is typically in 2038.%
25\index{Year 2038}
26
27\item
28Year 2000 (Y2K) issues: Python depends on the platform's C library,
29which generally doesn't have year 2000 issues, since all dates and
30times are represented internally as seconds since the epoch.%
31\index{Year 2000}%
32\index{Y2K}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000033
34\item
35UTC is Coordinated Universal Time (formerly known as Greenwich Mean
36Time). The acronym UTC is not a mistake but a compromise between
Fred Drakeeb4ed151998-04-11 04:52:15 +000037English and French.%
38\index{UTC}%
39\index{Coordinated Universal Time}%
40\index{Greenwich Mean Time}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000041
42\item
43DST is Daylight Saving Time, an adjustment of the timezone by
44(usually) one hour during part of the year. DST rules are magic
Fred Drake2cfc8351998-04-03 06:12:21 +000045(determined by local law) and can change from year to year. The \C{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000046library has a table containing the local rules (often it is read from
47a system file for flexibility) and is the only source of True Wisdom
Fred Drakeeb4ed151998-04-11 04:52:15 +000048in this respect.%
49\index{Daylight Saving Time}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000050
51\item
52The precision of the various real-time functions may be less than
53suggested by the units in which their value or argument is expressed.
Fred Drake094579e1996-12-13 22:09:52 +000054E.g.\ on most \UNIX{} systems, the clock ``ticks'' only 50 or 100 times a
Guido van Rossum470be141995-03-17 16:07:09 +000055second, and on the Mac, times are only accurate to whole seconds.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000056
Guido van Rossum8cf2db41996-07-30 18:32:04 +000057\item
Fred Drake2cfc8351998-04-03 06:12:21 +000058On the other hand, the precision of \function{time()} and
59\function{sleep()} is better than their \UNIX{} equivalents: times are
60expressed as floating point numbers, \function{time()} returns the
61most accurate time available (using \UNIX{} \cfunction{gettimeofday()}
62where available), and \function{sleep()} will accept a time with a
63nonzero fraction (\UNIX{} \cfunction{select()} is used to implement
64this, where available).
Guido van Rossum21be1471996-12-12 17:59:37 +000065
66\item
Guido van Rossum929bd0e1998-06-09 21:25:41 +000067The time tuple as returned by \function{gmtime()},
68\function{localtime()}, and \function{strptime()}, and accepted by
69\function{asctime()}, \function{mktime()} and \function{strftime()}, is a
Fred Drake2cfc8351998-04-03 06:12:21 +000070tuple of 9 integers: year (e.g.\ 1993), month (1--12), day (1--31),
71hour (0--23), minute (0--59), second (0--59), weekday (0--6, monday is
720), Julian day (1--366) and daylight savings flag (-1, 0 or 1).
73Note that unlike the \C{} structure, the month value is a range of 1-12, not
Guido van Rossumf259efe1997-11-25 01:00:40 +0000740-11. A year value less than 100 will typically be silently converted to
Fred Drake2cfc8351998-04-03 06:12:21 +0000751900 plus the year value. A \code{-1} argument as daylight savings
76flag, passed to \function{mktime()} will usually result in the correct
77daylight savings state to be filled in.
Guido van Rossum8cf2db41996-07-30 18:32:04 +000078
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000079\end{itemize}
80
Guido van Rossum470be141995-03-17 16:07:09 +000081The module defines the following functions and data items:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000082
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000083
84\begin{datadesc}{altzone}
85The offset of the local DST timezone, in seconds west of the 0th
Guido van Rossum470be141995-03-17 16:07:09 +000086meridian, if one is defined. Negative if the local DST timezone is
87east of the 0th meridian (as in Western Europe, including the UK).
88Only use this if \code{daylight} is nonzero.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000089\end{datadesc}
90
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000091\begin{funcdesc}{asctime}{tuple}
92Convert a tuple representing a time as returned by \code{gmtime()} or
93\code{localtime()} to a 24-character string of the following form:
94\code{'Sun Jun 20 23:21:05 1993'}. Note: unlike the C function of
95the same name, there is no trailing newline.
96\end{funcdesc}
97
Guido van Rossumbd851cd1994-08-23 13:26:22 +000098\begin{funcdesc}{clock}{}
99Return the current CPU time as a floating point number expressed in
Guido van Rossum470be141995-03-17 16:07:09 +0000100seconds. The precision, and in fact the very definiton of the meaning
Fred Drake2cfc8351998-04-03 06:12:21 +0000101of ``CPU time''\index{CPU time}, depends on that of the \C{} function
102of the same name, but in any case, this is the function to use for
103benchmarking\index{benchmarking} Python or timing algorithms.
Guido van Rossumbd851cd1994-08-23 13:26:22 +0000104\end{funcdesc}
105
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000106\begin{funcdesc}{ctime}{secs}
107Convert a time expressed in seconds since the epoch to a string
Fred Drake2cfc8351998-04-03 06:12:21 +0000108representing local time. \code{ctime(\var{secs})} is equivalent to
109\code{asctime(localtime(\var{secs}))}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000110\end{funcdesc}
111
112\begin{datadesc}{daylight}
113Nonzero if a DST timezone is defined.
114\end{datadesc}
115
116\begin{funcdesc}{gmtime}{secs}
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000117Convert a time expressed in seconds since the epoch to a time tuple
118in UTC in which the dst flag is always zero. Fractions of a second are
Guido van Rossum929bd0e1998-06-09 21:25:41 +0000119ignored. See above for a description of the tuple lay-out.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000120\end{funcdesc}
121
122\begin{funcdesc}{localtime}{secs}
Fred Drake2cfc8351998-04-03 06:12:21 +0000123Like \function{gmtime()} but converts to local time. The dst flag is
124set to \code{1} when DST applies to the given time.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000125\end{funcdesc}
126
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000127\begin{funcdesc}{mktime}{tuple}
128This is the inverse function of \code{localtime}. Its argument is the
Fred Drake2cfc8351998-04-03 06:12:21 +0000129full 9-tuple (since the dst flag is needed --- pass \code{-1} as the
130dst flag if it is unknown) which expresses the time
Fred Drake094579e1996-12-13 22:09:52 +0000131in \emph{local} time, not UTC. It returns a floating
Fred Drake2cfc8351998-04-03 06:12:21 +0000132point number, for compatibility with \function{time()}. If the input
133value cannot be represented as a valid time, \exception{OverflowError}
134is raised.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000135\end{funcdesc}
136
137\begin{funcdesc}{sleep}{secs}
138Suspend execution for the given number of seconds. The argument may
139be a floating point number to indicate a more precise sleep time.
140\end{funcdesc}
141
Guido van Rossum26ee8091995-09-13 17:37:49 +0000142\begin{funcdesc}{strftime}{format, tuple}
143Convert a tuple representing a time as returned by \code{gmtime()} or
144\code{localtime()} to a string as specified by the format argument.
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000145
Fred Drake094579e1996-12-13 22:09:52 +0000146The following directives, shown without the optional field width and
147precision specification, are replaced by the indicated characters:
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000148
Fred Drakeee601911998-04-11 20:53:03 +0000149\begin{tableii}{c|p{24em}}{code}{Directive}{Meaning}
Fred Drake2cfc8351998-04-03 06:12:21 +0000150 \lineii{\%a}{Locale's abbreviated weekday name.}
151 \lineii{\%A}{Locale's full weekday name.}
152 \lineii{\%b}{Locale's abbreviated month name.}
153 \lineii{\%B}{Locale's full month name.}
154 \lineii{\%c}{Locale's appropriate date and time representation.}
155 \lineii{\%d}{Day of the month as a decimal number [01,31].}
156 \lineii{\%H}{Hour (24-hour clock) as a decimal number [00,23].}
157 \lineii{\%I}{Hour (12-hour clock) as a decimal number [01,12].}
158 \lineii{\%j}{Day of the year as a decimal number [001,366].}
159 \lineii{\%m}{Month as a decimal number [01,12].}
160 \lineii{\%M}{Minute as a decimal number [00,59].}
161 \lineii{\%p}{Locale's equivalent of either AM or PM.}
162 \lineii{\%S}{Second as a decimal number [00,61].}
163 \lineii{\%U}{Week number of the year (Sunday as the first day of the
164 week) as a decimal number [00,53]. All days in a new year
165 preceding the first Sunday are considered to be in week 0.}
166 \lineii{\%w}{Weekday as a decimal number [0(Sunday),6].}
167 \lineii{\%W}{Week number of the year (Monday as the first day of the
168 week) as a decimal number [00,53]. All days in a new year
169 preceding the first Sunday are considered to be in week 0.}
170 \lineii{\%x}{Locale's appropriate date representation.}
171 \lineii{\%X}{Locale's appropriate time representation.}
172 \lineii{\%y}{Year without century as a decimal number [00,99].}
173 \lineii{\%Y}{Year with century as a decimal number.}
174 \lineii{\%Z}{Time zone name (or by no characters if no time zone exists).}
175 \lineii{\%\%}{\%}
Fred Drake094579e1996-12-13 22:09:52 +0000176\end{tableii}
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000177
Fred Drake094579e1996-12-13 22:09:52 +0000178Additional directives may be supported on certain platforms, but
179only the ones listed here have a meaning standardized by ANSI C.
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000180
Fred Drake094579e1996-12-13 22:09:52 +0000181On some platforms, an optional field width and precision
Fred Drake2cfc8351998-04-03 06:12:21 +0000182specification can immediately follow the initial \code{\%} of a
Fred Drake094579e1996-12-13 22:09:52 +0000183directive in the following order; this is also not portable.
Fred Drake2cfc8351998-04-03 06:12:21 +0000184The field width is normally 2 except for \code{\%j} where it is 3.
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000185
Guido van Rossum26ee8091995-09-13 17:37:49 +0000186\end{funcdesc}
187
Guido van Rossum5d237581998-06-09 16:30:56 +0000188\begin{funcdesc}{strptime}{string\optional{, format}}
189Parse a string representing a time according to a format. The return
190value is a tuple as returned by \code{gmtime()} or \code{localtime()}.
191The format uses the same directives as those used by
192\code{strftime()}; it defaults to \code{"\%a \%b \%d \%H:\%M:\%S \%Y"}
193which matches the formatting returned by \code{ctime()}. The same
194platform caveats apply; see the local Unix documentation for
195restrictions or additional supported directives. This function may
196not be defined on all platforms.
197
198\end{funcdesc}
199
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000200\begin{funcdesc}{time}{}
201Return the time as a floating point number expressed in seconds since
202the epoch, in UTC. Note that even though the time is always returned
203as a floating point number, not all systems provide time with a better
Guido van Rossumbd851cd1994-08-23 13:26:22 +0000204precision than 1 second.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000205\end{funcdesc}
206
207\begin{datadesc}{timezone}
208The offset of the local (non-DST) timezone, in seconds west of the 0th
209meridian (i.e. negative in most of Western Europe, positive in the US,
210zero in the UK).
211\end{datadesc}
212
213\begin{datadesc}{tzname}
214A tuple of two strings: the first is the name of the local non-DST
215timezone, the second is the name of the local DST timezone. If no DST
216timezone is defined, the second string should not be used.
217\end{datadesc}
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000218