blob: d251e0f40e686b32c70e01f48251847b728a12c5 [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
19efficient field extraction, for output formatting and manipulation.
20
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
23zone, daylight savings time, or other kind of algorithmic or political
Fred Drakea37e5cc2002-12-30 21:26:42 +000024time 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
31For applications requiring more, ``aware'' \class{datetime} subclasses add an
Fred Drakea37e5cc2002-12-30 21:26:42 +000032optional time zone information object to the basic naive classes.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000033These \class{tzinfo} objects capture information about the offset from
34UTC time, the time zone name, and whether Daylight Savings Time is in
35effect. Note that no concrete \class{tzinfo} classes are supplied by
36the \module{datetime} module. Instead, they provide a framework for
37incorporating the level of detail an app may require. The rules for
38time adjustment across the world are more political than rational, and
39there is no standard suitable for every app.
40
41The \module{datetime} module exports the following constants:
42
43\begin{datadesc}{MINYEAR}
Fred Drakebbdb2502002-12-23 18:58:06 +000044 The smallest year number allowed in a \class{date},
45 \class{datetime}, or \class{datetimetz} object. \constant{MINYEAR}
46 is \code{1}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000047\end{datadesc}
48
49\begin{datadesc}{MAXYEAR}
Fred Drakebbdb2502002-12-23 18:58:06 +000050 The largest year number allowed in a \class{date}, \class{datetime},
51 or \class{datetimetz} object. \constant{MAXYEAR} is \code{9999}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000052\end{datadesc}
53
Raymond Hettinger6005a342002-12-30 20:01:24 +000054\begin{seealso}
Raymond Hettinger62229582002-12-31 16:37:03 +000055 \seemodule{calendar}{General calendar related functions.}
Raymond Hettinger6005a342002-12-30 20:01:24 +000056 \seemodule{time}{Time access and conversions.}
57\end{seealso}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000058
59\subsection{Available Types}
60
Fred Drakebbdb2502002-12-23 18:58:06 +000061\begin{classdesc*}{date}
Fred Drakea37e5cc2002-12-30 21:26:42 +000062 An idealized naive date, assuming the current Gregorian calendar
Fred Drakebbdb2502002-12-23 18:58:06 +000063 always was, and always will be, in effect.
64 Attributes: \member{year}, \member{month}, and \member{day}.
65\end{classdesc*}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000066
Fred Drakebbdb2502002-12-23 18:58:06 +000067\begin{classdesc*}{time}
Fred Drakea37e5cc2002-12-30 21:26:42 +000068 An idealized naive time, independent of any particular day, assuming
Fred Drakebbdb2502002-12-23 18:58:06 +000069 that every day has exactly 24*60*60 seconds (there is no notion
70 of "leap seconds" here).
71 Attributes: \member{hour}, \member{minute}, \member{second}, and
72 \member{microsecond}
73\end{classdesc*}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000074
Fred Drakebbdb2502002-12-23 18:58:06 +000075\begin{classdesc*}{datetime}
Fred Drakea37e5cc2002-12-30 21:26:42 +000076 A combination of a naive date and a naive time.
Fred Drakebbdb2502002-12-23 18:58:06 +000077 Attributes: \member{year}, \member{month}, \member{day},
78 \member{hour}, \member{minute}, \member{second},
79 and \member{microsecond}.
80\end{classdesc*}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000081
Fred Drakebbdb2502002-12-23 18:58:06 +000082\begin{classdesc*}{timedelta}
83 A duration, expressing the difference between two \class{date},
84 \class{time}, or \class{datetime} instances, to microsecond
85 resolution.
86\end{classdesc*}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000087
Fred Drakebbdb2502002-12-23 18:58:06 +000088\begin{classdesc*}{tzinfo}
89 An abstract base class for time zone information objects. These
90 are used by the \class{datetimetz} and \class{timetz} classes to
91 provided a customizable notion of time adjustment (for example, to
92 account for time zone and/or daylight savings time).
93\end{classdesc*}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000094
Fred Drakebbdb2502002-12-23 18:58:06 +000095\begin{classdesc*}{timetz}
96 An aware subclass of \class{time}, supporting a customizable notion of
97 time adjustment.
98\end{classdesc*}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000099
Fred Drakebbdb2502002-12-23 18:58:06 +0000100\begin{classdesc*}{datetimetz}
101 An aware subclass of \class{datetime}, supporting a customizable notion of
102 time adjustment.
103\end{classdesc*}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000104
105Objects of these types are immutable.
106
Fred Drakebbdb2502002-12-23 18:58:06 +0000107Objects of the \class{date}, \class{datetime}, and \class{time} types
Fred Drakea37e5cc2002-12-30 21:26:42 +0000108are always naive.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000109
Fred Drakea37e5cc2002-12-30 21:26:42 +0000110An object \var{d} of type \class{timetz} or \class{datetimetz} may be
111naive or aware. \var{d} is aware if \code{\var{d}.tzinfo} is not
112\code{None}, and \code{\var{d}.tzinfo.utcoffset(\var{d})} does not return
113\code{None}. If \code{\var{d}.tzinfo} is \code{None}, or if
114\code{\var{d}.tzinfo} is not \code{None} but
115\code{\var{d}.tzinfo.utcoffset(\var{d})} returns \code{None}, \var{d}
116is naive.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000117
Fred Drakea37e5cc2002-12-30 21:26:42 +0000118The distinction between naive and aware doesn't apply to
Fred Drakebbdb2502002-12-23 18:58:06 +0000119\code{timedelta} objects.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000120
Fred Drakebbdb2502002-12-23 18:58:06 +0000121Subclass relationships:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000122
Fred Drakebbdb2502002-12-23 18:58:06 +0000123\begin{verbatim}
124object
125 timedelta
126 tzinfo
127 time
128 timetz
129 date
130 datetime
131 datetimetz
132\end{verbatim}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000133
Raymond Hettinger6005a342002-12-30 20:01:24 +0000134\subsection{\class{timedelta} Objects \label{datetime-timedelta}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000135
Fred Drakebbdb2502002-12-23 18:58:06 +0000136A \class{timedelta} object represents a duration, the difference
137between two dates or times.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000138
Fred Drake0f8e5432002-12-31 18:31:48 +0000139\begin{classdesc}{timedelta}{days=0, seconds=0, microseconds=0,
140 milliseconds=0, minutes=0, hours=0, weeks=0}
Raymond Hettingerf6212322002-12-31 17:24:50 +0000141
Fred Drake436eadd2002-12-31 18:13:11 +0000142 All arguments are optional. Arguments may be ints, longs, or floats,
143 and may be positive or negative.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000144
Fred Drake436eadd2002-12-31 18:13:11 +0000145 Only \var{days}, \var{seconds} and \var{microseconds} are stored
146 internally. Arguments are converted to those units:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000147
Raymond Hettinger6005a342002-12-30 20:01:24 +0000148\begin{verbatim}
Fred Drake436eadd2002-12-31 18:13:11 +0000149A millisecond is converted to 1000 microseconds.
150A minute is converted to 60 seconds.
151An hour is converted to 3600 seconds.
152A week is converted to 7 days.
Raymond Hettinger6005a342002-12-30 20:01:24 +0000153\end{verbatim}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000154
Fred Drake436eadd2002-12-31 18:13:11 +0000155 and days, seconds and microseconds are then normalized so that the
156 representation is unique, with
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000157
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000158\begin{itemize}
159 \item \code{0 <= \var{microseconds} < 1000000}
160 \item \code{0 <= \var{seconds} < 3600*24} (the number of seconds in one day)
161 \item \code{-999999999 <= \var{days} <= 999999999}
162\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000163
Fred Drake436eadd2002-12-31 18:13:11 +0000164 If any argument is a float, and there are fractional microseconds,
165 the fractional microseconds left over from all arguments are combined
166 and their sum is rounded to the nearest microsecond. If no
167 argument is a float, the conversion and normalization processes
168 are exact (no information is lost).
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000169
Fred Drake436eadd2002-12-31 18:13:11 +0000170 If the normalized value of days lies outside the indicated range,
171 \exception{OverflowError} is raised.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000172
Fred Drake436eadd2002-12-31 18:13:11 +0000173 Note that normalization of negative values may be surprising at first.
174 For example,
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000175
Fred Drakebbdb2502002-12-23 18:58:06 +0000176\begin{verbatim}
177>>> d = timedelta(microseconds=-1)
178>>> (d.days, d.seconds, d.microseconds)
179(-1, 86399, 999999)
180\end{verbatim}
Fred Drake0f8e5432002-12-31 18:31:48 +0000181\end{classdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000182
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000183Class attributes are:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000184
Fred Drake0f8e5432002-12-31 18:31:48 +0000185\begin{memberdesc}{min}
186 The most negative \class{timedelta} object,
187 \code{timedelta(-999999999)}.
188\end{memberdesc}
189
190\begin{memberdesc}{max}
191 The most positive \class{timedelta} object,
192 \code{timedelta(days=999999999, hours=23, minutes=59, seconds=59,
193 microseconds=999999)}.
194\end{memberdesc}
195
196\begin{memberdesc}{resolution}
197 The smallest possible difference between non-equal
198 \class{timedelta} objects, \code{timedelta(microseconds=1)}.
199\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000200
Fred Drakea37e5cc2002-12-30 21:26:42 +0000201Note that, because of normalization, \code{timedelta.max} \textgreater
202\code{-timedelta.min}. \code{-timedelta.max} is not representable as
203a \class{timedelta} object.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000204
205Instance attributes (read-only):
206
Raymond Hettinger6005a342002-12-30 20:01:24 +0000207\begin{tableii}{c|l}{code}{Attribute}{Value}
208 \lineii{days}{Between -999999999 and 999999999 inclusive}
209 \lineii{seconds}{Between 0 and 86399 inclusive}
210 \lineii{microseconds}{Between 0 and 999999 inclusive}
211\end{tableii}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000212
213Supported operations:
214
Raymond Hettinger6005a342002-12-30 20:01:24 +0000215\begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes}
Fred Drake9bdeee42002-12-30 20:35:32 +0000216 \lineiii{\var{t1} = \var{t2} + \var{t3}}
Tim Peters95322982002-12-31 16:01:47 +0000217 {Sum of \var{t2} and \var{t3}.
Fred Drake9bdeee42002-12-30 20:35:32 +0000218 Afterwards \var{t1}-\var{t2} == \var{t3} and \var{t1}-\var{t3}
219 == \var{t2} are true.}
220 {(1)}
221 \lineiii{\var{t1} = \var{t2} - \var{t3}}
222 {Difference of \var{t2} and \var{t3}. Afterwards \var{t1} ==
223 \var{t2} - \var{t3} and \var{t2} == \var{t1} + \var{t3} are
224 true.}
225 {(1)}
226 \lineiii{\var{t1} = \var{t2} * \var{i} or \var{t1} = \var{i} * \var{t2}}
227 {Delta multiplied by an integer or long.
Fred Drake436eadd2002-12-31 18:13:11 +0000228 Afterwards \var{t1} // i == \var{t2} is true,
229 provided \code{i != 0}.
Fred Drake9bdeee42002-12-30 20:35:32 +0000230 In general, \var{t1} * i == \var{t1} * (i-1) + \var{t1} is true.}
231 {(1)}
232 \lineiii{\var{t1} = \var{t2} // \var{i}}
233 {The floor is computed and the remainder (if any) is thrown away.}
Tim Peters397301e2003-01-02 21:28:08 +0000234 {(3)}
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000235 \lineiii{+\var{t1}}
Raymond Hettingercbd6cd22002-12-31 16:30:49 +0000236 {Returns a \class{timedelta} object with the same value.}
Tim Peters397301e2003-01-02 21:28:08 +0000237 {(2)}
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000238 \lineiii{-\var{t1}}
Raymond Hettingercbd6cd22002-12-31 16:30:49 +0000239 {equivalent to \class{timedelta}(-\var{t1.days}, -\var{t1.seconds},
240 -\var{t1.microseconds}),and to \var{t1}* -1.}
Tim Peters397301e2003-01-02 21:28:08 +0000241 {(1)(4)}
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000242 \lineiii{abs(\var{t})}
Fred Drake436eadd2002-12-31 18:13:11 +0000243 {equivalent to +\var{t} when \code{t.days >= 0}, and to
Tim Peters397301e2003-01-02 21:28:08 +0000244 -\var{t} when \code{t.days < 0}.
245 overflow.}
246 {(2)}
Fred Drake9bdeee42002-12-30 20:35:32 +0000247\end{tableiii}
Raymond Hettinger6005a342002-12-30 20:01:24 +0000248\noindent
249Notes:
250
251\begin{description}
252\item[(1)]
Fred Drake436eadd2002-12-31 18:13:11 +0000253 This is exact, but may overflow.
Raymond Hettinger6005a342002-12-30 20:01:24 +0000254
255\item[(2)]
Tim Peters397301e2003-01-02 21:28:08 +0000256 This is exact, and cannot overflow.
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000257
258\item[(3)]
Tim Peters397301e2003-01-02 21:28:08 +0000259 Division by 0 raises \exception{ZeroDivisionError}.
260
261\item[(4)]
Fred Drake436eadd2002-12-31 18:13:11 +0000262 -\var{timedelta.max} is not representable as a \class{timedelta} object.
Raymond Hettinger6005a342002-12-30 20:01:24 +0000263\end{description}
264
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000265In addition to the operations listed above \class{timedelta} objects
266support certain additions and subtractions with \class{date},
267\class{datetime}, and \class{datimetz} objects (see below).
Raymond Hettinger6005a342002-12-30 20:01:24 +0000268
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000269Comparisons of \class{timedelta} objects are supported with the
270\class{timedelta} object representing the smaller duration considered
271to be the smaller timedelta.
Raymond Hettinger6005a342002-12-30 20:01:24 +0000272
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000273\class{timedelta} objects are hashable (usable as dictionary key),
274support efficient pickling, and in Boolean contexts, a \class{timedelta}
275object is considered to be true if and only if it isn't equal to
276\code{timedelta(0)}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000277
278
Raymond Hettinger6005a342002-12-30 20:01:24 +0000279\subsection{\class{date} Objects \label{datetime-date}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000280
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000281A \class{date} object represents a date (year, month and day) in an idealized
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000282calendar, the current Gregorian calendar indefinitely extended in both
283directions. January 1 of year 1 is called day number 1, January 2 of year
2841 is called day number 2, and so on. This matches the definition of the
285"proleptic Gregorian" calendar in Dershowitz and Reingold's book
Fred Drakea37e5cc2002-12-30 21:26:42 +0000286\citetitle{Calendrical Calculations}, where it's the base calendar for all
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000287computations. See the book for algorithms for converting between
288proleptic Gregorian ordinals and many other calendar systems.
289
Fred Drake0f8e5432002-12-31 18:31:48 +0000290\begin{classdesc}{date}{year, month, day}
Fred Drake436eadd2002-12-31 18:13:11 +0000291 All arguments are required. Arguments may be ints or longs, in the
292 following ranges:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000293
Fred Drake436eadd2002-12-31 18:13:11 +0000294 \begin{itemize}
295 \item \code{MINYEAR <= \var{year} <= MAXYEAR}
296 \item \code{1 <= \var{month} <= 12}
297 \item \code{1 <= \var{day} <= number of days in the given month and year}
298 \end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000299
Fred Drake436eadd2002-12-31 18:13:11 +0000300 If an argument outside those ranges is given, \exception{ValueError}
301 is raised.
Fred Drake0f8e5432002-12-31 18:31:48 +0000302\end{classdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000303
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000304Other constructors, all class methods:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000305
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000306\begin{methoddesc}{today}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000307 Return the current local date. This is equivalent to
308 \code{date.fromtimestamp(time.time())}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000309\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000310
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000311\begin{methoddesc}{fromtimestamp}{timestamp}
Fred Drake436eadd2002-12-31 18:13:11 +0000312 Return the local date corresponding to the POSIX timestamp, such
313 as is returned by \function{time.time()}. This may raise
314 \exception{ValueError}, if the timestamp is out of the range of
315 values supported by the platform C \cfunction{localtime()}
316 function. It's common for this to be restricted to years from 1970
317 through 2038.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000318\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000319
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000320\begin{methoddesc}{fromordinal}{ordinal}
Fred Drake436eadd2002-12-31 18:13:11 +0000321 Return the date corresponding to the proleptic Gregorian ordinal,
322 where January 1 of year 1 has ordinal 1. \exception{ValueError} is
323 raised unless \code{1 <= \var{ordinal} <= date.max.toordinal()}.
324 For any date \var{d}, \code{date.fromordinal(\var{d}.toordinal()) ==
325 \var{d}}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000326\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000327
328Class attributes:
329
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000330\begin{memberdesc}{min}
Fred Drake436eadd2002-12-31 18:13:11 +0000331 The earliest representable date, \code{date(MINYEAR, 1, 1)}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000332\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000333
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000334\begin{memberdesc}{max}
Fred Drake436eadd2002-12-31 18:13:11 +0000335 The latest representable date, \code{date(MAXYEAR, 12, 31)}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000336\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000337
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000338\begin{memberdesc}{resolution}
Fred Drake436eadd2002-12-31 18:13:11 +0000339 The smallest possible difference between non-equal date
340 objects, \code{timedelta(days=1)}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000341\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000342
343Instance attributes (read-only):
344
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000345\begin{memberdesc}{year}
Fred Drake436eadd2002-12-31 18:13:11 +0000346 Between \constant{MINYEAR} and \constant{MAXYEAR} inclusive
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000347\end{memberdesc}
348
349\begin{memberdesc}{month}
Fred Drake436eadd2002-12-31 18:13:11 +0000350 Between 1 and 12 inclusive.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000351\end{memberdesc}
352
353\begin{memberdesc}{day}
Fred Drake436eadd2002-12-31 18:13:11 +0000354 Between 1 and the number of days in the given month of the given
355 year.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000356\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000357
358Supported operations:
359
Fred Drakebbdb2502002-12-23 18:58:06 +0000360\begin{itemize}
361 \item
362 date1 + timedelta -> date2
363 timedelta + date1 -> date2
364 date2 is timedelta.days days removed from the date1, moving forward
365 in time if timedelta.days > 0, or backward if timedetla.days < 0.
366 date2 - date1 == timedelta.days after. timedelta.seconds and
367 timedelta.microseconds are ignored. \exception{OverflowError} is
368 raised if date2.year would be smaller than \constant{MINYEAR} or
369 larger than \constant{MAXYEAR}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000370
Fred Drakebbdb2502002-12-23 18:58:06 +0000371 \item
372 date1 - timedelta -> date2
373 Computes the date2 such that date2 + timedelta == date1. This
374 isn't quite equivalent to date1 + (-timedelta), because -timedelta
375 in isolation can overflow in cases where date1 - timedelta does
376 not. timedelta.seconds and timedelta.microseconds are ignored.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000377
Fred Drakebbdb2502002-12-23 18:58:06 +0000378 \item
379 date1 - date2 -> timedelta
380 This is exact, and cannot overflow. timedelta.seconds and
381 timedelta.microseconds are 0, and date2 + timedelta == date1
382 after.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000383
Fred Drakebbdb2502002-12-23 18:58:06 +0000384 \item
385 comparison of date to date, where date1 is considered less than
386 date2 when date1 precedes date2 in time. In other words,
387 date1 < date2 if and only if date1.toordinal() < date2.toordinal().
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000388
Fred Drakebbdb2502002-12-23 18:58:06 +0000389 \item
390 hash, use as dict key
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000391
Fred Drakebbdb2502002-12-23 18:58:06 +0000392 \item
393 efficient pickling
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000394
Fred Drakebbdb2502002-12-23 18:58:06 +0000395 \item
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000396 in Boolean contexts, all \class{date} objects are considered to be true
Fred Drakebbdb2502002-12-23 18:58:06 +0000397\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000398
399Instance methods:
400
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000401\begin{methoddesc}{replace}{year, month, day}
Fred Drake436eadd2002-12-31 18:13:11 +0000402 Return a date with the same value, except for those fields given
403 new values by whichever keyword arguments are specified. For
404 example, if \code{d == date(2002, 12, 31)}, then
405 \code{d.replace(day=26) == date(2000, 12, 26)}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000406\end{methoddesc}
Tim Peters12bf3392002-12-24 05:41:27 +0000407
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000408\begin{methoddesc}{timetuple}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000409 Return a 9-element tuple of the form returned by
410 \function{time.localtime()}. The hours, minutes and seconds are
411 0, and the DST flag is -1.
412 \code{\var{d}.timetuple()} is equivalent to
413 \code{(\var{d}.year, \var{d}.month, \var{d}.day,
414 0, 0, 0, \# h, m, s
415 \var{d}.weekday(), \# 0 is Monday
416 \var{d}.toordinal() - date(\var{d}.year, 1, 1).toordinal() + 1,
417 \# day of year
418 -1)}
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000419\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000420
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000421\begin{methoddesc}{toordinal}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000422 Return the proleptic Gregorian ordinal of the date, where January 1
423 of year 1 has ordinal 1. For any \class{date} object \var{d},
424 \code{date.fromordinal(\var{d}.toordinal()) == \var{d}}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000425\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000426
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000427\begin{methoddesc}{weekday}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000428 Return the day of the week as an integer, where Monday is 0 and
429 Sunday is 6. For example, date(2002, 12, 4).weekday() == 2, a
430 Wednesday.
431 See also \method{isoweekday()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000432\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000433
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000434\begin{methoddesc}{isoweekday}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000435 Return the day of the week as an integer, where Monday is 1 and
436 Sunday is 7. For example, date(2002, 12, 4).isoweekday() == 3, a
437 Wednesday.
438 See also \method{weekday()}, \method{isocalendar()}.
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}{isocalendar}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000442 Return a 3-tuple, (ISO year, ISO week number, ISO weekday).
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000443
Fred Drake436eadd2002-12-31 18:13:11 +0000444 The ISO calendar is a widely used variant of the Gregorian calendar.
445 See \url{http://www.phys.uu.nl/~vgent/calendar/isocalendar.htm}
446 for a good explanation.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000447
Fred Drake436eadd2002-12-31 18:13:11 +0000448 The ISO year consists of 52 or 53 full weeks, and where a week starts
449 on a Monday and ends on a Sunday. The first week of an ISO year is
450 the first (Gregorian) calendar week of a year containing a Thursday.
451 This is called week number 1, and the ISO year of that Thursday is
452 the same as its Gregorian year.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000453
Fred Drake436eadd2002-12-31 18:13:11 +0000454 For example, 2004 begins on a Thursday, so the first week of ISO
455 year 2004 begins on Monday, 29 Dec 2003 and ends on Sunday, 4 Jan
456 2004, so that
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000457
Fred Drake436eadd2002-12-31 18:13:11 +0000458 date(2003, 12, 29).isocalendar() == (2004, 1, 1)
459 date(2004, 1, 4).isocalendar() == (2004, 1, 7)
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000460\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000461
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000462\begin{methoddesc}{isoformat}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000463 Return a string representing the date in ISO 8601 format,
464 'YYYY-MM-DD'. For example,
465 date(2002, 12, 4).isoformat() == '2002-12-04'.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000466\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000467
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000468\begin{methoddesc}{__str__}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000469 For a date \var{d}, \code{str(\var{d})} is equivalent to
470 \code{\var{d}.isoformat()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000471\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000472
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000473\begin{methoddesc}{ctime}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000474 Return a string representing the date, for example
475 date(2002, 12, 4).ctime() == 'Wed Dec 4 00:00:00 2002'.
476 \code{\var{d}.ctime()} is equivalent to
477 \code{time.ctime(time.mktime(\var{d}.timetuple()))}
478 on platforms where the native C \cfunction{ctime()} function
479 (which \function{time.ctime()} invokes, but which
480 \method{date.ctime()} does not invoke) conforms to the C standard.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000481\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000482
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000483\begin{methoddesc}{strftime}{format}
Fred Drake436eadd2002-12-31 18:13:11 +0000484 Return a string representing the date, controlled by an explicit
485 format string. Format codes referring to hours, minutes or seconds
486 will see 0 values.
487 See the section on \method{strftime()} behavior.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000488\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000489
490
Raymond Hettinger6005a342002-12-30 20:01:24 +0000491\subsection{\class{datetime} Objects \label{datetime-datetime}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000492
Fred Drakebbdb2502002-12-23 18:58:06 +0000493A \class{datetime} object is a single object containing all the
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000494information from a \class{date} object and a time object. Like a
495\class{date} object, \class{datetime} assumes the current Gregorian
496calendar extended in both directions; like a time object,
497\class{datetime} assumes there are exactly 3600*24 seconds in every
498day.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000499
Fred Drake0f8e5432002-12-31 18:31:48 +0000500\begin{classdesc}{datetime}{year, month, day,
501 hour=0, minute=0, second=0, microsecond=0}
Fred Drake436eadd2002-12-31 18:13:11 +0000502 The year, month and day arguments are required. Arguments may be
503 ints or longs, in the following ranges:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000504
Fred Drake436eadd2002-12-31 18:13:11 +0000505 \begin{itemize}
506 \item \code{\member{MINYEAR} <= \var{year} <= \member{MAXYEAR}}
507 \item \code{1 <= \var{month} <= 12}
508 \item \code{1 <= \var{day} <= number of days in the given month and year}
509 \item \code{0 <= \var{hour} < 24}
510 \item \code{0 <= \var{minute} < 60}
511 \item \code{0 <= \var{second} < 60}
512 \item \code{0 <= \var{microsecond} < 1000000}
513 \end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000514
Fred Drake436eadd2002-12-31 18:13:11 +0000515 If an argument outside those ranges is given, \exception{ValueError}
516 is raised.
Fred Drake0f8e5432002-12-31 18:31:48 +0000517\end{classdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000518
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000519Other constructors, all class methods:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000520
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000521\begin{methoddesc}{today}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000522 Return the current local datetime. This is equivalent to
523 \code{datetime.fromtimestamp(time.time())}.
524 See also \method{now()}, \method{fromtimestamp()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000525\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000526
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000527\begin{methoddesc}{now}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000528 Return the current local datetime. This is like \method{today()},
529 but, if possible, supplies more precision than can be gotten from
530 going through a \function{time.time()} timestamp (for example,
531 this may be possible on platforms that supply the C
532 \cfunction{gettimeofday()} function).
533 See also \method{today()}, \method{utcnow()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000534\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000535
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000536\begin{methoddesc}{utcnow}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000537 Return the current UTC datetime. This is like \method{now()}, but
538 returns the current UTC date and time.
539 See also \method{now()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000540\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000541
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000542\begin{methoddesc}{fromtimestamp}{timestamp}
Fred Drake436eadd2002-12-31 18:13:11 +0000543 Return the local \class{datetime} corresponding to the \POSIX{}
544 timestamp, such as is returned by \function{time.time()}. This
545 may raise \exception{ValueError}, if the timestamp is out of the
546 range of values supported by the platform C
547 \cfunction{localtime()} function. It's common for this to be
548 restricted to years in 1970 through 2038.
549 See also \method{utcfromtimestamp()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000550\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000551
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000552\begin{methoddesc}{utcfromtimestamp}{timestamp}
Fred Drake436eadd2002-12-31 18:13:11 +0000553 Return the UTC \class{datetime} corresponding to the \POSIX{}
554 timestamp. This may raise \exception{ValueError}, if the
555 timestamp is out of the range of values supported by the platform
556 C \cfunction{gmtime()} function. It's common for this to be
557 restricted to years in 1970 through 2038.
558 See also \method{fromtimestamp()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000559\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000560
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000561\begin{methoddesc}{fromordinal}{ordinal}
Fred Drake436eadd2002-12-31 18:13:11 +0000562 Return the \class{datetime} corresponding to the proleptic
563 Gregorian ordinal, where January 1 of year 1 has ordinal 1.
564 \exception{ValueError} is raised unless 1 <= ordinal <=
565 datetime.max.toordinal(). The hour, minute, second and
566 microsecond of the result are all 0.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000567\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000568
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000569\begin{methoddesc}{combine}{date, time}
Fred Drake436eadd2002-12-31 18:13:11 +0000570 Return a new \class{datetime} object whose date components are
571 equal to the given \class{date} object's, and whose time
572 components are equal to the given time object's. For any
573 \class{datetime} object \var{d}, \code{\var{d} ==
574 datetime.combine(\var{d}.date(), \var{d}.time())}. If date is a
575 \class{datetime} or \class{datetimetz} object, its time components
576 are ignored. If date is \class{datetimetz} object, its
577 \member{tzinfo} component is also ignored. If time is a
578 \class{timetz} object, its \member{tzinfo} component is ignored.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000579\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000580
581Class attributes:
582
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000583\begin{memberdesc}{min}
Fred Drake436eadd2002-12-31 18:13:11 +0000584 The earliest representable \class{datetime},
585 \code{datetime(MINYEAR, 1, 1)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000586\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000587
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000588\begin{memberdesc}{max}
Fred Drake436eadd2002-12-31 18:13:11 +0000589 The latest representable \class{datetime},
590 \code{datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000591\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000592
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000593\begin{memberdesc}{resolution}
Fred Drake436eadd2002-12-31 18:13:11 +0000594 The smallest possible difference between non-equal \class{datetime}
595 objects, \code{timedelta(microseconds=1)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000596\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000597
598Instance attributes (read-only):
599
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000600\begin{memberdesc}{year}
Fred Drake436eadd2002-12-31 18:13:11 +0000601 Between \constant{MINYEAR} and \constant{MAXYEAR} inclusive
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000602\end{memberdesc}
603
604\begin{memberdesc}{month}
Fred Drake436eadd2002-12-31 18:13:11 +0000605 Between 1 and 12 inclusive
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000606\end{memberdesc}
607
608\begin{memberdesc}{day}
Fred Drake436eadd2002-12-31 18:13:11 +0000609 Between 1 and the number of days in the given month of the given
610 year.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000611\end{memberdesc}
612
613\begin{memberdesc}{hour}
Fred Drake436eadd2002-12-31 18:13:11 +0000614 In \code{range(24)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000615\end{memberdesc}
616
617\begin{memberdesc}{minute}
Fred Drake436eadd2002-12-31 18:13:11 +0000618 In \code{range(60)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000619\end{memberdesc}
620
621\begin{memberdesc}{second}
Fred Drake436eadd2002-12-31 18:13:11 +0000622 In \code{range(60)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000623\end{memberdesc}
624
625\begin{memberdesc}{microsecond}
Fred Drake436eadd2002-12-31 18:13:11 +0000626 In \code{range(1000000)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000627\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000628
629Supported operations:
630
Fred Drakebbdb2502002-12-23 18:58:06 +0000631\begin{itemize}
632 \item
633 datetime1 + timedelta -> datetime2
634 timedelta + datetime1 -> datetime2
635 datetime2 is a duration of timedelta removed from datetime1, moving
636 forward in time if timedelta.days > 0, or backward if
637 timedelta.days < 0. datetime2 - datetime1 == timedelta after.
638 \exception{OverflowError} is raised if datetime2.year would be
639 smaller than \constant{MINYEAR} or larger than \constant{MAXYEAR}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000640
Fred Drakebbdb2502002-12-23 18:58:06 +0000641 \item
642 datetime1 - timedelta -> datetime2
643 Computes the datetime2 such that datetime2 + timedelta == datetime1.
644 This isn't quite equivalent to datetime1 + (-timedelta), because
645 -timedelta in isolation can overflow in cases where
646 datetime1 - timedelta does not.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000647
Fred Drakebbdb2502002-12-23 18:58:06 +0000648 \item
649 datetime1 - datetime2 -> timedelta
650 This is exact, and cannot overflow.
651 datetime2 + timedelta == datetime1 after.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000652
Fred Drakebbdb2502002-12-23 18:58:06 +0000653 \item
654 comparison of \class{datetime} to datetime, where datetime1 is
655 considered less than datetime2 when datetime1 precedes datetime2
656 in time.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000657
Fred Drakebbdb2502002-12-23 18:58:06 +0000658 \item
659 hash, use as dict key
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000660
Fred Drakebbdb2502002-12-23 18:58:06 +0000661 \item
662 efficient pickling
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000663
Fred Drakebbdb2502002-12-23 18:58:06 +0000664 \item
665 in Boolean contexts, all \class{datetime} objects are considered
666 to be true
667\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000668
669Instance methods:
670
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000671\begin{methoddesc}{date}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000672 Return \class{date} object with same year, month and day.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000673\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000674
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000675\begin{methoddesc}{time}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000676 Return time object with same hour, minute, second and microsecond.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000677\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000678
Fred Drake436eadd2002-12-31 18:13:11 +0000679\begin{methoddesc}{replace}{year=, month=, day=, hour=, minute=,
680 second=, microsecond=}
681 Return a datetime with the same value, except for those fields given
682 new values by whichever keyword arguments are specified.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000683\end{methoddesc}
Tim Peters12bf3392002-12-24 05:41:27 +0000684
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000685\begin{methoddesc}{astimezone}{tz}
Fred Drake436eadd2002-12-31 18:13:11 +0000686 Return a \class{datetimetz} with the same date and time fields, and
687 with \member{tzinfo} member \var{tz}. \var{tz} must be \code{None},
688 or an instance of a \class{tzinfo} subclass.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000689\end{methoddesc}
Tim Peters80475bb2002-12-25 07:40:55 +0000690
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000691\begin{methoddesc}{timetuple}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000692 Return a 9-element tuple of the form returned by
693 \function{time.localtime()}.
694 The DST flag is -1. \code{\var{d}.timetuple()} is equivalent to
695 \code{(\var{d}.year, \var{d}.month, \var{d}.day,
696 \var{d}.hour, \var{d}.minute, \var{d}.second,
697 \var{d}.weekday(), \# 0 is Monday
698 \var{d}.toordinal() - date(\var{d}.year, 1, 1).toordinal() + 1,
699 \# day of year
700 -1)}
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000701\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000702
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000703\begin{methoddesc}{toordinal}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000704 Return the proleptic Gregorian ordinal of the date. The same as
705 \method{date.toordinal()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000706\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000707
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000708\begin{methoddesc}{weekday}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000709 Return the day of the week as an integer, where Monday is 0 and
710 Sunday is 6. The same as \method{date.weekday()}.
711 See also \method{isoweekday()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000712\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000713
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000714\begin{methoddesc}{isoweekday}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000715 Return the day of the week as an integer, where Monday is 1 and
716 Sunday is 7. The same as \method{date.isoweekday()}.
717 See also \method{weekday()}, \method{isocalendar()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000718\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000719
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000720\begin{methoddesc}{isocalendar}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000721 Return a 3-tuple, (ISO year, ISO week number, ISO weekday). The
722 same as \method{date.isocalendar()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000723\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000724
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000725\begin{methoddesc}{isoformat}{sep='T'}
Fred Drake436eadd2002-12-31 18:13:11 +0000726 Return a string representing the date and time in ISO 8601 format,
727 YYYY-MM-DDTHH:MM:SS.mmmmmm
728 or, if self.microsecond is 0,
729 YYYY-MM-DDTHH:MM:SS
730 The optional argument \var{sep} (default \code{'T'}) is a
731 one-character separator, placed between the date and time portions
732 of the result. For example,
733 datetime(2002, 12, 4, 1, 2, 3, 4).isoformat(' ') ==
734 '2002-12-04 01:02:03.000004'
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000735\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000736
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000737\begin{methoddesc}{__str__}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000738 For a \class{datetime} instance \var{d}, \code{str(\var{d})} is
739 equivalent to \code{\var{d}.isoformat(' ')}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000740\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000741
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000742\begin{methoddesc}{ctime}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000743 Return a string representing the date, for example
744 datetime(2002, 12, 4, 20, 30, 40).ctime() == 'Wed Dec 4 20:30:40 2002'.
745 \code{d.ctime()} is equivalent to
746 \code{time.ctime(time.mktime(d.timetuple()))} on platforms where
747 the native C \cfunction{ctime()} function (which
748 \function{time.ctime()} invokes, but which
749 \method{datetime.ctime()} does not invoke) conforms to the C
750 standard.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000751\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000752
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000753\begin{methoddesc}{strftime}{format}
Fred Drake436eadd2002-12-31 18:13:11 +0000754 Return a string representing the date and time, controlled by an
755 explicit format string. See the section on \method{strftime()}
756 behavior.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000757\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000758
759
Raymond Hettinger6005a342002-12-30 20:01:24 +0000760\subsection{\class{time} Objects \label{datetime-time}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000761
Raymond Hettingercbd6cd22002-12-31 16:30:49 +0000762A \class{time} object represents an idealized time of day, independent
763of day and timezone.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000764
Fred Drake0f8e5432002-12-31 18:31:48 +0000765\begin{classdesc}{time}{hour=0, minute=0, second=0, microsecond=0}
766 All arguments are optional. They may be ints or longs, in the
767 following ranges:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000768
Raymond Hettingercbd6cd22002-12-31 16:30:49 +0000769\begin{itemize}
770 \item \code{0 <= \var{hour} < 24}
771 \item \code{0 <= \var{minute} < 60}
772 \item \code{0 <= \var{second} < 60}
773 \item \code{0 <= \var{microsecond} < 1000000}
774\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000775
Fred Drake0f8e5432002-12-31 18:31:48 +0000776 If an argument outside those ranges is given, \exception{ValueError}
777 is raised.
778\end{classdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000779
780Class attributes:
781
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000782\begin{memberdesc}{min}
Fred Drake436eadd2002-12-31 18:13:11 +0000783 The earliest representable \class{time}, \code{time(0, 0, 0, 0)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000784\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000785
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000786\begin{memberdesc}{max}
Fred Drake436eadd2002-12-31 18:13:11 +0000787 The latest representable \class{time}, \code{time(23, 59, 59, 999999)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000788\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000789
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000790\begin{memberdesc}{resolution}
Fred Drake436eadd2002-12-31 18:13:11 +0000791 The smallest possible difference between non-equal \class{time}
792 objects, \code{timedelta(microseconds=1)}, although note that
793 arithmetic on \class{time} objects is not supported.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000794\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000795
796Instance attributes (read-only):
797
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000798\begin{memberdesc}{hour}
Fred Drake436eadd2002-12-31 18:13:11 +0000799 In \code{range(24)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000800\end{memberdesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000801
Tim Petersbad8ff02002-12-30 20:52:32 +0000802\begin{memberdesc}{minute}
Fred Drake436eadd2002-12-31 18:13:11 +0000803 In \code{range(60)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000804\end{memberdesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000805
Tim Petersbad8ff02002-12-30 20:52:32 +0000806\begin{memberdesc}{second}
Fred Drake436eadd2002-12-31 18:13:11 +0000807 In \code{range(60)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000808\end{memberdesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000809
Tim Petersbad8ff02002-12-30 20:52:32 +0000810\begin{memberdesc}{microsecond}
Fred Drake436eadd2002-12-31 18:13:11 +0000811 In \code{range(1000000)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000812\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000813
814Supported operations:
815
Fred Drakebbdb2502002-12-23 18:58:06 +0000816\begin{itemize}
817 \item
818 comparison of time to time, where time1 is considered
819 less than time2 when time1 precedes time2 in time.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000820
Fred Drakebbdb2502002-12-23 18:58:06 +0000821 \item
822 hash, use as dict key
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000823
Fred Drakebbdb2502002-12-23 18:58:06 +0000824 \item
825 efficient pickling
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000826
Fred Drakebbdb2502002-12-23 18:58:06 +0000827 \item
828 in Boolean contexts, a time object is considered to be true
Fred Drakea37e5cc2002-12-30 21:26:42 +0000829 if and only if it isn't equal to \code{time(0)}
Fred Drakebbdb2502002-12-23 18:58:06 +0000830\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000831
832Instance methods:
833
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000834\begin{methoddesc}{replace}{hour=, minute=, second=, microsecond=}
Fred Drake436eadd2002-12-31 18:13:11 +0000835 Return a time with the same value, except for those fields given
836 new values by whichever keyword arguments are specified.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000837\end{methoddesc}
Tim Peters12bf3392002-12-24 05:41:27 +0000838
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000839\begin{methoddesc}{isoformat}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000840 Return a string representing the time in ISO 8601 format,
841 HH:MM:SS.mmmmmm
842 or, if self.microsecond is 0
843 HH:MM:SS
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000844\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000845
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000846\begin{methoddesc}{__str__}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000847 For a time \var{t}, \code{str(\var{t})} is equivalent to
848 \code{\var{t}.isoformat()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000849\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000850
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000851\begin{methoddesc}{strftime}{format}
Fred Drake436eadd2002-12-31 18:13:11 +0000852 Return a string representing the time, controlled by an explicit
853 format string. See the section on \method{strftime()} behavior.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000854\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000855
856
Raymond Hettinger6005a342002-12-30 20:01:24 +0000857\subsection{\class{tzinfo} Objects \label{datetime-tzinfo}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000858
Fred Drakebbdb2502002-12-23 18:58:06 +0000859\class{tzinfo} is an abstract base clase, meaning that this class
860should not be instantiated directly. You need to derive a concrete
861subclass, and (at least) supply implementations of the standard
862\class{tzinfo} methods needed by the \class{datetime} methods you
Tim Petersbad8ff02002-12-30 20:52:32 +0000863use. The \module{datetime} module does not supply any concrete
Fred Drakebbdb2502002-12-23 18:58:06 +0000864subclasses of \class{tzinfo}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000865
Fred Drakebbdb2502002-12-23 18:58:06 +0000866An instance of (a concrete subclass of) \class{tzinfo} can be passed
867to the constructors for \class{datetimetz} and \class{timetz} objects.
868The latter objects view their fields as being in local time, and the
869\class{tzinfo} object supports methods revealing offset of local time
870from UTC, the name of the time zone, and DST offset, all relative to a
871date or time object passed to them.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000872
Tim Petersbad8ff02002-12-30 20:52:32 +0000873Special requirement for pickling: A \class{tzinfo} subclass must have an
Tim Peters2483b612002-12-24 16:30:58 +0000874\method{__init__} method that can be called with no arguments, else it
875can be pickled but possibly not unpickled again. This is a technical
876requirement that may be relaxed in the future.
877
Fred Drakebbdb2502002-12-23 18:58:06 +0000878A concrete subclass of \class{tzinfo} may need to implement the
879following methods. Exactly which methods are needed depends on the
Tim Petersbad8ff02002-12-30 20:52:32 +0000880uses made of aware \module{datetime} objects. If in doubt, simply
881implement all of them.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000882
Tim Petersbad8ff02002-12-30 20:52:32 +0000883\begin{methoddesc}{utcoffset}{self, dt}
Fred Drake436eadd2002-12-31 18:13:11 +0000884 Return offset of local time from UTC, in minutes east of UTC. If
885 local time is west of UTC, this should be negative. Note that this
886 is intended to be the total offset from UTC; for example, if a
887 \class{tzinfo} object represents both time zone and DST adjustments,
888 \method{utcoffset()} should return their sum. If the UTC offset
889 isn't known, return \code{None}. Else the value returned must be
Tim Peters397301e2003-01-02 21:28:08 +0000890 a \class{timedelta} object specifying a whole number of minutes in the
891 range -1439 to 1439 inclusive (1440 = 24*60; the magnitude of the offset
892 must be less than one day). Most implementations of
893 \method{utcoffset()} will probably look like one of these two:
Fred Drake436eadd2002-12-31 18:13:11 +0000894
Tim Petersbad8ff02002-12-30 20:52:32 +0000895\begin{verbatim}
Tim Petersf3615152003-01-01 21:51:37 +0000896 return CONSTANT # fixed-offset class
Fred Drake436eadd2002-12-31 18:13:11 +0000897 return CONSTANT + self.dst(dt) # daylight-aware class
Guido van Rossum8e7ec7c2002-12-31 04:39:05 +0000898\end{verbatim}
Tim Peters710fb152003-01-02 19:35:54 +0000899
900 If \method{utcoffset()} does not return \code{None},
901 \method{dst()} should not return \code{None} either.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000902\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000903
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000904
Tim Petersbad8ff02002-12-30 20:52:32 +0000905\begin{methoddesc}{dst}{self, dt}
Tim Petersf3615152003-01-01 21:51:37 +0000906 Return the daylight savings time (DST) adjustment, in minutes east of
907 UTC, or \code{None} if DST information isn't known. Return \code{0} if
908 DST is not in effect.
Tim Peters397301e2003-01-02 21:28:08 +0000909 If DST is in effect, return the offset as a
Fred Drake436eadd2002-12-31 18:13:11 +0000910 \class{timedelta} object (see \method{utcoffset()} for details).
911 Note that DST offset, if applicable, has
912 already been added to the UTC offset returned by
913 \method{utcoffset()}, so there's no need to consult \method{dst()}
914 unless you're interested in displaying DST info separately. For
915 example, \method{datetimetz.timetuple()} calls its \member{tzinfo}
916 member's \method{dst()} method to determine how the
Tim Petersf3615152003-01-01 21:51:37 +0000917 \member{tm_isdst} flag should be set, and
918 \method{datetimetz.astimezone()} calls \method{dst()} to account for
919 DST changes when crossing time zones.
920
921 An instance \var{tz} of a \class{tzinfo} subclass that models both
922 standard and daylight times must be consistent in this sense:
923
924 \code{tz.utcoffset(dt) - tz.dst(dt)}
925
926 must return the same result for every \class{datetimetz} \var{dt}
927 in a given year with \code{dt.tzinfo==tz} For sane \class{tzinfo}
928 subclasses, this expression yields the time zone's "standard offset"
929 within the year, which should be the same across all days in the year.
930 The implementation of \method{datetimetz.astimezone()} relies on this,
931 but cannot detect violations; it's the programmer's responsibility to
932 ensure it.
933
Tim Peters710fb152003-01-02 19:35:54 +0000934\begin{methoddesc}{tzname}{self, dt}
935 Return the timezone name corresponding to the \class{datetime} represented
936 by \var{dt}, as a string. Nothing about string names is defined by the
937 \module{datetime} module, and there's no requirement that it mean anything
938 in particular. For example, "GMT", "UTC", "-500", "-5:00", "EDT",
939 "US/Eastern", "America/New York" are all valid replies. Return
940 \code{None} if a string name isn't known. Note that this is a method
941 rather than a fixed string primarily because some \class{tzinfo} objects
942 will wish to return different names depending on the specific value
943 of \var{dt} passed, especially if the \class{tzinfo} class is
944 accounting for daylight time.
945\end{methoddesc}
946
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000947\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000948
Tim Petersbad8ff02002-12-30 20:52:32 +0000949These methods are called by a \class{datetimetz} or \class{timetz} object,
950in response to their methods of the same names. A \class{datetimetz}
951object passes itself as the argument, and a \class{timetz} object passes
952\code{None} as the argument. A \class{tzinfo} subclass's methods should
953therefore be prepared to accept a \var{dt} argument of \code{None}, or of
954class \class{datetimetz}.
955
956When \code{None} is passed, it's up to the class designer to decide the
957best response. For example, returning \code{None} is appropriate if the
958class wishes to say that timetz objects don't participate in the
959\class{tzinfo} protocol. In other applications, it may be more useful
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000960for \code{utcoffset(None)} to return the standard UTC offset.
Tim Petersbad8ff02002-12-30 20:52:32 +0000961
962When a \class{datetimetz} object is passed in response to a
963\class{datetimetz} method, \code{dt.tzinfo} is the same object as
964\var{self}. \class{tzinfo} methods can rely on this, unless
965user code calls \class{tzinfo} methods directly. The intent is that
966the \class{tzinfo} methods interpret \var{dt} as being in local time,
967and not need to worry about objects in other timezones.
968
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000969Example \class{tzinfo} classes:
970
Fred Drakebbdb2502002-12-23 18:58:06 +0000971\verbatiminput{tzinfo-examples.py}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000972
973
Raymond Hettinger6005a342002-12-30 20:01:24 +0000974\subsection{\class{timetz} Objects \label{datetime-timetz}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000975
976A time object represents a (local) time of day, independent of any
977particular day, and subject to adjustment via a \class{tzinfo} object.
978
979Constructor:
980
Fred Drake0f8e5432002-12-31 18:31:48 +0000981\begin{classdesc}{time}{hour=0, minute=0, second=0, microsecond=0,
982 tzinfo=None}
Fred Drake436eadd2002-12-31 18:13:11 +0000983 All arguments are optional. \var{tzinfo} may be \code{None}, or
984 an instance of a \class{tzinfo} subclass. The remaining arguments
985 may be ints or longs, in the following ranges:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000986
Fred Drake0f8e5432002-12-31 18:31:48 +0000987 \begin{itemize}
988 \item \code{0 <= \var{hour} < 24}
989 \item \code{0 <= \var{minute} < 60}
990 \item \code{0 <= \var{second} < 60}
991 \item \code{0 <= \var{microsecond} < 1000000}.
992 \end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000993
Fred Drake436eadd2002-12-31 18:13:11 +0000994 If an argument outside those ranges is given,
995 \exception{ValueError} is raised.
Fred Drake0f8e5432002-12-31 18:31:48 +0000996\end{classdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000997
998Class attributes:
999
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001000\begin{memberdesc}{min}
Fred Drake436eadd2002-12-31 18:13:11 +00001001 The earliest representable time, \code{timetz(0, 0, 0, 0)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001002\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001003
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001004\begin{memberdesc}{max}
Fred Drake436eadd2002-12-31 18:13:11 +00001005 The latest representable time, \code{timetz(23, 59, 59, 999999)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001006\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001007
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001008\begin{memberdesc}{resolution}
Fred Drake436eadd2002-12-31 18:13:11 +00001009 The smallest possible difference between non-equal \class{timetz}
1010 objects, \code{timedelta(microseconds=1)}, although note that
1011 arithmetic on \class{timetz} objects is not supported.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001012\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001013
1014Instance attributes (read-only):
1015
Fred Drake436eadd2002-12-31 18:13:11 +00001016\begin{memberdesc}{hour}
1017 In \code{range(24)}.
1018\end{memberdesc}
1019
1020\begin{memberdesc}{minute}
1021 In \code{range(60)}.
1022\end{memberdesc}
1023
1024\begin{memberdesc}{second}
1025 In \code{range(60)}.
1026\end{memberdesc}
1027
1028\begin{memberdesc}{microsecond}
1029 In \code{range(1000000)}.
1030\end{memberdesc}
1031
1032\begin{memberdesc}{tzinfo}
1033 The object passed as the tzinfo argument to the \class{timetz}
1034 constructor, or \code{None} if none was passed.
1035\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001036
1037Supported operations:
1038
Fred Drakebbdb2502002-12-23 18:58:06 +00001039\begin{itemize}
1040 \item
Tim Peters60c76e42002-12-27 00:41:11 +00001041 comparison of \class{timetz} to \class{time} or \class{timetz},
1042 where \var{a} is considered less than \var{b} when \var{a} precedes
Fred Drakea37e5cc2002-12-30 21:26:42 +00001043 \var{b} in time. If one comparand is naive and the other is aware,
Tim Peters60c76e42002-12-27 00:41:11 +00001044 \exception{TypeError} is raised. If both comparands are aware, and
1045 have the same \member{tzinfo} member, the common \member{tzinfo}
1046 member is ignored and the base times are compared. If both
1047 comparands are aware and have different \member{tzinfo} members,
1048 the comparands are first adjusted by subtracting their UTC offsets
1049 (obtained from \code{self.utcoffset()}).
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001050
Fred Drakebbdb2502002-12-23 18:58:06 +00001051 \item
1052 hash, use as dict key
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001053
Fred Drakebbdb2502002-12-23 18:58:06 +00001054 \item
1055 pickling
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001056
Fred Drakebbdb2502002-12-23 18:58:06 +00001057 \item
1058 in Boolean contexts, a \class{timetz} object is considered to be
1059 true if and only if, after converting it to minutes and
1060 subtracting \method{utcoffset()} (or \code{0} if that's
1061 \code{None}), the result is non-zero.
1062\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001063
1064Instance methods:
1065
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001066\begin{methoddesc}{replace}(hour=, minute=, second=, microsecond=, tzinfo=)
Fred Drake436eadd2002-12-31 18:13:11 +00001067 Return a \class{timetz} with the same value, except for those fields given
1068 new values by whichever keyword arguments are specified. Note that
1069 \code{tzinfo=None} can be specified to create a naive \class{timetz} from an
1070 aware \class{timetz}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001071\end{methoddesc}
Tim Peters12bf3392002-12-24 05:41:27 +00001072
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001073\begin{methoddesc}{isoformat}{}
Fred Drake436eadd2002-12-31 18:13:11 +00001074 Return a string representing the time in ISO 8601 format,
1075 HH:MM:SS.mmmmmm
1076 or, if self.microsecond is 0,
1077 HH:MM:SS
1078 If \method{utcoffset()} does not return \code{None}, a 6-character
1079 string is appended, giving the UTC offset in (signed) hours and
1080 minutes:
1081 HH:MM:SS.mmmmmm+HH:MM
1082 or, if self.microsecond is 0,
1083 HH:MM:SS+HH:MM
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001084\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001085
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001086\begin{methoddesc}{__str__}{}
Fred Drake436eadd2002-12-31 18:13:11 +00001087 For a \class{timetz} \var{t}, \code{str(\var{t})} is equivalent to
1088 \code{\var{t}.isoformat()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001089\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001090
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001091\begin{methoddesc}{strftime}{format}
Fred Drake436eadd2002-12-31 18:13:11 +00001092 Return a string representing the time, controlled by an explicit
1093 format string. See the section on \method{strftime()} behavior.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001094\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001095
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001096\begin{methoddesc}{utcoffset}{}
Fred Drake436eadd2002-12-31 18:13:11 +00001097 If \member{tzinfo} is \code{None}, returns \code{None}, else
1098 \code{tzinfo.utcoffset(self)} converted to a \class{timedelta}
1099 object.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001100\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001101
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001102\begin{methoddesc}{tzname}{}
Fred Drake436eadd2002-12-31 18:13:11 +00001103 If \member{tzinfo} is \code{None}, returns \code{None}, else
1104 \code{tzinfo.tzname(self)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001105\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001106
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001107\begin{methoddesc}{dst}{}
Fred Drake436eadd2002-12-31 18:13:11 +00001108 If \member{tzinfo} is \code{None}, returns \code{None}, else
1109 \code{tzinfo.dst(self)} converted to a \class{timedelta} object.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001110\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001111
1112
1113
Raymond Hettinger6005a342002-12-30 20:01:24 +00001114\subsection{ \class{datetimetz} Objects \label{datetime-datetimetz}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001115
Fred Drakebbdb2502002-12-23 18:58:06 +00001116\begin{notice}[warning]
1117 I think this is \emph{still} missing some methods from the
1118 Python implementation.
1119\end{notice}
1120
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001121A \class{datetimetz} object is a single object containing all the information
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001122from a \class{date} object and a \class{timetz} object.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001123
1124Constructor:
1125
Fred Drake0f8e5432002-12-31 18:31:48 +00001126\begin{classdesc}{datetimetz}{year, month, day,
1127 hour=0, minute=0, second=0, microsecond=0,
1128 tzinfo=None}
Fred Drake436eadd2002-12-31 18:13:11 +00001129 The year, month and day arguments are required. \var{tzinfo} may
1130 be \code{None}, or an instance of a \class{tzinfo} subclass. The
1131 remaining arguments may be ints or longs, in the following ranges:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001132
Fred Drake436eadd2002-12-31 18:13:11 +00001133 \begin{itemize}
1134 \item \code{MINYEAR <= \var{year} <= MAXYEAR}
1135 \item \code{1 <= \var{month} <= 12}
1136 \item \code{1 <= \var{day} <= number of days in the given month and year}
1137 \item \code{0 <= \var{hour} < 24}
1138 \item \code{0 <= \var{minute} < 60}
1139 \item \code{0 <= \var{second} < 60}
1140 \item \code{0 <= \var{microsecond} < 1000000}
1141 \end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001142
Fred Drake436eadd2002-12-31 18:13:11 +00001143 If an argument outside those ranges is given,
1144 \exception{ValueError} is raised.
Fred Drake0f8e5432002-12-31 18:31:48 +00001145\end{classdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001146
1147Other constructors (class methods):
1148
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001149\begin{funcdesc}{today}{}
Fred Drake436eadd2002-12-31 18:13:11 +00001150\methodline{utcnow}{}
1151\methodline{utcfromtimestamp}{timestamp}
1152\methodline{fromordinal}{ordinal}
1153 These are the same as the \class{datetime} class methods of the
1154 same names, except that they construct a \class{datetimetz}
1155 object, with tzinfo \code{None}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001156\end{funcdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001157
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001158\begin{funcdesc}{now}{\optional{tzinfo=None}}
Fred Drake436eadd2002-12-31 18:13:11 +00001159\methodline{fromtimestamp}{timestamp\optional{, tzinfo=None}}
1160 These are the same as the \class{datetime} class methods of the
1161 same names, except that they accept an additional, optional tzinfo
1162 argument, and construct a \class{datetimetz} object with that
1163 \class{tzinfo} object attached.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001164\end{funcdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001165
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001166\begin{funcdesc}{combine}{date, time}
Fred Drake436eadd2002-12-31 18:13:11 +00001167 This is the same as \method{datetime.combine()}, except that it
1168 constructs a \class{datetimetz} object, and, if the time object is
1169 of type timetz, the \class{datetimetz} object has the same
1170 \class{tzinfo} object as the time object.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001171\end{funcdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001172
1173Class attributes:
1174
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001175\begin{memberdesc}{min}
Fred Drake436eadd2002-12-31 18:13:11 +00001176 The earliest representable \class{datetimetz},
1177 \code{datetimetz(MINYEAR, 1, 1)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001178\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001179
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001180\begin{memberdesc}{max}
Fred Drake436eadd2002-12-31 18:13:11 +00001181 The latest representable \class{datetime},
1182 \code{datetimetz(MAXYEAR, 12, 31, 23, 59, 59, 999999)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001183\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001184
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001185\begin{memberdesc}{resolution}
Fred Drake436eadd2002-12-31 18:13:11 +00001186 The smallest possible difference between non-equal \class{datetimetz}
1187 objects, \code{timedelta(microseconds=1)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001188\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001189
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001190Instance attributes, all read-only:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001191
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001192\begin{memberdesc}{year}
Fred Drake436eadd2002-12-31 18:13:11 +00001193 Between \constant{MINYEAR} and \constant{MAXYEAR}, inclusive.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001194\end{memberdesc}
Fred Drake436eadd2002-12-31 18:13:11 +00001195
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001196\begin{memberdesc}{month}
Fred Drake436eadd2002-12-31 18:13:11 +00001197 Between 1 and 12 inclusive
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001198\end{memberdesc}
Fred Drake436eadd2002-12-31 18:13:11 +00001199
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001200\begin{memberdesc}{day}
Fred Drake436eadd2002-12-31 18:13:11 +00001201 Between 1 and the number of days in the given month of the given
1202 year.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001203\end{memberdesc}
Fred Drake436eadd2002-12-31 18:13:11 +00001204
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001205\begin{memberdesc}{hour}
Fred Drake436eadd2002-12-31 18:13:11 +00001206 In \code{range(24)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001207\end{memberdesc}
Fred Drake436eadd2002-12-31 18:13:11 +00001208
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001209\begin{memberdesc}{minute}
Fred Drake436eadd2002-12-31 18:13:11 +00001210 In \code{range(60)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001211\end{memberdesc}
Fred Drake436eadd2002-12-31 18:13:11 +00001212
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001213\begin{memberdesc}{second}
Fred Drake436eadd2002-12-31 18:13:11 +00001214 In \code{range(60)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001215\end{memberdesc}
Fred Drake436eadd2002-12-31 18:13:11 +00001216
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001217\begin{memberdesc}{microsecond}
Fred Drake436eadd2002-12-31 18:13:11 +00001218 In \code{range(1000000)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001219\end{memberdesc}
Fred Drake436eadd2002-12-31 18:13:11 +00001220
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001221\begin{memberdesc}{tzinfo}
Fred Drake436eadd2002-12-31 18:13:11 +00001222 The object passed as the \var{tzinfo} argument to the
1223 \class{datetimetz} constructor, or \code{None} if none was passed.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001224\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001225
1226Supported operations:
1227
Fred Drakebbdb2502002-12-23 18:58:06 +00001228\begin{itemize}
1229 \item
1230 datetimetz1 + timedelta -> datetimetz2
1231 timedelta + datetimetz1 -> datetimetz2
Tim Peters60c76e42002-12-27 00:41:11 +00001232
Fred Drakebbdb2502002-12-23 18:58:06 +00001233 The same as addition of \class{datetime} objects, except that
1234 datetimetz2.tzinfo is set to datetimetz1.tzinfo.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001235
Fred Drakebbdb2502002-12-23 18:58:06 +00001236 \item
1237 datetimetz1 - timedelta -> datetimetz2
Tim Peters60c76e42002-12-27 00:41:11 +00001238
Fred Drakebbdb2502002-12-23 18:58:06 +00001239 The same as addition of \class{datetime} objects, except that
1240 datetimetz2.tzinfo is set to datetimetz1.tzinfo.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001241
Fred Drakebbdb2502002-12-23 18:58:06 +00001242 \item
1243 aware_datetimetz1 - aware_datetimetz2 -> timedelta
Fred Drakea37e5cc2002-12-30 21:26:42 +00001244 naive_datetimetz1 - naive_datetimetz2 -> timedelta
1245 naive_datetimetz1 - datetime2 -> timedelta
1246 datetime1 - naive_datetimetz2 -> timedelta
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001247
Tim Peters60c76e42002-12-27 00:41:11 +00001248 Subtraction of a \class{datetime} or \class{datetimetz}, from a
Fred Drakebbdb2502002-12-23 18:58:06 +00001249 \class{datetime} or \class{datetimetz}, is defined only if both
Fred Drakea37e5cc2002-12-30 21:26:42 +00001250 operands are naive, or if both are aware. If one is aware and the
1251 other is naive, \exception{TypeError} is raised.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001252
Fred Drakea37e5cc2002-12-30 21:26:42 +00001253 If both are naive, or both are aware and have the same \member{tzinfo}
Tim Peters60c76e42002-12-27 00:41:11 +00001254 member, subtraction acts as for \class{datetime} subtraction.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001255
Tim Peters60c76e42002-12-27 00:41:11 +00001256 If both are aware and have different \member{tzinfo} members,
1257 \code{a-b} acts as if \var{a} and \var{b} were first converted to UTC
1258 datetimes (by subtracting \code{a.utcoffset()} minutes from \var{a},
1259 and \code{b.utcoffset()} minutes from \var{b}), and then doing
Fred Drakebbdb2502002-12-23 18:58:06 +00001260 \class{datetime} subtraction, except that the implementation never
1261 overflows.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001262
Fred Drakebbdb2502002-12-23 18:58:06 +00001263 \item
Tim Peters60c76e42002-12-27 00:41:11 +00001264 comparison of \class{datetimetz} to \class{datetime} or
1265 \class{datetimetz}, where \var{a} is considered less than \var{b}
1266 when \var{a} precedes \var{b} in time. If one comparand is naive and
1267 the other is aware, \exception{TypeError} is raised. If both
1268 comparands are aware, and have the same \member{tzinfo} member,
1269 the common \member{tzinfo} member is ignored and the base datetimes
1270 are compared. If both comparands are aware and have different
1271 \member{tzinfo} members, the comparands are first adjusted by
1272 subtracting their UTC offsets (obtained from \code{self.utcoffset()}).
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001273
Fred Drakebbdb2502002-12-23 18:58:06 +00001274 \item
1275 hash, use as dict key
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001276
Fred Drakebbdb2502002-12-23 18:58:06 +00001277 \item
1278 efficient pickling
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001279
Fred Drakebbdb2502002-12-23 18:58:06 +00001280 \item
1281 in Boolean contexts, all \class{datetimetz} objects are considered to be
1282 true
1283\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001284
1285Instance methods:
1286
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001287\begin{methoddesc}{date}{}
Fred Drake436eadd2002-12-31 18:13:11 +00001288\methodline{time}{}
1289\methodline{toordinal}{}
1290\methodline{weekday}{}
1291\methodline{isoweekday}{}
1292\methodline{isocalendar}{}
1293\methodline{ctime}{}
1294\methodline{__str__}{}
1295\methodline{strftime}{format}
1296 These are the same as the \class{datetime} methods of the same names.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001297\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001298
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001299\begin{methoddesc}{timetz}{}
Fred Drake436eadd2002-12-31 18:13:11 +00001300 Return \class{timetz} object with same hour, minute, second, microsecond,
1301 and tzinfo.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001302\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001303
Fred Drake436eadd2002-12-31 18:13:11 +00001304\begin{methoddesc}{replace}{year=, month=, day=, hour=, minute=, second=,
1305 microsecond=, tzinfo=}
1306 Return a datetimetz with the same value, except for those fields given
1307 new values by whichever keyword arguments are specified. Note that
1308 \code{tzinfo=None} can be specified to create a naive datetimetz from
1309 an aware datetimetz.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001310\end{methoddesc}
Tim Peters12bf3392002-12-24 05:41:27 +00001311
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001312\begin{methoddesc}{astimezone}{tz}
Fred Drake436eadd2002-12-31 18:13:11 +00001313 Return a \class{datetimetz} with new tzinfo member \var{tz}. \var{tz}
1314 must be \code{None}, or an instance of a \class{tzinfo} subclass. If
1315 \var{tz} is \code{None}, self is naive, or
1316 \code{tz.utcoffset(self)} returns \code{None},
1317 \code{self.astimezone(tz)} is equivalent to
1318 \code{self.replace(tzinfo=tz)}: a new timezone object is attached
1319 without any conversion of date or time fields. If self is aware and
1320 \code{tz.utcoffset(self)} does not return \code{None}, the date and
1321 time fields are adjusted so that the result is local time in timezone
1322 tz, representing the same UTC time as self.
1323 XXX [The treatment of endcases remains unclear: for DST-aware
1324 classes, one hour per year has two spellings in local time, and
1325 another hour has no spelling in local time.] XXX
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001326\end{methoddesc}
Tim Peters80475bb2002-12-25 07:40:55 +00001327
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001328\begin{methoddesc}{utcoffset}{}
Fred Drake436eadd2002-12-31 18:13:11 +00001329 If \member{tzinfo} is \code{None}, returns \code{None}, else
1330 \code{tzinfo.utcoffset(self)} converted to a \class{timedelta}
1331 object.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001332\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001333
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001334\begin{methoddesc}{tzname}{}
Fred Drake436eadd2002-12-31 18:13:11 +00001335 If \member{tzinfo} is \code{None}, returns \code{None}, else
1336 returns \code{tzinfo.tzname(self)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001337\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001338
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001339\begin{methoddesc}{dst}{}
Fred Drake436eadd2002-12-31 18:13:11 +00001340 If \member{tzinfo} is \code{None}, returns \code{None}, else
1341 \code{tzinfo.dst(self)} converted to a \class{timedelta}
1342 object.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001343\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001344
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001345\begin{methoddesc}{timetuple}{}
Fred Drake436eadd2002-12-31 18:13:11 +00001346 Like \function{datetime.timetuple()}, but sets the
1347 \member{tm_isdst} flag according to the \method{dst()} method: if
1348 \method{dst()} returns \code{None}, \member{tm_isdst} is set to
1349 \code{-1}; else if \method{dst()} returns a non-zero value,
1350 \member{tm_isdst} is set to \code{1}; else \code{tm_isdst} is set
1351 to \code{0}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001352\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001353
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001354\begin{methoddesc}{utctimetuple}{}
Fred Drake436eadd2002-12-31 18:13:11 +00001355 If \class{datetimetz} instance \var{d} is naive, this is the same as
1356 \code{\var{d}.timetuple()} except that \member{tm_isdst} is forced to 0
1357 regardless of what \code{d.dst()} returns. DST is never in effect
1358 for a UTC time.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001359
Fred Drake436eadd2002-12-31 18:13:11 +00001360 If \var{d} is aware, \var{d} is normalized to UTC time, by subtracting
1361 \code{\var{d}.utcoffset()} minutes, and a timetuple for the
1362 normalized time is returned. \member{tm_isdst} is forced to 0.
1363 Note that the result's \member{tm_year} field may be
1364 \constant{MINYEAR}-1 or \constant{MAXYEAR}+1, if \var{d}.year was
1365 \code{MINYEAR} or \code{MAXYEAR} and UTC adjustment spills over a
1366 year boundary.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001367\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001368
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001369\begin{methoddesc}{isoformat}{sep='T'}
Fred Drake436eadd2002-12-31 18:13:11 +00001370 Return a string representing the date and time in ISO 8601 format,
1371 YYYY-MM-DDTHH:MM:SS.mmmmmm
1372 or, if \member{microsecond} is 0,
1373 YYYY-MM-DDTHH:MM:SS
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001374
Fred Drake436eadd2002-12-31 18:13:11 +00001375 If \method{utcoffset()} does not return \code{None}, a 6-character
1376 string is appended, giving the UTC offset in (signed) hours and
1377 minutes:
1378 YYYY-MM-DDTHH:MM:SS.mmmmmm+HH:MM
1379 or, if \member{microsecond} is 0
1380 YYYY-MM-DDTHH:MM:SS+HH:MM
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001381
Fred Drake436eadd2002-12-31 18:13:11 +00001382 The optional argument \var{sep} (default \code{'T'}) is a
1383 one-character separator, placed between the date and time portions
1384 of the result. For example,
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001385
Fred Drakebbdb2502002-12-23 18:58:06 +00001386\begin{verbatim}
1387>>> from datetime import *
1388>>> class TZ(tzinfo):
Tim Peters710fb152003-01-02 19:35:54 +00001389... def utcoffset(self, dt): return timedelta(minutes=-399)
Fred Drakebbdb2502002-12-23 18:58:06 +00001390...
1391>>> datetimetz(2002, 12, 25, tzinfo=TZ()).isoformat(' ')
1392'2002-12-25 00:00:00-06:39'
1393\end{verbatim}
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001394\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001395
Fred Drakebbdb2502002-12-23 18:58:06 +00001396\code{str(\var{d})} is equivalent to \code{\var{d}.isoformat(' ')}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001397
1398
Tim Peters29fb9c72002-12-23 22:21:52 +00001399\subsection{\method{strftime()} Behavior}
1400
1401\class{date}, \class{datetime}, \class{datetimetz}, \class{time},
1402and \class{timetz} objects all support a \code{strftime(\var{format})}
1403method, to create a string representing the time under the control of
1404an explicit format string. Broadly speaking,
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001405\code{d.strftime(fmt)}
Tim Peters29fb9c72002-12-23 22:21:52 +00001406acts like the \refmodule{time} module's
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001407\code{time.strftime(fmt, d.timetuple())}
Tim Peters29fb9c72002-12-23 22:21:52 +00001408although not all objects support a \method{timetuple()} method.
1409
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001410For \class{time} and \class{timetz} objects, the format codes for
1411year, month, and day should not be used, as time objects have no such
1412values. If they're used anyway, \code{1900} is substituted for the
1413year, and \code{0} for the month and day.
Tim Peters29fb9c72002-12-23 22:21:52 +00001414
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001415For \class{date} objects, the format codes for hours, minutes, and
1416seconds should not be used, as \class{date} objects have no such
1417values. If they're used anyway, \code{0} is substituted for them.
Tim Peters29fb9c72002-12-23 22:21:52 +00001418
Fred Drakea37e5cc2002-12-30 21:26:42 +00001419For a naive object, the \code{\%z} and \code{\%Z} format codes are
Tim Peters29fb9c72002-12-23 22:21:52 +00001420replaced by empty strings.
1421
1422For an aware object:
1423
1424\begin{itemize}
1425 \item[\code{\%z}]
1426 \method{utcoffset()} is transformed into a 5-character string of
1427 the form +HHMM or -HHMM, where HH is a 2-digit string giving the
1428 number of UTC offset hours, and MM is a 2-digit string giving the
1429 number of UTC offset minutes. For example, if
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001430 \method{utcoffset()} returns \code{timedelta(hours=-3, minutes=-30)},
Tim Peters1cff9fc2002-12-24 16:25:29 +00001431 \code{\%z} is replaced with the string \code{'-0330'}.
Tim Peters29fb9c72002-12-23 22:21:52 +00001432
1433 \item[\code{\%Z}]
1434 If \method{tzname()} returns \code{None}, \code{\%Z} is replaced
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001435 by an empty string. Otherwise \code{\%Z} is replaced by the returned
Tim Peters29fb9c72002-12-23 22:21:52 +00001436 value, which must be a string.
1437\end{itemize}
1438
1439The full set of format codes supported varies across platforms,
1440because Python calls the platform C library's \function{strftime()}
1441function, and platform variations are common. The documentation for
1442Python's \refmodule{time} module lists the format codes that the C
1443standard (1989 version) requires, and those work on all platforms
1444with a standard C implementation. Note that the 1999 version of the
1445C standard added additional format codes.
1446
1447The exact range of years for which \method{strftime()} works also
1448varies across platforms. Regardless of platform, years before 1900
1449cannot be used.
1450
1451
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001452\begin{comment}
1453
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001454\subsection{C API}
1455
1456Struct typedefs:
1457
1458 PyDateTime_Date
1459 PyDateTime_DateTime
1460 PyDateTime_DateTimeTZ
1461 PyDateTime_Time
1462 PyDateTime_TimeTZ
1463 PyDateTime_Delta
1464 PyDateTime_TZInfo
1465
1466Type-check macros:
1467
1468 PyDate_Check(op)
1469 PyDate_CheckExact(op)
1470
1471 PyDateTime_Check(op)
1472 PyDateTime_CheckExact(op)
1473
1474 PyDateTimeTZ_Check(op)
1475 PyDateTimeTZ_CheckExact(op)
1476
1477 PyTime_Check(op)
1478 PyTime_CheckExact(op)
1479
1480 PyTimeTZ_Check(op)
1481 PyTimeTZ_CheckExact(op)
1482
1483 PyDelta_Check(op)
1484 PyDelta_CheckExact(op)
1485
1486 PyTZInfo_Check(op)
Raymond Hettingercbd6cd22002-12-31 16:30:49 +00001487 PyTZInfo_CheckExact(op)
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001488
1489Accessor macros:
1490
1491All objects are immutable, so accessors are read-only. All macros
1492return ints:
1493
Tim Peters1cff9fc2002-12-24 16:25:29 +00001494 For \class{date}, \class{datetime}, and \class{datetimetz} instances:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001495 PyDateTime_GET_YEAR(o)
1496 PyDateTime_GET_MONTH(o)
1497 PyDateTime_GET_DAY(o)
1498
1499 For \class{datetime} and \class{datetimetz} instances:
1500 PyDateTime_DATE_GET_HOUR(o)
1501 PyDateTime_DATE_GET_MINUTE(o)
1502 PyDateTime_DATE_GET_SECOND(o)
1503 PyDateTime_DATE_GET_MICROSECOND(o)
1504
Tim Peters1cff9fc2002-12-24 16:25:29 +00001505 For \class{time} and \class{timetz} instances:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001506 PyDateTime_TIME_GET_HOUR(o)
1507 PyDateTime_TIME_GET_MINUTE(o)
1508 PyDateTime_TIME_GET_SECOND(o)
1509 PyDateTime_TIME_GET_MICROSECOND(o)
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001510
1511\end{comment}