blob: 7c52f7cc1a90e207e20a7adfcc7a98913b400134 [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
Guido van Rossum8cf2db41996-07-30 18:32:04 +000035\item
36The time tuple as returned by \code{gmtime()} and \code{localtime()},
37or as accpted by \code{mktime()} is a tuple of 9
38integers: year (e.g.\ 1993), month (1--12), day (1--31), hour
39(0--23), minute (0--59), second (0--59), weekday (0--6, monday is 0),
40Julian day (1--366) and daylight savings flag (-1, 0 or 1).
41Note that unlike the C structure, the month value is a range of 1-12, not
420-11. A year value of $<$ 100 will typically be silently converted to
431900 $+$ year value. A -1 argument as daylight savings flag, passed to
44\code{mktime()} will usually result in the correct daylight savings
45state to be filled in.
46
47
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000048\end{itemize}
49
Guido van Rossum470be141995-03-17 16:07:09 +000050The module defines the following functions and data items:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000051
52\renewcommand{\indexsubitem}{(in module time)}
53
54\begin{datadesc}{altzone}
55The offset of the local DST timezone, in seconds west of the 0th
Guido van Rossum470be141995-03-17 16:07:09 +000056meridian, if one is defined. Negative if the local DST timezone is
57east of the 0th meridian (as in Western Europe, including the UK).
58Only use this if \code{daylight} is nonzero.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000059\end{datadesc}
60
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000061\begin{funcdesc}{asctime}{tuple}
62Convert a tuple representing a time as returned by \code{gmtime()} or
63\code{localtime()} to a 24-character string of the following form:
64\code{'Sun Jun 20 23:21:05 1993'}. Note: unlike the C function of
65the same name, there is no trailing newline.
66\end{funcdesc}
67
Guido van Rossumbd851cd1994-08-23 13:26:22 +000068\begin{funcdesc}{clock}{}
69Return the current CPU time as a floating point number expressed in
Guido van Rossum470be141995-03-17 16:07:09 +000070seconds. The precision, and in fact the very definiton of the meaning
71of ``CPU time'', depends on that of the C function of the same name.
Guido van Rossumbd851cd1994-08-23 13:26:22 +000072\end{funcdesc}
73
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000074\begin{funcdesc}{ctime}{secs}
75Convert a time expressed in seconds since the epoch to a string
76representing local time. \code{ctime(t)} is equivalent to
77\code{asctime(localtime(t))}.
78\end{funcdesc}
79
80\begin{datadesc}{daylight}
81Nonzero if a DST timezone is defined.
82\end{datadesc}
83
84\begin{funcdesc}{gmtime}{secs}
Guido van Rossum8cf2db41996-07-30 18:32:04 +000085Convert a time expressed in seconds since the epoch to a time tuple
86in UTC in which the dst flag is always zero. Fractions of a second are
87ignored.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000088\end{funcdesc}
89
90\begin{funcdesc}{localtime}{secs}
91Like \code{gmtime} but converts to local time. The dst flag is set
92to 1 when DST applies to the given time.
93\end{funcdesc}
94
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000095\begin{funcdesc}{mktime}{tuple}
96This is the inverse function of \code{localtime}. Its argument is the
Guido van Rossum8cf2db41996-07-30 18:32:04 +000097full 9-tuple (since the dst flag is needed --- pass -1 as the dst flag if
98it is unknown) which expresses the time
99in \em{local} time, not UTC. It returns a floating
Guido van Rossum036eae61996-06-26 19:25:12 +0000100point number, for compatibility with \code{time.time()}. If the input
101value can't be represented as a valid time, OverflowError is raised.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000102\end{funcdesc}
103
104\begin{funcdesc}{sleep}{secs}
105Suspend execution for the given number of seconds. The argument may
106be a floating point number to indicate a more precise sleep time.
107\end{funcdesc}
108
Guido van Rossum26ee8091995-09-13 17:37:49 +0000109\begin{funcdesc}{strftime}{format, tuple}
110Convert a tuple representing a time as returned by \code{gmtime()} or
111\code{localtime()} to a string as specified by the format argument.
Guido van Rossum8cf2db41996-07-30 18:32:04 +0000112
113 The following directives, shown without the optional field width and
114 precision specification, are replaced by the indicated characters:
115
116\begin{tabular}{lp{25em}}
117 \%a & Locale's abbreviated weekday name. \\
118 \%A & Locale's full weekday name. \\
119 \%b & Locale's abbreviated month name. \\
120 \%B & Locale's full month name. \\
121 \%c & Locale's appropriate date and time representation. \\
122 \%d & Day of the month as a decimal number [01,31]. \\
123 \%E & Locale's combined Emperor/Era name and year. \\
124 \%H & Hour (24-hour clock) as a decimal number [00,23]. \\
125 \%I & Hour (12-hour clock) as a decimal number [01,12]. \\
126 \%j & Day of the year as a decimal number [001,366]. \\
127 \%m & Month as a decimal number [01,12]. \\
128 \%M & Minute as a decimal number [00,59]. \\
129 \%n & New-line character. \\
130 \%N & Locale's Emperor/Era name. \\
131 \%o & Locale's Emperor/Era year. \\
132 \%p & Locale's equivalent of either AM or PM. \\
133 \%S & Second as a decimal number [00,61]. \\
134 \%t & Tab character. \\
135 \%U & Week number of the year (Sunday as the first day of the
136 week) as a decimal number [00,53]. All days in a new
137 year preceding the first Sunday are considered to be in
138 week 0. \\
139 \%w & Weekday as a decimal number [0(Sunday),6]. \\
140 \%W & Week number of the year (Monday as the first day of the
141 week) as a decimal number [00,53]. All days in a new
142 year preceding the first Sunday are considered to be in
143 week 0. \\
144 \%x & Locale's appropriate date representation. \\
145 \%X & Locale's appropriate time representation. \\
146 \%y & Year without century as a decimal number [00,99]. \\
147 \%Y & Year with century as a decimal number. \\
148 \%Z & Time zone name (or by no characters if no time zone
149 exists). \\
150 \%\% & \% \\
151\end{tabular}
152
153 An optional field width and precision specification can immediately
154 follow the initial \% of a directive in the following order: \\
155
156\begin{tabular}{lp{25em}}
157 [-|0]w & the decimal digit string w specifies a minimum field
158 width in which the result of the conversion is right-
159 or left-justified. It is right-justified (with space
160 padding) by default. If the optional flag `-' is
161 specified, it is left-justified with space padding on
162 the right. If the optional flag `0' is specified, it
163 is right-justified and padded with zeros on the left. \\
164 .p & the decimal digit string p specifies the minimum number
165 of digits to appear for the d, H, I, j, m, M, o, S, U,
166 w, W, y and Y directives, and the maximum number of
167 characters to be used from the a, A, b, B, c, D, E, F,
168 h, n, N, p, r, t, T, x, X, z, Z, and % directives. In
169 the first case, if a directive supplies fewer digits
170 than specified by the precision, it will be expanded
171 with leading zeros. In the second case, if a directive
172 supplies more characters than specified by the
173 precision, excess characters will truncated on the
174 right.
175\end{tabular}
176
177 If no field width or precision is specified for a d, H, I, m, M, S, U,
178 W, y, or j directive, a default of .2 is used for all but j for which
179 .3 is used.
180
Guido van Rossum26ee8091995-09-13 17:37:49 +0000181\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