blob: 8878e3bee0544369934b09e1d139941951f0841a [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
Tim Peters75a6e3b2003-01-04 18:17:36 +0000317 through 2038. Note that on non-POSIX systems that include leap
318 seconds in their notion of a timestamp, leap seconds are ignored by
319 \method{fromtimestamp()}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000320
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000321\begin{methoddesc}{fromordinal}{ordinal}
Fred Drake436eadd2002-12-31 18:13:11 +0000322 Return the date corresponding to the proleptic Gregorian ordinal,
323 where January 1 of year 1 has ordinal 1. \exception{ValueError} is
324 raised unless \code{1 <= \var{ordinal} <= date.max.toordinal()}.
325 For any date \var{d}, \code{date.fromordinal(\var{d}.toordinal()) ==
326 \var{d}}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000327\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000328
329Class attributes:
330
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000331\begin{memberdesc}{min}
Fred Drake436eadd2002-12-31 18:13:11 +0000332 The earliest representable date, \code{date(MINYEAR, 1, 1)}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000333\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000334
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000335\begin{memberdesc}{max}
Fred Drake436eadd2002-12-31 18:13:11 +0000336 The latest representable date, \code{date(MAXYEAR, 12, 31)}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000337\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000338
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000339\begin{memberdesc}{resolution}
Fred Drake436eadd2002-12-31 18:13:11 +0000340 The smallest possible difference between non-equal date
341 objects, \code{timedelta(days=1)}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000342\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000343
344Instance attributes (read-only):
345
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000346\begin{memberdesc}{year}
Fred Drake436eadd2002-12-31 18:13:11 +0000347 Between \constant{MINYEAR} and \constant{MAXYEAR} inclusive
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000348\end{memberdesc}
349
350\begin{memberdesc}{month}
Fred Drake436eadd2002-12-31 18:13:11 +0000351 Between 1 and 12 inclusive.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000352\end{memberdesc}
353
354\begin{memberdesc}{day}
Fred Drake436eadd2002-12-31 18:13:11 +0000355 Between 1 and the number of days in the given month of the given
356 year.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000357\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000358
359Supported operations:
360
Fred Drakebbdb2502002-12-23 18:58:06 +0000361\begin{itemize}
362 \item
363 date1 + timedelta -> date2
364 timedelta + date1 -> date2
365 date2 is timedelta.days days removed from the date1, moving forward
366 in time if timedelta.days > 0, or backward if timedetla.days < 0.
367 date2 - date1 == timedelta.days after. timedelta.seconds and
368 timedelta.microseconds are ignored. \exception{OverflowError} is
369 raised if date2.year would be smaller than \constant{MINYEAR} or
370 larger than \constant{MAXYEAR}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000371
Fred Drakebbdb2502002-12-23 18:58:06 +0000372 \item
373 date1 - timedelta -> date2
374 Computes the date2 such that date2 + timedelta == date1. This
375 isn't quite equivalent to date1 + (-timedelta), because -timedelta
376 in isolation can overflow in cases where date1 - timedelta does
377 not. timedelta.seconds and timedelta.microseconds are ignored.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000378
Fred Drakebbdb2502002-12-23 18:58:06 +0000379 \item
380 date1 - date2 -> timedelta
381 This is exact, and cannot overflow. timedelta.seconds and
382 timedelta.microseconds are 0, and date2 + timedelta == date1
383 after.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000384
Fred Drakebbdb2502002-12-23 18:58:06 +0000385 \item
386 comparison of date to date, where date1 is considered less than
387 date2 when date1 precedes date2 in time. In other words,
388 date1 < date2 if and only if date1.toordinal() < date2.toordinal().
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000389
Fred Drakebbdb2502002-12-23 18:58:06 +0000390 \item
391 hash, use as dict key
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000392
Fred Drakebbdb2502002-12-23 18:58:06 +0000393 \item
394 efficient pickling
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000395
Fred Drakebbdb2502002-12-23 18:58:06 +0000396 \item
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000397 in Boolean contexts, all \class{date} objects are considered to be true
Fred Drakebbdb2502002-12-23 18:58:06 +0000398\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000399
400Instance methods:
401
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000402\begin{methoddesc}{replace}{year, month, day}
Fred Drake436eadd2002-12-31 18:13:11 +0000403 Return a date with the same value, except for those fields given
404 new values by whichever keyword arguments are specified. For
405 example, if \code{d == date(2002, 12, 31)}, then
406 \code{d.replace(day=26) == date(2000, 12, 26)}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000407\end{methoddesc}
Tim Peters12bf3392002-12-24 05:41:27 +0000408
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000409\begin{methoddesc}{timetuple}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000410 Return a 9-element tuple of the form returned by
411 \function{time.localtime()}. The hours, minutes and seconds are
412 0, and the DST flag is -1.
413 \code{\var{d}.timetuple()} is equivalent to
414 \code{(\var{d}.year, \var{d}.month, \var{d}.day,
415 0, 0, 0, \# h, m, s
416 \var{d}.weekday(), \# 0 is Monday
417 \var{d}.toordinal() - date(\var{d}.year, 1, 1).toordinal() + 1,
418 \# day of year
419 -1)}
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000420\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000421
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000422\begin{methoddesc}{toordinal}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000423 Return the proleptic Gregorian ordinal of the date, where January 1
424 of year 1 has ordinal 1. For any \class{date} object \var{d},
425 \code{date.fromordinal(\var{d}.toordinal()) == \var{d}}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000426\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000427
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000428\begin{methoddesc}{weekday}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000429 Return the day of the week as an integer, where Monday is 0 and
430 Sunday is 6. For example, date(2002, 12, 4).weekday() == 2, a
431 Wednesday.
432 See also \method{isoweekday()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000433\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000434
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000435\begin{methoddesc}{isoweekday}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000436 Return the day of the week as an integer, where Monday is 1 and
437 Sunday is 7. For example, date(2002, 12, 4).isoweekday() == 3, a
438 Wednesday.
439 See also \method{weekday()}, \method{isocalendar()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000440\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000441
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000442\begin{methoddesc}{isocalendar}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000443 Return a 3-tuple, (ISO year, ISO week number, ISO weekday).
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000444
Fred Drake436eadd2002-12-31 18:13:11 +0000445 The ISO calendar is a widely used variant of the Gregorian calendar.
446 See \url{http://www.phys.uu.nl/~vgent/calendar/isocalendar.htm}
447 for a good explanation.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000448
Fred Drake436eadd2002-12-31 18:13:11 +0000449 The ISO year consists of 52 or 53 full weeks, and where a week starts
450 on a Monday and ends on a Sunday. The first week of an ISO year is
451 the first (Gregorian) calendar week of a year containing a Thursday.
452 This is called week number 1, and the ISO year of that Thursday is
453 the same as its Gregorian year.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000454
Fred Drake436eadd2002-12-31 18:13:11 +0000455 For example, 2004 begins on a Thursday, so the first week of ISO
456 year 2004 begins on Monday, 29 Dec 2003 and ends on Sunday, 4 Jan
457 2004, so that
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000458
Fred Drake436eadd2002-12-31 18:13:11 +0000459 date(2003, 12, 29).isocalendar() == (2004, 1, 1)
460 date(2004, 1, 4).isocalendar() == (2004, 1, 7)
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000461\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000462
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000463\begin{methoddesc}{isoformat}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000464 Return a string representing the date in ISO 8601 format,
465 'YYYY-MM-DD'. For example,
466 date(2002, 12, 4).isoformat() == '2002-12-04'.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000467\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000468
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000469\begin{methoddesc}{__str__}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000470 For a date \var{d}, \code{str(\var{d})} is equivalent to
471 \code{\var{d}.isoformat()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000472\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000473
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000474\begin{methoddesc}{ctime}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000475 Return a string representing the date, for example
476 date(2002, 12, 4).ctime() == 'Wed Dec 4 00:00:00 2002'.
477 \code{\var{d}.ctime()} is equivalent to
478 \code{time.ctime(time.mktime(\var{d}.timetuple()))}
479 on platforms where the native C \cfunction{ctime()} function
480 (which \function{time.ctime()} invokes, but which
481 \method{date.ctime()} does not invoke) conforms to the C standard.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000482\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000483
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000484\begin{methoddesc}{strftime}{format}
Fred Drake436eadd2002-12-31 18:13:11 +0000485 Return a string representing the date, controlled by an explicit
486 format string. Format codes referring to hours, minutes or seconds
487 will see 0 values.
488 See the section on \method{strftime()} behavior.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000489\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000490
491
Raymond Hettinger6005a342002-12-30 20:01:24 +0000492\subsection{\class{datetime} Objects \label{datetime-datetime}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000493
Fred Drakebbdb2502002-12-23 18:58:06 +0000494A \class{datetime} object is a single object containing all the
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000495information from a \class{date} object and a time object. Like a
496\class{date} object, \class{datetime} assumes the current Gregorian
497calendar extended in both directions; like a time object,
498\class{datetime} assumes there are exactly 3600*24 seconds in every
499day.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000500
Fred Drake0f8e5432002-12-31 18:31:48 +0000501\begin{classdesc}{datetime}{year, month, day,
502 hour=0, minute=0, second=0, microsecond=0}
Fred Drake436eadd2002-12-31 18:13:11 +0000503 The year, month and day arguments are required. Arguments may be
504 ints or longs, in the following ranges:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000505
Fred Drake436eadd2002-12-31 18:13:11 +0000506 \begin{itemize}
507 \item \code{\member{MINYEAR} <= \var{year} <= \member{MAXYEAR}}
508 \item \code{1 <= \var{month} <= 12}
509 \item \code{1 <= \var{day} <= number of days in the given month and year}
510 \item \code{0 <= \var{hour} < 24}
511 \item \code{0 <= \var{minute} < 60}
512 \item \code{0 <= \var{second} < 60}
513 \item \code{0 <= \var{microsecond} < 1000000}
514 \end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000515
Fred Drake436eadd2002-12-31 18:13:11 +0000516 If an argument outside those ranges is given, \exception{ValueError}
517 is raised.
Fred Drake0f8e5432002-12-31 18:31:48 +0000518\end{classdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000519
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000520Other constructors, all class methods:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000521
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000522\begin{methoddesc}{today}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000523 Return the current local datetime. This is equivalent to
524 \code{datetime.fromtimestamp(time.time())}.
525 See also \method{now()}, \method{fromtimestamp()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000526\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000527
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000528\begin{methoddesc}{now}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000529 Return the current local datetime. This is like \method{today()},
530 but, if possible, supplies more precision than can be gotten from
531 going through a \function{time.time()} timestamp (for example,
532 this may be possible on platforms that supply the C
533 \cfunction{gettimeofday()} function).
534 See also \method{today()}, \method{utcnow()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000535\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000536
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000537\begin{methoddesc}{utcnow}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000538 Return the current UTC datetime. This is like \method{now()}, but
539 returns the current UTC date and time.
540 See also \method{now()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000541\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000542
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000543\begin{methoddesc}{fromtimestamp}{timestamp}
Fred Drake436eadd2002-12-31 18:13:11 +0000544 Return the local \class{datetime} corresponding to the \POSIX{}
545 timestamp, such as is returned by \function{time.time()}. This
546 may raise \exception{ValueError}, if the timestamp is out of the
547 range of values supported by the platform C
548 \cfunction{localtime()} function. It's common for this to be
549 restricted to years in 1970 through 2038.
Tim Peters75a6e3b2003-01-04 18:17:36 +0000550 Note that on non-POSIX systems that include leap seconds in their
551 notion of a timestamp, leap seconds are ignored by
552 \method{fromtimestamp()}, and then it's possible to have two timestamps
553 differing by a second that yield identical \class{datetime} objects.
554\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000555 See also \method{utcfromtimestamp()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000556\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000557
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000558\begin{methoddesc}{utcfromtimestamp}{timestamp}
Fred Drake436eadd2002-12-31 18:13:11 +0000559 Return the UTC \class{datetime} corresponding to the \POSIX{}
560 timestamp. This may raise \exception{ValueError}, if the
561 timestamp is out of the range of values supported by the platform
562 C \cfunction{gmtime()} function. It's common for this to be
563 restricted to years in 1970 through 2038.
564 See also \method{fromtimestamp()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000565\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000566
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000567\begin{methoddesc}{fromordinal}{ordinal}
Fred Drake436eadd2002-12-31 18:13:11 +0000568 Return the \class{datetime} corresponding to the proleptic
569 Gregorian ordinal, where January 1 of year 1 has ordinal 1.
570 \exception{ValueError} is raised unless 1 <= ordinal <=
571 datetime.max.toordinal(). The hour, minute, second and
572 microsecond of the result are all 0.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000573\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000574
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000575\begin{methoddesc}{combine}{date, time}
Fred Drake436eadd2002-12-31 18:13:11 +0000576 Return a new \class{datetime} object whose date components are
577 equal to the given \class{date} object's, and whose time
578 components are equal to the given time object's. For any
579 \class{datetime} object \var{d}, \code{\var{d} ==
580 datetime.combine(\var{d}.date(), \var{d}.time())}. If date is a
581 \class{datetime} or \class{datetimetz} object, its time components
582 are ignored. If date is \class{datetimetz} object, its
583 \member{tzinfo} component is also ignored. If time is a
584 \class{timetz} object, its \member{tzinfo} component is ignored.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000585\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000586
587Class attributes:
588
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000589\begin{memberdesc}{min}
Fred Drake436eadd2002-12-31 18:13:11 +0000590 The earliest representable \class{datetime},
591 \code{datetime(MINYEAR, 1, 1)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000592\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000593
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000594\begin{memberdesc}{max}
Fred Drake436eadd2002-12-31 18:13:11 +0000595 The latest representable \class{datetime},
596 \code{datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000597\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000598
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000599\begin{memberdesc}{resolution}
Fred Drake436eadd2002-12-31 18:13:11 +0000600 The smallest possible difference between non-equal \class{datetime}
601 objects, \code{timedelta(microseconds=1)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000602\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000603
604Instance attributes (read-only):
605
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000606\begin{memberdesc}{year}
Fred Drake436eadd2002-12-31 18:13:11 +0000607 Between \constant{MINYEAR} and \constant{MAXYEAR} inclusive
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000608\end{memberdesc}
609
610\begin{memberdesc}{month}
Fred Drake436eadd2002-12-31 18:13:11 +0000611 Between 1 and 12 inclusive
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000612\end{memberdesc}
613
614\begin{memberdesc}{day}
Fred Drake436eadd2002-12-31 18:13:11 +0000615 Between 1 and the number of days in the given month of the given
616 year.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000617\end{memberdesc}
618
619\begin{memberdesc}{hour}
Fred Drake436eadd2002-12-31 18:13:11 +0000620 In \code{range(24)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000621\end{memberdesc}
622
623\begin{memberdesc}{minute}
Fred Drake436eadd2002-12-31 18:13:11 +0000624 In \code{range(60)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000625\end{memberdesc}
626
627\begin{memberdesc}{second}
Fred Drake436eadd2002-12-31 18:13:11 +0000628 In \code{range(60)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000629\end{memberdesc}
630
631\begin{memberdesc}{microsecond}
Fred Drake436eadd2002-12-31 18:13:11 +0000632 In \code{range(1000000)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000633\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000634
635Supported operations:
636
Fred Drakebbdb2502002-12-23 18:58:06 +0000637\begin{itemize}
638 \item
639 datetime1 + timedelta -> datetime2
640 timedelta + datetime1 -> datetime2
641 datetime2 is a duration of timedelta removed from datetime1, moving
642 forward in time if timedelta.days > 0, or backward if
643 timedelta.days < 0. datetime2 - datetime1 == timedelta after.
644 \exception{OverflowError} is raised if datetime2.year would be
645 smaller than \constant{MINYEAR} or larger than \constant{MAXYEAR}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000646
Fred Drakebbdb2502002-12-23 18:58:06 +0000647 \item
648 datetime1 - timedelta -> datetime2
649 Computes the datetime2 such that datetime2 + timedelta == datetime1.
650 This isn't quite equivalent to datetime1 + (-timedelta), because
651 -timedelta in isolation can overflow in cases where
652 datetime1 - timedelta does not.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000653
Fred Drakebbdb2502002-12-23 18:58:06 +0000654 \item
655 datetime1 - datetime2 -> timedelta
656 This is exact, and cannot overflow.
657 datetime2 + timedelta == datetime1 after.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000658
Fred Drakebbdb2502002-12-23 18:58:06 +0000659 \item
660 comparison of \class{datetime} to datetime, where datetime1 is
661 considered less than datetime2 when datetime1 precedes datetime2
662 in time.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000663
Fred Drakebbdb2502002-12-23 18:58:06 +0000664 \item
665 hash, use as dict key
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000666
Fred Drakebbdb2502002-12-23 18:58:06 +0000667 \item
668 efficient pickling
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000669
Fred Drakebbdb2502002-12-23 18:58:06 +0000670 \item
671 in Boolean contexts, all \class{datetime} objects are considered
672 to be true
673\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000674
675Instance methods:
676
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000677\begin{methoddesc}{date}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000678 Return \class{date} object with same year, month and day.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000679\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000680
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000681\begin{methoddesc}{time}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000682 Return time object with same hour, minute, second and microsecond.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000683\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000684
Fred Drake436eadd2002-12-31 18:13:11 +0000685\begin{methoddesc}{replace}{year=, month=, day=, hour=, minute=,
686 second=, microsecond=}
687 Return a datetime with the same value, except for those fields given
688 new values by whichever keyword arguments are specified.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000689\end{methoddesc}
Tim Peters12bf3392002-12-24 05:41:27 +0000690
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000691\begin{methoddesc}{astimezone}{tz}
Fred Drake436eadd2002-12-31 18:13:11 +0000692 Return a \class{datetimetz} with the same date and time fields, and
693 with \member{tzinfo} member \var{tz}. \var{tz} must be \code{None},
694 or an instance of a \class{tzinfo} subclass.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000695\end{methoddesc}
Tim Peters80475bb2002-12-25 07:40:55 +0000696
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000697\begin{methoddesc}{timetuple}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000698 Return a 9-element tuple of the form returned by
699 \function{time.localtime()}.
700 The DST flag is -1. \code{\var{d}.timetuple()} is equivalent to
701 \code{(\var{d}.year, \var{d}.month, \var{d}.day,
702 \var{d}.hour, \var{d}.minute, \var{d}.second,
703 \var{d}.weekday(), \# 0 is Monday
704 \var{d}.toordinal() - date(\var{d}.year, 1, 1).toordinal() + 1,
705 \# day of year
706 -1)}
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000707\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000708
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000709\begin{methoddesc}{toordinal}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000710 Return the proleptic Gregorian ordinal of the date. The same as
711 \method{date.toordinal()}.
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}{weekday}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000715 Return the day of the week as an integer, where Monday is 0 and
716 Sunday is 6. The same as \method{date.weekday()}.
717 See also \method{isoweekday()}.
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}{isoweekday}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000721 Return the day of the week as an integer, where Monday is 1 and
722 Sunday is 7. The same as \method{date.isoweekday()}.
723 See also \method{weekday()}, \method{isocalendar()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000724\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000725
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000726\begin{methoddesc}{isocalendar}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000727 Return a 3-tuple, (ISO year, ISO week number, ISO weekday). The
728 same as \method{date.isocalendar()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000729\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000730
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000731\begin{methoddesc}{isoformat}{sep='T'}
Fred Drake436eadd2002-12-31 18:13:11 +0000732 Return a string representing the date and time in ISO 8601 format,
733 YYYY-MM-DDTHH:MM:SS.mmmmmm
734 or, if self.microsecond is 0,
735 YYYY-MM-DDTHH:MM:SS
736 The optional argument \var{sep} (default \code{'T'}) is a
737 one-character separator, placed between the date and time portions
738 of the result. For example,
739 datetime(2002, 12, 4, 1, 2, 3, 4).isoformat(' ') ==
740 '2002-12-04 01:02:03.000004'
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000741\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000742
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000743\begin{methoddesc}{__str__}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000744 For a \class{datetime} instance \var{d}, \code{str(\var{d})} is
745 equivalent to \code{\var{d}.isoformat(' ')}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000746\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000747
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000748\begin{methoddesc}{ctime}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000749 Return a string representing the date, for example
750 datetime(2002, 12, 4, 20, 30, 40).ctime() == 'Wed Dec 4 20:30:40 2002'.
751 \code{d.ctime()} is equivalent to
752 \code{time.ctime(time.mktime(d.timetuple()))} on platforms where
753 the native C \cfunction{ctime()} function (which
754 \function{time.ctime()} invokes, but which
755 \method{datetime.ctime()} does not invoke) conforms to the C
756 standard.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000757\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000758
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000759\begin{methoddesc}{strftime}{format}
Fred Drake436eadd2002-12-31 18:13:11 +0000760 Return a string representing the date and time, controlled by an
761 explicit format string. See the section on \method{strftime()}
762 behavior.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000763\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000764
765
Raymond Hettinger6005a342002-12-30 20:01:24 +0000766\subsection{\class{time} Objects \label{datetime-time}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000767
Raymond Hettingercbd6cd22002-12-31 16:30:49 +0000768A \class{time} object represents an idealized time of day, independent
769of day and timezone.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000770
Fred Drake0f8e5432002-12-31 18:31:48 +0000771\begin{classdesc}{time}{hour=0, minute=0, second=0, microsecond=0}
772 All arguments are optional. They may be ints or longs, in the
773 following ranges:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000774
Raymond Hettingercbd6cd22002-12-31 16:30:49 +0000775\begin{itemize}
776 \item \code{0 <= \var{hour} < 24}
777 \item \code{0 <= \var{minute} < 60}
778 \item \code{0 <= \var{second} < 60}
779 \item \code{0 <= \var{microsecond} < 1000000}
780\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000781
Fred Drake0f8e5432002-12-31 18:31:48 +0000782 If an argument outside those ranges is given, \exception{ValueError}
783 is raised.
784\end{classdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000785
786Class attributes:
787
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000788\begin{memberdesc}{min}
Fred Drake436eadd2002-12-31 18:13:11 +0000789 The earliest representable \class{time}, \code{time(0, 0, 0, 0)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000790\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000791
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000792\begin{memberdesc}{max}
Fred Drake436eadd2002-12-31 18:13:11 +0000793 The latest representable \class{time}, \code{time(23, 59, 59, 999999)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000794\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000795
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000796\begin{memberdesc}{resolution}
Fred Drake436eadd2002-12-31 18:13:11 +0000797 The smallest possible difference between non-equal \class{time}
798 objects, \code{timedelta(microseconds=1)}, although note that
799 arithmetic on \class{time} objects is not supported.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000800\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000801
802Instance attributes (read-only):
803
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000804\begin{memberdesc}{hour}
Fred Drake436eadd2002-12-31 18:13:11 +0000805 In \code{range(24)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000806\end{memberdesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000807
Tim Petersbad8ff02002-12-30 20:52:32 +0000808\begin{memberdesc}{minute}
Fred Drake436eadd2002-12-31 18:13:11 +0000809 In \code{range(60)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000810\end{memberdesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000811
Tim Petersbad8ff02002-12-30 20:52:32 +0000812\begin{memberdesc}{second}
Fred Drake436eadd2002-12-31 18:13:11 +0000813 In \code{range(60)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000814\end{memberdesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000815
Tim Petersbad8ff02002-12-30 20:52:32 +0000816\begin{memberdesc}{microsecond}
Fred Drake436eadd2002-12-31 18:13:11 +0000817 In \code{range(1000000)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000818\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000819
820Supported operations:
821
Fred Drakebbdb2502002-12-23 18:58:06 +0000822\begin{itemize}
823 \item
824 comparison of time to time, where time1 is considered
825 less than time2 when time1 precedes time2 in time.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000826
Fred Drakebbdb2502002-12-23 18:58:06 +0000827 \item
828 hash, use as dict key
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000829
Fred Drakebbdb2502002-12-23 18:58:06 +0000830 \item
831 efficient pickling
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000832
Fred Drakebbdb2502002-12-23 18:58:06 +0000833 \item
834 in Boolean contexts, a time object is considered to be true
Fred Drakea37e5cc2002-12-30 21:26:42 +0000835 if and only if it isn't equal to \code{time(0)}
Fred Drakebbdb2502002-12-23 18:58:06 +0000836\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000837
838Instance methods:
839
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000840\begin{methoddesc}{replace}{hour=, minute=, second=, microsecond=}
Fred Drake436eadd2002-12-31 18:13:11 +0000841 Return a time with the same value, except for those fields given
842 new values by whichever keyword arguments are specified.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000843\end{methoddesc}
Tim Peters12bf3392002-12-24 05:41:27 +0000844
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000845\begin{methoddesc}{isoformat}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000846 Return a string representing the time in ISO 8601 format,
847 HH:MM:SS.mmmmmm
848 or, if self.microsecond is 0
849 HH:MM:SS
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000850\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000851
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000852\begin{methoddesc}{__str__}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000853 For a time \var{t}, \code{str(\var{t})} is equivalent to
854 \code{\var{t}.isoformat()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000855\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000856
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000857\begin{methoddesc}{strftime}{format}
Fred Drake436eadd2002-12-31 18:13:11 +0000858 Return a string representing the time, controlled by an explicit
859 format string. See the section on \method{strftime()} behavior.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000860\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000861
862
Raymond Hettinger6005a342002-12-30 20:01:24 +0000863\subsection{\class{tzinfo} Objects \label{datetime-tzinfo}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000864
Fred Drakebbdb2502002-12-23 18:58:06 +0000865\class{tzinfo} is an abstract base clase, meaning that this class
866should not be instantiated directly. You need to derive a concrete
867subclass, and (at least) supply implementations of the standard
868\class{tzinfo} methods needed by the \class{datetime} methods you
Tim Petersbad8ff02002-12-30 20:52:32 +0000869use. The \module{datetime} module does not supply any concrete
Fred Drakebbdb2502002-12-23 18:58:06 +0000870subclasses of \class{tzinfo}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000871
Fred Drakebbdb2502002-12-23 18:58:06 +0000872An instance of (a concrete subclass of) \class{tzinfo} can be passed
873to the constructors for \class{datetimetz} and \class{timetz} objects.
874The latter objects view their fields as being in local time, and the
875\class{tzinfo} object supports methods revealing offset of local time
876from UTC, the name of the time zone, and DST offset, all relative to a
877date or time object passed to them.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000878
Tim Petersbad8ff02002-12-30 20:52:32 +0000879Special requirement for pickling: A \class{tzinfo} subclass must have an
Tim Peters2483b612002-12-24 16:30:58 +0000880\method{__init__} method that can be called with no arguments, else it
881can be pickled but possibly not unpickled again. This is a technical
882requirement that may be relaxed in the future.
883
Fred Drakebbdb2502002-12-23 18:58:06 +0000884A concrete subclass of \class{tzinfo} may need to implement the
885following methods. Exactly which methods are needed depends on the
Tim Petersbad8ff02002-12-30 20:52:32 +0000886uses made of aware \module{datetime} objects. If in doubt, simply
887implement all of them.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000888
Tim Petersbad8ff02002-12-30 20:52:32 +0000889\begin{methoddesc}{utcoffset}{self, dt}
Fred Drake436eadd2002-12-31 18:13:11 +0000890 Return offset of local time from UTC, in minutes east of UTC. If
891 local time is west of UTC, this should be negative. Note that this
892 is intended to be the total offset from UTC; for example, if a
893 \class{tzinfo} object represents both time zone and DST adjustments,
894 \method{utcoffset()} should return their sum. If the UTC offset
895 isn't known, return \code{None}. Else the value returned must be
Tim Peters397301e2003-01-02 21:28:08 +0000896 a \class{timedelta} object specifying a whole number of minutes in the
897 range -1439 to 1439 inclusive (1440 = 24*60; the magnitude of the offset
898 must be less than one day). Most implementations of
899 \method{utcoffset()} will probably look like one of these two:
Fred Drake436eadd2002-12-31 18:13:11 +0000900
Tim Petersbad8ff02002-12-30 20:52:32 +0000901\begin{verbatim}
Tim Petersf3615152003-01-01 21:51:37 +0000902 return CONSTANT # fixed-offset class
Fred Drake436eadd2002-12-31 18:13:11 +0000903 return CONSTANT + self.dst(dt) # daylight-aware class
Guido van Rossum8e7ec7c2002-12-31 04:39:05 +0000904\end{verbatim}
Tim Peters710fb152003-01-02 19:35:54 +0000905
906 If \method{utcoffset()} does not return \code{None},
907 \method{dst()} should not return \code{None} either.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000908\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000909
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000910
Tim Petersbad8ff02002-12-30 20:52:32 +0000911\begin{methoddesc}{dst}{self, dt}
Tim Petersf3615152003-01-01 21:51:37 +0000912 Return the daylight savings time (DST) adjustment, in minutes east of
913 UTC, or \code{None} if DST information isn't known. Return \code{0} if
914 DST is not in effect.
Tim Peters397301e2003-01-02 21:28:08 +0000915 If DST is in effect, return the offset as a
Fred Drake436eadd2002-12-31 18:13:11 +0000916 \class{timedelta} object (see \method{utcoffset()} for details).
917 Note that DST offset, if applicable, has
918 already been added to the UTC offset returned by
919 \method{utcoffset()}, so there's no need to consult \method{dst()}
920 unless you're interested in displaying DST info separately. For
921 example, \method{datetimetz.timetuple()} calls its \member{tzinfo}
922 member's \method{dst()} method to determine how the
Tim Petersf3615152003-01-01 21:51:37 +0000923 \member{tm_isdst} flag should be set, and
924 \method{datetimetz.astimezone()} calls \method{dst()} to account for
925 DST changes when crossing time zones.
926
927 An instance \var{tz} of a \class{tzinfo} subclass that models both
928 standard and daylight times must be consistent in this sense:
929
930 \code{tz.utcoffset(dt) - tz.dst(dt)}
931
932 must return the same result for every \class{datetimetz} \var{dt}
Tim Petersadf64202003-01-04 06:03:15 +0000933 with \code{dt.tzinfo==tz} For sane \class{tzinfo} subclasses, this
934 expression yields the time zone's "standard offset", which should not
935 depend on the date or the time, but only on geographic location. The
936 implementation of \method{datetimetz.astimezone()} relies on this, but
937 cannot detect violations; it's the programmer's responsibility to
Tim Petersf3615152003-01-01 21:51:37 +0000938 ensure it.
939
Tim Peters710fb152003-01-02 19:35:54 +0000940\begin{methoddesc}{tzname}{self, dt}
941 Return the timezone name corresponding to the \class{datetime} represented
942 by \var{dt}, as a string. Nothing about string names is defined by the
943 \module{datetime} module, and there's no requirement that it mean anything
944 in particular. For example, "GMT", "UTC", "-500", "-5:00", "EDT",
945 "US/Eastern", "America/New York" are all valid replies. Return
946 \code{None} if a string name isn't known. Note that this is a method
947 rather than a fixed string primarily because some \class{tzinfo} objects
948 will wish to return different names depending on the specific value
949 of \var{dt} passed, especially if the \class{tzinfo} class is
950 accounting for daylight time.
951\end{methoddesc}
952
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000953\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000954
Tim Petersbad8ff02002-12-30 20:52:32 +0000955These methods are called by a \class{datetimetz} or \class{timetz} object,
956in response to their methods of the same names. A \class{datetimetz}
957object passes itself as the argument, and a \class{timetz} object passes
958\code{None} as the argument. A \class{tzinfo} subclass's methods should
959therefore be prepared to accept a \var{dt} argument of \code{None}, or of
960class \class{datetimetz}.
961
962When \code{None} is passed, it's up to the class designer to decide the
963best response. For example, returning \code{None} is appropriate if the
964class wishes to say that timetz objects don't participate in the
965\class{tzinfo} protocol. In other applications, it may be more useful
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000966for \code{utcoffset(None)} to return the standard UTC offset.
Tim Petersbad8ff02002-12-30 20:52:32 +0000967
968When a \class{datetimetz} object is passed in response to a
969\class{datetimetz} method, \code{dt.tzinfo} is the same object as
970\var{self}. \class{tzinfo} methods can rely on this, unless
971user code calls \class{tzinfo} methods directly. The intent is that
972the \class{tzinfo} methods interpret \var{dt} as being in local time,
973and not need to worry about objects in other timezones.
974
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000975Example \class{tzinfo} classes:
976
Fred Drakebbdb2502002-12-23 18:58:06 +0000977\verbatiminput{tzinfo-examples.py}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000978
Tim Petersadf64202003-01-04 06:03:15 +0000979Note that there are unavoidable subtleties twice per year in a tzinfo
980subclass accounting for both standard and daylight time, at the DST
981transition points. For concreteness, consider US Eastern (UTC -0500),
982where EDT begins the minute after 1:59 (EST) on the first Sunday in
983April, and ends the minute after 1:59 (EDT) on the last Sunday in October:
984
985\begin{verbatim}
986 UTC 3:MM 4:MM 5:MM 6:MM 7:MM 8:MM
987 EST 22:MM 23:MM 0:MM 1:MM 2:MM 3:MM
988 EDT 23:MM 0:MM 1:MM 2:MM 3:MM 4:MM
989
990 start 22:MM 23:MM 0:MM 1:MM 3:MM 4:MM
991
992 end 23:MM 0:MM 1:MM 1:MM 2:MM 3:MM
993\end{verbatim}
994
995When DST starts (the "start" line), the local wall clock leaps from 1:59
996to 3:00. A wall time of the form 2:MM doesn't really make sense on that
Tim Peters75a6e3b2003-01-04 18:17:36 +0000997day, so \code{astimezone(Eastern)} won't deliver a result with
998\code{hour==2} on the
999day DST begins. How an Eastern instance chooses to interpret 2:MM on
1000that day is its business. The example Eastern implementation above
1001chose to
Tim Petersadf64202003-01-04 06:03:15 +00001002consider it as a time in EDT, simply because it "looks like it's
1003after 2:00", and so synonymous with the EST 1:MM times on that day.
1004Your Eastern class may wish, for example, to raise an exception instead
Tim Peters75a6e3b2003-01-04 18:17:36 +00001005when it sees a 2:MM time on the day EDT begins.
Tim Petersadf64202003-01-04 06:03:15 +00001006
1007When DST ends (the "end" line), there's a potentially worse problem:
Tim Peters75a6e3b2003-01-04 18:17:36 +00001008there's an hour that can't be spelled unambiguously in local wall time, the
Tim Petersadf64202003-01-04 06:03:15 +00001009hour beginning at the moment DST ends. In this example, that's times of
1010the form 6:MM UTC on the day daylight time ends. The local wall clock
1011leaps from 1:59 (daylight time) back to 1:00 (standard time) again.
10121:MM is taken as daylight time (it's "before 2:00"), so maps to 5:MM UTC.
10132:MM is taken as standard time (it's "after 2:00"), so maps to 7:MM UTC.
1014There is no local time that maps to 6:MM UTC on this day.
1015
Tim Peters75a6e3b2003-01-04 18:17:36 +00001016Just as the wall clock does, \code{astimezone(Eastern)} maps both UTC
1017hours 5:MM
Tim Petersadf64202003-01-04 06:03:15 +00001018and 6:MM to Eastern hour 1:MM on this day. However, this result is
1019ambiguous (there's no way for Eastern to know which repetition of 1:MM
Tim Peters75a6e3b2003-01-04 18:17:36 +00001020is intended). Applications that can't bear such ambiguity
1021should avoid using hybrid tzinfo classes; there are no
Tim Petersadf64202003-01-04 06:03:15 +00001022ambiguities when using UTC, or any other fixed-offset tzinfo subclass
1023(such as a class representing only EST (fixed offset -5 hours), or only
1024EDT (fixed offset -4 hours)).
1025
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001026
Raymond Hettinger6005a342002-12-30 20:01:24 +00001027\subsection{\class{timetz} Objects \label{datetime-timetz}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001028
1029A time object represents a (local) time of day, independent of any
1030particular day, and subject to adjustment via a \class{tzinfo} object.
1031
1032Constructor:
1033
Fred Drake0f8e5432002-12-31 18:31:48 +00001034\begin{classdesc}{time}{hour=0, minute=0, second=0, microsecond=0,
1035 tzinfo=None}
Fred Drake436eadd2002-12-31 18:13:11 +00001036 All arguments are optional. \var{tzinfo} may be \code{None}, or
1037 an instance of a \class{tzinfo} subclass. The remaining arguments
1038 may be ints or longs, in the following ranges:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001039
Fred Drake0f8e5432002-12-31 18:31:48 +00001040 \begin{itemize}
1041 \item \code{0 <= \var{hour} < 24}
1042 \item \code{0 <= \var{minute} < 60}
1043 \item \code{0 <= \var{second} < 60}
1044 \item \code{0 <= \var{microsecond} < 1000000}.
1045 \end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001046
Fred Drake436eadd2002-12-31 18:13:11 +00001047 If an argument outside those ranges is given,
1048 \exception{ValueError} is raised.
Fred Drake0f8e5432002-12-31 18:31:48 +00001049\end{classdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001050
1051Class attributes:
1052
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001053\begin{memberdesc}{min}
Fred Drake436eadd2002-12-31 18:13:11 +00001054 The earliest representable time, \code{timetz(0, 0, 0, 0)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001055\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001056
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001057\begin{memberdesc}{max}
Fred Drake436eadd2002-12-31 18:13:11 +00001058 The latest representable time, \code{timetz(23, 59, 59, 999999)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001059\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001060
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001061\begin{memberdesc}{resolution}
Fred Drake436eadd2002-12-31 18:13:11 +00001062 The smallest possible difference between non-equal \class{timetz}
1063 objects, \code{timedelta(microseconds=1)}, although note that
1064 arithmetic on \class{timetz} objects is not supported.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001065\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001066
1067Instance attributes (read-only):
1068
Fred Drake436eadd2002-12-31 18:13:11 +00001069\begin{memberdesc}{hour}
1070 In \code{range(24)}.
1071\end{memberdesc}
1072
1073\begin{memberdesc}{minute}
1074 In \code{range(60)}.
1075\end{memberdesc}
1076
1077\begin{memberdesc}{second}
1078 In \code{range(60)}.
1079\end{memberdesc}
1080
1081\begin{memberdesc}{microsecond}
1082 In \code{range(1000000)}.
1083\end{memberdesc}
1084
1085\begin{memberdesc}{tzinfo}
1086 The object passed as the tzinfo argument to the \class{timetz}
1087 constructor, or \code{None} if none was passed.
1088\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001089
1090Supported operations:
1091
Fred Drakebbdb2502002-12-23 18:58:06 +00001092\begin{itemize}
1093 \item
Tim Peters60c76e42002-12-27 00:41:11 +00001094 comparison of \class{timetz} to \class{time} or \class{timetz},
1095 where \var{a} is considered less than \var{b} when \var{a} precedes
Fred Drakea37e5cc2002-12-30 21:26:42 +00001096 \var{b} in time. If one comparand is naive and the other is aware,
Tim Peters60c76e42002-12-27 00:41:11 +00001097 \exception{TypeError} is raised. If both comparands are aware, and
1098 have the same \member{tzinfo} member, the common \member{tzinfo}
1099 member is ignored and the base times are compared. If both
1100 comparands are aware and have different \member{tzinfo} members,
1101 the comparands are first adjusted by subtracting their UTC offsets
1102 (obtained from \code{self.utcoffset()}).
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001103
Fred Drakebbdb2502002-12-23 18:58:06 +00001104 \item
1105 hash, use as dict key
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001106
Fred Drakebbdb2502002-12-23 18:58:06 +00001107 \item
1108 pickling
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001109
Fred Drakebbdb2502002-12-23 18:58:06 +00001110 \item
1111 in Boolean contexts, a \class{timetz} object is considered to be
1112 true if and only if, after converting it to minutes and
1113 subtracting \method{utcoffset()} (or \code{0} if that's
1114 \code{None}), the result is non-zero.
1115\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001116
1117Instance methods:
1118
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001119\begin{methoddesc}{replace}(hour=, minute=, second=, microsecond=, tzinfo=)
Fred Drake436eadd2002-12-31 18:13:11 +00001120 Return a \class{timetz} with the same value, except for those fields given
1121 new values by whichever keyword arguments are specified. Note that
1122 \code{tzinfo=None} can be specified to create a naive \class{timetz} from an
1123 aware \class{timetz}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001124\end{methoddesc}
Tim Peters12bf3392002-12-24 05:41:27 +00001125
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001126\begin{methoddesc}{isoformat}{}
Fred Drake436eadd2002-12-31 18:13:11 +00001127 Return a string representing the time in ISO 8601 format,
1128 HH:MM:SS.mmmmmm
1129 or, if self.microsecond is 0,
1130 HH:MM:SS
1131 If \method{utcoffset()} does not return \code{None}, a 6-character
1132 string is appended, giving the UTC offset in (signed) hours and
1133 minutes:
1134 HH:MM:SS.mmmmmm+HH:MM
1135 or, if self.microsecond is 0,
1136 HH:MM:SS+HH:MM
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001137\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001138
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001139\begin{methoddesc}{__str__}{}
Fred Drake436eadd2002-12-31 18:13:11 +00001140 For a \class{timetz} \var{t}, \code{str(\var{t})} is equivalent to
1141 \code{\var{t}.isoformat()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001142\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001143
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001144\begin{methoddesc}{strftime}{format}
Fred Drake436eadd2002-12-31 18:13:11 +00001145 Return a string representing the time, controlled by an explicit
1146 format string. See the section on \method{strftime()} behavior.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001147\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001148
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001149\begin{methoddesc}{utcoffset}{}
Fred Drake436eadd2002-12-31 18:13:11 +00001150 If \member{tzinfo} is \code{None}, returns \code{None}, else
1151 \code{tzinfo.utcoffset(self)} converted to a \class{timedelta}
1152 object.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001153\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001154
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001155\begin{methoddesc}{tzname}{}
Fred Drake436eadd2002-12-31 18:13:11 +00001156 If \member{tzinfo} is \code{None}, returns \code{None}, else
1157 \code{tzinfo.tzname(self)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001158\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001159
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001160\begin{methoddesc}{dst}{}
Fred Drake436eadd2002-12-31 18:13:11 +00001161 If \member{tzinfo} is \code{None}, returns \code{None}, else
1162 \code{tzinfo.dst(self)} converted to a \class{timedelta} object.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001163\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001164
1165
1166
Raymond Hettinger6005a342002-12-30 20:01:24 +00001167\subsection{ \class{datetimetz} Objects \label{datetime-datetimetz}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001168
Fred Drakebbdb2502002-12-23 18:58:06 +00001169\begin{notice}[warning]
1170 I think this is \emph{still} missing some methods from the
1171 Python implementation.
1172\end{notice}
1173
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001174A \class{datetimetz} object is a single object containing all the information
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001175from a \class{date} object and a \class{timetz} object.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001176
1177Constructor:
1178
Fred Drake0f8e5432002-12-31 18:31:48 +00001179\begin{classdesc}{datetimetz}{year, month, day,
1180 hour=0, minute=0, second=0, microsecond=0,
1181 tzinfo=None}
Fred Drake436eadd2002-12-31 18:13:11 +00001182 The year, month and day arguments are required. \var{tzinfo} may
1183 be \code{None}, or an instance of a \class{tzinfo} subclass. The
1184 remaining arguments may be ints or longs, in the following ranges:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001185
Fred Drake436eadd2002-12-31 18:13:11 +00001186 \begin{itemize}
1187 \item \code{MINYEAR <= \var{year} <= MAXYEAR}
1188 \item \code{1 <= \var{month} <= 12}
1189 \item \code{1 <= \var{day} <= number of days in the given month and year}
1190 \item \code{0 <= \var{hour} < 24}
1191 \item \code{0 <= \var{minute} < 60}
1192 \item \code{0 <= \var{second} < 60}
1193 \item \code{0 <= \var{microsecond} < 1000000}
1194 \end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001195
Fred Drake436eadd2002-12-31 18:13:11 +00001196 If an argument outside those ranges is given,
1197 \exception{ValueError} is raised.
Fred Drake0f8e5432002-12-31 18:31:48 +00001198\end{classdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001199
1200Other constructors (class methods):
1201
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001202\begin{funcdesc}{today}{}
Fred Drake436eadd2002-12-31 18:13:11 +00001203\methodline{utcnow}{}
1204\methodline{utcfromtimestamp}{timestamp}
1205\methodline{fromordinal}{ordinal}
1206 These are the same as the \class{datetime} class methods of the
1207 same names, except that they construct a \class{datetimetz}
1208 object, with tzinfo \code{None}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001209\end{funcdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001210
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001211\begin{funcdesc}{now}{\optional{tzinfo=None}}
Fred Drake436eadd2002-12-31 18:13:11 +00001212\methodline{fromtimestamp}{timestamp\optional{, tzinfo=None}}
1213 These are the same as the \class{datetime} class methods of the
1214 same names, except that they accept an additional, optional tzinfo
1215 argument, and construct a \class{datetimetz} object with that
1216 \class{tzinfo} object attached.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001217\end{funcdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001218
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001219\begin{funcdesc}{combine}{date, time}
Fred Drake436eadd2002-12-31 18:13:11 +00001220 This is the same as \method{datetime.combine()}, except that it
1221 constructs a \class{datetimetz} object, and, if the time object is
1222 of type timetz, the \class{datetimetz} object has the same
1223 \class{tzinfo} object as the time object.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001224\end{funcdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001225
1226Class attributes:
1227
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001228\begin{memberdesc}{min}
Fred Drake436eadd2002-12-31 18:13:11 +00001229 The earliest representable \class{datetimetz},
1230 \code{datetimetz(MINYEAR, 1, 1)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001231\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001232
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001233\begin{memberdesc}{max}
Fred Drake436eadd2002-12-31 18:13:11 +00001234 The latest representable \class{datetime},
1235 \code{datetimetz(MAXYEAR, 12, 31, 23, 59, 59, 999999)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001236\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001237
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001238\begin{memberdesc}{resolution}
Fred Drake436eadd2002-12-31 18:13:11 +00001239 The smallest possible difference between non-equal \class{datetimetz}
1240 objects, \code{timedelta(microseconds=1)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001241\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001242
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001243Instance attributes, all read-only:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001244
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001245\begin{memberdesc}{year}
Fred Drake436eadd2002-12-31 18:13:11 +00001246 Between \constant{MINYEAR} and \constant{MAXYEAR}, inclusive.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001247\end{memberdesc}
Fred Drake436eadd2002-12-31 18:13:11 +00001248
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001249\begin{memberdesc}{month}
Fred Drake436eadd2002-12-31 18:13:11 +00001250 Between 1 and 12 inclusive
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001251\end{memberdesc}
Fred Drake436eadd2002-12-31 18:13:11 +00001252
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001253\begin{memberdesc}{day}
Fred Drake436eadd2002-12-31 18:13:11 +00001254 Between 1 and the number of days in the given month of the given
1255 year.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001256\end{memberdesc}
Fred Drake436eadd2002-12-31 18:13:11 +00001257
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001258\begin{memberdesc}{hour}
Fred Drake436eadd2002-12-31 18:13:11 +00001259 In \code{range(24)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001260\end{memberdesc}
Fred Drake436eadd2002-12-31 18:13:11 +00001261
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001262\begin{memberdesc}{minute}
Fred Drake436eadd2002-12-31 18:13:11 +00001263 In \code{range(60)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001264\end{memberdesc}
Fred Drake436eadd2002-12-31 18:13:11 +00001265
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001266\begin{memberdesc}{second}
Fred Drake436eadd2002-12-31 18:13:11 +00001267 In \code{range(60)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001268\end{memberdesc}
Fred Drake436eadd2002-12-31 18:13:11 +00001269
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001270\begin{memberdesc}{microsecond}
Fred Drake436eadd2002-12-31 18:13:11 +00001271 In \code{range(1000000)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001272\end{memberdesc}
Fred Drake436eadd2002-12-31 18:13:11 +00001273
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001274\begin{memberdesc}{tzinfo}
Fred Drake436eadd2002-12-31 18:13:11 +00001275 The object passed as the \var{tzinfo} argument to the
1276 \class{datetimetz} constructor, or \code{None} if none was passed.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001277\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001278
1279Supported operations:
1280
Fred Drakebbdb2502002-12-23 18:58:06 +00001281\begin{itemize}
1282 \item
1283 datetimetz1 + timedelta -> datetimetz2
1284 timedelta + datetimetz1 -> datetimetz2
Tim Peters60c76e42002-12-27 00:41:11 +00001285
Fred Drakebbdb2502002-12-23 18:58:06 +00001286 The same as addition of \class{datetime} objects, except that
1287 datetimetz2.tzinfo is set to datetimetz1.tzinfo.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001288
Fred Drakebbdb2502002-12-23 18:58:06 +00001289 \item
1290 datetimetz1 - timedelta -> datetimetz2
Tim Peters60c76e42002-12-27 00:41:11 +00001291
Fred Drakebbdb2502002-12-23 18:58:06 +00001292 The same as addition of \class{datetime} objects, except that
1293 datetimetz2.tzinfo is set to datetimetz1.tzinfo.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001294
Fred Drakebbdb2502002-12-23 18:58:06 +00001295 \item
1296 aware_datetimetz1 - aware_datetimetz2 -> timedelta
Fred Drakea37e5cc2002-12-30 21:26:42 +00001297 naive_datetimetz1 - naive_datetimetz2 -> timedelta
1298 naive_datetimetz1 - datetime2 -> timedelta
1299 datetime1 - naive_datetimetz2 -> timedelta
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001300
Tim Peters60c76e42002-12-27 00:41:11 +00001301 Subtraction of a \class{datetime} or \class{datetimetz}, from a
Fred Drakebbdb2502002-12-23 18:58:06 +00001302 \class{datetime} or \class{datetimetz}, is defined only if both
Fred Drakea37e5cc2002-12-30 21:26:42 +00001303 operands are naive, or if both are aware. If one is aware and the
1304 other is naive, \exception{TypeError} is raised.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001305
Fred Drakea37e5cc2002-12-30 21:26:42 +00001306 If both are naive, or both are aware and have the same \member{tzinfo}
Tim Peters60c76e42002-12-27 00:41:11 +00001307 member, subtraction acts as for \class{datetime} subtraction.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001308
Tim Peters60c76e42002-12-27 00:41:11 +00001309 If both are aware and have different \member{tzinfo} members,
1310 \code{a-b} acts as if \var{a} and \var{b} were first converted to UTC
1311 datetimes (by subtracting \code{a.utcoffset()} minutes from \var{a},
1312 and \code{b.utcoffset()} minutes from \var{b}), and then doing
Fred Drakebbdb2502002-12-23 18:58:06 +00001313 \class{datetime} subtraction, except that the implementation never
1314 overflows.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001315
Fred Drakebbdb2502002-12-23 18:58:06 +00001316 \item
Tim Peters60c76e42002-12-27 00:41:11 +00001317 comparison of \class{datetimetz} to \class{datetime} or
1318 \class{datetimetz}, where \var{a} is considered less than \var{b}
1319 when \var{a} precedes \var{b} in time. If one comparand is naive and
1320 the other is aware, \exception{TypeError} is raised. If both
1321 comparands are aware, and have the same \member{tzinfo} member,
1322 the common \member{tzinfo} member is ignored and the base datetimes
1323 are compared. If both comparands are aware and have different
1324 \member{tzinfo} members, the comparands are first adjusted by
1325 subtracting their UTC offsets (obtained from \code{self.utcoffset()}).
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001326
Fred Drakebbdb2502002-12-23 18:58:06 +00001327 \item
1328 hash, use as dict key
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001329
Fred Drakebbdb2502002-12-23 18:58:06 +00001330 \item
1331 efficient pickling
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001332
Fred Drakebbdb2502002-12-23 18:58:06 +00001333 \item
1334 in Boolean contexts, all \class{datetimetz} objects are considered to be
1335 true
1336\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001337
1338Instance methods:
1339
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001340\begin{methoddesc}{date}{}
Fred Drake436eadd2002-12-31 18:13:11 +00001341\methodline{time}{}
1342\methodline{toordinal}{}
1343\methodline{weekday}{}
1344\methodline{isoweekday}{}
1345\methodline{isocalendar}{}
1346\methodline{ctime}{}
1347\methodline{__str__}{}
1348\methodline{strftime}{format}
1349 These are the same as the \class{datetime} methods of the same names.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001350\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001351
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001352\begin{methoddesc}{timetz}{}
Fred Drake436eadd2002-12-31 18:13:11 +00001353 Return \class{timetz} object with same hour, minute, second, microsecond,
1354 and tzinfo.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001355\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001356
Fred Drake436eadd2002-12-31 18:13:11 +00001357\begin{methoddesc}{replace}{year=, month=, day=, hour=, minute=, second=,
1358 microsecond=, tzinfo=}
1359 Return a datetimetz with the same value, except for those fields given
1360 new values by whichever keyword arguments are specified. Note that
1361 \code{tzinfo=None} can be specified to create a naive datetimetz from
1362 an aware datetimetz.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001363\end{methoddesc}
Tim Peters12bf3392002-12-24 05:41:27 +00001364
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001365\begin{methoddesc}{astimezone}{tz}
Fred Drake2b0a3d32003-01-06 15:03:11 +00001366 Return a \class{datetimetz} object with new \member{tzinfo} member
Tim Peters75a6e3b2003-01-04 18:17:36 +00001367 \var{tz}.
1368 \var{tz} must be \code{None}, or an instance of a \class{tzinfo} subclass.
1369 If \var{tz} is \code{None}, \var{self} is naive,
Fred Drake436eadd2002-12-31 18:13:11 +00001370 \code{tz.utcoffset(self)} returns \code{None},
Tim Peters75a6e3b2003-01-04 18:17:36 +00001371 or \code{self.tzinfo}\ is \var{tz},
Fred Drake436eadd2002-12-31 18:13:11 +00001372 \code{self.astimezone(tz)} is equivalent to
1373 \code{self.replace(tzinfo=tz)}: a new timezone object is attached
Tim Peters75a6e3b2003-01-04 18:17:36 +00001374 without any conversion of date or time fields. Else \code{self.tzinfo}
1375 and \var{tz} must implement the \method{utcoffset()} and \method{dst()}
1376 \class{tzinfo} methods, and the date and time fields are adjusted so
1377 that the result is local time in time zone \var{tz}, representing the
1378 same UTC time as \var{self}: after \code{astz = dt.astimezone(tz)},
1379 \code{astz - astz.utcoffset()} will usually have the same date and time
1380 members as \code{dt - dt.utcoffset()}. The discussion of class
1381 \class{tzinfo} explains the cases at Daylight Saving Time
1382 transition boundaries where this cannot be achieved (an issue only if
1383 \var{tz} models both standard and daylight time).
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001384\end{methoddesc}
Tim Peters80475bb2002-12-25 07:40:55 +00001385
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001386\begin{methoddesc}{utcoffset}{}
Fred Drake436eadd2002-12-31 18:13:11 +00001387 If \member{tzinfo} is \code{None}, returns \code{None}, else
1388 \code{tzinfo.utcoffset(self)} converted to a \class{timedelta}
1389 object.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001390\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001391
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001392\begin{methoddesc}{tzname}{}
Fred Drake436eadd2002-12-31 18:13:11 +00001393 If \member{tzinfo} is \code{None}, returns \code{None}, else
1394 returns \code{tzinfo.tzname(self)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001395\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001396
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001397\begin{methoddesc}{dst}{}
Fred Drake436eadd2002-12-31 18:13:11 +00001398 If \member{tzinfo} is \code{None}, returns \code{None}, else
1399 \code{tzinfo.dst(self)} converted to a \class{timedelta}
1400 object.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001401\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001402
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001403\begin{methoddesc}{timetuple}{}
Fred Drake436eadd2002-12-31 18:13:11 +00001404 Like \function{datetime.timetuple()}, but sets the
1405 \member{tm_isdst} flag according to the \method{dst()} method: if
1406 \method{dst()} returns \code{None}, \member{tm_isdst} is set to
1407 \code{-1}; else if \method{dst()} returns a non-zero value,
1408 \member{tm_isdst} is set to \code{1}; else \code{tm_isdst} is set
1409 to \code{0}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001410\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001411
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001412\begin{methoddesc}{utctimetuple}{}
Fred Drake436eadd2002-12-31 18:13:11 +00001413 If \class{datetimetz} instance \var{d} is naive, this is the same as
1414 \code{\var{d}.timetuple()} except that \member{tm_isdst} is forced to 0
1415 regardless of what \code{d.dst()} returns. DST is never in effect
1416 for a UTC time.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001417
Fred Drake436eadd2002-12-31 18:13:11 +00001418 If \var{d} is aware, \var{d} is normalized to UTC time, by subtracting
1419 \code{\var{d}.utcoffset()} minutes, and a timetuple for the
1420 normalized time is returned. \member{tm_isdst} is forced to 0.
1421 Note that the result's \member{tm_year} field may be
1422 \constant{MINYEAR}-1 or \constant{MAXYEAR}+1, if \var{d}.year was
1423 \code{MINYEAR} or \code{MAXYEAR} and UTC adjustment spills over a
1424 year boundary.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001425\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001426
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001427\begin{methoddesc}{isoformat}{sep='T'}
Fred Drake436eadd2002-12-31 18:13:11 +00001428 Return a string representing the date and time in ISO 8601 format,
1429 YYYY-MM-DDTHH:MM:SS.mmmmmm
1430 or, if \member{microsecond} is 0,
1431 YYYY-MM-DDTHH:MM:SS
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001432
Fred Drake436eadd2002-12-31 18:13:11 +00001433 If \method{utcoffset()} does not return \code{None}, a 6-character
1434 string is appended, giving the UTC offset in (signed) hours and
1435 minutes:
1436 YYYY-MM-DDTHH:MM:SS.mmmmmm+HH:MM
1437 or, if \member{microsecond} is 0
1438 YYYY-MM-DDTHH:MM:SS+HH:MM
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001439
Fred Drake436eadd2002-12-31 18:13:11 +00001440 The optional argument \var{sep} (default \code{'T'}) is a
1441 one-character separator, placed between the date and time portions
1442 of the result. For example,
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001443
Fred Drakebbdb2502002-12-23 18:58:06 +00001444\begin{verbatim}
1445>>> from datetime import *
1446>>> class TZ(tzinfo):
Tim Peters710fb152003-01-02 19:35:54 +00001447... def utcoffset(self, dt): return timedelta(minutes=-399)
Fred Drakebbdb2502002-12-23 18:58:06 +00001448...
1449>>> datetimetz(2002, 12, 25, tzinfo=TZ()).isoformat(' ')
1450'2002-12-25 00:00:00-06:39'
1451\end{verbatim}
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001452\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001453
Fred Drakebbdb2502002-12-23 18:58:06 +00001454\code{str(\var{d})} is equivalent to \code{\var{d}.isoformat(' ')}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001455
1456
Tim Peters29fb9c72002-12-23 22:21:52 +00001457\subsection{\method{strftime()} Behavior}
1458
1459\class{date}, \class{datetime}, \class{datetimetz}, \class{time},
1460and \class{timetz} objects all support a \code{strftime(\var{format})}
1461method, to create a string representing the time under the control of
1462an explicit format string. Broadly speaking,
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001463\code{d.strftime(fmt)}
Tim Peters29fb9c72002-12-23 22:21:52 +00001464acts like the \refmodule{time} module's
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001465\code{time.strftime(fmt, d.timetuple())}
Tim Peters29fb9c72002-12-23 22:21:52 +00001466although not all objects support a \method{timetuple()} method.
1467
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001468For \class{time} and \class{timetz} objects, the format codes for
1469year, month, and day should not be used, as time objects have no such
1470values. If they're used anyway, \code{1900} is substituted for the
1471year, and \code{0} for the month and day.
Tim Peters29fb9c72002-12-23 22:21:52 +00001472
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001473For \class{date} objects, the format codes for hours, minutes, and
1474seconds should not be used, as \class{date} objects have no such
1475values. If they're used anyway, \code{0} is substituted for them.
Tim Peters29fb9c72002-12-23 22:21:52 +00001476
Fred Drakea37e5cc2002-12-30 21:26:42 +00001477For a naive object, the \code{\%z} and \code{\%Z} format codes are
Tim Peters29fb9c72002-12-23 22:21:52 +00001478replaced by empty strings.
1479
1480For an aware object:
1481
1482\begin{itemize}
1483 \item[\code{\%z}]
1484 \method{utcoffset()} is transformed into a 5-character string of
1485 the form +HHMM or -HHMM, where HH is a 2-digit string giving the
1486 number of UTC offset hours, and MM is a 2-digit string giving the
1487 number of UTC offset minutes. For example, if
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001488 \method{utcoffset()} returns \code{timedelta(hours=-3, minutes=-30)},
Tim Peters1cff9fc2002-12-24 16:25:29 +00001489 \code{\%z} is replaced with the string \code{'-0330'}.
Tim Peters29fb9c72002-12-23 22:21:52 +00001490
1491 \item[\code{\%Z}]
1492 If \method{tzname()} returns \code{None}, \code{\%Z} is replaced
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001493 by an empty string. Otherwise \code{\%Z} is replaced by the returned
Tim Peters29fb9c72002-12-23 22:21:52 +00001494 value, which must be a string.
1495\end{itemize}
1496
1497The full set of format codes supported varies across platforms,
1498because Python calls the platform C library's \function{strftime()}
1499function, and platform variations are common. The documentation for
1500Python's \refmodule{time} module lists the format codes that the C
1501standard (1989 version) requires, and those work on all platforms
1502with a standard C implementation. Note that the 1999 version of the
1503C standard added additional format codes.
1504
1505The exact range of years for which \method{strftime()} works also
1506varies across platforms. Regardless of platform, years before 1900
1507cannot be used.
1508
1509
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001510\begin{comment}
1511
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001512\subsection{C API}
1513
1514Struct typedefs:
1515
1516 PyDateTime_Date
1517 PyDateTime_DateTime
1518 PyDateTime_DateTimeTZ
1519 PyDateTime_Time
1520 PyDateTime_TimeTZ
1521 PyDateTime_Delta
1522 PyDateTime_TZInfo
1523
1524Type-check macros:
1525
1526 PyDate_Check(op)
1527 PyDate_CheckExact(op)
1528
1529 PyDateTime_Check(op)
1530 PyDateTime_CheckExact(op)
1531
1532 PyDateTimeTZ_Check(op)
1533 PyDateTimeTZ_CheckExact(op)
1534
1535 PyTime_Check(op)
1536 PyTime_CheckExact(op)
1537
1538 PyTimeTZ_Check(op)
1539 PyTimeTZ_CheckExact(op)
1540
1541 PyDelta_Check(op)
1542 PyDelta_CheckExact(op)
1543
1544 PyTZInfo_Check(op)
Raymond Hettingercbd6cd22002-12-31 16:30:49 +00001545 PyTZInfo_CheckExact(op)
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001546
1547Accessor macros:
1548
1549All objects are immutable, so accessors are read-only. All macros
1550return ints:
1551
Tim Peters1cff9fc2002-12-24 16:25:29 +00001552 For \class{date}, \class{datetime}, and \class{datetimetz} instances:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001553 PyDateTime_GET_YEAR(o)
1554 PyDateTime_GET_MONTH(o)
1555 PyDateTime_GET_DAY(o)
1556
1557 For \class{datetime} and \class{datetimetz} instances:
1558 PyDateTime_DATE_GET_HOUR(o)
1559 PyDateTime_DATE_GET_MINUTE(o)
1560 PyDateTime_DATE_GET_SECOND(o)
1561 PyDateTime_DATE_GET_MICROSECOND(o)
1562
Tim Peters1cff9fc2002-12-24 16:25:29 +00001563 For \class{time} and \class{timetz} instances:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001564 PyDateTime_TIME_GET_HOUR(o)
1565 PyDateTime_TIME_GET_MINUTE(o)
1566 PyDateTime_TIME_GET_SECOND(o)
1567 PyDateTime_TIME_GET_MICROSECOND(o)
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001568
1569\end{comment}