blob: 693ae79d16cd2dbe9b68c5db1ae7ba2f33bac358 [file] [log] [blame]
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001% XXX what order should the types be discussed in?
Tim Petersbad8ff02002-12-30 20:52:32 +00002
Fred Drakebbdb2502002-12-23 18:58:06 +00003\section{\module{datetime} ---
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00004 Basic date and time types}
5
6\declaremodule{builtin}{datetime}
7\modulesynopsis{Basic date and time types.}
Fred Drakebbdb2502002-12-23 18:58:06 +00008\moduleauthor{Tim Peters}{tim@zope.com}
9\sectionauthor{Tim Peters}{tim@zope.com}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000010\sectionauthor{A.M. Kuchling}{amk@amk.ca}
Raymond Hettinger6005a342002-12-30 20:01:24 +000011\sectionauthor{Raymond D. Hettinger}{python@rcn.com}
12
13\versionadded{2.3}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000014
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000015
16The \module{datetime} module supplies classes for manipulating dates
17and times in both simple and complex ways. While date and time
18arithmetic is supported, the focus of the implementation is on
Andrew M. Kuchling9b445712003-01-09 13:46:30 +000019efficient member extraction for output formatting and manipulation.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000020
Fred Drakea37e5cc2002-12-30 21:26:42 +000021There are two kinds of date and time objects: ``naive'' and ``aware''.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000022This distinction refers to whether the object has any notion of time
Tim Peters00372022003-01-09 04:10:05 +000023zone, daylight saving time, or other kind of algorithmic or political
24time adjustment. Whether a naive \class{datetime} object represents
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000025Coordinated Universal Time (UTC), local time, or time in some other
26timezone is purely up to the program, just like it's up to the program
Fred Drakea37e5cc2002-12-30 21:26:42 +000027whether a particular number represents meters, miles, or mass. Naive
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000028\class{datetime} objects are easy to understand and to work with, at
29the cost of ignoring some aspects of reality.
30
Andrew M. Kuchling9b445712003-01-09 13:46:30 +000031For applications requiring more, \class{datetime} and \class{time}
32objects have an optional time zone information member,
Tim Peters327098a2003-01-20 22:54:38 +000033\member{tzinfo}, that can contain an instance of a subclass of
Andrew M. Kuchling9b445712003-01-09 13:46:30 +000034the abstract \class{tzinfo} class. These \class{tzinfo} objects
35capture information about the offset from UTC time, the time zone
36name, and whether Daylight Saving Time is in effect. Note that no
37concrete \class{tzinfo} classes are supplied by the \module{datetime}
38module. Instead, they provide a framework for incorporating the level
39of detail an application may require. The rules for time adjustment across
40the world are more political than rational, and there is no standard
41suitable for every application.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000042
43The \module{datetime} module exports the following constants:
44
45\begin{datadesc}{MINYEAR}
Tim Peters00372022003-01-09 04:10:05 +000046 The smallest year number allowed in a \class{date} or
47 \class{datetime} object. \constant{MINYEAR}
Fred Drakebbdb2502002-12-23 18:58:06 +000048 is \code{1}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000049\end{datadesc}
50
51\begin{datadesc}{MAXYEAR}
Tim Peters00372022003-01-09 04:10:05 +000052 The largest year number allowed in a \class{date} or \class{datetime}
53 object. \constant{MAXYEAR} is \code{9999}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000054\end{datadesc}
55
Raymond Hettinger6005a342002-12-30 20:01:24 +000056\begin{seealso}
Raymond Hettinger62229582002-12-31 16:37:03 +000057 \seemodule{calendar}{General calendar related functions.}
Raymond Hettinger6005a342002-12-30 20:01:24 +000058 \seemodule{time}{Time access and conversions.}
59\end{seealso}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000060
61\subsection{Available Types}
62
Fred Drakebbdb2502002-12-23 18:58:06 +000063\begin{classdesc*}{date}
Fred Drakea37e5cc2002-12-30 21:26:42 +000064 An idealized naive date, assuming the current Gregorian calendar
Fred Drakebbdb2502002-12-23 18:58:06 +000065 always was, and always will be, in effect.
66 Attributes: \member{year}, \member{month}, and \member{day}.
67\end{classdesc*}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000068
Fred Drakebbdb2502002-12-23 18:58:06 +000069\begin{classdesc*}{time}
Tim Peters00372022003-01-09 04:10:05 +000070 An idealized time, independent of any particular day, assuming
Fred Drakebbdb2502002-12-23 18:58:06 +000071 that every day has exactly 24*60*60 seconds (there is no notion
72 of "leap seconds" here).
Tim Peters00372022003-01-09 04:10:05 +000073 Attributes: \member{hour}, \member{minute}, \member{second},
74 \member{microsecond}, and \member{tzinfo}.
Fred Drakebbdb2502002-12-23 18:58:06 +000075\end{classdesc*}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000076
Fred Drakebbdb2502002-12-23 18:58:06 +000077\begin{classdesc*}{datetime}
Andrew M. Kuchling9b445712003-01-09 13:46:30 +000078 A combination of a date and a time.
Fred Drakebbdb2502002-12-23 18:58:06 +000079 Attributes: \member{year}, \member{month}, \member{day},
80 \member{hour}, \member{minute}, \member{second},
Tim Peters00372022003-01-09 04:10:05 +000081 \member{microsecond}, and \member{tzinfo}.
Fred Drakebbdb2502002-12-23 18:58:06 +000082\end{classdesc*}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000083
Fred Drakebbdb2502002-12-23 18:58:06 +000084\begin{classdesc*}{timedelta}
Andrew M. Kuchling9b445712003-01-09 13:46:30 +000085 A duration expressing the difference between two \class{date},
86 \class{time}, or \class{datetime} instances to microsecond
Fred Drakebbdb2502002-12-23 18:58:06 +000087 resolution.
88\end{classdesc*}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000089
Fred Drakebbdb2502002-12-23 18:58:06 +000090\begin{classdesc*}{tzinfo}
91 An abstract base class for time zone information objects. These
Tim Peters00372022003-01-09 04:10:05 +000092 are used by the \class{datetime} and \class{time} classes to
Andrew M. Kuchling9b445712003-01-09 13:46:30 +000093 provide a customizable notion of time adjustment (for example, to
Tim Peters00372022003-01-09 04:10:05 +000094 account for time zone and/or daylight saving time).
Fred Drakebbdb2502002-12-23 18:58:06 +000095\end{classdesc*}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000096
97Objects of these types are immutable.
98
Tim Peters00372022003-01-09 04:10:05 +000099Objects of the \class{date} type are always naive.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000100
Tim Peters00372022003-01-09 04:10:05 +0000101An object \var{d} of type \class{time} or \class{datetime} may be
Fred Drakea37e5cc2002-12-30 21:26:42 +0000102naive or aware. \var{d} is aware if \code{\var{d}.tzinfo} is not
Andrew M. Kuchling9b445712003-01-09 13:46:30 +0000103\code{None} and \code{\var{d}.tzinfo.utcoffset(\var{d})} does not return
Fred Drakea37e5cc2002-12-30 21:26:42 +0000104\code{None}. If \code{\var{d}.tzinfo} is \code{None}, or if
105\code{\var{d}.tzinfo} is not \code{None} but
106\code{\var{d}.tzinfo.utcoffset(\var{d})} returns \code{None}, \var{d}
107is naive.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000108
Fred Drakea37e5cc2002-12-30 21:26:42 +0000109The distinction between naive and aware doesn't apply to
Fred Drakebbdb2502002-12-23 18:58:06 +0000110\code{timedelta} objects.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000111
Fred Drakebbdb2502002-12-23 18:58:06 +0000112Subclass relationships:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000113
Fred Drakebbdb2502002-12-23 18:58:06 +0000114\begin{verbatim}
115object
116 timedelta
117 tzinfo
118 time
Fred Drakebbdb2502002-12-23 18:58:06 +0000119 date
120 datetime
Fred Drakebbdb2502002-12-23 18:58:06 +0000121\end{verbatim}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000122
Raymond Hettinger6005a342002-12-30 20:01:24 +0000123\subsection{\class{timedelta} Objects \label{datetime-timedelta}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000124
Fred Drakebbdb2502002-12-23 18:58:06 +0000125A \class{timedelta} object represents a duration, the difference
126between two dates or times.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000127
Fred Drake0f8e5432002-12-31 18:31:48 +0000128\begin{classdesc}{timedelta}{days=0, seconds=0, microseconds=0,
129 milliseconds=0, minutes=0, hours=0, weeks=0}
Raymond Hettingerf6212322002-12-31 17:24:50 +0000130
Fred Drake436eadd2002-12-31 18:13:11 +0000131 All arguments are optional. Arguments may be ints, longs, or floats,
132 and may be positive or negative.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000133
Fred Drake436eadd2002-12-31 18:13:11 +0000134 Only \var{days}, \var{seconds} and \var{microseconds} are stored
135 internally. Arguments are converted to those units:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000136
Andrew M. Kuchling9b445712003-01-09 13:46:30 +0000137\begin{itemize}
138 \item A millisecond is converted to 1000 microseconds.
139 \item A minute is converted to 60 seconds.
140 \item An hour is converted to 3600 seconds.
141 \item A week is converted to 7 days.
142\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000143
Fred Drake436eadd2002-12-31 18:13:11 +0000144 and days, seconds and microseconds are then normalized so that the
145 representation is unique, with
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000146
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000147\begin{itemize}
148 \item \code{0 <= \var{microseconds} < 1000000}
149 \item \code{0 <= \var{seconds} < 3600*24} (the number of seconds in one day)
150 \item \code{-999999999 <= \var{days} <= 999999999}
151\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000152
Andrew M. Kuchling9b445712003-01-09 13:46:30 +0000153 If any argument is a float and there are fractional microseconds,
Fred Drake436eadd2002-12-31 18:13:11 +0000154 the fractional microseconds left over from all arguments are combined
155 and their sum is rounded to the nearest microsecond. If no
156 argument is a float, the conversion and normalization processes
157 are exact (no information is lost).
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000158
Fred Drake436eadd2002-12-31 18:13:11 +0000159 If the normalized value of days lies outside the indicated range,
160 \exception{OverflowError} is raised.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000161
Fred Drake436eadd2002-12-31 18:13:11 +0000162 Note that normalization of negative values may be surprising at first.
163 For example,
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000164
Fred Drakebbdb2502002-12-23 18:58:06 +0000165\begin{verbatim}
166>>> d = timedelta(microseconds=-1)
167>>> (d.days, d.seconds, d.microseconds)
168(-1, 86399, 999999)
169\end{verbatim}
Fred Drake0f8e5432002-12-31 18:31:48 +0000170\end{classdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000171
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000172Class attributes are:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000173
Fred Drake0f8e5432002-12-31 18:31:48 +0000174\begin{memberdesc}{min}
175 The most negative \class{timedelta} object,
176 \code{timedelta(-999999999)}.
177\end{memberdesc}
178
179\begin{memberdesc}{max}
180 The most positive \class{timedelta} object,
181 \code{timedelta(days=999999999, hours=23, minutes=59, seconds=59,
182 microseconds=999999)}.
183\end{memberdesc}
184
185\begin{memberdesc}{resolution}
186 The smallest possible difference between non-equal
187 \class{timedelta} objects, \code{timedelta(microseconds=1)}.
188\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000189
Fred Drakea37e5cc2002-12-30 21:26:42 +0000190Note that, because of normalization, \code{timedelta.max} \textgreater
191\code{-timedelta.min}. \code{-timedelta.max} is not representable as
192a \class{timedelta} object.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000193
194Instance attributes (read-only):
195
Raymond Hettinger6005a342002-12-30 20:01:24 +0000196\begin{tableii}{c|l}{code}{Attribute}{Value}
197 \lineii{days}{Between -999999999 and 999999999 inclusive}
198 \lineii{seconds}{Between 0 and 86399 inclusive}
199 \lineii{microseconds}{Between 0 and 999999 inclusive}
200\end{tableii}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000201
202Supported operations:
203
Andrew M. Kuchling9b445712003-01-09 13:46:30 +0000204% XXX this table is too wide!
Raymond Hettinger6005a342002-12-30 20:01:24 +0000205\begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes}
Fred Drake9bdeee42002-12-30 20:35:32 +0000206 \lineiii{\var{t1} = \var{t2} + \var{t3}}
Tim Peters95322982002-12-31 16:01:47 +0000207 {Sum of \var{t2} and \var{t3}.
Fred Drake9bdeee42002-12-30 20:35:32 +0000208 Afterwards \var{t1}-\var{t2} == \var{t3} and \var{t1}-\var{t3}
209 == \var{t2} are true.}
210 {(1)}
211 \lineiii{\var{t1} = \var{t2} - \var{t3}}
212 {Difference of \var{t2} and \var{t3}. Afterwards \var{t1} ==
213 \var{t2} - \var{t3} and \var{t2} == \var{t1} + \var{t3} are
214 true.}
215 {(1)}
216 \lineiii{\var{t1} = \var{t2} * \var{i} or \var{t1} = \var{i} * \var{t2}}
217 {Delta multiplied by an integer or long.
Fred Drake436eadd2002-12-31 18:13:11 +0000218 Afterwards \var{t1} // i == \var{t2} is true,
219 provided \code{i != 0}.
Fred Drake9bdeee42002-12-30 20:35:32 +0000220 In general, \var{t1} * i == \var{t1} * (i-1) + \var{t1} is true.}
221 {(1)}
222 \lineiii{\var{t1} = \var{t2} // \var{i}}
223 {The floor is computed and the remainder (if any) is thrown away.}
Tim Peters397301e2003-01-02 21:28:08 +0000224 {(3)}
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000225 \lineiii{+\var{t1}}
Raymond Hettingercbd6cd22002-12-31 16:30:49 +0000226 {Returns a \class{timedelta} object with the same value.}
Tim Peters397301e2003-01-02 21:28:08 +0000227 {(2)}
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000228 \lineiii{-\var{t1}}
Raymond Hettingercbd6cd22002-12-31 16:30:49 +0000229 {equivalent to \class{timedelta}(-\var{t1.days}, -\var{t1.seconds},
Andrew M. Kuchling9b445712003-01-09 13:46:30 +0000230 -\var{t1.microseconds}), and to \var{t1}* -1.}
Tim Peters397301e2003-01-02 21:28:08 +0000231 {(1)(4)}
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000232 \lineiii{abs(\var{t})}
Fred Drake436eadd2002-12-31 18:13:11 +0000233 {equivalent to +\var{t} when \code{t.days >= 0}, and to
Andrew M. Kuchling9b445712003-01-09 13:46:30 +0000234 -\var{t} when \code{t.days < 0}.}
Tim Peters397301e2003-01-02 21:28:08 +0000235 {(2)}
Fred Drake9bdeee42002-12-30 20:35:32 +0000236\end{tableiii}
Raymond Hettinger6005a342002-12-30 20:01:24 +0000237\noindent
238Notes:
239
240\begin{description}
241\item[(1)]
Fred Drake436eadd2002-12-31 18:13:11 +0000242 This is exact, but may overflow.
Raymond Hettinger6005a342002-12-30 20:01:24 +0000243
244\item[(2)]
Tim Peters397301e2003-01-02 21:28:08 +0000245 This is exact, and cannot overflow.
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000246
247\item[(3)]
Tim Peters397301e2003-01-02 21:28:08 +0000248 Division by 0 raises \exception{ZeroDivisionError}.
249
250\item[(4)]
Fred Drake436eadd2002-12-31 18:13:11 +0000251 -\var{timedelta.max} is not representable as a \class{timedelta} object.
Raymond Hettinger6005a342002-12-30 20:01:24 +0000252\end{description}
253
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000254In addition to the operations listed above \class{timedelta} objects
Tim Peters00372022003-01-09 04:10:05 +0000255support certain additions and subtractions with \class{date} and
256\class{datetime} objects (see below).
Raymond Hettinger6005a342002-12-30 20:01:24 +0000257
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000258Comparisons of \class{timedelta} objects are supported with the
259\class{timedelta} object representing the smaller duration considered
260to be the smaller timedelta.
Raymond Hettinger6005a342002-12-30 20:01:24 +0000261
Andrew M. Kuchling9b445712003-01-09 13:46:30 +0000262\class{timedelta} objects are hashable (usable as dictionary keys),
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000263support efficient pickling, and in Boolean contexts, a \class{timedelta}
264object is considered to be true if and only if it isn't equal to
265\code{timedelta(0)}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000266
267
Raymond Hettinger6005a342002-12-30 20:01:24 +0000268\subsection{\class{date} Objects \label{datetime-date}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000269
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000270A \class{date} object represents a date (year, month and day) in an idealized
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000271calendar, the current Gregorian calendar indefinitely extended in both
272directions. January 1 of year 1 is called day number 1, January 2 of year
2731 is called day number 2, and so on. This matches the definition of the
274"proleptic Gregorian" calendar in Dershowitz and Reingold's book
Fred Drakea37e5cc2002-12-30 21:26:42 +0000275\citetitle{Calendrical Calculations}, where it's the base calendar for all
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000276computations. See the book for algorithms for converting between
277proleptic Gregorian ordinals and many other calendar systems.
278
Fred Drake0f8e5432002-12-31 18:31:48 +0000279\begin{classdesc}{date}{year, month, day}
Fred Drake436eadd2002-12-31 18:13:11 +0000280 All arguments are required. Arguments may be ints or longs, in the
281 following ranges:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000282
Fred Drake436eadd2002-12-31 18:13:11 +0000283 \begin{itemize}
284 \item \code{MINYEAR <= \var{year} <= MAXYEAR}
285 \item \code{1 <= \var{month} <= 12}
286 \item \code{1 <= \var{day} <= number of days in the given month and year}
287 \end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000288
Fred Drake436eadd2002-12-31 18:13:11 +0000289 If an argument outside those ranges is given, \exception{ValueError}
290 is raised.
Fred Drake0f8e5432002-12-31 18:31:48 +0000291\end{classdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000292
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000293Other constructors, all class methods:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000294
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000295\begin{methoddesc}{today}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000296 Return the current local date. This is equivalent to
297 \code{date.fromtimestamp(time.time())}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000298\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000299
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000300\begin{methoddesc}{fromtimestamp}{timestamp}
Fred Drake436eadd2002-12-31 18:13:11 +0000301 Return the local date corresponding to the POSIX timestamp, such
302 as is returned by \function{time.time()}. This may raise
303 \exception{ValueError}, if the timestamp is out of the range of
304 values supported by the platform C \cfunction{localtime()}
305 function. It's common for this to be restricted to years from 1970
Tim Peters75a6e3b2003-01-04 18:17:36 +0000306 through 2038. Note that on non-POSIX systems that include leap
307 seconds in their notion of a timestamp, leap seconds are ignored by
308 \method{fromtimestamp()}.
Andrew M. Kuchling0f0e6b92003-01-09 12:51:50 +0000309\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000310
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000311\begin{methoddesc}{fromordinal}{ordinal}
Fred Drake436eadd2002-12-31 18:13:11 +0000312 Return the date corresponding to the proleptic Gregorian ordinal,
313 where January 1 of year 1 has ordinal 1. \exception{ValueError} is
314 raised unless \code{1 <= \var{ordinal} <= date.max.toordinal()}.
315 For any date \var{d}, \code{date.fromordinal(\var{d}.toordinal()) ==
316 \var{d}}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000317\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000318
319Class attributes:
320
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000321\begin{memberdesc}{min}
Fred Drake436eadd2002-12-31 18:13:11 +0000322 The earliest representable date, \code{date(MINYEAR, 1, 1)}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000323\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000324
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000325\begin{memberdesc}{max}
Fred Drake436eadd2002-12-31 18:13:11 +0000326 The latest representable date, \code{date(MAXYEAR, 12, 31)}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000327\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000328
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000329\begin{memberdesc}{resolution}
Fred Drake436eadd2002-12-31 18:13:11 +0000330 The smallest possible difference between non-equal date
331 objects, \code{timedelta(days=1)}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000332\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000333
334Instance attributes (read-only):
335
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000336\begin{memberdesc}{year}
Andrew M. Kuchling9b445712003-01-09 13:46:30 +0000337 Between \constant{MINYEAR} and \constant{MAXYEAR} inclusive.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000338\end{memberdesc}
339
340\begin{memberdesc}{month}
Fred Drake436eadd2002-12-31 18:13:11 +0000341 Between 1 and 12 inclusive.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000342\end{memberdesc}
343
344\begin{memberdesc}{day}
Fred Drake436eadd2002-12-31 18:13:11 +0000345 Between 1 and the number of days in the given month of the given
346 year.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000347\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000348
349Supported operations:
350
Andrew M. Kuchling9b445712003-01-09 13:46:30 +0000351% XXX rewrite to be a table
Fred Drakebbdb2502002-12-23 18:58:06 +0000352\begin{itemize}
353 \item
354 date1 + timedelta -> date2
Tim Peters00372022003-01-09 04:10:05 +0000355
Fred Drakebbdb2502002-12-23 18:58:06 +0000356 timedelta + date1 -> date2
Tim Peters00372022003-01-09 04:10:05 +0000357
Fred Drakebbdb2502002-12-23 18:58:06 +0000358 date2 is timedelta.days days removed from the date1, moving forward
359 in time if timedelta.days > 0, or backward if timedetla.days < 0.
360 date2 - date1 == timedelta.days after. timedelta.seconds and
361 timedelta.microseconds are ignored. \exception{OverflowError} is
362 raised if date2.year would be smaller than \constant{MINYEAR} or
363 larger than \constant{MAXYEAR}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000364
Fred Drakebbdb2502002-12-23 18:58:06 +0000365 \item
366 date1 - timedelta -> date2
Tim Peters00372022003-01-09 04:10:05 +0000367
Fred Drakebbdb2502002-12-23 18:58:06 +0000368 Computes the date2 such that date2 + timedelta == date1. This
369 isn't quite equivalent to date1 + (-timedelta), because -timedelta
370 in isolation can overflow in cases where date1 - timedelta does
371 not. timedelta.seconds and timedelta.microseconds are ignored.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000372
Fred Drakebbdb2502002-12-23 18:58:06 +0000373 \item
374 date1 - date2 -> timedelta
Tim Peters00372022003-01-09 04:10:05 +0000375
Fred Drakebbdb2502002-12-23 18:58:06 +0000376 This is exact, and cannot overflow. timedelta.seconds and
377 timedelta.microseconds are 0, and date2 + timedelta == date1
378 after.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000379
Fred Drakebbdb2502002-12-23 18:58:06 +0000380 \item
381 comparison of date to date, where date1 is considered less than
382 date2 when date1 precedes date2 in time. In other words,
383 date1 < date2 if and only if date1.toordinal() < date2.toordinal().
Tim Peters8d81a012003-01-24 22:36:34 +0000384 \note{In order to stop comparison from falling back to the default
385 scheme of comparing object addresses, date comparison
386 normally raises \exception{TypeError} if the other comparand
387 isn't also a \class{date} object. However, \code{NotImplemented}
388 is returned instead if the other comparand has a
389 \method{timetuple} attribute. This hook gives other kinds of
390 date objects a chance at implementing mixed-type comparison.}
391
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000392
Fred Drakebbdb2502002-12-23 18:58:06 +0000393 \item
394 hash, use as dict key
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000395
Fred Drakebbdb2502002-12-23 18:58:06 +0000396 \item
397 efficient pickling
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000398
Fred Drakebbdb2502002-12-23 18:58:06 +0000399 \item
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000400 in Boolean contexts, all \class{date} objects are considered to be true
Fred Drakebbdb2502002-12-23 18:58:06 +0000401\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000402
403Instance methods:
404
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000405\begin{methoddesc}{replace}{year, month, day}
Tim Peters00372022003-01-09 04:10:05 +0000406 Return a date with the same value, except for those members given
Fred Drake436eadd2002-12-31 18:13:11 +0000407 new values by whichever keyword arguments are specified. For
408 example, if \code{d == date(2002, 12, 31)}, then
409 \code{d.replace(day=26) == date(2000, 12, 26)}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000410\end{methoddesc}
Tim Peters12bf3392002-12-24 05:41:27 +0000411
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000412\begin{methoddesc}{timetuple}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000413 Return a 9-element tuple of the form returned by
414 \function{time.localtime()}. The hours, minutes and seconds are
415 0, and the DST flag is -1.
416 \code{\var{d}.timetuple()} is equivalent to
417 \code{(\var{d}.year, \var{d}.month, \var{d}.day,
418 0, 0, 0, \# h, m, s
419 \var{d}.weekday(), \# 0 is Monday
420 \var{d}.toordinal() - date(\var{d}.year, 1, 1).toordinal() + 1,
421 \# day of year
422 -1)}
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000423\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000424
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000425\begin{methoddesc}{toordinal}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000426 Return the proleptic Gregorian ordinal of the date, where January 1
427 of year 1 has ordinal 1. For any \class{date} object \var{d},
428 \code{date.fromordinal(\var{d}.toordinal()) == \var{d}}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000429\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000430
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000431\begin{methoddesc}{weekday}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000432 Return the day of the week as an integer, where Monday is 0 and
433 Sunday is 6. For example, date(2002, 12, 4).weekday() == 2, a
434 Wednesday.
435 See also \method{isoweekday()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000436\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000437
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000438\begin{methoddesc}{isoweekday}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000439 Return the day of the week as an integer, where Monday is 1 and
440 Sunday is 7. For example, date(2002, 12, 4).isoweekday() == 3, a
441 Wednesday.
442 See also \method{weekday()}, \method{isocalendar()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000443\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000444
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000445\begin{methoddesc}{isocalendar}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000446 Return a 3-tuple, (ISO year, ISO week number, ISO weekday).
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000447
Fred Drake436eadd2002-12-31 18:13:11 +0000448 The ISO calendar is a widely used variant of the Gregorian calendar.
449 See \url{http://www.phys.uu.nl/~vgent/calendar/isocalendar.htm}
450 for a good explanation.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000451
Fred Drake436eadd2002-12-31 18:13:11 +0000452 The ISO year consists of 52 or 53 full weeks, and where a week starts
453 on a Monday and ends on a Sunday. The first week of an ISO year is
454 the first (Gregorian) calendar week of a year containing a Thursday.
455 This is called week number 1, and the ISO year of that Thursday is
456 the same as its Gregorian year.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000457
Fred Drake436eadd2002-12-31 18:13:11 +0000458 For example, 2004 begins on a Thursday, so the first week of ISO
459 year 2004 begins on Monday, 29 Dec 2003 and ends on Sunday, 4 Jan
460 2004, so that
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000461
Fred Drake436eadd2002-12-31 18:13:11 +0000462 date(2003, 12, 29).isocalendar() == (2004, 1, 1)
463 date(2004, 1, 4).isocalendar() == (2004, 1, 7)
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000464\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000465
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000466\begin{methoddesc}{isoformat}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000467 Return a string representing the date in ISO 8601 format,
468 'YYYY-MM-DD'. For example,
469 date(2002, 12, 4).isoformat() == '2002-12-04'.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000470\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000471
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000472\begin{methoddesc}{__str__}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000473 For a date \var{d}, \code{str(\var{d})} is equivalent to
474 \code{\var{d}.isoformat()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000475\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000476
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000477\begin{methoddesc}{ctime}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000478 Return a string representing the date, for example
479 date(2002, 12, 4).ctime() == 'Wed Dec 4 00:00:00 2002'.
480 \code{\var{d}.ctime()} is equivalent to
481 \code{time.ctime(time.mktime(\var{d}.timetuple()))}
482 on platforms where the native C \cfunction{ctime()} function
483 (which \function{time.ctime()} invokes, but which
484 \method{date.ctime()} does not invoke) conforms to the C standard.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000485\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000486
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000487\begin{methoddesc}{strftime}{format}
Fred Drake436eadd2002-12-31 18:13:11 +0000488 Return a string representing the date, controlled by an explicit
489 format string. Format codes referring to hours, minutes or seconds
490 will see 0 values.
491 See the section on \method{strftime()} behavior.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000492\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000493
494
Raymond Hettinger6005a342002-12-30 20:01:24 +0000495\subsection{\class{datetime} Objects \label{datetime-datetime}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000496
Fred Drakebbdb2502002-12-23 18:58:06 +0000497A \class{datetime} object is a single object containing all the
Tim Peters00372022003-01-09 04:10:05 +0000498information from a \class{date} object and a \class{time} object. Like a
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000499\class{date} object, \class{datetime} assumes the current Gregorian
500calendar extended in both directions; like a time object,
501\class{datetime} assumes there are exactly 3600*24 seconds in every
502day.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000503
Tim Peters00372022003-01-09 04:10:05 +0000504Constructor:
505
Fred Drake0f8e5432002-12-31 18:31:48 +0000506\begin{classdesc}{datetime}{year, month, day,
Tim Peters00372022003-01-09 04:10:05 +0000507 hour=0, minute=0, second=0, microsecond=0,
508 tzinfo=None}
509 The year, month and day arguments are required. \var{tzinfo} may
510 be \code{None}, or an instance of a \class{tzinfo} subclass. The
511 remaining arguments may be ints or longs, in the following ranges:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000512
Fred Drake436eadd2002-12-31 18:13:11 +0000513 \begin{itemize}
Tim Peters00372022003-01-09 04:10:05 +0000514 \item \code{MINYEAR <= \var{year} <= MAXYEAR}
Fred Drake436eadd2002-12-31 18:13:11 +0000515 \item \code{1 <= \var{month} <= 12}
516 \item \code{1 <= \var{day} <= number of days in the given month and year}
517 \item \code{0 <= \var{hour} < 24}
518 \item \code{0 <= \var{minute} < 60}
519 \item \code{0 <= \var{second} < 60}
520 \item \code{0 <= \var{microsecond} < 1000000}
521 \end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000522
Tim Peters00372022003-01-09 04:10:05 +0000523 If an argument outside those ranges is given,
524 \exception{ValueError} is raised.
Fred Drake0f8e5432002-12-31 18:31:48 +0000525\end{classdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000526
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000527Other constructors, all class methods:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000528
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000529\begin{methoddesc}{today}{}
Tim Peters00372022003-01-09 04:10:05 +0000530 Return the current local datetime, with \member{tzinfo} \code{None}.
531 This is equivalent to
Fred Drake436eadd2002-12-31 18:13:11 +0000532 \code{datetime.fromtimestamp(time.time())}.
533 See also \method{now()}, \method{fromtimestamp()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000534\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000535
Tim Peters10cadce2003-01-23 19:58:02 +0000536\begin{methoddesc}{now(tz=None)}{}
537 Return the current local date and time. If optional argument
538 \var{tz} is \code{None} or not specified, this is like
539 \method{today()}, but, if possible, supplies more precision than can
540 be gotten from going through a \function{time.time()} timestamp (for
541 example, this may be possible on platforms supplying the C
Fred Drake436eadd2002-12-31 18:13:11 +0000542 \cfunction{gettimeofday()} function).
Tim Peters10cadce2003-01-23 19:58:02 +0000543
544 Else \var{tz} must be an instance of a class \class{tzinfo} subclass,
Tim Peters2a44a8d2003-01-23 20:53:10 +0000545 and the current date and time are converted to \var{tz}'s time
Tim Peters10cadce2003-01-23 19:58:02 +0000546 zone. In this case the result is equivalent to
Tim Peters2a44a8d2003-01-23 20:53:10 +0000547 \code{\var{tz}.fromutc(datetime.utcnow().replace(tzinfo=\var{tz}))}.
Fred Drake436eadd2002-12-31 18:13:11 +0000548 See also \method{today()}, \method{utcnow()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000549\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000550
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000551\begin{methoddesc}{utcnow}{}
Tim Peters10cadce2003-01-23 19:58:02 +0000552 Return the current UTC date and time, with \member{tzinfo} \code{None}.
Tim Peters2a44a8d2003-01-23 20:53:10 +0000553 This is like \method{now()}, but returns the current UTC date and time,
Tim Peters10cadce2003-01-23 19:58:02 +0000554 as a naive \class{datetime} object.
Fred Drake436eadd2002-12-31 18:13:11 +0000555 See also \method{now()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000556\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000557
Tim Peters2a44a8d2003-01-23 20:53:10 +0000558\begin{methoddesc}{fromtimestamp}{timestamp, tz=None}
559 Return the local date and time corresponding to the \POSIX{}
560 timestamp, such as is returned by \function{time.time()}.
561 If optional argument \var{tz} is \code{None} or not specified, the
562 timestamp is converted to the platform's local date and time, and
563 the returned \class{datetime} object is naive.
564
565 Else \var{tz} must be an instance of a class \class{tzinfo} subclass,
566 and the timestamp is converted to \var{tz}'s time zone. In this case
567 the result is equivalent to
568 \code{\var{tz}.fromutc(datetime.utcfromtimestamp(\var{timestamp}).replace(tzinfo=\var{tz}))}.
569
570 \method{fromtimestamp()} may raise \exception{ValueError}, if the
571 timestamp is out of the range of values supported by the platform C
572 \cfunction{localtime()} or \cfunction(gmtime()} functions. It's common
573 for this to be restricted to years in 1970 through 2038.
Tim Peters75a6e3b2003-01-04 18:17:36 +0000574 Note that on non-POSIX systems that include leap seconds in their
575 notion of a timestamp, leap seconds are ignored by
576 \method{fromtimestamp()}, and then it's possible to have two timestamps
577 differing by a second that yield identical \class{datetime} objects.
Fred Drake436eadd2002-12-31 18:13:11 +0000578 See also \method{utcfromtimestamp()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000579\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000580
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000581\begin{methoddesc}{utcfromtimestamp}{timestamp}
Fred Drake436eadd2002-12-31 18:13:11 +0000582 Return the UTC \class{datetime} corresponding to the \POSIX{}
Tim Peters00372022003-01-09 04:10:05 +0000583 timestamp, with \member{tzinfo} \code{None}.
584 This may raise \exception{ValueError}, if the
Fred Drake436eadd2002-12-31 18:13:11 +0000585 timestamp is out of the range of values supported by the platform
586 C \cfunction{gmtime()} function. It's common for this to be
587 restricted to years in 1970 through 2038.
588 See also \method{fromtimestamp()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000589\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000590
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000591\begin{methoddesc}{fromordinal}{ordinal}
Fred Drake436eadd2002-12-31 18:13:11 +0000592 Return the \class{datetime} corresponding to the proleptic
593 Gregorian ordinal, where January 1 of year 1 has ordinal 1.
594 \exception{ValueError} is raised unless 1 <= ordinal <=
595 datetime.max.toordinal(). The hour, minute, second and
Tim Peters00372022003-01-09 04:10:05 +0000596 microsecond of the result are all 0,
597 and \member{tzinfo} is \code{None}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000598\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000599
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000600\begin{methoddesc}{combine}{date, time}
Tim Peters00372022003-01-09 04:10:05 +0000601 Return a new \class{datetime} object whose date members are
Fred Drake436eadd2002-12-31 18:13:11 +0000602 equal to the given \class{date} object's, and whose time
Tim Peters00372022003-01-09 04:10:05 +0000603 and \member{tzinfo} members are equal to the given \class{time} object's.
604 For any \class{datetime} object \var{d}, \code{\var{d} ==
605 datetime.combine(\var{d}.date(), \var{d}.timetz())}. If date is a
606 \class{datetime} object, its time and \member{tzinfo} members are
607 ignored.
608 \end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000609
610Class attributes:
611
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000612\begin{memberdesc}{min}
Fred Drake436eadd2002-12-31 18:13:11 +0000613 The earliest representable \class{datetime},
Tim Peters00372022003-01-09 04:10:05 +0000614 \code{datetime(MINYEAR, 1, 1, tzinfo=None)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000615\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000616
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000617\begin{memberdesc}{max}
Fred Drake436eadd2002-12-31 18:13:11 +0000618 The latest representable \class{datetime},
Tim Peters00372022003-01-09 04:10:05 +0000619 \code{datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999, tzinfo=None)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000620\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000621
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000622\begin{memberdesc}{resolution}
Fred Drake436eadd2002-12-31 18:13:11 +0000623 The smallest possible difference between non-equal \class{datetime}
624 objects, \code{timedelta(microseconds=1)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000625\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000626
627Instance attributes (read-only):
628
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000629\begin{memberdesc}{year}
Tim Peters00372022003-01-09 04:10:05 +0000630 Between \constant{MINYEAR} and \constant{MAXYEAR} inclusive.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000631\end{memberdesc}
632
633\begin{memberdesc}{month}
Tim Peters00372022003-01-09 04:10:05 +0000634 Between 1 and 12 inclusive.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000635\end{memberdesc}
636
637\begin{memberdesc}{day}
Fred Drake436eadd2002-12-31 18:13:11 +0000638 Between 1 and the number of days in the given month of the given
639 year.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000640\end{memberdesc}
641
642\begin{memberdesc}{hour}
Fred Drake436eadd2002-12-31 18:13:11 +0000643 In \code{range(24)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000644\end{memberdesc}
645
646\begin{memberdesc}{minute}
Fred Drake436eadd2002-12-31 18:13:11 +0000647 In \code{range(60)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000648\end{memberdesc}
649
650\begin{memberdesc}{second}
Fred Drake436eadd2002-12-31 18:13:11 +0000651 In \code{range(60)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000652\end{memberdesc}
653
654\begin{memberdesc}{microsecond}
Fred Drake436eadd2002-12-31 18:13:11 +0000655 In \code{range(1000000)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000656\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000657
Tim Peters00372022003-01-09 04:10:05 +0000658\begin{memberdesc}{tzinfo}
659 The object passed as the \var{tzinfo} argument to the
660 \class{datetime} constructor, or \code{None} if none was passed.
661\end{memberdesc}
662
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000663Supported operations:
664
Fred Drakebbdb2502002-12-23 18:58:06 +0000665\begin{itemize}
666 \item
667 datetime1 + timedelta -> datetime2
Tim Peters00372022003-01-09 04:10:05 +0000668
Fred Drakebbdb2502002-12-23 18:58:06 +0000669 timedelta + datetime1 -> datetime2
Tim Peters00372022003-01-09 04:10:05 +0000670
Fred Drakebbdb2502002-12-23 18:58:06 +0000671 datetime2 is a duration of timedelta removed from datetime1, moving
672 forward in time if timedelta.days > 0, or backward if
Tim Peters00372022003-01-09 04:10:05 +0000673 timedelta.days < 0. The result has the same \member{tzinfo} member
674 as the input datetime, and datetime2 - datetime1 == timedelta after.
Fred Drakebbdb2502002-12-23 18:58:06 +0000675 \exception{OverflowError} is raised if datetime2.year would be
676 smaller than \constant{MINYEAR} or larger than \constant{MAXYEAR}.
Tim Peters00372022003-01-09 04:10:05 +0000677 Note that no time zone adjustments are done even if the input is an
678 aware object.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000679
Fred Drakebbdb2502002-12-23 18:58:06 +0000680 \item
681 datetime1 - timedelta -> datetime2
Tim Peters00372022003-01-09 04:10:05 +0000682
Fred Drakebbdb2502002-12-23 18:58:06 +0000683 Computes the datetime2 such that datetime2 + timedelta == datetime1.
Tim Peters00372022003-01-09 04:10:05 +0000684 As for addition, the result has the same \member{tzinfo} member
685 as the input datetime, and no time zone adjustments are done even
686 if the input is aware.
Fred Drakebbdb2502002-12-23 18:58:06 +0000687 This isn't quite equivalent to datetime1 + (-timedelta), because
688 -timedelta in isolation can overflow in cases where
689 datetime1 - timedelta does not.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000690
Fred Drakebbdb2502002-12-23 18:58:06 +0000691 \item
692 datetime1 - datetime2 -> timedelta
Tim Peters00372022003-01-09 04:10:05 +0000693
694 Subtraction of a \class{datetime} from a
695 \class{datetime} is defined only if both
696 operands are naive, or if both are aware. If one is aware and the
697 other is naive, \exception{TypeError} is raised.
698
699 If both are naive, or both are aware and have the same \member{tzinfo}
700 member, the \member{tzinfo} members are ignored, and the result is
701 a \class{timedelta} object \var{t} such that
702 \code{\var{datetime2} + \var{t} == \var{datetime1}}. No time zone
703 adjustments are done in this case.
704
705 If both are aware and have different \member{tzinfo} members,
706 \code{a-b} acts as if \var{a} and \var{b} were first converted to
707 naive UTC datetimes first. The result is
708 \code{(\var{a}.replace(tzinfo=None) - \var{a}.utcoffset()) -
709 (\var{b}.replace(tzinfo=None) - \var{b}.utcoffset())}
710 except that the implementation never overflows.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000711
Fred Drakebbdb2502002-12-23 18:58:06 +0000712 \item
Tim Peters00372022003-01-09 04:10:05 +0000713 comparison of \class{datetime} to \class{datetime},
714 where \var{a} is considered less than \var{b}
715 when \var{a} precedes \var{b} in time. If one comparand is naive and
716 the other is aware, \exception{TypeError} is raised. If both
717 comparands are aware, and have the same \member{tzinfo} member,
718 the common \member{tzinfo} member is ignored and the base datetimes
719 are compared. If both comparands are aware and have different
720 \member{tzinfo} members, the comparands are first adjusted by
721 subtracting their UTC offsets (obtained from \code{self.utcoffset()}).
Tim Peters8d81a012003-01-24 22:36:34 +0000722 \note{In order to stop comparison from falling back to the default
723 scheme of comparing object addresses, datetime comparison
724 normally raises \exception{TypeError} if the other comparand
725 isn't also a \class{datetime} object. However,
726 \code{NotImplemented} is returned instead if the other comparand
727 has a \method{timetuple} attribute. This hook gives other
728 kinds of date objects a chance at implementing mixed-type
729 comparison.}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000730
Fred Drakebbdb2502002-12-23 18:58:06 +0000731 \item
732 hash, use as dict key
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000733
Fred Drakebbdb2502002-12-23 18:58:06 +0000734 \item
735 efficient pickling
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000736
Fred Drakebbdb2502002-12-23 18:58:06 +0000737 \item
738 in Boolean contexts, all \class{datetime} objects are considered
739 to be true
740\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000741
742Instance methods:
743
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000744\begin{methoddesc}{date}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000745 Return \class{date} object with same year, month and day.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000746\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000747
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000748\begin{methoddesc}{time}{}
Tim Peters00372022003-01-09 04:10:05 +0000749 Return \class{time} object with same hour, minute, second and microsecond.
750 \member{tzinfo} is \code{None}. See also method \method{timetz()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000751\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000752
Tim Peters00372022003-01-09 04:10:05 +0000753\begin{methoddesc}{timetz}{}
754 Return \class{time} object with same hour, minute, second, microsecond,
755 and tzinfo members. See also method \method{time()}.
756\end{methoddesc}
757
758\begin{methoddesc}{replace}{year=, month=, day=, hour=, minute=, second=,
759 microsecond=, tzinfo=}
760 Return a datetime with the same members, except for those members given
761 new values by whichever keyword arguments are specified. Note that
762 \code{tzinfo=None} can be specified to create a naive datetime from
763 an aware datetime with no conversion of date and time members.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000764\end{methoddesc}
Tim Peters12bf3392002-12-24 05:41:27 +0000765
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000766\begin{methoddesc}{astimezone}{tz}
Tim Peters00372022003-01-09 04:10:05 +0000767 Return a \class{datetime} object with new \member{tzinfo} member
Tim Petersf196a0a2003-01-22 04:45:50 +0000768 \var{tz}, adjusting the date and time members so the result is the
769 same UTC time as \var{self}, but in \var{tz}'s local time.
770
771 \var{tz} must be an instance of a \class{tzinfo} subclass, and its
772 \method{utcoffset()} and \method{dst()} methods must not return
773 \code{None}. \var{self} must be aware (\code{\var{self}.tzinfo} must
774 not be \code{None}, and \code{\var{self}.utcoffset()} must not return
775 \code{None}).
776
777 If code{\var{self}.tzinfo} is \var{tz},
778 \code{\var{self}.astimezone(\var{tz})} is equal to \var{self}: no
779 adjustment of date or time members is performed.
780 Else the result is local time in time zone \var{tz}, representing the
781 same UTC time as \var{self}: after \code{\var{astz} =
782 \var{dt}.astimezone(\var{tz})},
783 \code{\var{astz} - \var{astz}.utcoffset()} will usually have the same
784 date and time members as \code{\var{dt} - \var{dt}.utcoffset()}.
785 The discussion of class \class{tzinfo} explains the cases at Daylight
786 Saving Time transition boundaries where this cannot be achieved (an issue
787 only if \var{tz} models both standard and daylight time).
788
789 If you merely want to attach a time zone object \var{tz} to a
790 datetime \var{dt} without adjustment of date and time members,
791 use \code{\var{dt}.replace(tzinfo=\var{tz})}. If
792 you merely want to remove the time zone object from an aware datetime
793 \var{dt} without conversion of date and time members, use
794 \code{\var{dt}.replace(tzinfo=None)}.
795
796 Note that the default \method{tzinfo.fromutc()} method can be overridden
797 in a \class{tzinfo} subclass to effect the result returned by
798 \method{astimezone()}. Ignoring error cases, \method{astimezone()}
799 acts like:
800
801 \begin{verbatim}
802 def astimezone(self, tz):
803 if self.tzinfo is tz:
804 return self
805 # Convert self to UTC, and attach the new time zone object.
806 utc = (self - self.utcoffset()).replace(tzinfo=tz)
807 # Convert from UTC to tz's local time.
808 return tz.fromutc(utc)
809 \end{verbatim}
Tim Peters00372022003-01-09 04:10:05 +0000810\end{methoddesc}
811
812\begin{methoddesc}{utcoffset}{}
813 If \member{tzinfo} is \code{None}, returns \code{None}, else
Tim Petersf196a0a2003-01-22 04:45:50 +0000814 returns \code{\var{self}.tzinfo.utcoffset(\var{self})}, and
815 raises an exception if the latter doesn't return \code{None}, or
816 a \class{timedelta} object representing a whole number of minutes
817 with magnitude less than one day.
818\end{methoddesc}
819
820\begin{methoddesc}{dst}{}
821 If \member{tzinfo} is \code{None}, returns \code{None}, else
822 returns \code{\var{self}.tzinfo.dst(\var{self})}, and
823 raises an exception if the latter doesn't return \code{None}, or
824 a \class{timedelta} object representing a whole number of minutes
825 with magnitude less than one day.
Tim Peters00372022003-01-09 04:10:05 +0000826\end{methoddesc}
827
828\begin{methoddesc}{tzname}{}
829 If \member{tzinfo} is \code{None}, returns \code{None}, else
Tim Petersf196a0a2003-01-22 04:45:50 +0000830 returns \code{\var{self}.tzinfo.tzname(\var{self})},
831 raises an exception if the latter doesn't return \code{None} or
832 a string object,
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000833\end{methoddesc}
Tim Peters80475bb2002-12-25 07:40:55 +0000834
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000835\begin{methoddesc}{timetuple}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000836 Return a 9-element tuple of the form returned by
837 \function{time.localtime()}.
Tim Peters00372022003-01-09 04:10:05 +0000838 \code{\var{d}.timetuple()} is equivalent to
Fred Drake436eadd2002-12-31 18:13:11 +0000839 \code{(\var{d}.year, \var{d}.month, \var{d}.day,
840 \var{d}.hour, \var{d}.minute, \var{d}.second,
Tim Peters00372022003-01-09 04:10:05 +0000841 \var{d}.weekday(),
Fred Drake436eadd2002-12-31 18:13:11 +0000842 \var{d}.toordinal() - date(\var{d}.year, 1, 1).toordinal() + 1,
Tim Peters00372022003-01-09 04:10:05 +0000843 dst)}
844 The \member{tm_isdst} flag of the result is set according to
845 the \method{dst()} method: \member{tzinfo} is \code{None} or
846 \method{dst()} returns \code{None},
847 \member{tm_isdst} is set to \code{-1}; else if \method{dst()} returns
848 a non-zero value, \member{tm_isdst} is set to \code{1};
849 else \code{tm_isdst} is set to \code{0}.
850\end{methoddesc}
851
852\begin{methoddesc}{utctimetuple}{}
853 If \class{datetime} instance \var{d} is naive, this is the same as
854 \code{\var{d}.timetuple()} except that \member{tm_isdst} is forced to 0
855 regardless of what \code{d.dst()} returns. DST is never in effect
856 for a UTC time.
857
858 If \var{d} is aware, \var{d} is normalized to UTC time, by subtracting
859 \code{\var{d}.utcoffset()}, and a timetuple for the
860 normalized time is returned. \member{tm_isdst} is forced to 0.
861 Note that the result's \member{tm_year} member may be
862 \constant{MINYEAR}-1 or \constant{MAXYEAR}+1, if \var{d}.year was
863 \code{MINYEAR} or \code{MAXYEAR} and UTC adjustment spills over a
864 year boundary.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000865\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000866
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000867\begin{methoddesc}{toordinal}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000868 Return the proleptic Gregorian ordinal of the date. The same as
Tim Peters00372022003-01-09 04:10:05 +0000869 \code{self.date().toordinal()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000870\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000871
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000872\begin{methoddesc}{weekday}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000873 Return the day of the week as an integer, where Monday is 0 and
Tim Peters00372022003-01-09 04:10:05 +0000874 Sunday is 6. The same as \code{self.date().weekday()}.
Fred Drake436eadd2002-12-31 18:13:11 +0000875 See also \method{isoweekday()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000876\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000877
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000878\begin{methoddesc}{isoweekday}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000879 Return the day of the week as an integer, where Monday is 1 and
Tim Peters00372022003-01-09 04:10:05 +0000880 Sunday is 7. The same as \code{self.date().isoweekday)}.
Fred Drake436eadd2002-12-31 18:13:11 +0000881 See also \method{weekday()}, \method{isocalendar()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000882\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000883
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000884\begin{methoddesc}{isocalendar}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000885 Return a 3-tuple, (ISO year, ISO week number, ISO weekday). The
Tim Peters00372022003-01-09 04:10:05 +0000886 same as \code{self.date().isocalendar()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000887\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000888
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000889\begin{methoddesc}{isoformat}{sep='T'}
Fred Drake436eadd2002-12-31 18:13:11 +0000890 Return a string representing the date and time in ISO 8601 format,
891 YYYY-MM-DDTHH:MM:SS.mmmmmm
Tim Peters00372022003-01-09 04:10:05 +0000892 or, if \member{microsecond} is 0,
Fred Drake436eadd2002-12-31 18:13:11 +0000893 YYYY-MM-DDTHH:MM:SS
Tim Peters00372022003-01-09 04:10:05 +0000894
895 If \method{utcoffset()} does not return \code{None}, a 6-character
896 string is appended, giving the UTC offset in (signed) hours and
897 minutes:
898 YYYY-MM-DDTHH:MM:SS.mmmmmm+HH:MM
899 or, if \member{microsecond} is 0
900 YYYY-MM-DDTHH:MM:SS+HH:MM
901
Fred Drake436eadd2002-12-31 18:13:11 +0000902 The optional argument \var{sep} (default \code{'T'}) is a
903 one-character separator, placed between the date and time portions
904 of the result. For example,
Tim Peters00372022003-01-09 04:10:05 +0000905
906\begin{verbatim}
907>>> from datetime import tzinfo, timedelta, datetime
908>>> class TZ(tzinfo):
909... def utcoffset(self, dt): return timedelta(minutes=-399)
910...
911>>> datetime(2002, 12, 25, tzinfo=TZ()).isoformat(' ')
912'2002-12-25 00:00:00-06:39'
913\end{verbatim}
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000914\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000915
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000916\begin{methoddesc}{__str__}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000917 For a \class{datetime} instance \var{d}, \code{str(\var{d})} is
918 equivalent to \code{\var{d}.isoformat(' ')}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000919\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000920
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000921\begin{methoddesc}{ctime}{}
Tim Peters00372022003-01-09 04:10:05 +0000922 Return a string representing the date and time, for example
923 \code{datetime(2002, 12, 4, 20, 30, 40).ctime() ==
924 'Wed Dec 4 20:30:40 2002'}.
Fred Drake436eadd2002-12-31 18:13:11 +0000925 \code{d.ctime()} is equivalent to
926 \code{time.ctime(time.mktime(d.timetuple()))} on platforms where
927 the native C \cfunction{ctime()} function (which
928 \function{time.ctime()} invokes, but which
929 \method{datetime.ctime()} does not invoke) conforms to the C
930 standard.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000931\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000932
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000933\begin{methoddesc}{strftime}{format}
Fred Drake436eadd2002-12-31 18:13:11 +0000934 Return a string representing the date and time, controlled by an
935 explicit format string. See the section on \method{strftime()}
936 behavior.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000937\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000938
939
Raymond Hettinger6005a342002-12-30 20:01:24 +0000940\subsection{\class{time} Objects \label{datetime-time}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000941
Tim Peters00372022003-01-09 04:10:05 +0000942A time object represents a (local) time of day, independent of any
943particular day, and subject to adjustment via a \class{tzinfo} object.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000944
Tim Peters00372022003-01-09 04:10:05 +0000945\begin{classdesc}{time}{hour=0, minute=0, second=0, microsecond=0,
946 tzinfo=None}
947 All arguments are optional. \var{tzinfo} may be \code{None}, or
948 an instance of a \class{tzinfo} subclass. The remaining arguments
949 may be ints or longs, in the following ranges:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000950
Tim Peters00372022003-01-09 04:10:05 +0000951 \begin{itemize}
952 \item \code{0 <= \var{hour} < 24}
953 \item \code{0 <= \var{minute} < 60}
954 \item \code{0 <= \var{second} < 60}
955 \item \code{0 <= \var{microsecond} < 1000000}.
956 \end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000957
Tim Peters00372022003-01-09 04:10:05 +0000958 If an argument outside those ranges is given,
959 \exception{ValueError} is raised.
Fred Drake0f8e5432002-12-31 18:31:48 +0000960\end{classdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000961
962Class attributes:
963
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000964\begin{memberdesc}{min}
Fred Drake436eadd2002-12-31 18:13:11 +0000965 The earliest representable \class{time}, \code{time(0, 0, 0, 0)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000966\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000967
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000968\begin{memberdesc}{max}
Fred Drake436eadd2002-12-31 18:13:11 +0000969 The latest representable \class{time}, \code{time(23, 59, 59, 999999)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000970\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000971
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000972\begin{memberdesc}{resolution}
Fred Drake436eadd2002-12-31 18:13:11 +0000973 The smallest possible difference between non-equal \class{time}
974 objects, \code{timedelta(microseconds=1)}, although note that
975 arithmetic on \class{time} objects is not supported.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000976\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000977
978Instance attributes (read-only):
979
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000980\begin{memberdesc}{hour}
Fred Drake436eadd2002-12-31 18:13:11 +0000981 In \code{range(24)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000982\end{memberdesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000983
Tim Petersbad8ff02002-12-30 20:52:32 +0000984\begin{memberdesc}{minute}
Fred Drake436eadd2002-12-31 18:13:11 +0000985 In \code{range(60)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000986\end{memberdesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000987
Tim Petersbad8ff02002-12-30 20:52:32 +0000988\begin{memberdesc}{second}
Fred Drake436eadd2002-12-31 18:13:11 +0000989 In \code{range(60)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000990\end{memberdesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000991
Tim Petersbad8ff02002-12-30 20:52:32 +0000992\begin{memberdesc}{microsecond}
Fred Drake436eadd2002-12-31 18:13:11 +0000993 In \code{range(1000000)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000994\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000995
Tim Peters00372022003-01-09 04:10:05 +0000996\begin{memberdesc}{tzinfo}
997 The object passed as the tzinfo argument to the \class{time}
998 constructor, or \code{None} if none was passed.
999\end{memberdesc}
1000
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001001Supported operations:
1002
Fred Drakebbdb2502002-12-23 18:58:06 +00001003\begin{itemize}
1004 \item
Tim Peters00372022003-01-09 04:10:05 +00001005 comparison of \class{time} to \class{time},
1006 where \var{a} is considered less than \var{b} when \var{a} precedes
1007 \var{b} in time. If one comparand is naive and the other is aware,
1008 \exception{TypeError} is raised. If both comparands are aware, and
1009 have the same \member{tzinfo} member, the common \member{tzinfo}
1010 member is ignored and the base times are compared. If both
1011 comparands are aware and have different \member{tzinfo} members,
1012 the comparands are first adjusted by subtracting their UTC offsets
1013 (obtained from \code{self.utcoffset()}).
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001014
Fred Drakebbdb2502002-12-23 18:58:06 +00001015 \item
1016 hash, use as dict key
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001017
Fred Drakebbdb2502002-12-23 18:58:06 +00001018 \item
1019 efficient pickling
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001020
Fred Drakebbdb2502002-12-23 18:58:06 +00001021 \item
Tim Peters00372022003-01-09 04:10:05 +00001022 in Boolean contexts, a \class{time} object is considered to be
1023 true if and only if, after converting it to minutes and
1024 subtracting \method{utcoffset()} (or \code{0} if that's
1025 \code{None}), the result is non-zero.
Fred Drakebbdb2502002-12-23 18:58:06 +00001026\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001027
1028Instance methods:
1029
Tim Peters00372022003-01-09 04:10:05 +00001030\begin{methoddesc}{replace}(hour=, minute=, second=, microsecond=, tzinfo=)
1031 Return a \class{time} with the same value, except for those members given
1032 new values by whichever keyword arguments are specified. Note that
1033 \code{tzinfo=None} can be specified to create a naive \class{time} from
1034 an aware \class{time}, without conversion of the time members.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001035\end{methoddesc}
Tim Peters12bf3392002-12-24 05:41:27 +00001036
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001037\begin{methoddesc}{isoformat}{}
Fred Drake436eadd2002-12-31 18:13:11 +00001038 Return a string representing the time in ISO 8601 format,
1039 HH:MM:SS.mmmmmm
Tim Peters00372022003-01-09 04:10:05 +00001040 or, if self.microsecond is 0,
Fred Drake436eadd2002-12-31 18:13:11 +00001041 HH:MM:SS
Tim Peters00372022003-01-09 04:10:05 +00001042 If \method{utcoffset()} does not return \code{None}, a 6-character
1043 string is appended, giving the UTC offset in (signed) hours and
1044 minutes:
1045 HH:MM:SS.mmmmmm+HH:MM
1046 or, if self.microsecond is 0,
1047 HH:MM:SS+HH:MM
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001048\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001049
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001050\begin{methoddesc}{__str__}{}
Fred Drake436eadd2002-12-31 18:13:11 +00001051 For a time \var{t}, \code{str(\var{t})} is equivalent to
1052 \code{\var{t}.isoformat()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001053\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001054
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001055\begin{methoddesc}{strftime}{format}
Fred Drake436eadd2002-12-31 18:13:11 +00001056 Return a string representing the time, controlled by an explicit
1057 format string. See the section on \method{strftime()} behavior.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001058\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001059
Tim Peters00372022003-01-09 04:10:05 +00001060\begin{methoddesc}{utcoffset}{}
1061 If \member{tzinfo} is \code{None}, returns \code{None}, else
Tim Petersf196a0a2003-01-22 04:45:50 +00001062 returns \code{\var{self}.tzinfo.utcoffset(None)}, and
1063 raises an exception if the latter doesn't return \code{None} or
1064 a \class{timedelta} object representing a whole number of minutes
1065 with magnitude less than one day.
Tim Peters00372022003-01-09 04:10:05 +00001066\end{methoddesc}
1067
1068\begin{methoddesc}{dst}{}
1069 If \member{tzinfo} is \code{None}, returns \code{None}, else
Tim Petersf196a0a2003-01-22 04:45:50 +00001070 returns \code{\var{self}.tzinfo.dst(None)}, and
1071 raises an exception if the latter doesn't return \code{None}, or
1072 a \class{timedelta} object representing a whole number of minutes
1073 with magnitude less than one day.
Tim Peters00372022003-01-09 04:10:05 +00001074\end{methoddesc}
1075
1076\begin{methoddesc}{tzname}{}
1077 If \member{tzinfo} is \code{None}, returns \code{None}, else
Tim Petersf196a0a2003-01-22 04:45:50 +00001078 returns \code{\var{self}.tzinfo.tzname(None)}, or
1079 raises an exception if the latter doesn't return \code{None} or
1080 a string object.
Tim Peters00372022003-01-09 04:10:05 +00001081\end{methoddesc}
1082
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001083
Raymond Hettinger6005a342002-12-30 20:01:24 +00001084\subsection{\class{tzinfo} Objects \label{datetime-tzinfo}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001085
Fred Drakebbdb2502002-12-23 18:58:06 +00001086\class{tzinfo} is an abstract base clase, meaning that this class
1087should not be instantiated directly. You need to derive a concrete
1088subclass, and (at least) supply implementations of the standard
1089\class{tzinfo} methods needed by the \class{datetime} methods you
Tim Petersbad8ff02002-12-30 20:52:32 +00001090use. The \module{datetime} module does not supply any concrete
Fred Drakebbdb2502002-12-23 18:58:06 +00001091subclasses of \class{tzinfo}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001092
Fred Drakebbdb2502002-12-23 18:58:06 +00001093An instance of (a concrete subclass of) \class{tzinfo} can be passed
Tim Peters00372022003-01-09 04:10:05 +00001094to the constructors for \class{datetime} and \class{time} objects.
1095The latter objects view their members as being in local time, and the
Fred Drakebbdb2502002-12-23 18:58:06 +00001096\class{tzinfo} object supports methods revealing offset of local time
1097from UTC, the name of the time zone, and DST offset, all relative to a
1098date or time object passed to them.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001099
Tim Petersbad8ff02002-12-30 20:52:32 +00001100Special requirement for pickling: A \class{tzinfo} subclass must have an
Tim Peters2483b612002-12-24 16:30:58 +00001101\method{__init__} method that can be called with no arguments, else it
1102can be pickled but possibly not unpickled again. This is a technical
1103requirement that may be relaxed in the future.
1104
Fred Drakebbdb2502002-12-23 18:58:06 +00001105A concrete subclass of \class{tzinfo} may need to implement the
1106following methods. Exactly which methods are needed depends on the
Tim Petersbad8ff02002-12-30 20:52:32 +00001107uses made of aware \module{datetime} objects. If in doubt, simply
1108implement all of them.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001109
Tim Petersbad8ff02002-12-30 20:52:32 +00001110\begin{methoddesc}{utcoffset}{self, dt}
Fred Drake436eadd2002-12-31 18:13:11 +00001111 Return offset of local time from UTC, in minutes east of UTC. If
1112 local time is west of UTC, this should be negative. Note that this
1113 is intended to be the total offset from UTC; for example, if a
1114 \class{tzinfo} object represents both time zone and DST adjustments,
1115 \method{utcoffset()} should return their sum. If the UTC offset
1116 isn't known, return \code{None}. Else the value returned must be
Tim Peters397301e2003-01-02 21:28:08 +00001117 a \class{timedelta} object specifying a whole number of minutes in the
1118 range -1439 to 1439 inclusive (1440 = 24*60; the magnitude of the offset
1119 must be less than one day). Most implementations of
1120 \method{utcoffset()} will probably look like one of these two:
Fred Drake436eadd2002-12-31 18:13:11 +00001121
Tim Petersbad8ff02002-12-30 20:52:32 +00001122\begin{verbatim}
Tim Petersf3615152003-01-01 21:51:37 +00001123 return CONSTANT # fixed-offset class
Fred Drake436eadd2002-12-31 18:13:11 +00001124 return CONSTANT + self.dst(dt) # daylight-aware class
Guido van Rossum8e7ec7c2002-12-31 04:39:05 +00001125\end{verbatim}
Tim Peters710fb152003-01-02 19:35:54 +00001126
1127 If \method{utcoffset()} does not return \code{None},
1128 \method{dst()} should not return \code{None} either.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001129
Tim Peters327098a2003-01-20 22:54:38 +00001130 The default implementation of \method{utcoffset()} raises
1131 \exception{NotImplementedError}.
1132\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001133
Tim Petersbad8ff02002-12-30 20:52:32 +00001134\begin{methoddesc}{dst}{self, dt}
Tim Peters00372022003-01-09 04:10:05 +00001135 Return the daylight saving time (DST) adjustment, in minutes east of
Tim Petersb01c39b2003-01-21 16:44:27 +00001136 UTC, or \code{None} if DST information isn't known. Return
1137 \code{timedelta(0)} if DST is not in effect.
Tim Peters397301e2003-01-02 21:28:08 +00001138 If DST is in effect, return the offset as a
Fred Drake436eadd2002-12-31 18:13:11 +00001139 \class{timedelta} object (see \method{utcoffset()} for details).
1140 Note that DST offset, if applicable, has
1141 already been added to the UTC offset returned by
1142 \method{utcoffset()}, so there's no need to consult \method{dst()}
Tim Peters327098a2003-01-20 22:54:38 +00001143 unless you're interested in obtaining DST info separately. For
Tim Peters00372022003-01-09 04:10:05 +00001144 example, \method{datetime.timetuple()} calls its \member{tzinfo}
Fred Drake436eadd2002-12-31 18:13:11 +00001145 member's \method{dst()} method to determine how the
Tim Petersf3615152003-01-01 21:51:37 +00001146 \member{tm_isdst} flag should be set, and
Tim Petersf196a0a2003-01-22 04:45:50 +00001147 \method{tzinfo.fromutc()} calls \method{dst()} to account for
Tim Petersf3615152003-01-01 21:51:37 +00001148 DST changes when crossing time zones.
1149
1150 An instance \var{tz} of a \class{tzinfo} subclass that models both
1151 standard and daylight times must be consistent in this sense:
1152
Tim Petersf196a0a2003-01-22 04:45:50 +00001153 \code{\var{tz}.utcoffset(\var{dt}) - \var{tz}.dst(\var{dt})}
Tim Petersf3615152003-01-01 21:51:37 +00001154
Tim Peters00372022003-01-09 04:10:05 +00001155 must return the same result for every \class{datetime} \var{dt}
Tim Petersf196a0a2003-01-22 04:45:50 +00001156 with \code{\var{dt}.tzinfo==\var{tz}} For sane \class{tzinfo}
1157 subclasses, this expression yields the time zone's "standard offset",
1158 which should not depend on the date or the time, but only on geographic
1159 location. The implementation of \method{datetime.astimezone()} relies
1160 on this, but cannot detect violations; it's the programmer's
1161 responsibility to ensure it. If a \class{tzinfo} subclass cannot
1162 guarantee this, it may be able to override the default implementation
1163 of \method{tzinfo.fromutc()} to work correctly with \method{astimezone()}
1164 regardless.
1165
1166 Most implementations of \method{dst()} will probably look like one
1167 of these two:
1168
1169\begin{verbatim}
1170 return timedelta(0) # a fixed-offset class: doesn't account for DST
1171
1172 or
1173
1174 # Code to set dston and dstoff to the time zone's DST transition
1175 # times based on the input dt.year, and expressed in standard local
1176 # time. Then
1177
1178 if dston <= dt.replace(tzinfo=None) < dstoff:
1179 return timedelta(hours=1)
1180 else:
1181 return timedelta(0)
1182\end{verbatim}
Tim Petersf3615152003-01-01 21:51:37 +00001183
Tim Peters327098a2003-01-20 22:54:38 +00001184 The default implementation of \method{dst()} raises
1185 \exception{NotImplementedError}.
1186\end{methoddesc}
1187
Tim Peters710fb152003-01-02 19:35:54 +00001188\begin{methoddesc}{tzname}{self, dt}
Tim Petersf196a0a2003-01-22 04:45:50 +00001189 Return the time zone name corresponding to the \class{datetime}
1190 object \var{dt}, as a string.
1191 Nothing about string names is defined by the
1192 \module{datetime} module, and there's no requirement that it mean
1193 anything in particular. For example, "GMT", "UTC", "-500", "-5:00",
1194 "EDT", "US/Eastern", "America/New York" are all valid replies. Return
Tim Peters710fb152003-01-02 19:35:54 +00001195 \code{None} if a string name isn't known. Note that this is a method
Tim Petersf196a0a2003-01-22 04:45:50 +00001196 rather than a fixed string primarily because some \class{tzinfo}
1197 subclasses will wish to return different names depending on the specific
1198 value of \var{dt} passed, especially if the \class{tzinfo} class is
Tim Peters710fb152003-01-02 19:35:54 +00001199 accounting for daylight time.
Tim Peters710fb152003-01-02 19:35:54 +00001200
Tim Peters327098a2003-01-20 22:54:38 +00001201 The default implementation of \method{tzname()} raises
1202 \exception{NotImplementedError}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001203\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001204
Tim Peters00372022003-01-09 04:10:05 +00001205These methods are called by a \class{datetime} or \class{time} object,
1206in response to their methods of the same names. A \class{datetime}
1207object passes itself as the argument, and a \class{time} object passes
Tim Petersbad8ff02002-12-30 20:52:32 +00001208\code{None} as the argument. A \class{tzinfo} subclass's methods should
1209therefore be prepared to accept a \var{dt} argument of \code{None}, or of
Tim Peters00372022003-01-09 04:10:05 +00001210class \class{datetime}.
Tim Petersbad8ff02002-12-30 20:52:32 +00001211
1212When \code{None} is passed, it's up to the class designer to decide the
1213best response. For example, returning \code{None} is appropriate if the
Tim Peters00372022003-01-09 04:10:05 +00001214class wishes to say that time objects don't participate in the
Tim Petersf196a0a2003-01-22 04:45:50 +00001215\class{tzinfo} protocols. It may be more useful for \code{utcoffset(None)}
Tim Peters327098a2003-01-20 22:54:38 +00001216to return the standard UTC offset, as there is no other convention for
1217discovering the standard offset.
Tim Petersbad8ff02002-12-30 20:52:32 +00001218
Tim Peters00372022003-01-09 04:10:05 +00001219When a \class{datetime} object is passed in response to a
1220\class{datetime} method, \code{dt.tzinfo} is the same object as
Tim Petersbad8ff02002-12-30 20:52:32 +00001221\var{self}. \class{tzinfo} methods can rely on this, unless
1222user code calls \class{tzinfo} methods directly. The intent is that
1223the \class{tzinfo} methods interpret \var{dt} as being in local time,
Tim Peters327098a2003-01-20 22:54:38 +00001224and not need worry about objects in other timezones.
Tim Petersbad8ff02002-12-30 20:52:32 +00001225
Tim Petersf196a0a2003-01-22 04:45:50 +00001226There is one more \class{tzinfo} method that a subclass may wish to
1227override:
1228
1229\begin{methoddesc}{fromutc}{self, dt}
1230 This is called from the default \class{datetime.astimezone()}
1231 implementation. When called from that, \code{\var{dt}.tzinfo} is
1232 \var{self}, and \var{dt}'s date and time members are to be viewed as
1233 expressing a UTC time. The purpose of \method{fromutc()} is to
1234 adjust the date and time members, returning an equivalent datetime in
1235 \var{self}'s local time.
1236
1237 Most \class{tzinfo} subclasses should be able to inherit the default
1238 \method{fromutc()} implementation without problems. It's strong enough
1239 to handle fixed-offset time zones, and time zones accounting for both
1240 standard and daylight time, and the latter even if the DST transition
1241 times differ in different years. An example of a time zone the default
1242 \method{fromutc()} implementation may not handle correctly in all cases
1243 is one where the standard offset (from UTC) depends on the specific date
1244 and time passed, which can happen for political reasons.
1245 The default implementations of \method{astimezone()} and
1246 \method{fromutc()} may not produce the result you want if the result is
1247 one of the hours straddling the moment the standard offset changes.
1248
1249 Skipping code for error cases, the default \method{fromutc()}
1250 implementation acts like:
1251
1252 \begin{verbatim}
1253 def fromutc(self, dt):
1254 # raise ValueError error if dt.tzinfo is not self
1255 dtoff = dt.utcoffset()
1256 dtdst = dt.dst()
1257 # raise ValueError if dtoff is None or dtdst is None
1258 delta = dtoff - dtdst # this is self's standard offset
1259 if delta:
1260 dt += delta # convert to standard local time
1261 dtdst = dt.dst()
1262 # raise ValueError if dtdst is None
1263 if dtdst:
1264 return dt + dtdst
1265 else:
1266 return dt
1267 \end{verbatim}
1268\end{methoddesc}
1269
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001270Example \class{tzinfo} classes:
1271
Fred Drakebbdb2502002-12-23 18:58:06 +00001272\verbatiminput{tzinfo-examples.py}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001273
Tim Peters327098a2003-01-20 22:54:38 +00001274Note that there are unavoidable subtleties twice per year in a
1275\class{tzinfo}
Tim Petersadf64202003-01-04 06:03:15 +00001276subclass accounting for both standard and daylight time, at the DST
1277transition points. For concreteness, consider US Eastern (UTC -0500),
1278where EDT begins the minute after 1:59 (EST) on the first Sunday in
1279April, and ends the minute after 1:59 (EDT) on the last Sunday in October:
1280
1281\begin{verbatim}
1282 UTC 3:MM 4:MM 5:MM 6:MM 7:MM 8:MM
1283 EST 22:MM 23:MM 0:MM 1:MM 2:MM 3:MM
1284 EDT 23:MM 0:MM 1:MM 2:MM 3:MM 4:MM
1285
1286 start 22:MM 23:MM 0:MM 1:MM 3:MM 4:MM
1287
1288 end 23:MM 0:MM 1:MM 1:MM 2:MM 3:MM
1289\end{verbatim}
1290
1291When DST starts (the "start" line), the local wall clock leaps from 1:59
1292to 3:00. A wall time of the form 2:MM doesn't really make sense on that
Tim Peters75a6e3b2003-01-04 18:17:36 +00001293day, so \code{astimezone(Eastern)} won't deliver a result with
1294\code{hour==2} on the
Tim Peters327098a2003-01-20 22:54:38 +00001295day DST begins. In order for \method{astimezone()} to make this
Tim Petersf196a0a2003-01-22 04:45:50 +00001296guarantee, the \method{rzinfo.dst()} method must consider times
Tim Peters327098a2003-01-20 22:54:38 +00001297in the "missing hour" (2:MM for Eastern) to be in daylight time.
Tim Petersadf64202003-01-04 06:03:15 +00001298
1299When DST ends (the "end" line), there's a potentially worse problem:
Tim Peters327098a2003-01-20 22:54:38 +00001300there's an hour that can't be spelled unambiguously in local wall time:
1301the last hour of daylight time. In Eastern, that's times of
1302the form 5:MM UTC on the day daylight time ends. The local wall clock
Tim Petersadf64202003-01-04 06:03:15 +00001303leaps from 1:59 (daylight time) back to 1:00 (standard time) again.
Tim Peters327098a2003-01-20 22:54:38 +00001304Local times of the form 1:MM are ambiguous. \method{astimezone()} mimics
1305the local clock's behavior by mapping two adjacent UTC hours into the
1306same local hour then. In the Eastern example, UTC times of the form
13075:MM and 6:MM both map to 1:MM when converted to Eastern. In order for
Tim Petersf196a0a2003-01-22 04:45:50 +00001308\method{astimezone()} to make this guarantee, the \method{tzinfo.dst()}
1309method must consider times in the "repeated hour" to be in
Tim Peters327098a2003-01-20 22:54:38 +00001310standard time. This is easily arranged, as in the example, by expressing
1311DST switch times in the time zone's standard local time.
Tim Petersadf64202003-01-04 06:03:15 +00001312
Tim Peters327098a2003-01-20 22:54:38 +00001313Applications that can't bear such ambiguities should avoid using hybrid
1314\class{tzinfo} subclasses; there are no ambiguities when using UTC, or
1315any other fixed-offset \class{tzinfo} subclass (such as a class
1316representing only EST (fixed offset -5 hours), or only EDT (fixed offset
1317-4 hours)).
Tim Petersadf64202003-01-04 06:03:15 +00001318
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001319
Tim Peters29fb9c72002-12-23 22:21:52 +00001320\subsection{\method{strftime()} Behavior}
1321
Tim Peters00372022003-01-09 04:10:05 +00001322\class{date}, \class{datetime}, and \class{time}
1323objects all support a \code{strftime(\var{format})}
Tim Peters29fb9c72002-12-23 22:21:52 +00001324method, to create a string representing the time under the control of
1325an explicit format string. Broadly speaking,
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001326\code{d.strftime(fmt)}
Tim Peters29fb9c72002-12-23 22:21:52 +00001327acts like the \refmodule{time} module's
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001328\code{time.strftime(fmt, d.timetuple())}
Tim Peters29fb9c72002-12-23 22:21:52 +00001329although not all objects support a \method{timetuple()} method.
1330
Tim Peters00372022003-01-09 04:10:05 +00001331For \class{time} objects, the format codes for
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001332year, month, and day should not be used, as time objects have no such
1333values. If they're used anyway, \code{1900} is substituted for the
1334year, and \code{0} for the month and day.
Tim Peters29fb9c72002-12-23 22:21:52 +00001335
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001336For \class{date} objects, the format codes for hours, minutes, and
1337seconds should not be used, as \class{date} objects have no such
1338values. If they're used anyway, \code{0} is substituted for them.
Tim Peters29fb9c72002-12-23 22:21:52 +00001339
Fred Drakea37e5cc2002-12-30 21:26:42 +00001340For a naive object, the \code{\%z} and \code{\%Z} format codes are
Tim Peters29fb9c72002-12-23 22:21:52 +00001341replaced by empty strings.
1342
1343For an aware object:
1344
1345\begin{itemize}
1346 \item[\code{\%z}]
1347 \method{utcoffset()} is transformed into a 5-character string of
1348 the form +HHMM or -HHMM, where HH is a 2-digit string giving the
1349 number of UTC offset hours, and MM is a 2-digit string giving the
1350 number of UTC offset minutes. For example, if
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001351 \method{utcoffset()} returns \code{timedelta(hours=-3, minutes=-30)},
Tim Peters1cff9fc2002-12-24 16:25:29 +00001352 \code{\%z} is replaced with the string \code{'-0330'}.
Tim Peters29fb9c72002-12-23 22:21:52 +00001353
1354 \item[\code{\%Z}]
1355 If \method{tzname()} returns \code{None}, \code{\%Z} is replaced
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001356 by an empty string. Otherwise \code{\%Z} is replaced by the returned
Tim Peters29fb9c72002-12-23 22:21:52 +00001357 value, which must be a string.
1358\end{itemize}
1359
1360The full set of format codes supported varies across platforms,
1361because Python calls the platform C library's \function{strftime()}
1362function, and platform variations are common. The documentation for
1363Python's \refmodule{time} module lists the format codes that the C
1364standard (1989 version) requires, and those work on all platforms
1365with a standard C implementation. Note that the 1999 version of the
1366C standard added additional format codes.
1367
1368The exact range of years for which \method{strftime()} works also
1369varies across platforms. Regardless of platform, years before 1900
1370cannot be used.
1371
1372
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001373\begin{comment}
1374
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001375\subsection{C API}
1376
1377Struct typedefs:
1378
1379 PyDateTime_Date
1380 PyDateTime_DateTime
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001381 PyDateTime_Time
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001382 PyDateTime_Delta
1383 PyDateTime_TZInfo
1384
1385Type-check macros:
1386
1387 PyDate_Check(op)
1388 PyDate_CheckExact(op)
1389
1390 PyDateTime_Check(op)
1391 PyDateTime_CheckExact(op)
1392
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001393 PyTime_Check(op)
1394 PyTime_CheckExact(op)
1395
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001396 PyDelta_Check(op)
1397 PyDelta_CheckExact(op)
1398
1399 PyTZInfo_Check(op)
Raymond Hettingercbd6cd22002-12-31 16:30:49 +00001400 PyTZInfo_CheckExact(op)
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001401
1402Accessor macros:
1403
1404All objects are immutable, so accessors are read-only. All macros
1405return ints:
1406
Tim Petersf196a0a2003-01-22 04:45:50 +00001407 For \class{date} and \class{datetime} instances:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001408 PyDateTime_GET_YEAR(o)
1409 PyDateTime_GET_MONTH(o)
1410 PyDateTime_GET_DAY(o)
1411
Tim Petersf196a0a2003-01-22 04:45:50 +00001412 For \class{datetime} instances:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001413 PyDateTime_DATE_GET_HOUR(o)
1414 PyDateTime_DATE_GET_MINUTE(o)
1415 PyDateTime_DATE_GET_SECOND(o)
1416 PyDateTime_DATE_GET_MICROSECOND(o)
1417
Tim Petersf196a0a2003-01-22 04:45:50 +00001418 For \class{time} instances:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001419 PyDateTime_TIME_GET_HOUR(o)
1420 PyDateTime_TIME_GET_MINUTE(o)
1421 PyDateTime_TIME_GET_SECOND(o)
1422 PyDateTime_TIME_GET_MICROSECOND(o)
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001423
1424\end{comment}