blob: e9344081abdfd9285281dbdd8ceedaa2efd5155c [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
Guido van Rossum5fdeeea1994-01-02 01:22:07 +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.
Guido van Rossum470be141995-03-17 16:07:09 +000032E.g.\ on most UNIX systems, the clock ``ticks'' only 50 or 100 times a
33second, and on the Mac, times are only accurate to whole seconds.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000034
35\end{itemize}
36
Guido van Rossum470be141995-03-17 16:07:09 +000037The module defines the following functions and data items:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000038
39\renewcommand{\indexsubitem}{(in module time)}
40
41\begin{datadesc}{altzone}
42The offset of the local DST timezone, in seconds west of the 0th
Guido van Rossum470be141995-03-17 16:07:09 +000043meridian, if one is defined. Negative if the local DST timezone is
44east of the 0th meridian (as in Western Europe, including the UK).
45Only use this if \code{daylight} is nonzero.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000046\end{datadesc}
47
48
49\begin{funcdesc}{asctime}{tuple}
50Convert a tuple representing a time as returned by \code{gmtime()} or
51\code{localtime()} to a 24-character string of the following form:
52\code{'Sun Jun 20 23:21:05 1993'}. Note: unlike the C function of
53the same name, there is no trailing newline.
54\end{funcdesc}
55
56
Guido van Rossumbd851cd1994-08-23 13:26:22 +000057\begin{funcdesc}{clock}{}
58Return the current CPU time as a floating point number expressed in
Guido van Rossum470be141995-03-17 16:07:09 +000059seconds. The precision, and in fact the very definiton of the meaning
60of ``CPU time'', depends on that of the C function of the same name.
Guido van Rossumbd851cd1994-08-23 13:26:22 +000061\end{funcdesc}
62
63
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000064\begin{funcdesc}{ctime}{secs}
65Convert a time expressed in seconds since the epoch to a string
66representing local time. \code{ctime(t)} is equivalent to
67\code{asctime(localtime(t))}.
68\end{funcdesc}
69
70\begin{datadesc}{daylight}
71Nonzero if a DST timezone is defined.
72\end{datadesc}
73
74\begin{funcdesc}{gmtime}{secs}
75Convert a time expressed in seconds since the epoch to a tuple of 9
Guido van Rossum6c4f0031995-03-07 10:14:09 +000076integers, in UTC: year (e.g.\ 1993), month (1--12), day (1--31), hour
77(0--23), minute (0--59), second (0--59), weekday (0--6, monday is 0),
78Julian day (1--366), dst flag (always zero). Fractions of a second are
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000079ignored. Note subtle differences with the C function of this name.
80\end{funcdesc}
81
82\begin{funcdesc}{localtime}{secs}
83Like \code{gmtime} but converts to local time. The dst flag is set
84to 1 when DST applies to the given time.
85\end{funcdesc}
86
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000087\begin{funcdesc}{mktime}{tuple}
88This is the inverse function of \code{localtime}. Its argument is the
Guido van Rossum036eae61996-06-26 19:25:12 +000089full 9-tuple (since the dst flag is needed). It returns a floating
90point number, for compatibility with \code{time.time()}. If the input
91value can't be represented as a valid time, OverflowError is raised.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000092\end{funcdesc}
93
94\begin{funcdesc}{sleep}{secs}
95Suspend execution for the given number of seconds. The argument may
96be a floating point number to indicate a more precise sleep time.
97\end{funcdesc}
98
Guido van Rossum26ee8091995-09-13 17:37:49 +000099\begin{funcdesc}{strftime}{format, tuple}
100Convert a tuple representing a time as returned by \code{gmtime()} or
101\code{localtime()} to a string as specified by the format argument.
102See the \code{strftime(3)} man page for details of the syntax of
103format strings.
104\end{funcdesc}
105
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000106\begin{funcdesc}{time}{}
107Return the time as a floating point number expressed in seconds since
108the epoch, in UTC. Note that even though the time is always returned
109as a floating point number, not all systems provide time with a better
Guido van Rossumbd851cd1994-08-23 13:26:22 +0000110precision than 1 second.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000111\end{funcdesc}
112
113\begin{datadesc}{timezone}
114The offset of the local (non-DST) timezone, in seconds west of the 0th
115meridian (i.e. negative in most of Western Europe, positive in the US,
116zero in the UK).
117\end{datadesc}
118
119\begin{datadesc}{tzname}
120A tuple of two strings: the first is the name of the local non-DST
121timezone, the second is the name of the local DST timezone. If no DST
122timezone is defined, the second string should not be used.
123\end{datadesc}