blob: 7ee886d69f9b47a0a2d42dc890093e1762c73e2e [file] [log] [blame]
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001\section{Built-in Module \sectcode{time}}
2
3\bimodindex{time}
4This module provides various time-related functions.
Guido van Rossumbd851cd1994-08-23 13:26:22 +00005It is always available.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00006
7An explanation of some terminology and conventions is in order.
8
9\begin{itemize}
10
11\item
Guido van Rossum16d6e711994-08-08 12:30:22 +000012The ``epoch'' is the point where the time starts. On January 1st of that
Fred Drake094579e1996-12-13 22:09:52 +000013year, at 0 hours, the ``time since the epoch'' is zero. For \UNIX{}, the
Guido van Rossum470be141995-03-17 16:07:09 +000014epoch is 1970. To find out what the epoch is, look at \code{gmtime(0)}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000015
16\item
17UTC is Coordinated Universal Time (formerly known as Greenwich Mean
18Time). The acronym UTC is not a mistake but a compromise between
19English and French.
20
21\item
22DST is Daylight Saving Time, an adjustment of the timezone by
23(usually) one hour during part of the year. DST rules are magic
24(determined by local law) and can change from year to year. The C
25library has a table containing the local rules (often it is read from
26a system file for flexibility) and is the only source of True Wisdom
27in this respect.
28
29\item
30The precision of the various real-time functions may be less than
31suggested by the units in which their value or argument is expressed.
Fred Drake094579e1996-12-13 22:09:52 +000032E.g.\ on most \UNIX{} systems, the clock ``ticks'' only 50 or 100 times a
Guido van Rossum470be141995-03-17 16:07:09 +000033second, and on the Mac, times are only accurate to whole seconds.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000034
Guido van Rossum8cf2db41996-07-30 18:32:04 +000035\item
Guido van Rossum21be1471996-12-12 17:59:37 +000036On the other hand, the precision of \code{time()} and \code{sleep()}
Fred Drake094579e1996-12-13 22:09:52 +000037is better than their \UNIX{} equivalents: times are expressed as floating
Guido van Rossum21be1471996-12-12 17:59:37 +000038point numbers, \code{time()} returns the most accurate time available
Fred Drake094579e1996-12-13 22:09:52 +000039(using \UNIX{} \code{gettimeofday()} where available), and \code{sleep()}
40will accept a time with a nonzero fraction (\UNIX{} \code{select()} is
Guido van Rossum21be1471996-12-12 17:59:37 +000041used to implement this, where available).
42
43\item
Guido van Rossum8cf2db41996-07-30 18:32:04 +000044The time tuple as returned by \code{gmtime()} and \code{localtime()},
45or as accpted by \code{mktime()} is a tuple of 9
46integers: year (e.g.\ 1993), month (1--12), day (1--31), hour
47(0--23), minute (0--59), second (0--59), weekday (0--6, monday is 0),
48Julian day (1--366) and daylight savings flag (-1, 0 or 1).
49Note that unlike the C structure, the month value is a range of 1-12, not
500-11. A year value of $<$ 100 will typically be silently converted to
511900 $+$ year value. A -1 argument as daylight savings flag, passed to
52\code{mktime()} will usually result in the correct daylight savings
53state to be filled in.
54
55
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000056\end{itemize}
57
Guido van Rossum470be141995-03-17 16:07:09 +000058The module defines the following functions and data items:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000059
60\renewcommand{\indexsubitem}{(in module time)}
61
62\begin{datadesc}{altzone}
63The offset of the local DST timezone, in seconds west of the 0th
Guido van Rossum470be141995-03-17 16:07:09 +000064meridian, if one is defined. Negative if the local DST timezone is
65east of the 0th meridian (as in Western Europe, including the UK).
66Only use this if \code{daylight} is nonzero.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000067\end{datadesc}
68
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000069\begin{funcdesc}{asctime}{tuple}
70Convert a tuple representing a time as returned by \code{gmtime()} or
71\code{localtime()} to a 24-character string of the following form:
72\code{'Sun Jun 20 23:21:05 1993'}. Note: unlike the C function of
73the same name, there is no trailing newline.
74\end{funcdesc}
75
Guido van Rossumbd851cd1994-08-23 13:26:22 +000076\begin{funcdesc}{clock}{}
77Return the current CPU time as a floating point number expressed in
Guido van Rossum470be141995-03-17 16:07:09 +000078seconds. The precision, and in fact the very definiton of the meaning
Guido van Rossum21be1471996-12-12 17:59:37 +000079of ``CPU time'', depends on that of the C function of the same name,
80but in any case, this is the function to use for benchmarking Python
81or timing algorithms.
Guido van Rossumbd851cd1994-08-23 13:26:22 +000082\end{funcdesc}
83
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000084\begin{funcdesc}{ctime}{secs}
85Convert a time expressed in seconds since the epoch to a string
86representing local time. \code{ctime(t)} is equivalent to
87\code{asctime(localtime(t))}.
88\end{funcdesc}
89
90\begin{datadesc}{daylight}
91Nonzero if a DST timezone is defined.
92\end{datadesc}
93
94\begin{funcdesc}{gmtime}{secs}
Guido van Rossum8cf2db41996-07-30 18:32:04 +000095Convert a time expressed in seconds since the epoch to a time tuple
96in UTC in which the dst flag is always zero. Fractions of a second are
97ignored.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000098\end{funcdesc}
99
100\begin{funcdesc}{localtime}{secs}
101Like \code{gmtime} but converts to local time. The dst flag is set
102to 1 when DST applies to the given time.
103\end{funcdesc}
104
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000105\begin{funcdesc}{mktime}{tuple}
106This is the inverse function of \code{localtime}. Its argument is the
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000107full 9-tuple (since the dst flag is needed --- pass -1 as the dst flag if
108it is unknown) which expresses the time
Fred Drake094579e1996-12-13 22:09:52 +0000109in \emph{local} time, not UTC. It returns a floating
Guido van Rossum036eae61996-06-26 19:25:12 +0000110point number, for compatibility with \code{time.time()}. If the input
111value can't be represented as a valid time, OverflowError is raised.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000112\end{funcdesc}
113
114\begin{funcdesc}{sleep}{secs}
115Suspend execution for the given number of seconds. The argument may
116be a floating point number to indicate a more precise sleep time.
117\end{funcdesc}
118
Guido van Rossum26ee8091995-09-13 17:37:49 +0000119\begin{funcdesc}{strftime}{format, tuple}
120Convert a tuple representing a time as returned by \code{gmtime()} or
121\code{localtime()} to a string as specified by the format argument.
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000122
Fred Drake094579e1996-12-13 22:09:52 +0000123The following directives, shown without the optional field width and
124precision specification, are replaced by the indicated characters:
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000125
Fred Drake094579e1996-12-13 22:09:52 +0000126\begin{tableii}{|c|p{24em}|}{code}{Directive}{Meaning}
127\lineii{\%a}{Locale's abbreviated weekday name.}
128\lineii{\%A}{Locale's full weekday name.}
129\lineii{\%b}{Locale's abbreviated month name.}
130\lineii{\%B}{Locale's full month name.}
131\lineii{\%c}{Locale's appropriate date and time representation.}
132\lineii{\%d}{Day of the month as a decimal number [01,31].}
133\lineii{\%H}{Hour (24-hour clock) as a decimal number [00,23].}
134\lineii{\%I}{Hour (12-hour clock) as a decimal number [01,12].}
135\lineii{\%j}{Day of the year as a decimal number [001,366].}
136\lineii{\%m}{Month as a decimal number [01,12].}
137\lineii{\%M}{Minute as a decimal number [00,59].}
138\lineii{\%p}{Locale's equivalent of either AM or PM.}
139\lineii{\%S}{Second as a decimal number [00,61].}
140\lineii{\%U}{Week number of the year (Sunday as the first day of the
141 week) as a decimal number [00,53]. All days in a new year
142 preceding the first Sunday are considered to be in week 0.}
143\lineii{\%w}{Weekday as a decimal number [0(Sunday),6].}
144\lineii{\%W}{Week number of the year (Monday as the first day of the
145 week) as a decimal number [00,53]. All days in a new year
146 preceding the first Sunday are considered to be in week 0.}
147\lineii{\%x}{Locale's appropriate date representation.}
148\lineii{\%X}{Locale's appropriate time representation.}
149\lineii{\%y}{Year without century as a decimal number [00,99].}
150\lineii{\%Y}{Year with century as a decimal number.}
151\lineii{\%Z}{Time zone name (or by no characters if no time zone exists).}
152\lineii{\%\%}{\%}
153\end{tableii}
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000154
Fred Drake094579e1996-12-13 22:09:52 +0000155Additional directives may be supported on certain platforms, but
156only the ones listed here have a meaning standardized by ANSI C.
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000157
Fred Drake094579e1996-12-13 22:09:52 +0000158On some platforms, an optional field width and precision
159specification can immediately follow the initial \% of a
160directive in the following order; this is also not portable.
161The field width is normally 2 except for \%j where it is 3.
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000162
Guido van Rossum26ee8091995-09-13 17:37:49 +0000163\end{funcdesc}
164
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000165\begin{funcdesc}{time}{}
166Return the time as a floating point number expressed in seconds since
167the epoch, in UTC. Note that even though the time is always returned
168as a floating point number, not all systems provide time with a better
Guido van Rossumbd851cd1994-08-23 13:26:22 +0000169precision than 1 second.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000170\end{funcdesc}
171
172\begin{datadesc}{timezone}
173The offset of the local (non-DST) timezone, in seconds west of the 0th
174meridian (i.e. negative in most of Western Europe, positive in the US,
175zero in the UK).
176\end{datadesc}
177
178\begin{datadesc}{tzname}
179A tuple of two strings: the first is the name of the local non-DST
180timezone, the second is the name of the local DST timezone. If no DST
181timezone is defined, the second string should not be used.
182\end{datadesc}
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000183