blob: f530a4752c4b0c7b79e6e362bda0fe091425f6a1 [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
Fred Drake0ad55fb1998-12-08 19:59:36 +000023The functions in this module do not handle dates and times before the
Guido van Rossum929bd0e1998-06-09 21:25:41 +000024epoch or far in the future. The cut-off point in the future is
Fred Drake0ad55fb1998-12-08 19:59:36 +000025determined by the \C{} library; for \UNIX{}, it is typically in 2038.%
Guido van Rossum929bd0e1998-06-09 21:25:41 +000026\index{Year 2038}
27
28\item
Fred Drake0ad55fb1998-12-08 19:59:36 +000029\strong{Year 2000 (Y2K) issues}: Python depends on the platform's \C{}
30library, which generally doesn't have year 2000 issues, since all
31dates and times are represented internally as seconds since the
32epoch. Functions accepting a time tuple (see below) generally require
33a 4-digit year. For backward compatibility, 2-digit years are
34supported if the module variable \code{accept2dyear} is a non-zero
35integer; this variable is initialized to \code{1} unless the
36environment variable \envvar{PYTHONY2K} is set to a non-empty string,
37in which case it is initialized to \code{0}. Thus, you can set
38\envvar{PYTHONY2K} in the environment to \code{x} to require 4-digit
39years for all year input. When 2-digit years are accepted, they are
40converted according to the \POSIX{} or X/Open standard: values 69-99
41are mapped to 1969-1999, and values 0--68 are mapped to 2000--2068.
42Values 100--1899 are always illegal. Note that this is new as of
43Python 1.5.2(a2); earlier versions, up to Python 1.5.1 and 1.5.2a1,
44would add 1900 to year values below 1900.%
Guido van Rossum929bd0e1998-06-09 21:25:41 +000045\index{Year 2000}%
46\index{Y2K}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000047
48\item
49UTC is Coordinated Universal Time (formerly known as Greenwich Mean
Fred Drake0ad55fb1998-12-08 19:59:36 +000050Time, or GMT). The acronym UTC is not a mistake but a compromise
51between English and French.%
Fred Drakeeb4ed151998-04-11 04:52:15 +000052\index{UTC}%
53\index{Coordinated Universal Time}%
54\index{Greenwich Mean Time}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000055
56\item
57DST is Daylight Saving Time, an adjustment of the timezone by
58(usually) one hour during part of the year. DST rules are magic
Fred Drake2cfc8351998-04-03 06:12:21 +000059(determined by local law) and can change from year to year. The \C{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000060library has a table containing the local rules (often it is read from
61a system file for flexibility) and is the only source of True Wisdom
Fred Drakeeb4ed151998-04-11 04:52:15 +000062in this respect.%
63\index{Daylight Saving Time}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000064
65\item
66The precision of the various real-time functions may be less than
67suggested by the units in which their value or argument is expressed.
Fred Drake094579e1996-12-13 22:09:52 +000068E.g.\ on most \UNIX{} systems, the clock ``ticks'' only 50 or 100 times a
Guido van Rossum470be141995-03-17 16:07:09 +000069second, and on the Mac, times are only accurate to whole seconds.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000070
Guido van Rossum8cf2db41996-07-30 18:32:04 +000071\item
Fred Drake2cfc8351998-04-03 06:12:21 +000072On the other hand, the precision of \function{time()} and
73\function{sleep()} is better than their \UNIX{} equivalents: times are
74expressed as floating point numbers, \function{time()} returns the
75most accurate time available (using \UNIX{} \cfunction{gettimeofday()}
76where available), and \function{sleep()} will accept a time with a
77nonzero fraction (\UNIX{} \cfunction{select()} is used to implement
78this, where available).
Guido van Rossum21be1471996-12-12 17:59:37 +000079
80\item
Guido van Rossum929bd0e1998-06-09 21:25:41 +000081The 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 Drake2cfc8351998-04-03 06:12:21 +000084tuple of 9 integers: year (e.g.\ 1993), month (1--12), day (1--31),
85hour (0--23), minute (0--59), second (0--59), weekday (0--6, monday is
860), Julian day (1--366) and daylight savings flag (-1, 0 or 1).
87Note that unlike the \C{} structure, the month value is a range of 1-12, not
Guido van Rossumf259efe1997-11-25 01:00:40 +0000880-11. A year value less than 100 will typically be silently converted to
Fred Drake2cfc8351998-04-03 06:12:21 +0000891900 plus the year value. A \code{-1} argument as daylight savings
90flag, passed to \function{mktime()} will usually result in the correct
91daylight savings state to be filled in.
Guido van Rossum8cf2db41996-07-30 18:32:04 +000092
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000093\end{itemize}
94
Guido van Rossum470be141995-03-17 16:07:09 +000095The module defines the following functions and data items:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000096
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000097
98\begin{datadesc}{altzone}
99The offset of the local DST timezone, in seconds west of the 0th
Guido van Rossum470be141995-03-17 16:07:09 +0000100meridian, if one is defined. Negative if the local DST timezone is
101east of the 0th meridian (as in Western Europe, including the UK).
102Only use this if \code{daylight} is nonzero.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000103\end{datadesc}
104
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000105\begin{funcdesc}{asctime}{tuple}
Fred Drake0ad55fb1998-12-08 19:59:36 +0000106Convert a tuple representing a time as returned by \function{gmtime()}
107or \function{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
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000109the same name, there is no trailing newline.
110\end{funcdesc}
111
Guido van Rossumbd851cd1994-08-23 13:26:22 +0000112\begin{funcdesc}{clock}{}
113Return the current CPU time as a floating point number expressed in
Guido van Rossum470be141995-03-17 16:07:09 +0000114seconds. The precision, and in fact the very definiton of the meaning
Fred Drake2cfc8351998-04-03 06:12:21 +0000115of ``CPU time''\index{CPU time}, depends on that of the \C{} function
116of the same name, but in any case, this is the function to use for
117benchmarking\index{benchmarking} Python or timing algorithms.
Guido van Rossumbd851cd1994-08-23 13:26:22 +0000118\end{funcdesc}
119
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000120\begin{funcdesc}{ctime}{secs}
121Convert a time expressed in seconds since the epoch to a string
Fred Drake2cfc8351998-04-03 06:12:21 +0000122representing local time. \code{ctime(\var{secs})} is equivalent to
123\code{asctime(localtime(\var{secs}))}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000124\end{funcdesc}
125
126\begin{datadesc}{daylight}
127Nonzero if a DST timezone is defined.
128\end{datadesc}
129
130\begin{funcdesc}{gmtime}{secs}
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000131Convert a time expressed in seconds since the epoch to a time tuple
132in UTC in which the dst flag is always zero. Fractions of a second are
Guido van Rossum929bd0e1998-06-09 21:25:41 +0000133ignored. See above for a description of the tuple lay-out.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000134\end{funcdesc}
135
136\begin{funcdesc}{localtime}{secs}
Fred Drake2cfc8351998-04-03 06:12:21 +0000137Like \function{gmtime()} but converts to local time. The dst flag is
138set to \code{1} when DST applies to the given time.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000139\end{funcdesc}
140
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000141\begin{funcdesc}{mktime}{tuple}
Fred Drake0ad55fb1998-12-08 19:59:36 +0000142This is the inverse function of \function{localtime()}. Its argument
143is the full 9-tuple (since the dst flag is needed --- pass \code{-1}
144as the dst flag if it is unknown) which expresses the time in
145\emph{local} time, not UTC. It returns a floating point number, for
146compatibility with \function{time()}. If the input value cannot be
147represented as a valid time, \exception{OverflowError} is raised.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000148\end{funcdesc}
149
150\begin{funcdesc}{sleep}{secs}
151Suspend execution for the given number of seconds. The argument may
152be a floating point number to indicate a more precise sleep time.
153\end{funcdesc}
154
Guido van Rossum26ee8091995-09-13 17:37:49 +0000155\begin{funcdesc}{strftime}{format, tuple}
Fred Drake0ad55fb1998-12-08 19:59:36 +0000156Convert a tuple representing a time as returned by \function{gmtime()}
157or \function{localtime()} to a string as specified by the \var{format}
158argument. \var{format} must be a string.
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000159
Fred Drake0ad55fb1998-12-08 19:59:36 +0000160The following directives can be embedded in the \var{format} string.
161They are shown without the optional field width and precision
162specification, and are replaced by the indicated characters in the
163\function{strftime()} result:
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000164
Fred Drakeee601911998-04-11 20:53:03 +0000165\begin{tableii}{c|p{24em}}{code}{Directive}{Meaning}
Fred Drake2cfc8351998-04-03 06:12:21 +0000166 \lineii{\%a}{Locale's abbreviated weekday name.}
167 \lineii{\%A}{Locale's full weekday name.}
168 \lineii{\%b}{Locale's abbreviated month name.}
169 \lineii{\%B}{Locale's full month name.}
170 \lineii{\%c}{Locale's appropriate date and time representation.}
171 \lineii{\%d}{Day of the month as a decimal number [01,31].}
172 \lineii{\%H}{Hour (24-hour clock) as a decimal number [00,23].}
173 \lineii{\%I}{Hour (12-hour clock) as a decimal number [01,12].}
174 \lineii{\%j}{Day of the year as a decimal number [001,366].}
175 \lineii{\%m}{Month as a decimal number [01,12].}
176 \lineii{\%M}{Minute as a decimal number [00,59].}
177 \lineii{\%p}{Locale's equivalent of either AM or PM.}
178 \lineii{\%S}{Second as a decimal number [00,61].}
179 \lineii{\%U}{Week number of the year (Sunday as the first day of the
180 week) as a decimal number [00,53]. All days in a new year
181 preceding the first Sunday are considered to be in week 0.}
182 \lineii{\%w}{Weekday as a decimal number [0(Sunday),6].}
183 \lineii{\%W}{Week number of the year (Monday as the first day of the
184 week) as a decimal number [00,53]. All days in a new year
185 preceding the first Sunday are considered to be in week 0.}
186 \lineii{\%x}{Locale's appropriate date representation.}
187 \lineii{\%X}{Locale's appropriate time representation.}
188 \lineii{\%y}{Year without century as a decimal number [00,99].}
189 \lineii{\%Y}{Year with century as a decimal number.}
190 \lineii{\%Z}{Time zone name (or by no characters if no time zone exists).}
191 \lineii{\%\%}{\%}
Fred Drake094579e1996-12-13 22:09:52 +0000192\end{tableii}
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000193
Fred Drake094579e1996-12-13 22:09:52 +0000194Additional directives may be supported on certain platforms, but
Fred Drake0ad55fb1998-12-08 19:59:36 +0000195only the ones listed here have a meaning standardized by ANSI \C{}.
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000196
Fred Drake094579e1996-12-13 22:09:52 +0000197On some platforms, an optional field width and precision
Fred Drake0ad55fb1998-12-08 19:59:36 +0000198specification can immediately follow the initial \character{\%} of a
Fred Drake094579e1996-12-13 22:09:52 +0000199directive in the following order; this is also not portable.
Fred Drake2cfc8351998-04-03 06:12:21 +0000200The field width is normally 2 except for \code{\%j} where it is 3.
Guido van Rossum26ee8091995-09-13 17:37:49 +0000201\end{funcdesc}
202
Guido van Rossum5d237581998-06-09 16:30:56 +0000203\begin{funcdesc}{strptime}{string\optional{, format}}
204Parse a string representing a time according to a format. The return
Fred Drake0ad55fb1998-12-08 19:59:36 +0000205value is a tuple as returned by \function{gmtime()} or
206\function{localtime()}. The \var{format} parameter uses the same
207directives as those used by \function{strftime()}; it defaults to
208\code{"\%a \%b \%d \%H:\%M:\%S \%Y"} which matches the formatting
209returned by \function{ctime()}. The same platform caveats apply; see
210the local \UNIX{} documentation for restrictions or additional
211supported directives. If \var{string} cannot be parsed according to
212\var{format}, \exception{ValueError} is raised. This function may not
213be defined on all platforms.
Guido van Rossum5d237581998-06-09 16:30:56 +0000214\end{funcdesc}
215
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000216\begin{funcdesc}{time}{}
217Return the time as a floating point number expressed in seconds since
218the epoch, in UTC. Note that even though the time is always returned
219as a floating point number, not all systems provide time with a better
Guido van Rossumbd851cd1994-08-23 13:26:22 +0000220precision than 1 second.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000221\end{funcdesc}
222
223\begin{datadesc}{timezone}
224The offset of the local (non-DST) timezone, in seconds west of the 0th
225meridian (i.e. negative in most of Western Europe, positive in the US,
226zero in the UK).
227\end{datadesc}
228
229\begin{datadesc}{tzname}
230A tuple of two strings: the first is the name of the local non-DST
231timezone, the second is the name of the local DST timezone. If no DST
232timezone is defined, the second string should not be used.
233\end{datadesc}
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000234