blob: 483469d86d5710bc6cd7bfd311f581a48864dcd6 [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,
16look at \code{gmtime(0)}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000017
18\item
19UTC is Coordinated Universal Time (formerly known as Greenwich Mean
20Time). The acronym UTC is not a mistake but a compromise between
Fred Drakeeb4ed151998-04-11 04:52:15 +000021English and French.%
22\index{UTC}%
23\index{Coordinated Universal Time}%
24\index{Greenwich Mean Time}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000025
26\item
27DST is Daylight Saving Time, an adjustment of the timezone by
28(usually) one hour during part of the year. DST rules are magic
Fred Drake2cfc8351998-04-03 06:12:21 +000029(determined by local law) and can change from year to year. The \C{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000030library has a table containing the local rules (often it is read from
31a system file for flexibility) and is the only source of True Wisdom
Fred Drakeeb4ed151998-04-11 04:52:15 +000032in this respect.%
33\index{Daylight Saving Time}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000034
35\item
36The precision of the various real-time functions may be less than
37suggested by the units in which their value or argument is expressed.
Fred Drake094579e1996-12-13 22:09:52 +000038E.g.\ on most \UNIX{} systems, the clock ``ticks'' only 50 or 100 times a
Guido van Rossum470be141995-03-17 16:07:09 +000039second, and on the Mac, times are only accurate to whole seconds.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000040
Guido van Rossum8cf2db41996-07-30 18:32:04 +000041\item
Fred Drake2cfc8351998-04-03 06:12:21 +000042On the other hand, the precision of \function{time()} and
43\function{sleep()} is better than their \UNIX{} equivalents: times are
44expressed as floating point numbers, \function{time()} returns the
45most accurate time available (using \UNIX{} \cfunction{gettimeofday()}
46where available), and \function{sleep()} will accept a time with a
47nonzero fraction (\UNIX{} \cfunction{select()} is used to implement
48this, where available).
Guido van Rossum21be1471996-12-12 17:59:37 +000049
50\item
Fred Drake2cfc8351998-04-03 06:12:21 +000051The time tuple as returned by \function{gmtime()} and
52\function{localtime()}, or as accpted by \function{mktime()} is a
53tuple of 9 integers: year (e.g.\ 1993), month (1--12), day (1--31),
54hour (0--23), minute (0--59), second (0--59), weekday (0--6, monday is
550), Julian day (1--366) and daylight savings flag (-1, 0 or 1).
56Note that unlike the \C{} structure, the month value is a range of 1-12, not
Guido van Rossumf259efe1997-11-25 01:00:40 +0000570-11. A year value less than 100 will typically be silently converted to
Fred Drake2cfc8351998-04-03 06:12:21 +0000581900 plus the year value. A \code{-1} argument as daylight savings
59flag, passed to \function{mktime()} will usually result in the correct
60daylight savings state to be filled in.
Guido van Rossum8cf2db41996-07-30 18:32:04 +000061
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000062\end{itemize}
63
Guido van Rossum470be141995-03-17 16:07:09 +000064The module defines the following functions and data items:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000065
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000066
67\begin{datadesc}{altzone}
68The offset of the local DST timezone, in seconds west of the 0th
Guido van Rossum470be141995-03-17 16:07:09 +000069meridian, if one is defined. Negative if the local DST timezone is
70east of the 0th meridian (as in Western Europe, including the UK).
71Only use this if \code{daylight} is nonzero.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000072\end{datadesc}
73
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000074\begin{funcdesc}{asctime}{tuple}
75Convert 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
78the same name, there is no trailing newline.
79\end{funcdesc}
80
Guido van Rossumbd851cd1994-08-23 13:26:22 +000081\begin{funcdesc}{clock}{}
82Return the current CPU time as a floating point number expressed in
Guido van Rossum470be141995-03-17 16:07:09 +000083seconds. The precision, and in fact the very definiton of the meaning
Fred Drake2cfc8351998-04-03 06:12:21 +000084of ``CPU time''\index{CPU time}, depends on that of the \C{} function
85of the same name, but in any case, this is the function to use for
86benchmarking\index{benchmarking} Python or timing algorithms.
Guido van Rossumbd851cd1994-08-23 13:26:22 +000087\end{funcdesc}
88
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000089\begin{funcdesc}{ctime}{secs}
90Convert a time expressed in seconds since the epoch to a string
Fred Drake2cfc8351998-04-03 06:12:21 +000091representing local time. \code{ctime(\var{secs})} is equivalent to
92\code{asctime(localtime(\var{secs}))}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000093\end{funcdesc}
94
95\begin{datadesc}{daylight}
96Nonzero if a DST timezone is defined.
97\end{datadesc}
98
99\begin{funcdesc}{gmtime}{secs}
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000100Convert a time expressed in seconds since the epoch to a time tuple
101in UTC in which the dst flag is always zero. Fractions of a second are
102ignored.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000103\end{funcdesc}
104
105\begin{funcdesc}{localtime}{secs}
Fred Drake2cfc8351998-04-03 06:12:21 +0000106Like \function{gmtime()} but converts to local time. The dst flag is
107set to \code{1} when DST applies to the given time.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000108\end{funcdesc}
109
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000110\begin{funcdesc}{mktime}{tuple}
111This is the inverse function of \code{localtime}. Its argument is the
Fred Drake2cfc8351998-04-03 06:12:21 +0000112full 9-tuple (since the dst flag is needed --- pass \code{-1} as the
113dst flag if it is unknown) which expresses the time
Fred Drake094579e1996-12-13 22:09:52 +0000114in \emph{local} time, not UTC. It returns a floating
Fred Drake2cfc8351998-04-03 06:12:21 +0000115point number, for compatibility with \function{time()}. If the input
116value cannot be represented as a valid time, \exception{OverflowError}
117is raised.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000118\end{funcdesc}
119
120\begin{funcdesc}{sleep}{secs}
121Suspend execution for the given number of seconds. The argument may
122be a floating point number to indicate a more precise sleep time.
123\end{funcdesc}
124
Guido van Rossum26ee8091995-09-13 17:37:49 +0000125\begin{funcdesc}{strftime}{format, tuple}
126Convert 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 Rossum8cf2db41996-07-30 18:32:04 +0000128
Fred Drake094579e1996-12-13 22:09:52 +0000129The following directives, shown without the optional field width and
130precision specification, are replaced by the indicated characters:
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000131
Fred Drakeee601911998-04-11 20:53:03 +0000132\begin{tableii}{c|p{24em}}{code}{Directive}{Meaning}
Fred Drake2cfc8351998-04-03 06:12:21 +0000133 \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 Drake094579e1996-12-13 22:09:52 +0000159\end{tableii}
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000160
Fred Drake094579e1996-12-13 22:09:52 +0000161Additional directives may be supported on certain platforms, but
162only the ones listed here have a meaning standardized by ANSI C.
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000163
Fred Drake094579e1996-12-13 22:09:52 +0000164On some platforms, an optional field width and precision
Fred Drake2cfc8351998-04-03 06:12:21 +0000165specification can immediately follow the initial \code{\%} of a
Fred Drake094579e1996-12-13 22:09:52 +0000166directive in the following order; this is also not portable.
Fred Drake2cfc8351998-04-03 06:12:21 +0000167The field width is normally 2 except for \code{\%j} where it is 3.
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000168
Guido van Rossum26ee8091995-09-13 17:37:49 +0000169\end{funcdesc}
170
Guido van Rossum5d237581998-06-09 16:30:56 +0000171\begin{funcdesc}{strptime}{string\optional{, format}}
172Parse a string representing a time according to a format. The return
173value is a tuple as returned by \code{gmtime()} or \code{localtime()}.
174The format uses the same directives as those used by
175\code{strftime()}; it defaults to \code{"\%a \%b \%d \%H:\%M:\%S \%Y"}
176which matches the formatting returned by \code{ctime()}. The same
177platform caveats apply; see the local Unix documentation for
178restrictions or additional supported directives. This function may
179not be defined on all platforms.
180
181\end{funcdesc}
182
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000183\begin{funcdesc}{time}{}
184Return the time as a floating point number expressed in seconds since
185the epoch, in UTC. Note that even though the time is always returned
186as a floating point number, not all systems provide time with a better
Guido van Rossumbd851cd1994-08-23 13:26:22 +0000187precision than 1 second.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000188\end{funcdesc}
189
190\begin{datadesc}{timezone}
191The offset of the local (non-DST) timezone, in seconds west of the 0th
192meridian (i.e. negative in most of Western Europe, positive in the US,
193zero in the UK).
194\end{datadesc}
195
196\begin{datadesc}{tzname}
197A tuple of two strings: the first is the name of the local non-DST
198timezone, the second is the name of the local DST timezone. If no DST
199timezone is defined, the second string should not be used.
200\end{datadesc}
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000201