blob: c9939ece097e116120bb19656c6fc1e971020051 [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
12\versionadded{2.3}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000013
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000014
15The \module{datetime} module supplies classes for manipulating dates
16and times in both simple and complex ways. While date and time
17arithmetic is supported, the focus of the implementation is on
Andrew M. Kuchling9b445712003-01-09 13:46:30 +000018efficient member extraction for output formatting and manipulation.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000019
Fred Drakea37e5cc2002-12-30 21:26:42 +000020There are two kinds of date and time objects: ``naive'' and ``aware''.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000021This distinction refers to whether the object has any notion of time
Tim Peters00372022003-01-09 04:10:05 +000022zone, daylight saving time, or other kind of algorithmic or political
23time adjustment. Whether a naive \class{datetime} object represents
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000024Coordinated Universal Time (UTC), local time, or time in some other
25timezone is purely up to the program, just like it's up to the program
Fred Drakea37e5cc2002-12-30 21:26:42 +000026whether a particular number represents meters, miles, or mass. Naive
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000027\class{datetime} objects are easy to understand and to work with, at
28the cost of ignoring some aspects of reality.
29
Andrew M. Kuchling9b445712003-01-09 13:46:30 +000030For applications requiring more, \class{datetime} and \class{time}
31objects have an optional time zone information member,
Tim Peters327098a2003-01-20 22:54:38 +000032\member{tzinfo}, that can contain an instance of a subclass of
Andrew M. Kuchling9b445712003-01-09 13:46:30 +000033the abstract \class{tzinfo} class. These \class{tzinfo} objects
34capture information about the offset from UTC time, the time zone
35name, and whether Daylight Saving Time is in effect. Note that no
36concrete \class{tzinfo} classes are supplied by the \module{datetime}
Andrew M. Kuchling570e3582003-02-05 21:15:38 +000037module. Supporting timezones at whatever level of detail is required
38is up to the application. The rules for time adjustment across the
39world are more political than rational, and there is no standard
Andrew M. Kuchling9b445712003-01-09 13:46:30 +000040suitable for every application.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000041
42The \module{datetime} module exports the following constants:
43
44\begin{datadesc}{MINYEAR}
Tim Peters00372022003-01-09 04:10:05 +000045 The smallest year number allowed in a \class{date} or
46 \class{datetime} object. \constant{MINYEAR}
Fred Drakebbdb2502002-12-23 18:58:06 +000047 is \code{1}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000048\end{datadesc}
49
50\begin{datadesc}{MAXYEAR}
Tim Peters00372022003-01-09 04:10:05 +000051 The largest year number allowed in a \class{date} or \class{datetime}
52 object. \constant{MAXYEAR} is \code{9999}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000053\end{datadesc}
54
Raymond Hettinger6005a342002-12-30 20:01:24 +000055\begin{seealso}
Raymond Hettinger62229582002-12-31 16:37:03 +000056 \seemodule{calendar}{General calendar related functions.}
Raymond Hettinger6005a342002-12-30 20:01:24 +000057 \seemodule{time}{Time access and conversions.}
58\end{seealso}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000059
60\subsection{Available Types}
61
Fred Drakebbdb2502002-12-23 18:58:06 +000062\begin{classdesc*}{date}
Fred Drakea37e5cc2002-12-30 21:26:42 +000063 An idealized naive date, assuming the current Gregorian calendar
Fred Drakebbdb2502002-12-23 18:58:06 +000064 always was, and always will be, in effect.
65 Attributes: \member{year}, \member{month}, and \member{day}.
66\end{classdesc*}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000067
Fred Drakebbdb2502002-12-23 18:58:06 +000068\begin{classdesc*}{time}
Tim Peters00372022003-01-09 04:10:05 +000069 An idealized time, independent of any particular day, assuming
Fred Drakebbdb2502002-12-23 18:58:06 +000070 that every day has exactly 24*60*60 seconds (there is no notion
71 of "leap seconds" here).
Tim Peters00372022003-01-09 04:10:05 +000072 Attributes: \member{hour}, \member{minute}, \member{second},
73 \member{microsecond}, and \member{tzinfo}.
Fred Drakebbdb2502002-12-23 18:58:06 +000074\end{classdesc*}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000075
Fred Drakebbdb2502002-12-23 18:58:06 +000076\begin{classdesc*}{datetime}
Andrew M. Kuchling9b445712003-01-09 13:46:30 +000077 A combination of a date and a time.
Fred Drakebbdb2502002-12-23 18:58:06 +000078 Attributes: \member{year}, \member{month}, \member{day},
79 \member{hour}, \member{minute}, \member{second},
Tim Peters00372022003-01-09 04:10:05 +000080 \member{microsecond}, and \member{tzinfo}.
Fred Drakebbdb2502002-12-23 18:58:06 +000081\end{classdesc*}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000082
Fred Drakebbdb2502002-12-23 18:58:06 +000083\begin{classdesc*}{timedelta}
Andrew M. Kuchling9b445712003-01-09 13:46:30 +000084 A duration expressing the difference between two \class{date},
85 \class{time}, or \class{datetime} instances to microsecond
Fred Drakebbdb2502002-12-23 18:58:06 +000086 resolution.
87\end{classdesc*}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000088
Fred Drakebbdb2502002-12-23 18:58:06 +000089\begin{classdesc*}{tzinfo}
90 An abstract base class for time zone information objects. These
Tim Peters00372022003-01-09 04:10:05 +000091 are used by the \class{datetime} and \class{time} classes to
Andrew M. Kuchling9b445712003-01-09 13:46:30 +000092 provide a customizable notion of time adjustment (for example, to
Tim Peters00372022003-01-09 04:10:05 +000093 account for time zone and/or daylight saving time).
Fred Drakebbdb2502002-12-23 18:58:06 +000094\end{classdesc*}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000095
96Objects of these types are immutable.
97
Tim Peters00372022003-01-09 04:10:05 +000098Objects of the \class{date} type are always naive.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000099
Tim Peters00372022003-01-09 04:10:05 +0000100An object \var{d} of type \class{time} or \class{datetime} may be
Fred Drakea37e5cc2002-12-30 21:26:42 +0000101naive or aware. \var{d} is aware if \code{\var{d}.tzinfo} is not
Andrew M. Kuchling9b445712003-01-09 13:46:30 +0000102\code{None} and \code{\var{d}.tzinfo.utcoffset(\var{d})} does not return
Fred Drakea37e5cc2002-12-30 21:26:42 +0000103\code{None}. If \code{\var{d}.tzinfo} is \code{None}, or if
104\code{\var{d}.tzinfo} is not \code{None} but
105\code{\var{d}.tzinfo.utcoffset(\var{d})} returns \code{None}, \var{d}
106is naive.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000107
Fred Drakea37e5cc2002-12-30 21:26:42 +0000108The distinction between naive and aware doesn't apply to
Fred Drakeb0e8f5d2003-12-30 20:36:20 +0000109\class{timedelta} objects.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000110
Fred Drakebbdb2502002-12-23 18:58:06 +0000111Subclass relationships:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000112
Fred Drakebbdb2502002-12-23 18:58:06 +0000113\begin{verbatim}
114object
115 timedelta
116 tzinfo
117 time
Fred Drakebbdb2502002-12-23 18:58:06 +0000118 date
119 datetime
Fred Drakebbdb2502002-12-23 18:58:06 +0000120\end{verbatim}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000121
Raymond Hettinger6005a342002-12-30 20:01:24 +0000122\subsection{\class{timedelta} Objects \label{datetime-timedelta}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000123
Fred Drakebbdb2502002-12-23 18:58:06 +0000124A \class{timedelta} object represents a duration, the difference
125between two dates or times.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000126
Fred Drakeb0e8f5d2003-12-30 20:36:20 +0000127\begin{classdesc}{timedelta}{\optional{days\optional{, seconds\optional{,
128 microseconds\optional{, milliseconds\optional{,
129 minutes\optional{, hours\optional{, weeks}}}}}}}}
130 All arguments are optional and default to \code{0}. Arguments may
131 be ints, longs, or floats, and may be positive or negative.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000132
Fred Drake436eadd2002-12-31 18:13:11 +0000133 Only \var{days}, \var{seconds} and \var{microseconds} are stored
134 internally. Arguments are converted to those units:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000135
Andrew M. Kuchling9b445712003-01-09 13:46:30 +0000136\begin{itemize}
137 \item A millisecond is converted to 1000 microseconds.
138 \item A minute is converted to 60 seconds.
139 \item An hour is converted to 3600 seconds.
140 \item A week is converted to 7 days.
141\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000142
Fred Drake436eadd2002-12-31 18:13:11 +0000143 and days, seconds and microseconds are then normalized so that the
144 representation is unique, with
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000145
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000146\begin{itemize}
147 \item \code{0 <= \var{microseconds} < 1000000}
148 \item \code{0 <= \var{seconds} < 3600*24} (the number of seconds in one day)
149 \item \code{-999999999 <= \var{days} <= 999999999}
150\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000151
Andrew M. Kuchling9b445712003-01-09 13:46:30 +0000152 If any argument is a float and there are fractional microseconds,
Fred Drake436eadd2002-12-31 18:13:11 +0000153 the fractional microseconds left over from all arguments are combined
154 and their sum is rounded to the nearest microsecond. If no
155 argument is a float, the conversion and normalization processes
156 are exact (no information is lost).
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000157
Fred Drake436eadd2002-12-31 18:13:11 +0000158 If the normalized value of days lies outside the indicated range,
159 \exception{OverflowError} is raised.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000160
Fred Drake436eadd2002-12-31 18:13:11 +0000161 Note that normalization of negative values may be surprising at first.
162 For example,
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000163
Fred Drakebbdb2502002-12-23 18:58:06 +0000164\begin{verbatim}
165>>> d = timedelta(microseconds=-1)
166>>> (d.days, d.seconds, d.microseconds)
167(-1, 86399, 999999)
168\end{verbatim}
Fred Drake0f8e5432002-12-31 18:31:48 +0000169\end{classdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000170
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000171Class attributes are:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000172
Fred Drake0f8e5432002-12-31 18:31:48 +0000173\begin{memberdesc}{min}
174 The most negative \class{timedelta} object,
175 \code{timedelta(-999999999)}.
176\end{memberdesc}
177
178\begin{memberdesc}{max}
179 The most positive \class{timedelta} object,
180 \code{timedelta(days=999999999, hours=23, minutes=59, seconds=59,
181 microseconds=999999)}.
182\end{memberdesc}
183
184\begin{memberdesc}{resolution}
185 The smallest possible difference between non-equal
186 \class{timedelta} objects, \code{timedelta(microseconds=1)}.
187\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000188
Fred Drakea37e5cc2002-12-30 21:26:42 +0000189Note that, because of normalization, \code{timedelta.max} \textgreater
190\code{-timedelta.min}. \code{-timedelta.max} is not representable as
191a \class{timedelta} object.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000192
193Instance attributes (read-only):
194
Raymond Hettinger6005a342002-12-30 20:01:24 +0000195\begin{tableii}{c|l}{code}{Attribute}{Value}
196 \lineii{days}{Between -999999999 and 999999999 inclusive}
197 \lineii{seconds}{Between 0 and 86399 inclusive}
198 \lineii{microseconds}{Between 0 and 999999 inclusive}
199\end{tableii}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000200
201Supported operations:
202
Andrew M. Kuchling9b445712003-01-09 13:46:30 +0000203% XXX this table is too wide!
Andrew M. Kuchling570e3582003-02-05 21:15:38 +0000204\begin{tableii}{c|l}{code}{Operation}{Result}
205 \lineii{\var{t1} = \var{t2} + \var{t3}}
Tim Peters95322982002-12-31 16:01:47 +0000206 {Sum of \var{t2} and \var{t3}.
Fred Drake9bdeee42002-12-30 20:35:32 +0000207 Afterwards \var{t1}-\var{t2} == \var{t3} and \var{t1}-\var{t3}
Andrew M. Kuchling570e3582003-02-05 21:15:38 +0000208 == \var{t2} are true.
209 (1)}
210 \lineii{\var{t1} = \var{t2} - \var{t3}}
211 {Difference of \var{t2} and \var{t3}.
Fred Drakeb0e8f5d2003-12-30 20:36:20 +0000212 Afterwards \var{t1} == \var{t2} - \var{t3} and
213 \var{t2} == \var{t1} + \var{t3} are true.
Andrew M. Kuchling570e3582003-02-05 21:15:38 +0000214 (1)}
215 \lineii{\var{t1} = \var{t2} * \var{i} or \var{t1} = \var{i} * \var{t2}}
Fred Drake9bdeee42002-12-30 20:35:32 +0000216 {Delta multiplied by an integer or long.
Fred Drake436eadd2002-12-31 18:13:11 +0000217 Afterwards \var{t1} // i == \var{t2} is true,
Andrew M. Kuchling570e3582003-02-05 21:15:38 +0000218 provided \code{i != 0}.}
219 \lineii{}{In general, \var{t1} * i == \var{t1} * (i-1) + \var{t1} is true.
220 (1)}
221 \lineii{\var{t1} = \var{t2} // \var{i}}
222 {The floor is computed and the remainder (if any) is thrown away.
223 (3)}
224 \lineii{+\var{t1}}
225 {Returns a \class{timedelta} object with the same value.
226 (2)}
227 \lineii{-\var{t1}}
Raymond Hettingercbd6cd22002-12-31 16:30:49 +0000228 {equivalent to \class{timedelta}(-\var{t1.days}, -\var{t1.seconds},
Andrew M. Kuchling570e3582003-02-05 21:15:38 +0000229 -\var{t1.microseconds}), and to \var{t1}* -1.
230 (1)(4)}
231 \lineii{abs(\var{t})}
Fred Drake436eadd2002-12-31 18:13:11 +0000232 {equivalent to +\var{t} when \code{t.days >= 0}, and to
Andrew M. Kuchling570e3582003-02-05 21:15:38 +0000233 -\var{t} when \code{t.days < 0}.
234 (2)}
235\end{tableii}
Raymond Hettinger6005a342002-12-30 20:01:24 +0000236\noindent
237Notes:
238
239\begin{description}
240\item[(1)]
Fred Drake436eadd2002-12-31 18:13:11 +0000241 This is exact, but may overflow.
Raymond Hettinger6005a342002-12-30 20:01:24 +0000242
243\item[(2)]
Tim Peters397301e2003-01-02 21:28:08 +0000244 This is exact, and cannot overflow.
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000245
246\item[(3)]
Tim Peters397301e2003-01-02 21:28:08 +0000247 Division by 0 raises \exception{ZeroDivisionError}.
248
249\item[(4)]
Fred Drake436eadd2002-12-31 18:13:11 +0000250 -\var{timedelta.max} is not representable as a \class{timedelta} object.
Raymond Hettinger6005a342002-12-30 20:01:24 +0000251\end{description}
252
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000253In addition to the operations listed above \class{timedelta} objects
Tim Peters00372022003-01-09 04:10:05 +0000254support certain additions and subtractions with \class{date} and
255\class{datetime} objects (see below).
Raymond Hettinger6005a342002-12-30 20:01:24 +0000256
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000257Comparisons of \class{timedelta} objects are supported with the
258\class{timedelta} object representing the smaller duration considered
259to be the smaller timedelta.
Tim Peters07534a62003-02-07 22:50:28 +0000260In order to stop mixed-type comparisons from falling back to the
261default comparison by object address, when a \class{timedelta} object is
262compared to an object of a different type, \exception{TypeError} is
263raised unless the comparison is \code{==} or \code{!=}. The latter
264cases return \constant{False} or \constant{True}, respectively.
Raymond Hettinger6005a342002-12-30 20:01:24 +0000265
Andrew M. Kuchling9b445712003-01-09 13:46:30 +0000266\class{timedelta} objects are hashable (usable as dictionary keys),
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000267support efficient pickling, and in Boolean contexts, a \class{timedelta}
268object is considered to be true if and only if it isn't equal to
269\code{timedelta(0)}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000270
271
Raymond Hettinger6005a342002-12-30 20:01:24 +0000272\subsection{\class{date} Objects \label{datetime-date}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000273
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000274A \class{date} object represents a date (year, month and day) in an idealized
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000275calendar, the current Gregorian calendar indefinitely extended in both
276directions. January 1 of year 1 is called day number 1, January 2 of year
2771 is called day number 2, and so on. This matches the definition of the
278"proleptic Gregorian" calendar in Dershowitz and Reingold's book
Fred Drakea37e5cc2002-12-30 21:26:42 +0000279\citetitle{Calendrical Calculations}, where it's the base calendar for all
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000280computations. See the book for algorithms for converting between
281proleptic Gregorian ordinals and many other calendar systems.
282
Fred Drake0f8e5432002-12-31 18:31:48 +0000283\begin{classdesc}{date}{year, month, day}
Fred Drake436eadd2002-12-31 18:13:11 +0000284 All arguments are required. Arguments may be ints or longs, in the
285 following ranges:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000286
Fred Drake436eadd2002-12-31 18:13:11 +0000287 \begin{itemize}
288 \item \code{MINYEAR <= \var{year} <= MAXYEAR}
289 \item \code{1 <= \var{month} <= 12}
290 \item \code{1 <= \var{day} <= number of days in the given month and year}
291 \end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000292
Fred Drake436eadd2002-12-31 18:13:11 +0000293 If an argument outside those ranges is given, \exception{ValueError}
294 is raised.
Fred Drake0f8e5432002-12-31 18:31:48 +0000295\end{classdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000296
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000297Other constructors, all class methods:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000298
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000299\begin{methoddesc}{today}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000300 Return the current local date. This is equivalent to
301 \code{date.fromtimestamp(time.time())}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000302\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000303
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000304\begin{methoddesc}{fromtimestamp}{timestamp}
Fred Drake436eadd2002-12-31 18:13:11 +0000305 Return the local date corresponding to the POSIX timestamp, such
306 as is returned by \function{time.time()}. This may raise
307 \exception{ValueError}, if the timestamp is out of the range of
308 values supported by the platform C \cfunction{localtime()}
309 function. It's common for this to be restricted to years from 1970
Tim Peters75a6e3b2003-01-04 18:17:36 +0000310 through 2038. Note that on non-POSIX systems that include leap
311 seconds in their notion of a timestamp, leap seconds are ignored by
312 \method{fromtimestamp()}.
Andrew M. Kuchling0f0e6b92003-01-09 12:51:50 +0000313\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000314
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000315\begin{methoddesc}{fromordinal}{ordinal}
Fred Drake436eadd2002-12-31 18:13:11 +0000316 Return the date corresponding to the proleptic Gregorian ordinal,
317 where January 1 of year 1 has ordinal 1. \exception{ValueError} is
318 raised unless \code{1 <= \var{ordinal} <= date.max.toordinal()}.
319 For any date \var{d}, \code{date.fromordinal(\var{d}.toordinal()) ==
320 \var{d}}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000321\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000322
323Class attributes:
324
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000325\begin{memberdesc}{min}
Fred Drake436eadd2002-12-31 18:13:11 +0000326 The earliest representable date, \code{date(MINYEAR, 1, 1)}.
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}{max}
Fred Drake436eadd2002-12-31 18:13:11 +0000330 The latest representable date, \code{date(MAXYEAR, 12, 31)}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000331\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000332
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000333\begin{memberdesc}{resolution}
Fred Drake436eadd2002-12-31 18:13:11 +0000334 The smallest possible difference between non-equal date
335 objects, \code{timedelta(days=1)}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000336\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000337
338Instance attributes (read-only):
339
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000340\begin{memberdesc}{year}
Andrew M. Kuchling9b445712003-01-09 13:46:30 +0000341 Between \constant{MINYEAR} and \constant{MAXYEAR} inclusive.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000342\end{memberdesc}
343
344\begin{memberdesc}{month}
Fred Drake436eadd2002-12-31 18:13:11 +0000345 Between 1 and 12 inclusive.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000346\end{memberdesc}
347
348\begin{memberdesc}{day}
Fred Drake436eadd2002-12-31 18:13:11 +0000349 Between 1 and the number of days in the given month of the given
350 year.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000351\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000352
353Supported operations:
354
Andrew M. Kuchling570e3582003-02-05 21:15:38 +0000355\begin{tableii}{c|l}{code}{Operation}{Result}
356 \lineii{\var{date2} = \var{date1} + \var{timedelta}}
357 {\var{date2} is \code{\var{timedelta}.days} days removed from
358 \var{date1}. (1)}
Tim Peters00372022003-01-09 04:10:05 +0000359
Tim Peters00372022003-01-09 04:10:05 +0000360
Andrew M. Kuchling570e3582003-02-05 21:15:38 +0000361 \lineii{\var{date2} = \var{date1} - \var{timedelta}}
362 {Computes \var{date2} such that \code{\var{date2} + \var{timedelta}
363 == \var{date1}}. (2)}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000364
Andrew M. Kuchling570e3582003-02-05 21:15:38 +0000365 \lineii{\var{timedelta} = \var{date1} - \var{date2}}
366 {(3)}
Tim Peters00372022003-01-09 04:10:05 +0000367
Fred Drakeb0e8f5d2003-12-30 20:36:20 +0000368 \lineii{\var{date1} < \var{date2}}
Andrew M. Kuchling570e3582003-02-05 21:15:38 +0000369 {\var{date1} is considered less than \var{date2} when \var{date1}
370 precedes \var{date2} in time. (4)}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000371
Andrew M. Kuchling570e3582003-02-05 21:15:38 +0000372\end{tableii}
Tim Peters00372022003-01-09 04:10:05 +0000373
Andrew M. Kuchling570e3582003-02-05 21:15:38 +0000374Notes:
375\begin{description}
376
377\item[(1)]
378 \var{date2} is moved forward in time if \code{\var{timedelta}.days
379 > 0}, or backward if \code{\var{timedelta}.days < 0}. Afterward
380 \code{\var{date2} - \var{date1} == \var{timedelta}.days}.
381 \code{\var{timedelta}.seconds} and
382 \code{\var{timedelta}.microseconds} are ignored.
383 \exception{OverflowError} is raised if \code{\var{date2}.year}
384 would be smaller than \constant{MINYEAR} or larger than
385 \constant{MAXYEAR}.
386
387\item[(2)]
388 This isn't quite equivalent to date1 +
389 (-timedelta), because -timedelta in isolation can overflow in cases
390 where date1 - timedelta does not. \code{\var{timedelta}.seconds}
391 and \code{\var{timedelta}.microseconds} are ignored.
392
393\item[(3)]
394This is exact, and cannot overflow. timedelta.seconds and
Fred Drakebbdb2502002-12-23 18:58:06 +0000395 timedelta.microseconds are 0, and date2 + timedelta == date1
396 after.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000397
Andrew M. Kuchling570e3582003-02-05 21:15:38 +0000398\item[(4)]
399In other words, \code{date1 < date2}
400 if and only if \code{\var{date1}.toordinal() <
401 \var{date2}.toordinal()}.
402In order to stop comparison from falling back to the default
403scheme of comparing object addresses, date comparison
404normally raises \exception{TypeError} if the other comparand
405isn't also a \class{date} object. However, \code{NotImplemented}
406is returned instead if the other comparand has a
407\method{timetuple} attribute. This hook gives other kinds of
408date objects a chance at implementing mixed-type comparison.
Tim Peters07534a62003-02-07 22:50:28 +0000409If not, when a \class{date} object is
410compared to an object of a different type, \exception{TypeError} is
411raised unless the comparison is \code{==} or \code{!=}. The latter
412cases return \constant{False} or \constant{True}, respectively.
Andrew M. Kuchling570e3582003-02-05 21:15:38 +0000413
414\end{description}
Tim Peters8d81a012003-01-24 22:36:34 +0000415
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000416
Andrew M. Kuchling570e3582003-02-05 21:15:38 +0000417Dates can be used as dictionary keys. In Boolean contexts, all
418\class{date} objects are considered to be true.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000419
420Instance methods:
421
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000422\begin{methoddesc}{replace}{year, month, day}
Tim Peters00372022003-01-09 04:10:05 +0000423 Return a date with the same value, except for those members given
Fred Drake436eadd2002-12-31 18:13:11 +0000424 new values by whichever keyword arguments are specified. For
425 example, if \code{d == date(2002, 12, 31)}, then
426 \code{d.replace(day=26) == date(2000, 12, 26)}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000427\end{methoddesc}
Tim Peters12bf3392002-12-24 05:41:27 +0000428
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000429\begin{methoddesc}{timetuple}{}
Martin v. Löwisef365372003-09-04 18:29:53 +0000430 Return a \class{time.struct_time} such as returned by
Fred Drake436eadd2002-12-31 18:13:11 +0000431 \function{time.localtime()}. The hours, minutes and seconds are
432 0, and the DST flag is -1.
433 \code{\var{d}.timetuple()} is equivalent to
Martin v. Löwisef365372003-09-04 18:29:53 +0000434 \code{time.struct_time((\var{d}.year, \var{d}.month, \var{d}.day,
Andrew M. Kuchling570e3582003-02-05 21:15:38 +0000435 0, 0, 0,
436 \var{d}.weekday(),
Fred Drake436eadd2002-12-31 18:13:11 +0000437 \var{d}.toordinal() - date(\var{d}.year, 1, 1).toordinal() + 1,
Martin v. Löwisef365372003-09-04 18:29:53 +0000438 -1))}
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000439\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000440
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000441\begin{methoddesc}{toordinal}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000442 Return the proleptic Gregorian ordinal of the date, where January 1
443 of year 1 has ordinal 1. For any \class{date} object \var{d},
444 \code{date.fromordinal(\var{d}.toordinal()) == \var{d}}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000445\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000446
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000447\begin{methoddesc}{weekday}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000448 Return the day of the week as an integer, where Monday is 0 and
Andrew M. Kuchling570e3582003-02-05 21:15:38 +0000449 Sunday is 6. For example, \code{date(2002, 12, 4).weekday() == 2}, a
Fred Drake436eadd2002-12-31 18:13:11 +0000450 Wednesday.
451 See also \method{isoweekday()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000452\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000453
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000454\begin{methoddesc}{isoweekday}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000455 Return the day of the week as an integer, where Monday is 1 and
Andrew M. Kuchling570e3582003-02-05 21:15:38 +0000456 Sunday is 7. For example, \code{date(2002, 12, 4).isoweekday() == 3}, a
Fred Drake436eadd2002-12-31 18:13:11 +0000457 Wednesday.
458 See also \method{weekday()}, \method{isocalendar()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000459\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000460
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000461\begin{methoddesc}{isocalendar}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000462 Return a 3-tuple, (ISO year, ISO week number, ISO weekday).
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000463
Fred Drake436eadd2002-12-31 18:13:11 +0000464 The ISO calendar is a widely used variant of the Gregorian calendar.
465 See \url{http://www.phys.uu.nl/~vgent/calendar/isocalendar.htm}
466 for a good explanation.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000467
Fred Drake436eadd2002-12-31 18:13:11 +0000468 The ISO year consists of 52 or 53 full weeks, and where a week starts
469 on a Monday and ends on a Sunday. The first week of an ISO year is
470 the first (Gregorian) calendar week of a year containing a Thursday.
471 This is called week number 1, and the ISO year of that Thursday is
472 the same as its Gregorian year.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000473
Fred Drake436eadd2002-12-31 18:13:11 +0000474 For example, 2004 begins on a Thursday, so the first week of ISO
475 year 2004 begins on Monday, 29 Dec 2003 and ends on Sunday, 4 Jan
476 2004, so that
Andrew M. Kuchling570e3582003-02-05 21:15:38 +0000477 \code{date(2003, 12, 29).isocalendar() == (2004, 1, 1)}
478 and
479 \code{date(2004, 1, 4).isocalendar() == (2004, 1, 7)}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000480\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000481
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000482\begin{methoddesc}{isoformat}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000483 Return a string representing the date in ISO 8601 format,
484 'YYYY-MM-DD'. For example,
Andrew M. Kuchling570e3582003-02-05 21:15:38 +0000485 \code{date(2002, 12, 4).isoformat() == '2002-12-04'}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000486\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000487
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000488\begin{methoddesc}{__str__}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000489 For a date \var{d}, \code{str(\var{d})} is equivalent to
490 \code{\var{d}.isoformat()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000491\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000492
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000493\begin{methoddesc}{ctime}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000494 Return a string representing the date, for example
495 date(2002, 12, 4).ctime() == 'Wed Dec 4 00:00:00 2002'.
496 \code{\var{d}.ctime()} is equivalent to
497 \code{time.ctime(time.mktime(\var{d}.timetuple()))}
498 on platforms where the native C \cfunction{ctime()} function
499 (which \function{time.ctime()} invokes, but which
500 \method{date.ctime()} does not invoke) conforms to the C standard.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000501\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000502
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000503\begin{methoddesc}{strftime}{format}
Fred Drake436eadd2002-12-31 18:13:11 +0000504 Return a string representing the date, controlled by an explicit
505 format string. Format codes referring to hours, minutes or seconds
506 will see 0 values.
507 See the section on \method{strftime()} behavior.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000508\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000509
510
Raymond Hettinger6005a342002-12-30 20:01:24 +0000511\subsection{\class{datetime} Objects \label{datetime-datetime}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000512
Fred Drakebbdb2502002-12-23 18:58:06 +0000513A \class{datetime} object is a single object containing all the
Tim Peters00372022003-01-09 04:10:05 +0000514information from a \class{date} object and a \class{time} object. Like a
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000515\class{date} object, \class{datetime} assumes the current Gregorian
516calendar extended in both directions; like a time object,
517\class{datetime} assumes there are exactly 3600*24 seconds in every
518day.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000519
Tim Peters00372022003-01-09 04:10:05 +0000520Constructor:
521
Fred Drakeb0e8f5d2003-12-30 20:36:20 +0000522\begin{classdesc}{datetime}{year, month, day\optional{,
523 hour\optional{, minute\optional{,
524 second\optional{, microsecond\optional{,
525 tzinfo}}}}}}
Tim Peters00372022003-01-09 04:10:05 +0000526 The year, month and day arguments are required. \var{tzinfo} may
527 be \code{None}, or an instance of a \class{tzinfo} subclass. The
528 remaining arguments may be ints or longs, in the following ranges:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000529
Fred Drake436eadd2002-12-31 18:13:11 +0000530 \begin{itemize}
Tim Peters00372022003-01-09 04:10:05 +0000531 \item \code{MINYEAR <= \var{year} <= MAXYEAR}
Fred Drake436eadd2002-12-31 18:13:11 +0000532 \item \code{1 <= \var{month} <= 12}
533 \item \code{1 <= \var{day} <= number of days in the given month and year}
534 \item \code{0 <= \var{hour} < 24}
535 \item \code{0 <= \var{minute} < 60}
536 \item \code{0 <= \var{second} < 60}
537 \item \code{0 <= \var{microsecond} < 1000000}
538 \end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000539
Tim Peters00372022003-01-09 04:10:05 +0000540 If an argument outside those ranges is given,
541 \exception{ValueError} is raised.
Fred Drake0f8e5432002-12-31 18:31:48 +0000542\end{classdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000543
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000544Other constructors, all class methods:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000545
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000546\begin{methoddesc}{today}{}
Tim Peters00372022003-01-09 04:10:05 +0000547 Return the current local datetime, with \member{tzinfo} \code{None}.
548 This is equivalent to
Fred Drake436eadd2002-12-31 18:13:11 +0000549 \code{datetime.fromtimestamp(time.time())}.
550 See also \method{now()}, \method{fromtimestamp()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000551\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000552
Fred Drakeb0e8f5d2003-12-30 20:36:20 +0000553\begin{methoddesc}{now}{\optional{tz}}
Tim Peters10cadce2003-01-23 19:58:02 +0000554 Return the current local date and time. If optional argument
555 \var{tz} is \code{None} or not specified, this is like
556 \method{today()}, but, if possible, supplies more precision than can
557 be gotten from going through a \function{time.time()} timestamp (for
558 example, this may be possible on platforms supplying the C
Fred Drake436eadd2002-12-31 18:13:11 +0000559 \cfunction{gettimeofday()} function).
Tim Peters10cadce2003-01-23 19:58:02 +0000560
561 Else \var{tz} must be an instance of a class \class{tzinfo} subclass,
Tim Peters2a44a8d2003-01-23 20:53:10 +0000562 and the current date and time are converted to \var{tz}'s time
Tim Peters10cadce2003-01-23 19:58:02 +0000563 zone. In this case the result is equivalent to
Tim Peters2a44a8d2003-01-23 20:53:10 +0000564 \code{\var{tz}.fromutc(datetime.utcnow().replace(tzinfo=\var{tz}))}.
Fred Drake436eadd2002-12-31 18:13:11 +0000565 See also \method{today()}, \method{utcnow()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000566\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000567
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000568\begin{methoddesc}{utcnow}{}
Tim Peters10cadce2003-01-23 19:58:02 +0000569 Return the current UTC date and time, with \member{tzinfo} \code{None}.
Tim Peters2a44a8d2003-01-23 20:53:10 +0000570 This is like \method{now()}, but returns the current UTC date and time,
Tim Peters10cadce2003-01-23 19:58:02 +0000571 as a naive \class{datetime} object.
Fred Drake436eadd2002-12-31 18:13:11 +0000572 See also \method{now()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000573\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000574
Fred Drakeb0e8f5d2003-12-30 20:36:20 +0000575\begin{methoddesc}{fromtimestamp}{timestamp\optional{, tz}}
Tim Peters2a44a8d2003-01-23 20:53:10 +0000576 Return the local date and time corresponding to the \POSIX{}
577 timestamp, such as is returned by \function{time.time()}.
578 If optional argument \var{tz} is \code{None} or not specified, the
579 timestamp is converted to the platform's local date and time, and
580 the returned \class{datetime} object is naive.
581
582 Else \var{tz} must be an instance of a class \class{tzinfo} subclass,
583 and the timestamp is converted to \var{tz}'s time zone. In this case
584 the result is equivalent to
585 \code{\var{tz}.fromutc(datetime.utcfromtimestamp(\var{timestamp}).replace(tzinfo=\var{tz}))}.
586
587 \method{fromtimestamp()} may raise \exception{ValueError}, if the
588 timestamp is out of the range of values supported by the platform C
Neal Norwitzdaae3272003-01-25 21:08:30 +0000589 \cfunction{localtime()} or \cfunction{gmtime()} functions. It's common
Tim Peters2a44a8d2003-01-23 20:53:10 +0000590 for this to be restricted to years in 1970 through 2038.
Tim Peters75a6e3b2003-01-04 18:17:36 +0000591 Note that on non-POSIX systems that include leap seconds in their
592 notion of a timestamp, leap seconds are ignored by
593 \method{fromtimestamp()}, and then it's possible to have two timestamps
594 differing by a second that yield identical \class{datetime} objects.
Fred Drake436eadd2002-12-31 18:13:11 +0000595 See also \method{utcfromtimestamp()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000596\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000597
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000598\begin{methoddesc}{utcfromtimestamp}{timestamp}
Fred Drake436eadd2002-12-31 18:13:11 +0000599 Return the UTC \class{datetime} corresponding to the \POSIX{}
Tim Peters00372022003-01-09 04:10:05 +0000600 timestamp, with \member{tzinfo} \code{None}.
601 This may raise \exception{ValueError}, if the
Fred Drake436eadd2002-12-31 18:13:11 +0000602 timestamp is out of the range of values supported by the platform
603 C \cfunction{gmtime()} function. It's common for this to be
604 restricted to years in 1970 through 2038.
605 See also \method{fromtimestamp()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000606\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000607
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000608\begin{methoddesc}{fromordinal}{ordinal}
Fred Drake436eadd2002-12-31 18:13:11 +0000609 Return the \class{datetime} corresponding to the proleptic
610 Gregorian ordinal, where January 1 of year 1 has ordinal 1.
Andrew M. Kuchling570e3582003-02-05 21:15:38 +0000611 \exception{ValueError} is raised unless \code{1 <= ordinal <=
612 datetime.max.toordinal()}. The hour, minute, second and
Tim Peters00372022003-01-09 04:10:05 +0000613 microsecond of the result are all 0,
614 and \member{tzinfo} is \code{None}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000615\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000616
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000617\begin{methoddesc}{combine}{date, time}
Tim Peters00372022003-01-09 04:10:05 +0000618 Return a new \class{datetime} object whose date members are
Fred Drake436eadd2002-12-31 18:13:11 +0000619 equal to the given \class{date} object's, and whose time
Tim Peters00372022003-01-09 04:10:05 +0000620 and \member{tzinfo} members are equal to the given \class{time} object's.
621 For any \class{datetime} object \var{d}, \code{\var{d} ==
622 datetime.combine(\var{d}.date(), \var{d}.timetz())}. If date is a
623 \class{datetime} object, its time and \member{tzinfo} members are
624 ignored.
625 \end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000626
627Class attributes:
628
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000629\begin{memberdesc}{min}
Fred Drake436eadd2002-12-31 18:13:11 +0000630 The earliest representable \class{datetime},
Tim Peters00372022003-01-09 04:10:05 +0000631 \code{datetime(MINYEAR, 1, 1, tzinfo=None)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000632\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000633
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000634\begin{memberdesc}{max}
Fred Drake436eadd2002-12-31 18:13:11 +0000635 The latest representable \class{datetime},
Tim Peters00372022003-01-09 04:10:05 +0000636 \code{datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999, tzinfo=None)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000637\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000638
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000639\begin{memberdesc}{resolution}
Fred Drake436eadd2002-12-31 18:13:11 +0000640 The smallest possible difference between non-equal \class{datetime}
641 objects, \code{timedelta(microseconds=1)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000642\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000643
644Instance attributes (read-only):
645
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000646\begin{memberdesc}{year}
Tim Peters00372022003-01-09 04:10:05 +0000647 Between \constant{MINYEAR} and \constant{MAXYEAR} inclusive.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000648\end{memberdesc}
649
650\begin{memberdesc}{month}
Tim Peters00372022003-01-09 04:10:05 +0000651 Between 1 and 12 inclusive.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000652\end{memberdesc}
653
654\begin{memberdesc}{day}
Fred Drake436eadd2002-12-31 18:13:11 +0000655 Between 1 and the number of days in the given month of the given
656 year.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000657\end{memberdesc}
658
659\begin{memberdesc}{hour}
Fred Drake436eadd2002-12-31 18:13:11 +0000660 In \code{range(24)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000661\end{memberdesc}
662
663\begin{memberdesc}{minute}
Fred Drake436eadd2002-12-31 18:13:11 +0000664 In \code{range(60)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000665\end{memberdesc}
666
667\begin{memberdesc}{second}
Fred Drake436eadd2002-12-31 18:13:11 +0000668 In \code{range(60)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000669\end{memberdesc}
670
671\begin{memberdesc}{microsecond}
Fred Drake436eadd2002-12-31 18:13:11 +0000672 In \code{range(1000000)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000673\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000674
Tim Peters00372022003-01-09 04:10:05 +0000675\begin{memberdesc}{tzinfo}
676 The object passed as the \var{tzinfo} argument to the
677 \class{datetime} constructor, or \code{None} if none was passed.
678\end{memberdesc}
679
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000680Supported operations:
681
Andrew M. Kuchling570e3582003-02-05 21:15:38 +0000682\begin{tableii}{c|l}{code}{Operation}{Result}
683 \lineii{\var{datetime2} = \var{datetime1} + \var{timedelta}}{(1)}
Tim Peters00372022003-01-09 04:10:05 +0000684
Andrew M. Kuchling570e3582003-02-05 21:15:38 +0000685 \lineii{\var{datetime2} = \var{datetime1} - \var{timedelta}}{(2)}
686
687 \lineii{\var{timedelta} = \var{datetime1} - \var{datetime2}}{(3)}
688
689 \lineii{\var{datetime1} < \var{datetime2}}
690 {Compares \class{datetime} to \class{datetime}.
691 (4)}
692
693\end{tableii}
694
695\begin{description}
696
697\item[(1)]
Tim Peters00372022003-01-09 04:10:05 +0000698
Fred Drakebbdb2502002-12-23 18:58:06 +0000699 datetime2 is a duration of timedelta removed from datetime1, moving
Andrew M. Kuchling570e3582003-02-05 21:15:38 +0000700 forward in time if \code{\var{timedelta}.days} > 0, or backward if
701 \code{\var{timedelta}.days} < 0. The result has the same \member{tzinfo} member
Tim Peters00372022003-01-09 04:10:05 +0000702 as the input datetime, and datetime2 - datetime1 == timedelta after.
Fred Drakebbdb2502002-12-23 18:58:06 +0000703 \exception{OverflowError} is raised if datetime2.year would be
704 smaller than \constant{MINYEAR} or larger than \constant{MAXYEAR}.
Tim Peters00372022003-01-09 04:10:05 +0000705 Note that no time zone adjustments are done even if the input is an
706 aware object.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000707
Andrew M. Kuchling570e3582003-02-05 21:15:38 +0000708\item[(2)]
Fred Drakebbdb2502002-12-23 18:58:06 +0000709 Computes the datetime2 such that datetime2 + timedelta == datetime1.
Tim Peters00372022003-01-09 04:10:05 +0000710 As for addition, the result has the same \member{tzinfo} member
711 as the input datetime, and no time zone adjustments are done even
712 if the input is aware.
Fred Drakebbdb2502002-12-23 18:58:06 +0000713 This isn't quite equivalent to datetime1 + (-timedelta), because
714 -timedelta in isolation can overflow in cases where
715 datetime1 - timedelta does not.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000716
Andrew M. Kuchling570e3582003-02-05 21:15:38 +0000717\item[(3)]
Tim Peters00372022003-01-09 04:10:05 +0000718 Subtraction of a \class{datetime} from a
719 \class{datetime} is defined only if both
720 operands are naive, or if both are aware. If one is aware and the
721 other is naive, \exception{TypeError} is raised.
722
723 If both are naive, or both are aware and have the same \member{tzinfo}
724 member, the \member{tzinfo} members are ignored, and the result is
725 a \class{timedelta} object \var{t} such that
726 \code{\var{datetime2} + \var{t} == \var{datetime1}}. No time zone
727 adjustments are done in this case.
728
729 If both are aware and have different \member{tzinfo} members,
730 \code{a-b} acts as if \var{a} and \var{b} were first converted to
731 naive UTC datetimes first. The result is
732 \code{(\var{a}.replace(tzinfo=None) - \var{a}.utcoffset()) -
733 (\var{b}.replace(tzinfo=None) - \var{b}.utcoffset())}
734 except that the implementation never overflows.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000735
Andrew M. Kuchling570e3582003-02-05 21:15:38 +0000736\item[(4)]
737
738\var{datetime1} is considered less than \var{datetime2}
739when \var{datetime1} precedes \var{datetime2} in time.
740
741If one comparand is naive and
742the other is aware, \exception{TypeError} is raised. If both
Tim Peters00372022003-01-09 04:10:05 +0000743 comparands are aware, and have the same \member{tzinfo} member,
744 the common \member{tzinfo} member is ignored and the base datetimes
745 are compared. If both comparands are aware and have different
746 \member{tzinfo} members, the comparands are first adjusted by
747 subtracting their UTC offsets (obtained from \code{self.utcoffset()}).
Tim Peters8d81a012003-01-24 22:36:34 +0000748 \note{In order to stop comparison from falling back to the default
749 scheme of comparing object addresses, datetime comparison
750 normally raises \exception{TypeError} if the other comparand
751 isn't also a \class{datetime} object. However,
752 \code{NotImplemented} is returned instead if the other comparand
753 has a \method{timetuple} attribute. This hook gives other
754 kinds of date objects a chance at implementing mixed-type
Tim Peters07534a62003-02-07 22:50:28 +0000755 comparison. If not, when a \class{datetime} object is
756 compared to an object of a different type, \exception{TypeError}
757 is raised unless the comparison is \code{==} or \code{!=}. The
758 latter cases return \constant{False} or \constant{True},
759 respectively.}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000760
Andrew M. Kuchling570e3582003-02-05 21:15:38 +0000761\end{description}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000762
Andrew M. Kuchling570e3582003-02-05 21:15:38 +0000763\class{datetime} objects can be used as dictionary keys. In Boolean
764contexts, all \class{datetime} objects are considered to be true.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000765
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000766
767Instance methods:
768
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000769\begin{methoddesc}{date}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000770 Return \class{date} object with same year, month and day.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000771\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000772
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000773\begin{methoddesc}{time}{}
Tim Peters00372022003-01-09 04:10:05 +0000774 Return \class{time} object with same hour, minute, second and microsecond.
775 \member{tzinfo} is \code{None}. See also method \method{timetz()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000776\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000777
Tim Peters00372022003-01-09 04:10:05 +0000778\begin{methoddesc}{timetz}{}
779 Return \class{time} object with same hour, minute, second, microsecond,
780 and tzinfo members. See also method \method{time()}.
781\end{methoddesc}
782
Fred Drakeb0e8f5d2003-12-30 20:36:20 +0000783\begin{methoddesc}{replace}{\optional{year\optional{, month\optional{,
784 day\optional{, hour\optional{, minute\optional{,
785 second\optional{, microsecond\optional{,
786 tzinfo}}}}}}}}}
Tim Peters00372022003-01-09 04:10:05 +0000787 Return a datetime with the same members, except for those members given
788 new values by whichever keyword arguments are specified. Note that
789 \code{tzinfo=None} can be specified to create a naive datetime from
790 an aware datetime with no conversion of date and time members.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000791\end{methoddesc}
Tim Peters12bf3392002-12-24 05:41:27 +0000792
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000793\begin{methoddesc}{astimezone}{tz}
Tim Peters00372022003-01-09 04:10:05 +0000794 Return a \class{datetime} object with new \member{tzinfo} member
Tim Petersf196a0a2003-01-22 04:45:50 +0000795 \var{tz}, adjusting the date and time members so the result is the
796 same UTC time as \var{self}, but in \var{tz}'s local time.
797
798 \var{tz} must be an instance of a \class{tzinfo} subclass, and its
799 \method{utcoffset()} and \method{dst()} methods must not return
800 \code{None}. \var{self} must be aware (\code{\var{self}.tzinfo} must
801 not be \code{None}, and \code{\var{self}.utcoffset()} must not return
802 \code{None}).
803
Andrew M. Kuchling570e3582003-02-05 21:15:38 +0000804 If \code{\var{self}.tzinfo} is \var{tz},
Tim Petersf196a0a2003-01-22 04:45:50 +0000805 \code{\var{self}.astimezone(\var{tz})} is equal to \var{self}: no
806 adjustment of date or time members is performed.
807 Else the result is local time in time zone \var{tz}, representing the
808 same UTC time as \var{self}: after \code{\var{astz} =
809 \var{dt}.astimezone(\var{tz})},
810 \code{\var{astz} - \var{astz}.utcoffset()} will usually have the same
811 date and time members as \code{\var{dt} - \var{dt}.utcoffset()}.
812 The discussion of class \class{tzinfo} explains the cases at Daylight
813 Saving Time transition boundaries where this cannot be achieved (an issue
814 only if \var{tz} models both standard and daylight time).
815
816 If you merely want to attach a time zone object \var{tz} to a
817 datetime \var{dt} without adjustment of date and time members,
818 use \code{\var{dt}.replace(tzinfo=\var{tz})}. If
819 you merely want to remove the time zone object from an aware datetime
820 \var{dt} without conversion of date and time members, use
821 \code{\var{dt}.replace(tzinfo=None)}.
822
823 Note that the default \method{tzinfo.fromutc()} method can be overridden
Raymond Hettinger1c90a672003-09-06 05:36:13 +0000824 in a \class{tzinfo} subclass to affect the result returned by
Tim Petersf196a0a2003-01-22 04:45:50 +0000825 \method{astimezone()}. Ignoring error cases, \method{astimezone()}
826 acts like:
827
828 \begin{verbatim}
829 def astimezone(self, tz):
830 if self.tzinfo is tz:
831 return self
832 # Convert self to UTC, and attach the new time zone object.
833 utc = (self - self.utcoffset()).replace(tzinfo=tz)
834 # Convert from UTC to tz's local time.
835 return tz.fromutc(utc)
836 \end{verbatim}
Tim Peters00372022003-01-09 04:10:05 +0000837\end{methoddesc}
838
839\begin{methoddesc}{utcoffset}{}
840 If \member{tzinfo} is \code{None}, returns \code{None}, else
Tim Petersf196a0a2003-01-22 04:45:50 +0000841 returns \code{\var{self}.tzinfo.utcoffset(\var{self})}, and
842 raises an exception if the latter doesn't return \code{None}, or
843 a \class{timedelta} object representing a whole number of minutes
844 with magnitude less than one day.
845\end{methoddesc}
846
847\begin{methoddesc}{dst}{}
848 If \member{tzinfo} is \code{None}, returns \code{None}, else
849 returns \code{\var{self}.tzinfo.dst(\var{self})}, and
850 raises an exception if the latter doesn't return \code{None}, or
851 a \class{timedelta} object representing a whole number of minutes
852 with magnitude less than one day.
Tim Peters00372022003-01-09 04:10:05 +0000853\end{methoddesc}
854
855\begin{methoddesc}{tzname}{}
856 If \member{tzinfo} is \code{None}, returns \code{None}, else
Tim Petersf196a0a2003-01-22 04:45:50 +0000857 returns \code{\var{self}.tzinfo.tzname(\var{self})},
858 raises an exception if the latter doesn't return \code{None} or
859 a string object,
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000860\end{methoddesc}
Tim Peters80475bb2002-12-25 07:40:55 +0000861
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000862\begin{methoddesc}{timetuple}{}
Martin v. Löwisef365372003-09-04 18:29:53 +0000863 Return a \class{time.struct_time} such as returned by
Fred Drake436eadd2002-12-31 18:13:11 +0000864 \function{time.localtime()}.
Tim Peters00372022003-01-09 04:10:05 +0000865 \code{\var{d}.timetuple()} is equivalent to
Martin v. Löwisef365372003-09-04 18:29:53 +0000866 \code{time.struct_time((\var{d}.year, \var{d}.month, \var{d}.day,
Fred Drake436eadd2002-12-31 18:13:11 +0000867 \var{d}.hour, \var{d}.minute, \var{d}.second,
Tim Peters00372022003-01-09 04:10:05 +0000868 \var{d}.weekday(),
Fred Drake436eadd2002-12-31 18:13:11 +0000869 \var{d}.toordinal() - date(\var{d}.year, 1, 1).toordinal() + 1,
Martin v. Löwisef365372003-09-04 18:29:53 +0000870 dst))}
Tim Peters00372022003-01-09 04:10:05 +0000871 The \member{tm_isdst} flag of the result is set according to
872 the \method{dst()} method: \member{tzinfo} is \code{None} or
873 \method{dst()} returns \code{None},
874 \member{tm_isdst} is set to \code{-1}; else if \method{dst()} returns
875 a non-zero value, \member{tm_isdst} is set to \code{1};
876 else \code{tm_isdst} is set to \code{0}.
877\end{methoddesc}
878
879\begin{methoddesc}{utctimetuple}{}
880 If \class{datetime} instance \var{d} is naive, this is the same as
881 \code{\var{d}.timetuple()} except that \member{tm_isdst} is forced to 0
882 regardless of what \code{d.dst()} returns. DST is never in effect
883 for a UTC time.
884
885 If \var{d} is aware, \var{d} is normalized to UTC time, by subtracting
Martin v. Löwisef365372003-09-04 18:29:53 +0000886 \code{\var{d}.utcoffset()}, and a \class{time.struct_time} for the
Tim Peters00372022003-01-09 04:10:05 +0000887 normalized time is returned. \member{tm_isdst} is forced to 0.
888 Note that the result's \member{tm_year} member may be
889 \constant{MINYEAR}-1 or \constant{MAXYEAR}+1, if \var{d}.year was
890 \code{MINYEAR} or \code{MAXYEAR} and UTC adjustment spills over a
891 year boundary.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000892\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000893
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000894\begin{methoddesc}{toordinal}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000895 Return the proleptic Gregorian ordinal of the date. The same as
Tim Peters00372022003-01-09 04:10:05 +0000896 \code{self.date().toordinal()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000897\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000898
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000899\begin{methoddesc}{weekday}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000900 Return the day of the week as an integer, where Monday is 0 and
Tim Peters00372022003-01-09 04:10:05 +0000901 Sunday is 6. The same as \code{self.date().weekday()}.
Fred Drake436eadd2002-12-31 18:13:11 +0000902 See also \method{isoweekday()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000903\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000904
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000905\begin{methoddesc}{isoweekday}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000906 Return the day of the week as an integer, where Monday is 1 and
Raymond Hettingereca984f2003-05-10 04:21:08 +0000907 Sunday is 7. The same as \code{self.date().isoweekday()}.
Fred Drake436eadd2002-12-31 18:13:11 +0000908 See also \method{weekday()}, \method{isocalendar()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000909\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000910
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000911\begin{methoddesc}{isocalendar}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000912 Return a 3-tuple, (ISO year, ISO week number, ISO weekday). The
Tim Peters00372022003-01-09 04:10:05 +0000913 same as \code{self.date().isocalendar()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000914\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000915
Fred Drakeb0e8f5d2003-12-30 20:36:20 +0000916\begin{methoddesc}{isoformat}{\optional{sep}}
Fred Drake436eadd2002-12-31 18:13:11 +0000917 Return a string representing the date and time in ISO 8601 format,
918 YYYY-MM-DDTHH:MM:SS.mmmmmm
Tim Peters00372022003-01-09 04:10:05 +0000919 or, if \member{microsecond} is 0,
Fred Drake436eadd2002-12-31 18:13:11 +0000920 YYYY-MM-DDTHH:MM:SS
Tim Peters00372022003-01-09 04:10:05 +0000921
922 If \method{utcoffset()} does not return \code{None}, a 6-character
923 string is appended, giving the UTC offset in (signed) hours and
924 minutes:
925 YYYY-MM-DDTHH:MM:SS.mmmmmm+HH:MM
926 or, if \member{microsecond} is 0
927 YYYY-MM-DDTHH:MM:SS+HH:MM
928
Fred Drake436eadd2002-12-31 18:13:11 +0000929 The optional argument \var{sep} (default \code{'T'}) is a
930 one-character separator, placed between the date and time portions
931 of the result. For example,
Tim Peters00372022003-01-09 04:10:05 +0000932
933\begin{verbatim}
934>>> from datetime import tzinfo, timedelta, datetime
935>>> class TZ(tzinfo):
936... def utcoffset(self, dt): return timedelta(minutes=-399)
937...
938>>> datetime(2002, 12, 25, tzinfo=TZ()).isoformat(' ')
939'2002-12-25 00:00:00-06:39'
940\end{verbatim}
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000941\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000942
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000943\begin{methoddesc}{__str__}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000944 For a \class{datetime} instance \var{d}, \code{str(\var{d})} is
945 equivalent to \code{\var{d}.isoformat(' ')}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000946\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000947
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000948\begin{methoddesc}{ctime}{}
Tim Peters00372022003-01-09 04:10:05 +0000949 Return a string representing the date and time, for example
950 \code{datetime(2002, 12, 4, 20, 30, 40).ctime() ==
951 'Wed Dec 4 20:30:40 2002'}.
Fred Drake436eadd2002-12-31 18:13:11 +0000952 \code{d.ctime()} is equivalent to
953 \code{time.ctime(time.mktime(d.timetuple()))} on platforms where
954 the native C \cfunction{ctime()} function (which
955 \function{time.ctime()} invokes, but which
956 \method{datetime.ctime()} does not invoke) conforms to the C
957 standard.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000958\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000959
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000960\begin{methoddesc}{strftime}{format}
Fred Drake436eadd2002-12-31 18:13:11 +0000961 Return a string representing the date and time, controlled by an
962 explicit format string. See the section on \method{strftime()}
963 behavior.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000964\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000965
966
Raymond Hettinger6005a342002-12-30 20:01:24 +0000967\subsection{\class{time} Objects \label{datetime-time}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000968
Tim Peters00372022003-01-09 04:10:05 +0000969A time object represents a (local) time of day, independent of any
970particular day, and subject to adjustment via a \class{tzinfo} object.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000971
Fred Drakeb0e8f5d2003-12-30 20:36:20 +0000972\begin{classdesc}{time}{hour\optional{, minute\optional{, second\optional{,
973 microsecond\optional{, tzinfo}}}}}
Tim Peters00372022003-01-09 04:10:05 +0000974 All arguments are optional. \var{tzinfo} may be \code{None}, or
975 an instance of a \class{tzinfo} subclass. The remaining arguments
976 may be ints or longs, in the following ranges:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000977
Tim Peters00372022003-01-09 04:10:05 +0000978 \begin{itemize}
979 \item \code{0 <= \var{hour} < 24}
980 \item \code{0 <= \var{minute} < 60}
981 \item \code{0 <= \var{second} < 60}
982 \item \code{0 <= \var{microsecond} < 1000000}.
983 \end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000984
Tim Peters00372022003-01-09 04:10:05 +0000985 If an argument outside those ranges is given,
Fred Drakeb0e8f5d2003-12-30 20:36:20 +0000986 \exception{ValueError} is raised. All default to \code{0} except
987 \var{tzinfo}, which defaults to \constant{None}.
Fred Drake0f8e5432002-12-31 18:31:48 +0000988\end{classdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000989
990Class attributes:
991
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000992\begin{memberdesc}{min}
Fred Drake436eadd2002-12-31 18:13:11 +0000993 The earliest representable \class{time}, \code{time(0, 0, 0, 0)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000994\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000995
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000996\begin{memberdesc}{max}
Fred Drake436eadd2002-12-31 18:13:11 +0000997 The latest representable \class{time}, \code{time(23, 59, 59, 999999)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000998\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000999
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001000\begin{memberdesc}{resolution}
Fred Drake436eadd2002-12-31 18:13:11 +00001001 The smallest possible difference between non-equal \class{time}
1002 objects, \code{timedelta(microseconds=1)}, although note that
1003 arithmetic on \class{time} objects is not supported.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001004\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001005
1006Instance attributes (read-only):
1007
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001008\begin{memberdesc}{hour}
Fred Drake436eadd2002-12-31 18:13:11 +00001009 In \code{range(24)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001010\end{memberdesc}
Fred Drake436eadd2002-12-31 18:13:11 +00001011
Tim Petersbad8ff02002-12-30 20:52:32 +00001012\begin{memberdesc}{minute}
Fred Drake436eadd2002-12-31 18:13:11 +00001013 In \code{range(60)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001014\end{memberdesc}
Fred Drake436eadd2002-12-31 18:13:11 +00001015
Tim Petersbad8ff02002-12-30 20:52:32 +00001016\begin{memberdesc}{second}
Fred Drake436eadd2002-12-31 18:13:11 +00001017 In \code{range(60)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001018\end{memberdesc}
Fred Drake436eadd2002-12-31 18:13:11 +00001019
Tim Petersbad8ff02002-12-30 20:52:32 +00001020\begin{memberdesc}{microsecond}
Fred Drake436eadd2002-12-31 18:13:11 +00001021 In \code{range(1000000)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001022\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001023
Tim Peters00372022003-01-09 04:10:05 +00001024\begin{memberdesc}{tzinfo}
1025 The object passed as the tzinfo argument to the \class{time}
1026 constructor, or \code{None} if none was passed.
1027\end{memberdesc}
1028
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001029Supported operations:
1030
Fred Drakebbdb2502002-12-23 18:58:06 +00001031\begin{itemize}
1032 \item
Tim Peters00372022003-01-09 04:10:05 +00001033 comparison of \class{time} to \class{time},
1034 where \var{a} is considered less than \var{b} when \var{a} precedes
1035 \var{b} in time. If one comparand is naive and the other is aware,
1036 \exception{TypeError} is raised. If both comparands are aware, and
1037 have the same \member{tzinfo} member, the common \member{tzinfo}
1038 member is ignored and the base times are compared. If both
1039 comparands are aware and have different \member{tzinfo} members,
1040 the comparands are first adjusted by subtracting their UTC offsets
1041 (obtained from \code{self.utcoffset()}).
Tim Peters07534a62003-02-07 22:50:28 +00001042 In order to stop mixed-type comparisons from falling back to the
1043 default comparison by object address, when a \class{time} object is
1044 compared to an object of a different type, \exception{TypeError} is
1045 raised unless the comparison is \code{==} or \code{!=}. The latter
1046 cases return \constant{False} or \constant{True}, respectively.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001047
Fred Drakebbdb2502002-12-23 18:58:06 +00001048 \item
1049 hash, use as dict key
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001050
Fred Drakebbdb2502002-12-23 18:58:06 +00001051 \item
1052 efficient pickling
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001053
Fred Drakebbdb2502002-12-23 18:58:06 +00001054 \item
Tim Peters00372022003-01-09 04:10:05 +00001055 in Boolean contexts, a \class{time} object is considered to be
1056 true if and only if, after converting it to minutes and
1057 subtracting \method{utcoffset()} (or \code{0} if that's
1058 \code{None}), the result is non-zero.
Fred Drakebbdb2502002-12-23 18:58:06 +00001059\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001060
1061Instance methods:
1062
Fred Drakeb0e8f5d2003-12-30 20:36:20 +00001063\begin{methoddesc}{replace}{\optional{hour\optional{, minute\optional{,
1064 second\optional{, microsecond\optional{,
1065 tzinfo}}}}}}
Tim Peters00372022003-01-09 04:10:05 +00001066 Return a \class{time} with the same value, except for those members given
1067 new values by whichever keyword arguments are specified. Note that
1068 \code{tzinfo=None} can be specified to create a naive \class{time} from
1069 an aware \class{time}, without conversion of the time members.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001070\end{methoddesc}
Tim Peters12bf3392002-12-24 05:41:27 +00001071
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001072\begin{methoddesc}{isoformat}{}
Fred Drake436eadd2002-12-31 18:13:11 +00001073 Return a string representing the time in ISO 8601 format,
1074 HH:MM:SS.mmmmmm
Tim Peters00372022003-01-09 04:10:05 +00001075 or, if self.microsecond is 0,
Fred Drake436eadd2002-12-31 18:13:11 +00001076 HH:MM:SS
Tim Peters00372022003-01-09 04:10:05 +00001077 If \method{utcoffset()} does not return \code{None}, a 6-character
1078 string is appended, giving the UTC offset in (signed) hours and
1079 minutes:
1080 HH:MM:SS.mmmmmm+HH:MM
1081 or, if self.microsecond is 0,
1082 HH:MM:SS+HH:MM
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001083\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001084
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001085\begin{methoddesc}{__str__}{}
Fred Drake436eadd2002-12-31 18:13:11 +00001086 For a time \var{t}, \code{str(\var{t})} is equivalent to
1087 \code{\var{t}.isoformat()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001088\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001089
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001090\begin{methoddesc}{strftime}{format}
Fred Drake436eadd2002-12-31 18:13:11 +00001091 Return a string representing the time, controlled by an explicit
1092 format string. See the section on \method{strftime()} behavior.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001093\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001094
Tim Peters00372022003-01-09 04:10:05 +00001095\begin{methoddesc}{utcoffset}{}
1096 If \member{tzinfo} is \code{None}, returns \code{None}, else
Tim Petersf196a0a2003-01-22 04:45:50 +00001097 returns \code{\var{self}.tzinfo.utcoffset(None)}, and
1098 raises an exception if the latter doesn't return \code{None} or
1099 a \class{timedelta} object representing a whole number of minutes
1100 with magnitude less than one day.
Tim Peters00372022003-01-09 04:10:05 +00001101\end{methoddesc}
1102
1103\begin{methoddesc}{dst}{}
1104 If \member{tzinfo} is \code{None}, returns \code{None}, else
Tim Petersf196a0a2003-01-22 04:45:50 +00001105 returns \code{\var{self}.tzinfo.dst(None)}, and
1106 raises an exception if the latter doesn't return \code{None}, or
1107 a \class{timedelta} object representing a whole number of minutes
1108 with magnitude less than one day.
Tim Peters00372022003-01-09 04:10:05 +00001109\end{methoddesc}
1110
1111\begin{methoddesc}{tzname}{}
1112 If \member{tzinfo} is \code{None}, returns \code{None}, else
Tim Petersf196a0a2003-01-22 04:45:50 +00001113 returns \code{\var{self}.tzinfo.tzname(None)}, or
1114 raises an exception if the latter doesn't return \code{None} or
1115 a string object.
Tim Peters00372022003-01-09 04:10:05 +00001116\end{methoddesc}
1117
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001118
Raymond Hettinger6005a342002-12-30 20:01:24 +00001119\subsection{\class{tzinfo} Objects \label{datetime-tzinfo}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001120
Fred Drakebbdb2502002-12-23 18:58:06 +00001121\class{tzinfo} is an abstract base clase, meaning that this class
1122should not be instantiated directly. You need to derive a concrete
1123subclass, and (at least) supply implementations of the standard
1124\class{tzinfo} methods needed by the \class{datetime} methods you
Tim Petersbad8ff02002-12-30 20:52:32 +00001125use. The \module{datetime} module does not supply any concrete
Fred Drakebbdb2502002-12-23 18:58:06 +00001126subclasses of \class{tzinfo}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001127
Fred Drakebbdb2502002-12-23 18:58:06 +00001128An instance of (a concrete subclass of) \class{tzinfo} can be passed
Tim Peters00372022003-01-09 04:10:05 +00001129to the constructors for \class{datetime} and \class{time} objects.
1130The latter objects view their members as being in local time, and the
Fred Drakebbdb2502002-12-23 18:58:06 +00001131\class{tzinfo} object supports methods revealing offset of local time
1132from UTC, the name of the time zone, and DST offset, all relative to a
1133date or time object passed to them.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001134
Tim Petersbad8ff02002-12-30 20:52:32 +00001135Special requirement for pickling: A \class{tzinfo} subclass must have an
Tim Peters2483b612002-12-24 16:30:58 +00001136\method{__init__} method that can be called with no arguments, else it
1137can be pickled but possibly not unpickled again. This is a technical
1138requirement that may be relaxed in the future.
1139
Fred Drakebbdb2502002-12-23 18:58:06 +00001140A concrete subclass of \class{tzinfo} may need to implement the
1141following methods. Exactly which methods are needed depends on the
Tim Petersbad8ff02002-12-30 20:52:32 +00001142uses made of aware \module{datetime} objects. If in doubt, simply
1143implement all of them.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001144
Tim Petersbad8ff02002-12-30 20:52:32 +00001145\begin{methoddesc}{utcoffset}{self, dt}
Fred Drake436eadd2002-12-31 18:13:11 +00001146 Return offset of local time from UTC, in minutes east of UTC. If
1147 local time is west of UTC, this should be negative. Note that this
1148 is intended to be the total offset from UTC; for example, if a
1149 \class{tzinfo} object represents both time zone and DST adjustments,
1150 \method{utcoffset()} should return their sum. If the UTC offset
1151 isn't known, return \code{None}. Else the value returned must be
Tim Peters397301e2003-01-02 21:28:08 +00001152 a \class{timedelta} object specifying a whole number of minutes in the
1153 range -1439 to 1439 inclusive (1440 = 24*60; the magnitude of the offset
1154 must be less than one day). Most implementations of
1155 \method{utcoffset()} will probably look like one of these two:
Fred Drake436eadd2002-12-31 18:13:11 +00001156
Tim Petersbad8ff02002-12-30 20:52:32 +00001157\begin{verbatim}
Tim Petersf3615152003-01-01 21:51:37 +00001158 return CONSTANT # fixed-offset class
Fred Drake436eadd2002-12-31 18:13:11 +00001159 return CONSTANT + self.dst(dt) # daylight-aware class
Guido van Rossum8e7ec7c2002-12-31 04:39:05 +00001160\end{verbatim}
Tim Peters710fb152003-01-02 19:35:54 +00001161
1162 If \method{utcoffset()} does not return \code{None},
1163 \method{dst()} should not return \code{None} either.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001164
Tim Peters327098a2003-01-20 22:54:38 +00001165 The default implementation of \method{utcoffset()} raises
1166 \exception{NotImplementedError}.
1167\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001168
Tim Petersbad8ff02002-12-30 20:52:32 +00001169\begin{methoddesc}{dst}{self, dt}
Tim Peters00372022003-01-09 04:10:05 +00001170 Return the daylight saving time (DST) adjustment, in minutes east of
Tim Petersb01c39b2003-01-21 16:44:27 +00001171 UTC, or \code{None} if DST information isn't known. Return
1172 \code{timedelta(0)} if DST is not in effect.
Tim Peters397301e2003-01-02 21:28:08 +00001173 If DST is in effect, return the offset as a
Fred Drake436eadd2002-12-31 18:13:11 +00001174 \class{timedelta} object (see \method{utcoffset()} for details).
1175 Note that DST offset, if applicable, has
1176 already been added to the UTC offset returned by
1177 \method{utcoffset()}, so there's no need to consult \method{dst()}
Tim Peters327098a2003-01-20 22:54:38 +00001178 unless you're interested in obtaining DST info separately. For
Tim Peters00372022003-01-09 04:10:05 +00001179 example, \method{datetime.timetuple()} calls its \member{tzinfo}
Fred Drake436eadd2002-12-31 18:13:11 +00001180 member's \method{dst()} method to determine how the
Tim Petersf3615152003-01-01 21:51:37 +00001181 \member{tm_isdst} flag should be set, and
Tim Petersf196a0a2003-01-22 04:45:50 +00001182 \method{tzinfo.fromutc()} calls \method{dst()} to account for
Tim Petersf3615152003-01-01 21:51:37 +00001183 DST changes when crossing time zones.
1184
1185 An instance \var{tz} of a \class{tzinfo} subclass that models both
1186 standard and daylight times must be consistent in this sense:
1187
Tim Petersf196a0a2003-01-22 04:45:50 +00001188 \code{\var{tz}.utcoffset(\var{dt}) - \var{tz}.dst(\var{dt})}
Tim Petersf3615152003-01-01 21:51:37 +00001189
Tim Peters00372022003-01-09 04:10:05 +00001190 must return the same result for every \class{datetime} \var{dt}
Fred Drakeb0e8f5d2003-12-30 20:36:20 +00001191 with \code{\var{dt}.tzinfo == \var{tz}} For sane \class{tzinfo}
Tim Petersf196a0a2003-01-22 04:45:50 +00001192 subclasses, this expression yields the time zone's "standard offset",
1193 which should not depend on the date or the time, but only on geographic
1194 location. The implementation of \method{datetime.astimezone()} relies
1195 on this, but cannot detect violations; it's the programmer's
1196 responsibility to ensure it. If a \class{tzinfo} subclass cannot
1197 guarantee this, it may be able to override the default implementation
1198 of \method{tzinfo.fromutc()} to work correctly with \method{astimezone()}
1199 regardless.
1200
1201 Most implementations of \method{dst()} will probably look like one
1202 of these two:
1203
1204\begin{verbatim}
Fred Drakeb0e8f5d2003-12-30 20:36:20 +00001205 def dst(self):
1206 # a fixed-offset class: doesn't account for DST
1207 return timedelta(0)
1208\end{verbatim}
Tim Petersf196a0a2003-01-22 04:45:50 +00001209
1210 or
1211
Fred Drakeb0e8f5d2003-12-30 20:36:20 +00001212\begin{verbatim}
1213 def dst(self):
1214 # Code to set dston and dstoff to the time zone's DST
1215 # transition times based on the input dt.year, and expressed
1216 # in standard local time. Then
Tim Petersf196a0a2003-01-22 04:45:50 +00001217
Fred Drakeb0e8f5d2003-12-30 20:36:20 +00001218 if dston <= dt.replace(tzinfo=None) < dstoff:
1219 return timedelta(hours=1)
1220 else:
1221 return timedelta(0)
Tim Petersf196a0a2003-01-22 04:45:50 +00001222\end{verbatim}
Tim Petersf3615152003-01-01 21:51:37 +00001223
Tim Peters327098a2003-01-20 22:54:38 +00001224 The default implementation of \method{dst()} raises
1225 \exception{NotImplementedError}.
1226\end{methoddesc}
1227
Tim Peters710fb152003-01-02 19:35:54 +00001228\begin{methoddesc}{tzname}{self, dt}
Tim Petersf196a0a2003-01-22 04:45:50 +00001229 Return the time zone name corresponding to the \class{datetime}
1230 object \var{dt}, as a string.
1231 Nothing about string names is defined by the
1232 \module{datetime} module, and there's no requirement that it mean
1233 anything in particular. For example, "GMT", "UTC", "-500", "-5:00",
1234 "EDT", "US/Eastern", "America/New York" are all valid replies. Return
Tim Peters710fb152003-01-02 19:35:54 +00001235 \code{None} if a string name isn't known. Note that this is a method
Tim Petersf196a0a2003-01-22 04:45:50 +00001236 rather than a fixed string primarily because some \class{tzinfo}
1237 subclasses will wish to return different names depending on the specific
1238 value of \var{dt} passed, especially if the \class{tzinfo} class is
Tim Peters710fb152003-01-02 19:35:54 +00001239 accounting for daylight time.
Tim Peters710fb152003-01-02 19:35:54 +00001240
Tim Peters327098a2003-01-20 22:54:38 +00001241 The default implementation of \method{tzname()} raises
1242 \exception{NotImplementedError}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001243\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001244
Tim Peters00372022003-01-09 04:10:05 +00001245These methods are called by a \class{datetime} or \class{time} object,
1246in response to their methods of the same names. A \class{datetime}
1247object passes itself as the argument, and a \class{time} object passes
Tim Petersbad8ff02002-12-30 20:52:32 +00001248\code{None} as the argument. A \class{tzinfo} subclass's methods should
1249therefore be prepared to accept a \var{dt} argument of \code{None}, or of
Tim Peters00372022003-01-09 04:10:05 +00001250class \class{datetime}.
Tim Petersbad8ff02002-12-30 20:52:32 +00001251
1252When \code{None} is passed, it's up to the class designer to decide the
1253best response. For example, returning \code{None} is appropriate if the
Tim Peters00372022003-01-09 04:10:05 +00001254class wishes to say that time objects don't participate in the
Tim Petersf196a0a2003-01-22 04:45:50 +00001255\class{tzinfo} protocols. It may be more useful for \code{utcoffset(None)}
Tim Peters327098a2003-01-20 22:54:38 +00001256to return the standard UTC offset, as there is no other convention for
1257discovering the standard offset.
Tim Petersbad8ff02002-12-30 20:52:32 +00001258
Tim Peters00372022003-01-09 04:10:05 +00001259When a \class{datetime} object is passed in response to a
1260\class{datetime} method, \code{dt.tzinfo} is the same object as
Tim Petersbad8ff02002-12-30 20:52:32 +00001261\var{self}. \class{tzinfo} methods can rely on this, unless
1262user code calls \class{tzinfo} methods directly. The intent is that
1263the \class{tzinfo} methods interpret \var{dt} as being in local time,
Tim Peters327098a2003-01-20 22:54:38 +00001264and not need worry about objects in other timezones.
Tim Petersbad8ff02002-12-30 20:52:32 +00001265
Tim Petersf196a0a2003-01-22 04:45:50 +00001266There is one more \class{tzinfo} method that a subclass may wish to
1267override:
1268
1269\begin{methoddesc}{fromutc}{self, dt}
1270 This is called from the default \class{datetime.astimezone()}
1271 implementation. When called from that, \code{\var{dt}.tzinfo} is
1272 \var{self}, and \var{dt}'s date and time members are to be viewed as
1273 expressing a UTC time. The purpose of \method{fromutc()} is to
1274 adjust the date and time members, returning an equivalent datetime in
1275 \var{self}'s local time.
1276
1277 Most \class{tzinfo} subclasses should be able to inherit the default
1278 \method{fromutc()} implementation without problems. It's strong enough
1279 to handle fixed-offset time zones, and time zones accounting for both
1280 standard and daylight time, and the latter even if the DST transition
1281 times differ in different years. An example of a time zone the default
1282 \method{fromutc()} implementation may not handle correctly in all cases
1283 is one where the standard offset (from UTC) depends on the specific date
1284 and time passed, which can happen for political reasons.
1285 The default implementations of \method{astimezone()} and
1286 \method{fromutc()} may not produce the result you want if the result is
1287 one of the hours straddling the moment the standard offset changes.
1288
1289 Skipping code for error cases, the default \method{fromutc()}
1290 implementation acts like:
1291
1292 \begin{verbatim}
1293 def fromutc(self, dt):
1294 # raise ValueError error if dt.tzinfo is not self
1295 dtoff = dt.utcoffset()
1296 dtdst = dt.dst()
1297 # raise ValueError if dtoff is None or dtdst is None
1298 delta = dtoff - dtdst # this is self's standard offset
1299 if delta:
1300 dt += delta # convert to standard local time
1301 dtdst = dt.dst()
1302 # raise ValueError if dtdst is None
1303 if dtdst:
1304 return dt + dtdst
1305 else:
1306 return dt
1307 \end{verbatim}
1308\end{methoddesc}
1309
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001310Example \class{tzinfo} classes:
1311
Fred Drakebbdb2502002-12-23 18:58:06 +00001312\verbatiminput{tzinfo-examples.py}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001313
Tim Peters327098a2003-01-20 22:54:38 +00001314Note that there are unavoidable subtleties twice per year in a
1315\class{tzinfo}
Tim Petersadf64202003-01-04 06:03:15 +00001316subclass accounting for both standard and daylight time, at the DST
1317transition points. For concreteness, consider US Eastern (UTC -0500),
1318where EDT begins the minute after 1:59 (EST) on the first Sunday in
1319April, and ends the minute after 1:59 (EDT) on the last Sunday in October:
1320
1321\begin{verbatim}
1322 UTC 3:MM 4:MM 5:MM 6:MM 7:MM 8:MM
1323 EST 22:MM 23:MM 0:MM 1:MM 2:MM 3:MM
1324 EDT 23:MM 0:MM 1:MM 2:MM 3:MM 4:MM
1325
1326 start 22:MM 23:MM 0:MM 1:MM 3:MM 4:MM
1327
1328 end 23:MM 0:MM 1:MM 1:MM 2:MM 3:MM
1329\end{verbatim}
1330
1331When DST starts (the "start" line), the local wall clock leaps from 1:59
1332to 3:00. A wall time of the form 2:MM doesn't really make sense on that
Tim Peters75a6e3b2003-01-04 18:17:36 +00001333day, so \code{astimezone(Eastern)} won't deliver a result with
Fred Drakeb0e8f5d2003-12-30 20:36:20 +00001334\code{hour == 2} on the
Tim Peters327098a2003-01-20 22:54:38 +00001335day DST begins. In order for \method{astimezone()} to make this
Tim Petersf196a0a2003-01-22 04:45:50 +00001336guarantee, the \method{rzinfo.dst()} method must consider times
Tim Peters327098a2003-01-20 22:54:38 +00001337in the "missing hour" (2:MM for Eastern) to be in daylight time.
Tim Petersadf64202003-01-04 06:03:15 +00001338
1339When DST ends (the "end" line), there's a potentially worse problem:
Tim Peters327098a2003-01-20 22:54:38 +00001340there's an hour that can't be spelled unambiguously in local wall time:
1341the last hour of daylight time. In Eastern, that's times of
1342the form 5:MM UTC on the day daylight time ends. The local wall clock
Tim Petersadf64202003-01-04 06:03:15 +00001343leaps from 1:59 (daylight time) back to 1:00 (standard time) again.
Tim Peters327098a2003-01-20 22:54:38 +00001344Local times of the form 1:MM are ambiguous. \method{astimezone()} mimics
1345the local clock's behavior by mapping two adjacent UTC hours into the
1346same local hour then. In the Eastern example, UTC times of the form
13475:MM and 6:MM both map to 1:MM when converted to Eastern. In order for
Tim Petersf196a0a2003-01-22 04:45:50 +00001348\method{astimezone()} to make this guarantee, the \method{tzinfo.dst()}
1349method must consider times in the "repeated hour" to be in
Tim Peters327098a2003-01-20 22:54:38 +00001350standard time. This is easily arranged, as in the example, by expressing
1351DST switch times in the time zone's standard local time.
Tim Petersadf64202003-01-04 06:03:15 +00001352
Tim Peters327098a2003-01-20 22:54:38 +00001353Applications that can't bear such ambiguities should avoid using hybrid
1354\class{tzinfo} subclasses; there are no ambiguities when using UTC, or
1355any other fixed-offset \class{tzinfo} subclass (such as a class
1356representing only EST (fixed offset -5 hours), or only EDT (fixed offset
1357-4 hours)).
Tim Petersadf64202003-01-04 06:03:15 +00001358
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001359
Tim Peters29fb9c72002-12-23 22:21:52 +00001360\subsection{\method{strftime()} Behavior}
1361
Tim Peters00372022003-01-09 04:10:05 +00001362\class{date}, \class{datetime}, and \class{time}
1363objects all support a \code{strftime(\var{format})}
Tim Peters29fb9c72002-12-23 22:21:52 +00001364method, to create a string representing the time under the control of
1365an explicit format string. Broadly speaking,
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001366\code{d.strftime(fmt)}
Tim Peters29fb9c72002-12-23 22:21:52 +00001367acts like the \refmodule{time} module's
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001368\code{time.strftime(fmt, d.timetuple())}
Tim Peters29fb9c72002-12-23 22:21:52 +00001369although not all objects support a \method{timetuple()} method.
1370
Tim Peters00372022003-01-09 04:10:05 +00001371For \class{time} objects, the format codes for
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001372year, month, and day should not be used, as time objects have no such
1373values. If they're used anyway, \code{1900} is substituted for the
1374year, and \code{0} for the month and day.
Tim Peters29fb9c72002-12-23 22:21:52 +00001375
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001376For \class{date} objects, the format codes for hours, minutes, and
1377seconds should not be used, as \class{date} objects have no such
1378values. If they're used anyway, \code{0} is substituted for them.
Tim Peters29fb9c72002-12-23 22:21:52 +00001379
Fred Drakea37e5cc2002-12-30 21:26:42 +00001380For a naive object, the \code{\%z} and \code{\%Z} format codes are
Tim Peters29fb9c72002-12-23 22:21:52 +00001381replaced by empty strings.
1382
1383For an aware object:
1384
1385\begin{itemize}
1386 \item[\code{\%z}]
1387 \method{utcoffset()} is transformed into a 5-character string of
1388 the form +HHMM or -HHMM, where HH is a 2-digit string giving the
1389 number of UTC offset hours, and MM is a 2-digit string giving the
1390 number of UTC offset minutes. For example, if
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001391 \method{utcoffset()} returns \code{timedelta(hours=-3, minutes=-30)},
Tim Peters1cff9fc2002-12-24 16:25:29 +00001392 \code{\%z} is replaced with the string \code{'-0330'}.
Tim Peters29fb9c72002-12-23 22:21:52 +00001393
1394 \item[\code{\%Z}]
1395 If \method{tzname()} returns \code{None}, \code{\%Z} is replaced
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001396 by an empty string. Otherwise \code{\%Z} is replaced by the returned
Tim Peters29fb9c72002-12-23 22:21:52 +00001397 value, which must be a string.
1398\end{itemize}
1399
1400The full set of format codes supported varies across platforms,
1401because Python calls the platform C library's \function{strftime()}
1402function, and platform variations are common. The documentation for
1403Python's \refmodule{time} module lists the format codes that the C
1404standard (1989 version) requires, and those work on all platforms
1405with a standard C implementation. Note that the 1999 version of the
1406C standard added additional format codes.
1407
1408The exact range of years for which \method{strftime()} works also
1409varies across platforms. Regardless of platform, years before 1900
1410cannot be used.