blob: 711347b30b879b5b2de08de645ef5f7f18c556d7 [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Built-in Module \module{time}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-time}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00003\bimodindex{time}
Fred Drake2cfc8351998-04-03 06:12:21 +00004
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00005This module provides various time-related functions.
Guido van Rossumbd851cd1994-08-23 13:26:22 +00006It is always available.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00007
8An explanation of some terminology and conventions is in order.
9
10\begin{itemize}
11
12\item
Fred Drakeeb4ed151998-04-11 04:52:15 +000013The \dfn{epoch}\index{epoch} is the point where the time starts. On
14January 1st of that year, at 0 hours, the ``time since the epoch'' is
15zero. For \UNIX{}, the epoch is 1970. To find out what the epoch is,
Guido van Rossum929bd0e1998-06-09 21:25:41 +000016look at \code{gmtime(0)}.%
17\index{epoch}
18
19\item
20The functions in this module don't handle dates and times before the
21epoch or far in the future. The cut-off point in the future is
22determined by the C library; for \UNIX{}, it is typically in 2038.%
23\index{Year 2038}
24
25\item
26Year 2000 (Y2K) issues: Python depends on the platform's C library,
27which generally doesn't have year 2000 issues, since all dates and
28times are represented internally as seconds since the epoch.%
29\index{Year 2000}%
30\index{Y2K}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000031
32\item
33UTC is Coordinated Universal Time (formerly known as Greenwich Mean
34Time). The acronym UTC is not a mistake but a compromise between
Fred Drakeeb4ed151998-04-11 04:52:15 +000035English and French.%
36\index{UTC}%
37\index{Coordinated Universal Time}%
38\index{Greenwich Mean Time}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000039
40\item
41DST is Daylight Saving Time, an adjustment of the timezone by
42(usually) one hour during part of the year. DST rules are magic
Fred Drake2cfc8351998-04-03 06:12:21 +000043(determined by local law) and can change from year to year. The \C{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000044library has a table containing the local rules (often it is read from
45a system file for flexibility) and is the only source of True Wisdom
Fred Drakeeb4ed151998-04-11 04:52:15 +000046in this respect.%
47\index{Daylight Saving Time}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000048
49\item
50The precision of the various real-time functions may be less than
51suggested by the units in which their value or argument is expressed.
Fred Drake094579e1996-12-13 22:09:52 +000052E.g.\ on most \UNIX{} systems, the clock ``ticks'' only 50 or 100 times a
Guido van Rossum470be141995-03-17 16:07:09 +000053second, and on the Mac, times are only accurate to whole seconds.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000054
Guido van Rossum8cf2db41996-07-30 18:32:04 +000055\item
Fred Drake2cfc8351998-04-03 06:12:21 +000056On the other hand, the precision of \function{time()} and
57\function{sleep()} is better than their \UNIX{} equivalents: times are
58expressed as floating point numbers, \function{time()} returns the
59most accurate time available (using \UNIX{} \cfunction{gettimeofday()}
60where available), and \function{sleep()} will accept a time with a
61nonzero fraction (\UNIX{} \cfunction{select()} is used to implement
62this, where available).
Guido van Rossum21be1471996-12-12 17:59:37 +000063
64\item
Guido van Rossum929bd0e1998-06-09 21:25:41 +000065The time tuple as returned by \function{gmtime()},
66\function{localtime()}, and \function{strptime()}, and accepted by
67\function{asctime()}, \function{mktime()} and \function{strftime()}, is a
Fred Drake2cfc8351998-04-03 06:12:21 +000068tuple of 9 integers: year (e.g.\ 1993), month (1--12), day (1--31),
69hour (0--23), minute (0--59), second (0--59), weekday (0--6, monday is
700), Julian day (1--366) and daylight savings flag (-1, 0 or 1).
71Note that unlike the \C{} structure, the month value is a range of 1-12, not
Guido van Rossumf259efe1997-11-25 01:00:40 +0000720-11. A year value less than 100 will typically be silently converted to
Fred Drake2cfc8351998-04-03 06:12:21 +0000731900 plus the year value. A \code{-1} argument as daylight savings
74flag, passed to \function{mktime()} will usually result in the correct
75daylight savings state to be filled in.
Guido van Rossum8cf2db41996-07-30 18:32:04 +000076
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000077\end{itemize}
78
Guido van Rossum470be141995-03-17 16:07:09 +000079The module defines the following functions and data items:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000080
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000081
82\begin{datadesc}{altzone}
83The offset of the local DST timezone, in seconds west of the 0th
Guido van Rossum470be141995-03-17 16:07:09 +000084meridian, if one is defined. Negative if the local DST timezone is
85east of the 0th meridian (as in Western Europe, including the UK).
86Only use this if \code{daylight} is nonzero.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000087\end{datadesc}
88
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000089\begin{funcdesc}{asctime}{tuple}
90Convert a tuple representing a time as returned by \code{gmtime()} or
91\code{localtime()} to a 24-character string of the following form:
92\code{'Sun Jun 20 23:21:05 1993'}. Note: unlike the C function of
93the same name, there is no trailing newline.
94\end{funcdesc}
95
Guido van Rossumbd851cd1994-08-23 13:26:22 +000096\begin{funcdesc}{clock}{}
97Return the current CPU time as a floating point number expressed in
Guido van Rossum470be141995-03-17 16:07:09 +000098seconds. The precision, and in fact the very definiton of the meaning
Fred Drake2cfc8351998-04-03 06:12:21 +000099of ``CPU time''\index{CPU time}, depends on that of the \C{} function
100of the same name, but in any case, this is the function to use for
101benchmarking\index{benchmarking} Python or timing algorithms.
Guido van Rossumbd851cd1994-08-23 13:26:22 +0000102\end{funcdesc}
103
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000104\begin{funcdesc}{ctime}{secs}
105Convert a time expressed in seconds since the epoch to a string
Fred Drake2cfc8351998-04-03 06:12:21 +0000106representing local time. \code{ctime(\var{secs})} is equivalent to
107\code{asctime(localtime(\var{secs}))}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000108\end{funcdesc}
109
110\begin{datadesc}{daylight}
111Nonzero if a DST timezone is defined.
112\end{datadesc}
113
114\begin{funcdesc}{gmtime}{secs}
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000115Convert a time expressed in seconds since the epoch to a time tuple
116in UTC in which the dst flag is always zero. Fractions of a second are
Guido van Rossum929bd0e1998-06-09 21:25:41 +0000117ignored. See above for a description of the tuple lay-out.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000118\end{funcdesc}
119
120\begin{funcdesc}{localtime}{secs}
Fred Drake2cfc8351998-04-03 06:12:21 +0000121Like \function{gmtime()} but converts to local time. The dst flag is
122set to \code{1} when DST applies to the given time.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000123\end{funcdesc}
124
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000125\begin{funcdesc}{mktime}{tuple}
126This is the inverse function of \code{localtime}. Its argument is the
Fred Drake2cfc8351998-04-03 06:12:21 +0000127full 9-tuple (since the dst flag is needed --- pass \code{-1} as the
128dst flag if it is unknown) which expresses the time
Fred Drake094579e1996-12-13 22:09:52 +0000129in \emph{local} time, not UTC. It returns a floating
Fred Drake2cfc8351998-04-03 06:12:21 +0000130point number, for compatibility with \function{time()}. If the input
131value cannot be represented as a valid time, \exception{OverflowError}
132is raised.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000133\end{funcdesc}
134
135\begin{funcdesc}{sleep}{secs}
136Suspend execution for the given number of seconds. The argument may
137be a floating point number to indicate a more precise sleep time.
138\end{funcdesc}
139
Guido van Rossum26ee8091995-09-13 17:37:49 +0000140\begin{funcdesc}{strftime}{format, tuple}
141Convert a tuple representing a time as returned by \code{gmtime()} or
142\code{localtime()} to a string as specified by the format argument.
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000143
Fred Drake094579e1996-12-13 22:09:52 +0000144The following directives, shown without the optional field width and
145precision specification, are replaced by the indicated characters:
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000146
Fred Drakeee601911998-04-11 20:53:03 +0000147\begin{tableii}{c|p{24em}}{code}{Directive}{Meaning}
Fred Drake2cfc8351998-04-03 06:12:21 +0000148 \lineii{\%a}{Locale's abbreviated weekday name.}
149 \lineii{\%A}{Locale's full weekday name.}
150 \lineii{\%b}{Locale's abbreviated month name.}
151 \lineii{\%B}{Locale's full month name.}
152 \lineii{\%c}{Locale's appropriate date and time representation.}
153 \lineii{\%d}{Day of the month as a decimal number [01,31].}
154 \lineii{\%H}{Hour (24-hour clock) as a decimal number [00,23].}
155 \lineii{\%I}{Hour (12-hour clock) as a decimal number [01,12].}
156 \lineii{\%j}{Day of the year as a decimal number [001,366].}
157 \lineii{\%m}{Month as a decimal number [01,12].}
158 \lineii{\%M}{Minute as a decimal number [00,59].}
159 \lineii{\%p}{Locale's equivalent of either AM or PM.}
160 \lineii{\%S}{Second as a decimal number [00,61].}
161 \lineii{\%U}{Week number of the year (Sunday as the first day of the
162 week) as a decimal number [00,53]. All days in a new year
163 preceding the first Sunday are considered to be in week 0.}
164 \lineii{\%w}{Weekday as a decimal number [0(Sunday),6].}
165 \lineii{\%W}{Week number of the year (Monday as the first day of the
166 week) as a decimal number [00,53]. All days in a new year
167 preceding the first Sunday are considered to be in week 0.}
168 \lineii{\%x}{Locale's appropriate date representation.}
169 \lineii{\%X}{Locale's appropriate time representation.}
170 \lineii{\%y}{Year without century as a decimal number [00,99].}
171 \lineii{\%Y}{Year with century as a decimal number.}
172 \lineii{\%Z}{Time zone name (or by no characters if no time zone exists).}
173 \lineii{\%\%}{\%}
Fred Drake094579e1996-12-13 22:09:52 +0000174\end{tableii}
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000175
Fred Drake094579e1996-12-13 22:09:52 +0000176Additional directives may be supported on certain platforms, but
177only the ones listed here have a meaning standardized by ANSI C.
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000178
Fred Drake094579e1996-12-13 22:09:52 +0000179On some platforms, an optional field width and precision
Fred Drake2cfc8351998-04-03 06:12:21 +0000180specification can immediately follow the initial \code{\%} of a
Fred Drake094579e1996-12-13 22:09:52 +0000181directive in the following order; this is also not portable.
Fred Drake2cfc8351998-04-03 06:12:21 +0000182The field width is normally 2 except for \code{\%j} where it is 3.
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000183
Guido van Rossum26ee8091995-09-13 17:37:49 +0000184\end{funcdesc}
185
Guido van Rossum5d237581998-06-09 16:30:56 +0000186\begin{funcdesc}{strptime}{string\optional{, format}}
187Parse a string representing a time according to a format. The return
188value is a tuple as returned by \code{gmtime()} or \code{localtime()}.
189The format uses the same directives as those used by
190\code{strftime()}; it defaults to \code{"\%a \%b \%d \%H:\%M:\%S \%Y"}
191which matches the formatting returned by \code{ctime()}. The same
192platform caveats apply; see the local Unix documentation for
193restrictions or additional supported directives. This function may
194not be defined on all platforms.
195
196\end{funcdesc}
197
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000198\begin{funcdesc}{time}{}
199Return the time as a floating point number expressed in seconds since
200the epoch, in UTC. Note that even though the time is always returned
201as a floating point number, not all systems provide time with a better
Guido van Rossumbd851cd1994-08-23 13:26:22 +0000202precision than 1 second.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000203\end{funcdesc}
204
205\begin{datadesc}{timezone}
206The offset of the local (non-DST) timezone, in seconds west of the 0th
207meridian (i.e. negative in most of Western Europe, positive in the US,
208zero in the UK).
209\end{datadesc}
210
211\begin{datadesc}{tzname}
212A tuple of two strings: the first is the name of the local non-DST
213timezone, the second is the name of the local DST timezone. If no DST
214timezone is defined, the second string should not be used.
215\end{datadesc}
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000216