blob: 9133f1e00d44afc8b6e363ea5141e17ebdc955dc [file] [log] [blame]
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001% XXX what order should the types be discussed in?
Tim Petersbad8ff02002-12-30 20:52:32 +00002
Fred Drakebbdb2502002-12-23 18:58:06 +00003\section{\module{datetime} ---
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00004 Basic date and time types}
5
6\declaremodule{builtin}{datetime}
7\modulesynopsis{Basic date and time types.}
Fred Drakebbdb2502002-12-23 18:58:06 +00008\moduleauthor{Tim Peters}{tim@zope.com}
9\sectionauthor{Tim Peters}{tim@zope.com}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000010\sectionauthor{A.M. Kuchling}{amk@amk.ca}
Raymond Hettinger6005a342002-12-30 20:01:24 +000011\sectionauthor{Raymond D. Hettinger}{python@rcn.com}
12
13\versionadded{2.3}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000014
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000015
16The \module{datetime} module supplies classes for manipulating dates
17and times in both simple and complex ways. While date and time
18arithmetic is supported, the focus of the implementation is on
Andrew M. Kuchling9b445712003-01-09 13:46:30 +000019efficient member extraction for output formatting and manipulation.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000020
Fred Drakea37e5cc2002-12-30 21:26:42 +000021There are two kinds of date and time objects: ``naive'' and ``aware''.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000022This distinction refers to whether the object has any notion of time
Tim Peters00372022003-01-09 04:10:05 +000023zone, daylight saving time, or other kind of algorithmic or political
24time adjustment. Whether a naive \class{datetime} object represents
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000025Coordinated Universal Time (UTC), local time, or time in some other
26timezone is purely up to the program, just like it's up to the program
Fred Drakea37e5cc2002-12-30 21:26:42 +000027whether a particular number represents meters, miles, or mass. Naive
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000028\class{datetime} objects are easy to understand and to work with, at
29the cost of ignoring some aspects of reality.
30
Andrew M. Kuchling9b445712003-01-09 13:46:30 +000031For applications requiring more, \class{datetime} and \class{time}
32objects have an optional time zone information member,
Tim Peters327098a2003-01-20 22:54:38 +000033\member{tzinfo}, that can contain an instance of a subclass of
Andrew M. Kuchling9b445712003-01-09 13:46:30 +000034the abstract \class{tzinfo} class. These \class{tzinfo} objects
35capture information about the offset from UTC time, the time zone
36name, and whether Daylight Saving Time is in effect. Note that no
37concrete \class{tzinfo} classes are supplied by the \module{datetime}
38module. Instead, they provide a framework for incorporating the level
39of detail an application may require. The rules for time adjustment across
40the world are more political than rational, and there is no standard
41suitable for every application.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000042
43The \module{datetime} module exports the following constants:
44
45\begin{datadesc}{MINYEAR}
Tim Peters00372022003-01-09 04:10:05 +000046 The smallest year number allowed in a \class{date} or
47 \class{datetime} object. \constant{MINYEAR}
Fred Drakebbdb2502002-12-23 18:58:06 +000048 is \code{1}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000049\end{datadesc}
50
51\begin{datadesc}{MAXYEAR}
Tim Peters00372022003-01-09 04:10:05 +000052 The largest year number allowed in a \class{date} or \class{datetime}
53 object. \constant{MAXYEAR} is \code{9999}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000054\end{datadesc}
55
Raymond Hettinger6005a342002-12-30 20:01:24 +000056\begin{seealso}
Raymond Hettinger62229582002-12-31 16:37:03 +000057 \seemodule{calendar}{General calendar related functions.}
Raymond Hettinger6005a342002-12-30 20:01:24 +000058 \seemodule{time}{Time access and conversions.}
59\end{seealso}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000060
61\subsection{Available Types}
62
Fred Drakebbdb2502002-12-23 18:58:06 +000063\begin{classdesc*}{date}
Fred Drakea37e5cc2002-12-30 21:26:42 +000064 An idealized naive date, assuming the current Gregorian calendar
Fred Drakebbdb2502002-12-23 18:58:06 +000065 always was, and always will be, in effect.
66 Attributes: \member{year}, \member{month}, and \member{day}.
67\end{classdesc*}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000068
Fred Drakebbdb2502002-12-23 18:58:06 +000069\begin{classdesc*}{time}
Tim Peters00372022003-01-09 04:10:05 +000070 An idealized time, independent of any particular day, assuming
Fred Drakebbdb2502002-12-23 18:58:06 +000071 that every day has exactly 24*60*60 seconds (there is no notion
72 of "leap seconds" here).
Tim Peters00372022003-01-09 04:10:05 +000073 Attributes: \member{hour}, \member{minute}, \member{second},
74 \member{microsecond}, and \member{tzinfo}.
Fred Drakebbdb2502002-12-23 18:58:06 +000075\end{classdesc*}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000076
Fred Drakebbdb2502002-12-23 18:58:06 +000077\begin{classdesc*}{datetime}
Andrew M. Kuchling9b445712003-01-09 13:46:30 +000078 A combination of a date and a time.
Fred Drakebbdb2502002-12-23 18:58:06 +000079 Attributes: \member{year}, \member{month}, \member{day},
80 \member{hour}, \member{minute}, \member{second},
Tim Peters00372022003-01-09 04:10:05 +000081 \member{microsecond}, and \member{tzinfo}.
Fred Drakebbdb2502002-12-23 18:58:06 +000082\end{classdesc*}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000083
Fred Drakebbdb2502002-12-23 18:58:06 +000084\begin{classdesc*}{timedelta}
Andrew M. Kuchling9b445712003-01-09 13:46:30 +000085 A duration expressing the difference between two \class{date},
86 \class{time}, or \class{datetime} instances to microsecond
Fred Drakebbdb2502002-12-23 18:58:06 +000087 resolution.
88\end{classdesc*}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000089
Fred Drakebbdb2502002-12-23 18:58:06 +000090\begin{classdesc*}{tzinfo}
91 An abstract base class for time zone information objects. These
Tim Peters00372022003-01-09 04:10:05 +000092 are used by the \class{datetime} and \class{time} classes to
Andrew M. Kuchling9b445712003-01-09 13:46:30 +000093 provide a customizable notion of time adjustment (for example, to
Tim Peters00372022003-01-09 04:10:05 +000094 account for time zone and/or daylight saving time).
Fred Drakebbdb2502002-12-23 18:58:06 +000095\end{classdesc*}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000096
97Objects of these types are immutable.
98
Tim Peters00372022003-01-09 04:10:05 +000099Objects of the \class{date} type are always naive.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000100
Tim Peters00372022003-01-09 04:10:05 +0000101An object \var{d} of type \class{time} or \class{datetime} may be
Fred Drakea37e5cc2002-12-30 21:26:42 +0000102naive or aware. \var{d} is aware if \code{\var{d}.tzinfo} is not
Andrew M. Kuchling9b445712003-01-09 13:46:30 +0000103\code{None} and \code{\var{d}.tzinfo.utcoffset(\var{d})} does not return
Fred Drakea37e5cc2002-12-30 21:26:42 +0000104\code{None}. If \code{\var{d}.tzinfo} is \code{None}, or if
105\code{\var{d}.tzinfo} is not \code{None} but
106\code{\var{d}.tzinfo.utcoffset(\var{d})} returns \code{None}, \var{d}
107is naive.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000108
Fred Drakea37e5cc2002-12-30 21:26:42 +0000109The distinction between naive and aware doesn't apply to
Fred Drakebbdb2502002-12-23 18:58:06 +0000110\code{timedelta} objects.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000111
Fred Drakebbdb2502002-12-23 18:58:06 +0000112Subclass relationships:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000113
Fred Drakebbdb2502002-12-23 18:58:06 +0000114\begin{verbatim}
115object
116 timedelta
117 tzinfo
118 time
Fred Drakebbdb2502002-12-23 18:58:06 +0000119 date
120 datetime
Fred Drakebbdb2502002-12-23 18:58:06 +0000121\end{verbatim}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000122
Raymond Hettinger6005a342002-12-30 20:01:24 +0000123\subsection{\class{timedelta} Objects \label{datetime-timedelta}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000124
Fred Drakebbdb2502002-12-23 18:58:06 +0000125A \class{timedelta} object represents a duration, the difference
126between two dates or times.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000127
Fred Drake0f8e5432002-12-31 18:31:48 +0000128\begin{classdesc}{timedelta}{days=0, seconds=0, microseconds=0,
129 milliseconds=0, minutes=0, hours=0, weeks=0}
Raymond Hettingerf6212322002-12-31 17:24:50 +0000130
Fred Drake436eadd2002-12-31 18:13:11 +0000131 All arguments are optional. Arguments may be ints, longs, or floats,
132 and may be positive or negative.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000133
Fred Drake436eadd2002-12-31 18:13:11 +0000134 Only \var{days}, \var{seconds} and \var{microseconds} are stored
135 internally. Arguments are converted to those units:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000136
Andrew M. Kuchling9b445712003-01-09 13:46:30 +0000137\begin{itemize}
138 \item A millisecond is converted to 1000 microseconds.
139 \item A minute is converted to 60 seconds.
140 \item An hour is converted to 3600 seconds.
141 \item A week is converted to 7 days.
142\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000143
Fred Drake436eadd2002-12-31 18:13:11 +0000144 and days, seconds and microseconds are then normalized so that the
145 representation is unique, with
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000146
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000147\begin{itemize}
148 \item \code{0 <= \var{microseconds} < 1000000}
149 \item \code{0 <= \var{seconds} < 3600*24} (the number of seconds in one day)
150 \item \code{-999999999 <= \var{days} <= 999999999}
151\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000152
Andrew M. Kuchling9b445712003-01-09 13:46:30 +0000153 If any argument is a float and there are fractional microseconds,
Fred Drake436eadd2002-12-31 18:13:11 +0000154 the fractional microseconds left over from all arguments are combined
155 and their sum is rounded to the nearest microsecond. If no
156 argument is a float, the conversion and normalization processes
157 are exact (no information is lost).
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000158
Fred Drake436eadd2002-12-31 18:13:11 +0000159 If the normalized value of days lies outside the indicated range,
160 \exception{OverflowError} is raised.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000161
Fred Drake436eadd2002-12-31 18:13:11 +0000162 Note that normalization of negative values may be surprising at first.
163 For example,
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000164
Fred Drakebbdb2502002-12-23 18:58:06 +0000165\begin{verbatim}
166>>> d = timedelta(microseconds=-1)
167>>> (d.days, d.seconds, d.microseconds)
168(-1, 86399, 999999)
169\end{verbatim}
Fred Drake0f8e5432002-12-31 18:31:48 +0000170\end{classdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000171
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000172Class attributes are:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000173
Fred Drake0f8e5432002-12-31 18:31:48 +0000174\begin{memberdesc}{min}
175 The most negative \class{timedelta} object,
176 \code{timedelta(-999999999)}.
177\end{memberdesc}
178
179\begin{memberdesc}{max}
180 The most positive \class{timedelta} object,
181 \code{timedelta(days=999999999, hours=23, minutes=59, seconds=59,
182 microseconds=999999)}.
183\end{memberdesc}
184
185\begin{memberdesc}{resolution}
186 The smallest possible difference between non-equal
187 \class{timedelta} objects, \code{timedelta(microseconds=1)}.
188\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000189
Fred Drakea37e5cc2002-12-30 21:26:42 +0000190Note that, because of normalization, \code{timedelta.max} \textgreater
191\code{-timedelta.min}. \code{-timedelta.max} is not representable as
192a \class{timedelta} object.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000193
194Instance attributes (read-only):
195
Raymond Hettinger6005a342002-12-30 20:01:24 +0000196\begin{tableii}{c|l}{code}{Attribute}{Value}
197 \lineii{days}{Between -999999999 and 999999999 inclusive}
198 \lineii{seconds}{Between 0 and 86399 inclusive}
199 \lineii{microseconds}{Between 0 and 999999 inclusive}
200\end{tableii}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000201
202Supported operations:
203
Andrew M. Kuchling9b445712003-01-09 13:46:30 +0000204% XXX this table is too wide!
Raymond Hettinger6005a342002-12-30 20:01:24 +0000205\begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes}
Fred Drake9bdeee42002-12-30 20:35:32 +0000206 \lineiii{\var{t1} = \var{t2} + \var{t3}}
Tim Peters95322982002-12-31 16:01:47 +0000207 {Sum of \var{t2} and \var{t3}.
Fred Drake9bdeee42002-12-30 20:35:32 +0000208 Afterwards \var{t1}-\var{t2} == \var{t3} and \var{t1}-\var{t3}
209 == \var{t2} are true.}
210 {(1)}
211 \lineiii{\var{t1} = \var{t2} - \var{t3}}
212 {Difference of \var{t2} and \var{t3}. Afterwards \var{t1} ==
213 \var{t2} - \var{t3} and \var{t2} == \var{t1} + \var{t3} are
214 true.}
215 {(1)}
216 \lineiii{\var{t1} = \var{t2} * \var{i} or \var{t1} = \var{i} * \var{t2}}
217 {Delta multiplied by an integer or long.
Fred Drake436eadd2002-12-31 18:13:11 +0000218 Afterwards \var{t1} // i == \var{t2} is true,
219 provided \code{i != 0}.
Fred Drake9bdeee42002-12-30 20:35:32 +0000220 In general, \var{t1} * i == \var{t1} * (i-1) + \var{t1} is true.}
221 {(1)}
222 \lineiii{\var{t1} = \var{t2} // \var{i}}
223 {The floor is computed and the remainder (if any) is thrown away.}
Tim Peters397301e2003-01-02 21:28:08 +0000224 {(3)}
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000225 \lineiii{+\var{t1}}
Raymond Hettingercbd6cd22002-12-31 16:30:49 +0000226 {Returns a \class{timedelta} object with the same value.}
Tim Peters397301e2003-01-02 21:28:08 +0000227 {(2)}
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000228 \lineiii{-\var{t1}}
Raymond Hettingercbd6cd22002-12-31 16:30:49 +0000229 {equivalent to \class{timedelta}(-\var{t1.days}, -\var{t1.seconds},
Andrew M. Kuchling9b445712003-01-09 13:46:30 +0000230 -\var{t1.microseconds}), and to \var{t1}* -1.}
Tim Peters397301e2003-01-02 21:28:08 +0000231 {(1)(4)}
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000232 \lineiii{abs(\var{t})}
Fred Drake436eadd2002-12-31 18:13:11 +0000233 {equivalent to +\var{t} when \code{t.days >= 0}, and to
Andrew M. Kuchling9b445712003-01-09 13:46:30 +0000234 -\var{t} when \code{t.days < 0}.}
Tim Peters397301e2003-01-02 21:28:08 +0000235 {(2)}
Fred Drake9bdeee42002-12-30 20:35:32 +0000236\end{tableiii}
Raymond Hettinger6005a342002-12-30 20:01:24 +0000237\noindent
238Notes:
239
240\begin{description}
241\item[(1)]
Fred Drake436eadd2002-12-31 18:13:11 +0000242 This is exact, but may overflow.
Raymond Hettinger6005a342002-12-30 20:01:24 +0000243
244\item[(2)]
Tim Peters397301e2003-01-02 21:28:08 +0000245 This is exact, and cannot overflow.
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000246
247\item[(3)]
Tim Peters397301e2003-01-02 21:28:08 +0000248 Division by 0 raises \exception{ZeroDivisionError}.
249
250\item[(4)]
Fred Drake436eadd2002-12-31 18:13:11 +0000251 -\var{timedelta.max} is not representable as a \class{timedelta} object.
Raymond Hettinger6005a342002-12-30 20:01:24 +0000252\end{description}
253
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000254In addition to the operations listed above \class{timedelta} objects
Tim Peters00372022003-01-09 04:10:05 +0000255support certain additions and subtractions with \class{date} and
256\class{datetime} objects (see below).
Raymond Hettinger6005a342002-12-30 20:01:24 +0000257
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000258Comparisons of \class{timedelta} objects are supported with the
259\class{timedelta} object representing the smaller duration considered
260to be the smaller timedelta.
Raymond Hettinger6005a342002-12-30 20:01:24 +0000261
Andrew M. Kuchling9b445712003-01-09 13:46:30 +0000262\class{timedelta} objects are hashable (usable as dictionary keys),
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000263support efficient pickling, and in Boolean contexts, a \class{timedelta}
264object is considered to be true if and only if it isn't equal to
265\code{timedelta(0)}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000266
267
Raymond Hettinger6005a342002-12-30 20:01:24 +0000268\subsection{\class{date} Objects \label{datetime-date}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000269
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000270A \class{date} object represents a date (year, month and day) in an idealized
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000271calendar, the current Gregorian calendar indefinitely extended in both
272directions. January 1 of year 1 is called day number 1, January 2 of year
2731 is called day number 2, and so on. This matches the definition of the
274"proleptic Gregorian" calendar in Dershowitz and Reingold's book
Fred Drakea37e5cc2002-12-30 21:26:42 +0000275\citetitle{Calendrical Calculations}, where it's the base calendar for all
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000276computations. See the book for algorithms for converting between
277proleptic Gregorian ordinals and many other calendar systems.
278
Fred Drake0f8e5432002-12-31 18:31:48 +0000279\begin{classdesc}{date}{year, month, day}
Fred Drake436eadd2002-12-31 18:13:11 +0000280 All arguments are required. Arguments may be ints or longs, in the
281 following ranges:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000282
Fred Drake436eadd2002-12-31 18:13:11 +0000283 \begin{itemize}
284 \item \code{MINYEAR <= \var{year} <= MAXYEAR}
285 \item \code{1 <= \var{month} <= 12}
286 \item \code{1 <= \var{day} <= number of days in the given month and year}
287 \end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000288
Fred Drake436eadd2002-12-31 18:13:11 +0000289 If an argument outside those ranges is given, \exception{ValueError}
290 is raised.
Fred Drake0f8e5432002-12-31 18:31:48 +0000291\end{classdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000292
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000293Other constructors, all class methods:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000294
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000295\begin{methoddesc}{today}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000296 Return the current local date. This is equivalent to
297 \code{date.fromtimestamp(time.time())}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000298\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000299
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000300\begin{methoddesc}{fromtimestamp}{timestamp}
Fred Drake436eadd2002-12-31 18:13:11 +0000301 Return the local date corresponding to the POSIX timestamp, such
302 as is returned by \function{time.time()}. This may raise
303 \exception{ValueError}, if the timestamp is out of the range of
304 values supported by the platform C \cfunction{localtime()}
305 function. It's common for this to be restricted to years from 1970
Tim Peters75a6e3b2003-01-04 18:17:36 +0000306 through 2038. Note that on non-POSIX systems that include leap
307 seconds in their notion of a timestamp, leap seconds are ignored by
308 \method{fromtimestamp()}.
Andrew M. Kuchling0f0e6b92003-01-09 12:51:50 +0000309\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000310
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000311\begin{methoddesc}{fromordinal}{ordinal}
Fred Drake436eadd2002-12-31 18:13:11 +0000312 Return the date corresponding to the proleptic Gregorian ordinal,
313 where January 1 of year 1 has ordinal 1. \exception{ValueError} is
314 raised unless \code{1 <= \var{ordinal} <= date.max.toordinal()}.
315 For any date \var{d}, \code{date.fromordinal(\var{d}.toordinal()) ==
316 \var{d}}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000317\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000318
319Class attributes:
320
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000321\begin{memberdesc}{min}
Fred Drake436eadd2002-12-31 18:13:11 +0000322 The earliest representable date, \code{date(MINYEAR, 1, 1)}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000323\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000324
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000325\begin{memberdesc}{max}
Fred Drake436eadd2002-12-31 18:13:11 +0000326 The latest representable date, \code{date(MAXYEAR, 12, 31)}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000327\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000328
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000329\begin{memberdesc}{resolution}
Fred Drake436eadd2002-12-31 18:13:11 +0000330 The smallest possible difference between non-equal date
331 objects, \code{timedelta(days=1)}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000332\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000333
334Instance attributes (read-only):
335
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000336\begin{memberdesc}{year}
Andrew M. Kuchling9b445712003-01-09 13:46:30 +0000337 Between \constant{MINYEAR} and \constant{MAXYEAR} inclusive.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000338\end{memberdesc}
339
340\begin{memberdesc}{month}
Fred Drake436eadd2002-12-31 18:13:11 +0000341 Between 1 and 12 inclusive.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000342\end{memberdesc}
343
344\begin{memberdesc}{day}
Fred Drake436eadd2002-12-31 18:13:11 +0000345 Between 1 and the number of days in the given month of the given
346 year.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000347\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000348
349Supported operations:
350
Andrew M. Kuchling9b445712003-01-09 13:46:30 +0000351% XXX rewrite to be a table
Fred Drakebbdb2502002-12-23 18:58:06 +0000352\begin{itemize}
353 \item
354 date1 + timedelta -> date2
Tim Peters00372022003-01-09 04:10:05 +0000355
Fred Drakebbdb2502002-12-23 18:58:06 +0000356 timedelta + date1 -> date2
Tim Peters00372022003-01-09 04:10:05 +0000357
Fred Drakebbdb2502002-12-23 18:58:06 +0000358 date2 is timedelta.days days removed from the date1, moving forward
359 in time if timedelta.days > 0, or backward if timedetla.days < 0.
360 date2 - date1 == timedelta.days after. timedelta.seconds and
361 timedelta.microseconds are ignored. \exception{OverflowError} is
362 raised if date2.year would be smaller than \constant{MINYEAR} or
363 larger than \constant{MAXYEAR}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000364
Fred Drakebbdb2502002-12-23 18:58:06 +0000365 \item
366 date1 - timedelta -> date2
Tim Peters00372022003-01-09 04:10:05 +0000367
Fred Drakebbdb2502002-12-23 18:58:06 +0000368 Computes the date2 such that date2 + timedelta == date1. This
369 isn't quite equivalent to date1 + (-timedelta), because -timedelta
370 in isolation can overflow in cases where date1 - timedelta does
371 not. timedelta.seconds and timedelta.microseconds are ignored.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000372
Fred Drakebbdb2502002-12-23 18:58:06 +0000373 \item
374 date1 - date2 -> timedelta
Tim Peters00372022003-01-09 04:10:05 +0000375
Fred Drakebbdb2502002-12-23 18:58:06 +0000376 This is exact, and cannot overflow. timedelta.seconds and
377 timedelta.microseconds are 0, and date2 + timedelta == date1
378 after.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000379
Fred Drakebbdb2502002-12-23 18:58:06 +0000380 \item
381 comparison of date to date, where date1 is considered less than
382 date2 when date1 precedes date2 in time. In other words,
383 date1 < date2 if and only if date1.toordinal() < date2.toordinal().
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000384
Fred Drakebbdb2502002-12-23 18:58:06 +0000385 \item
386 hash, use as dict key
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000387
Fred Drakebbdb2502002-12-23 18:58:06 +0000388 \item
389 efficient pickling
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000390
Fred Drakebbdb2502002-12-23 18:58:06 +0000391 \item
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000392 in Boolean contexts, all \class{date} objects are considered to be true
Fred Drakebbdb2502002-12-23 18:58:06 +0000393\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000394
395Instance methods:
396
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000397\begin{methoddesc}{replace}{year, month, day}
Tim Peters00372022003-01-09 04:10:05 +0000398 Return a date with the same value, except for those members given
Fred Drake436eadd2002-12-31 18:13:11 +0000399 new values by whichever keyword arguments are specified. For
400 example, if \code{d == date(2002, 12, 31)}, then
401 \code{d.replace(day=26) == date(2000, 12, 26)}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000402\end{methoddesc}
Tim Peters12bf3392002-12-24 05:41:27 +0000403
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000404\begin{methoddesc}{timetuple}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000405 Return a 9-element tuple of the form returned by
406 \function{time.localtime()}. The hours, minutes and seconds are
407 0, and the DST flag is -1.
408 \code{\var{d}.timetuple()} is equivalent to
409 \code{(\var{d}.year, \var{d}.month, \var{d}.day,
410 0, 0, 0, \# h, m, s
411 \var{d}.weekday(), \# 0 is Monday
412 \var{d}.toordinal() - date(\var{d}.year, 1, 1).toordinal() + 1,
413 \# day of year
414 -1)}
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000415\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000416
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000417\begin{methoddesc}{toordinal}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000418 Return the proleptic Gregorian ordinal of the date, where January 1
419 of year 1 has ordinal 1. For any \class{date} object \var{d},
420 \code{date.fromordinal(\var{d}.toordinal()) == \var{d}}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000421\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000422
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000423\begin{methoddesc}{weekday}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000424 Return the day of the week as an integer, where Monday is 0 and
425 Sunday is 6. For example, date(2002, 12, 4).weekday() == 2, a
426 Wednesday.
427 See also \method{isoweekday()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000428\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000429
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000430\begin{methoddesc}{isoweekday}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000431 Return the day of the week as an integer, where Monday is 1 and
432 Sunday is 7. For example, date(2002, 12, 4).isoweekday() == 3, a
433 Wednesday.
434 See also \method{weekday()}, \method{isocalendar()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000435\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000436
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000437\begin{methoddesc}{isocalendar}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000438 Return a 3-tuple, (ISO year, ISO week number, ISO weekday).
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000439
Fred Drake436eadd2002-12-31 18:13:11 +0000440 The ISO calendar is a widely used variant of the Gregorian calendar.
441 See \url{http://www.phys.uu.nl/~vgent/calendar/isocalendar.htm}
442 for a good explanation.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000443
Fred Drake436eadd2002-12-31 18:13:11 +0000444 The ISO year consists of 52 or 53 full weeks, and where a week starts
445 on a Monday and ends on a Sunday. The first week of an ISO year is
446 the first (Gregorian) calendar week of a year containing a Thursday.
447 This is called week number 1, and the ISO year of that Thursday is
448 the same as its Gregorian year.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000449
Fred Drake436eadd2002-12-31 18:13:11 +0000450 For example, 2004 begins on a Thursday, so the first week of ISO
451 year 2004 begins on Monday, 29 Dec 2003 and ends on Sunday, 4 Jan
452 2004, so that
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000453
Fred Drake436eadd2002-12-31 18:13:11 +0000454 date(2003, 12, 29).isocalendar() == (2004, 1, 1)
455 date(2004, 1, 4).isocalendar() == (2004, 1, 7)
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000456\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000457
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000458\begin{methoddesc}{isoformat}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000459 Return a string representing the date in ISO 8601 format,
460 'YYYY-MM-DD'. For example,
461 date(2002, 12, 4).isoformat() == '2002-12-04'.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000462\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000463
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000464\begin{methoddesc}{__str__}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000465 For a date \var{d}, \code{str(\var{d})} is equivalent to
466 \code{\var{d}.isoformat()}.
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}{ctime}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000470 Return a string representing the date, for example
471 date(2002, 12, 4).ctime() == 'Wed Dec 4 00:00:00 2002'.
472 \code{\var{d}.ctime()} is equivalent to
473 \code{time.ctime(time.mktime(\var{d}.timetuple()))}
474 on platforms where the native C \cfunction{ctime()} function
475 (which \function{time.ctime()} invokes, but which
476 \method{date.ctime()} does not invoke) conforms to the C standard.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000477\end{methoddesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000478
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000479\begin{methoddesc}{strftime}{format}
Fred Drake436eadd2002-12-31 18:13:11 +0000480 Return a string representing the date, controlled by an explicit
481 format string. Format codes referring to hours, minutes or seconds
482 will see 0 values.
483 See the section on \method{strftime()} behavior.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000484\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000485
486
Raymond Hettinger6005a342002-12-30 20:01:24 +0000487\subsection{\class{datetime} Objects \label{datetime-datetime}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000488
Fred Drakebbdb2502002-12-23 18:58:06 +0000489A \class{datetime} object is a single object containing all the
Tim Peters00372022003-01-09 04:10:05 +0000490information from a \class{date} object and a \class{time} object. Like a
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000491\class{date} object, \class{datetime} assumes the current Gregorian
492calendar extended in both directions; like a time object,
493\class{datetime} assumes there are exactly 3600*24 seconds in every
494day.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000495
Tim Peters00372022003-01-09 04:10:05 +0000496Constructor:
497
Fred Drake0f8e5432002-12-31 18:31:48 +0000498\begin{classdesc}{datetime}{year, month, day,
Tim Peters00372022003-01-09 04:10:05 +0000499 hour=0, minute=0, second=0, microsecond=0,
500 tzinfo=None}
501 The year, month and day arguments are required. \var{tzinfo} may
502 be \code{None}, or an instance of a \class{tzinfo} subclass. The
503 remaining arguments may be ints or longs, in the following ranges:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000504
Fred Drake436eadd2002-12-31 18:13:11 +0000505 \begin{itemize}
Tim Peters00372022003-01-09 04:10:05 +0000506 \item \code{MINYEAR <= \var{year} <= MAXYEAR}
Fred Drake436eadd2002-12-31 18:13:11 +0000507 \item \code{1 <= \var{month} <= 12}
508 \item \code{1 <= \var{day} <= number of days in the given month and year}
509 \item \code{0 <= \var{hour} < 24}
510 \item \code{0 <= \var{minute} < 60}
511 \item \code{0 <= \var{second} < 60}
512 \item \code{0 <= \var{microsecond} < 1000000}
513 \end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000514
Tim Peters00372022003-01-09 04:10:05 +0000515 If an argument outside those ranges is given,
516 \exception{ValueError} is raised.
Fred Drake0f8e5432002-12-31 18:31:48 +0000517\end{classdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000518
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000519Other constructors, all class methods:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000520
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000521\begin{methoddesc}{today}{}
Tim Peters00372022003-01-09 04:10:05 +0000522 Return the current local datetime, with \member{tzinfo} \code{None}.
523 This is equivalent to
Fred Drake436eadd2002-12-31 18:13:11 +0000524 \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}{}
Tim Peters00372022003-01-09 04:10:05 +0000538 Return the current UTC datetime, with \member{tzinfo} \code{None}.
539 This is like \method{now()}, but
Fred Drake436eadd2002-12-31 18:13:11 +0000540 returns the current UTC date and time.
541 See also \method{now()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000542\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000543
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000544\begin{methoddesc}{fromtimestamp}{timestamp}
Fred Drake436eadd2002-12-31 18:13:11 +0000545 Return the local \class{datetime} corresponding to the \POSIX{}
546 timestamp, such as is returned by \function{time.time()}. This
547 may raise \exception{ValueError}, if the timestamp is out of the
548 range of values supported by the platform C
549 \cfunction{localtime()} function. It's common for this to be
550 restricted to years in 1970 through 2038.
Tim Peters75a6e3b2003-01-04 18:17:36 +0000551 Note that on non-POSIX systems that include leap seconds in their
552 notion of a timestamp, leap seconds are ignored by
553 \method{fromtimestamp()}, and then it's possible to have two timestamps
554 differing by a second that yield identical \class{datetime} objects.
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{}
Tim Peters00372022003-01-09 04:10:05 +0000560 timestamp, with \member{tzinfo} \code{None}.
561 This may raise \exception{ValueError}, if the
Fred Drake436eadd2002-12-31 18:13:11 +0000562 timestamp is out of the range of values supported by the platform
563 C \cfunction{gmtime()} function. It's common for this to be
564 restricted to years in 1970 through 2038.
565 See also \method{fromtimestamp()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000566\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000567
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000568\begin{methoddesc}{fromordinal}{ordinal}
Fred Drake436eadd2002-12-31 18:13:11 +0000569 Return the \class{datetime} corresponding to the proleptic
570 Gregorian ordinal, where January 1 of year 1 has ordinal 1.
571 \exception{ValueError} is raised unless 1 <= ordinal <=
572 datetime.max.toordinal(). The hour, minute, second and
Tim Peters00372022003-01-09 04:10:05 +0000573 microsecond of the result are all 0,
574 and \member{tzinfo} is \code{None}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000575\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000576
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000577\begin{methoddesc}{combine}{date, time}
Tim Peters00372022003-01-09 04:10:05 +0000578 Return a new \class{datetime} object whose date members are
Fred Drake436eadd2002-12-31 18:13:11 +0000579 equal to the given \class{date} object's, and whose time
Tim Peters00372022003-01-09 04:10:05 +0000580 and \member{tzinfo} members are equal to the given \class{time} object's.
581 For any \class{datetime} object \var{d}, \code{\var{d} ==
582 datetime.combine(\var{d}.date(), \var{d}.timetz())}. If date is a
583 \class{datetime} object, its time and \member{tzinfo} members are
584 ignored.
585 \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},
Tim Peters00372022003-01-09 04:10:05 +0000591 \code{datetime(MINYEAR, 1, 1, tzinfo=None)}.
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},
Tim Peters00372022003-01-09 04:10:05 +0000596 \code{datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999, tzinfo=None)}.
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}
Tim Peters00372022003-01-09 04:10:05 +0000607 Between \constant{MINYEAR} and \constant{MAXYEAR} inclusive.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000608\end{memberdesc}
609
610\begin{memberdesc}{month}
Tim Peters00372022003-01-09 04:10:05 +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
Tim Peters00372022003-01-09 04:10:05 +0000635\begin{memberdesc}{tzinfo}
636 The object passed as the \var{tzinfo} argument to the
637 \class{datetime} constructor, or \code{None} if none was passed.
638\end{memberdesc}
639
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000640Supported operations:
641
Fred Drakebbdb2502002-12-23 18:58:06 +0000642\begin{itemize}
643 \item
644 datetime1 + timedelta -> datetime2
Tim Peters00372022003-01-09 04:10:05 +0000645
Fred Drakebbdb2502002-12-23 18:58:06 +0000646 timedelta + datetime1 -> datetime2
Tim Peters00372022003-01-09 04:10:05 +0000647
Fred Drakebbdb2502002-12-23 18:58:06 +0000648 datetime2 is a duration of timedelta removed from datetime1, moving
649 forward in time if timedelta.days > 0, or backward if
Tim Peters00372022003-01-09 04:10:05 +0000650 timedelta.days < 0. The result has the same \member{tzinfo} member
651 as the input datetime, and datetime2 - datetime1 == timedelta after.
Fred Drakebbdb2502002-12-23 18:58:06 +0000652 \exception{OverflowError} is raised if datetime2.year would be
653 smaller than \constant{MINYEAR} or larger than \constant{MAXYEAR}.
Tim Peters00372022003-01-09 04:10:05 +0000654 Note that no time zone adjustments are done even if the input is an
655 aware object.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000656
Fred Drakebbdb2502002-12-23 18:58:06 +0000657 \item
658 datetime1 - timedelta -> datetime2
Tim Peters00372022003-01-09 04:10:05 +0000659
Fred Drakebbdb2502002-12-23 18:58:06 +0000660 Computes the datetime2 such that datetime2 + timedelta == datetime1.
Tim Peters00372022003-01-09 04:10:05 +0000661 As for addition, the result has the same \member{tzinfo} member
662 as the input datetime, and no time zone adjustments are done even
663 if the input is aware.
Fred Drakebbdb2502002-12-23 18:58:06 +0000664 This isn't quite equivalent to datetime1 + (-timedelta), because
665 -timedelta in isolation can overflow in cases where
666 datetime1 - timedelta does not.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000667
Fred Drakebbdb2502002-12-23 18:58:06 +0000668 \item
669 datetime1 - datetime2 -> timedelta
Tim Peters00372022003-01-09 04:10:05 +0000670
671 Subtraction of a \class{datetime} from a
672 \class{datetime} is defined only if both
673 operands are naive, or if both are aware. If one is aware and the
674 other is naive, \exception{TypeError} is raised.
675
676 If both are naive, or both are aware and have the same \member{tzinfo}
677 member, the \member{tzinfo} members are ignored, and the result is
678 a \class{timedelta} object \var{t} such that
679 \code{\var{datetime2} + \var{t} == \var{datetime1}}. No time zone
680 adjustments are done in this case.
681
682 If both are aware and have different \member{tzinfo} members,
683 \code{a-b} acts as if \var{a} and \var{b} were first converted to
684 naive UTC datetimes first. The result is
685 \code{(\var{a}.replace(tzinfo=None) - \var{a}.utcoffset()) -
686 (\var{b}.replace(tzinfo=None) - \var{b}.utcoffset())}
687 except that the implementation never overflows.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000688
Fred Drakebbdb2502002-12-23 18:58:06 +0000689 \item
Tim Peters00372022003-01-09 04:10:05 +0000690 comparison of \class{datetime} to \class{datetime},
691 where \var{a} is considered less than \var{b}
692 when \var{a} precedes \var{b} in time. If one comparand is naive and
693 the other is aware, \exception{TypeError} is raised. If both
694 comparands are aware, and have the same \member{tzinfo} member,
695 the common \member{tzinfo} member is ignored and the base datetimes
696 are compared. If both comparands are aware and have different
697 \member{tzinfo} members, the comparands are first adjusted by
698 subtracting their UTC offsets (obtained from \code{self.utcoffset()}).
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000699
Fred Drakebbdb2502002-12-23 18:58:06 +0000700 \item
701 hash, use as dict key
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000702
Fred Drakebbdb2502002-12-23 18:58:06 +0000703 \item
704 efficient pickling
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000705
Fred Drakebbdb2502002-12-23 18:58:06 +0000706 \item
707 in Boolean contexts, all \class{datetime} objects are considered
708 to be true
709\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000710
711Instance methods:
712
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000713\begin{methoddesc}{date}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000714 Return \class{date} object with same year, month and day.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000715\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000716
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000717\begin{methoddesc}{time}{}
Tim Peters00372022003-01-09 04:10:05 +0000718 Return \class{time} object with same hour, minute, second and microsecond.
719 \member{tzinfo} is \code{None}. See also method \method{timetz()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000720\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000721
Tim Peters00372022003-01-09 04:10:05 +0000722\begin{methoddesc}{timetz}{}
723 Return \class{time} object with same hour, minute, second, microsecond,
724 and tzinfo members. See also method \method{time()}.
725\end{methoddesc}
726
727\begin{methoddesc}{replace}{year=, month=, day=, hour=, minute=, second=,
728 microsecond=, tzinfo=}
729 Return a datetime with the same members, except for those members given
730 new values by whichever keyword arguments are specified. Note that
731 \code{tzinfo=None} can be specified to create a naive datetime from
732 an aware datetime with no conversion of date and time members.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000733\end{methoddesc}
Tim Peters12bf3392002-12-24 05:41:27 +0000734
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000735\begin{methoddesc}{astimezone}{tz}
Tim Peters00372022003-01-09 04:10:05 +0000736 Return a \class{datetime} object with new \member{tzinfo} member
Tim Petersf196a0a2003-01-22 04:45:50 +0000737 \var{tz}, adjusting the date and time members so the result is the
738 same UTC time as \var{self}, but in \var{tz}'s local time.
739
740 \var{tz} must be an instance of a \class{tzinfo} subclass, and its
741 \method{utcoffset()} and \method{dst()} methods must not return
742 \code{None}. \var{self} must be aware (\code{\var{self}.tzinfo} must
743 not be \code{None}, and \code{\var{self}.utcoffset()} must not return
744 \code{None}).
745
746 If code{\var{self}.tzinfo} is \var{tz},
747 \code{\var{self}.astimezone(\var{tz})} is equal to \var{self}: no
748 adjustment of date or time members is performed.
749 Else the result is local time in time zone \var{tz}, representing the
750 same UTC time as \var{self}: after \code{\var{astz} =
751 \var{dt}.astimezone(\var{tz})},
752 \code{\var{astz} - \var{astz}.utcoffset()} will usually have the same
753 date and time members as \code{\var{dt} - \var{dt}.utcoffset()}.
754 The discussion of class \class{tzinfo} explains the cases at Daylight
755 Saving Time transition boundaries where this cannot be achieved (an issue
756 only if \var{tz} models both standard and daylight time).
757
758 If you merely want to attach a time zone object \var{tz} to a
759 datetime \var{dt} without adjustment of date and time members,
760 use \code{\var{dt}.replace(tzinfo=\var{tz})}. If
761 you merely want to remove the time zone object from an aware datetime
762 \var{dt} without conversion of date and time members, use
763 \code{\var{dt}.replace(tzinfo=None)}.
764
765 Note that the default \method{tzinfo.fromutc()} method can be overridden
766 in a \class{tzinfo} subclass to effect the result returned by
767 \method{astimezone()}. Ignoring error cases, \method{astimezone()}
768 acts like:
769
770 \begin{verbatim}
771 def astimezone(self, tz):
772 if self.tzinfo is tz:
773 return self
774 # Convert self to UTC, and attach the new time zone object.
775 utc = (self - self.utcoffset()).replace(tzinfo=tz)
776 # Convert from UTC to tz's local time.
777 return tz.fromutc(utc)
778 \end{verbatim}
Tim Peters00372022003-01-09 04:10:05 +0000779\end{methoddesc}
780
781\begin{methoddesc}{utcoffset}{}
782 If \member{tzinfo} is \code{None}, returns \code{None}, else
Tim Petersf196a0a2003-01-22 04:45:50 +0000783 returns \code{\var{self}.tzinfo.utcoffset(\var{self})}, and
784 raises an exception if the latter doesn't return \code{None}, or
785 a \class{timedelta} object representing a whole number of minutes
786 with magnitude less than one day.
787\end{methoddesc}
788
789\begin{methoddesc}{dst}{}
790 If \member{tzinfo} is \code{None}, returns \code{None}, else
791 returns \code{\var{self}.tzinfo.dst(\var{self})}, and
792 raises an exception if the latter doesn't return \code{None}, or
793 a \class{timedelta} object representing a whole number of minutes
794 with magnitude less than one day.
Tim Peters00372022003-01-09 04:10:05 +0000795\end{methoddesc}
796
797\begin{methoddesc}{tzname}{}
798 If \member{tzinfo} is \code{None}, returns \code{None}, else
Tim Petersf196a0a2003-01-22 04:45:50 +0000799 returns \code{\var{self}.tzinfo.tzname(\var{self})},
800 raises an exception if the latter doesn't return \code{None} or
801 a string object,
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000802\end{methoddesc}
Tim Peters80475bb2002-12-25 07:40:55 +0000803
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000804\begin{methoddesc}{timetuple}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000805 Return a 9-element tuple of the form returned by
806 \function{time.localtime()}.
Tim Peters00372022003-01-09 04:10:05 +0000807 \code{\var{d}.timetuple()} is equivalent to
Fred Drake436eadd2002-12-31 18:13:11 +0000808 \code{(\var{d}.year, \var{d}.month, \var{d}.day,
809 \var{d}.hour, \var{d}.minute, \var{d}.second,
Tim Peters00372022003-01-09 04:10:05 +0000810 \var{d}.weekday(),
Fred Drake436eadd2002-12-31 18:13:11 +0000811 \var{d}.toordinal() - date(\var{d}.year, 1, 1).toordinal() + 1,
Tim Peters00372022003-01-09 04:10:05 +0000812 dst)}
813 The \member{tm_isdst} flag of the result is set according to
814 the \method{dst()} method: \member{tzinfo} is \code{None} or
815 \method{dst()} returns \code{None},
816 \member{tm_isdst} is set to \code{-1}; else if \method{dst()} returns
817 a non-zero value, \member{tm_isdst} is set to \code{1};
818 else \code{tm_isdst} is set to \code{0}.
819\end{methoddesc}
820
821\begin{methoddesc}{utctimetuple}{}
822 If \class{datetime} instance \var{d} is naive, this is the same as
823 \code{\var{d}.timetuple()} except that \member{tm_isdst} is forced to 0
824 regardless of what \code{d.dst()} returns. DST is never in effect
825 for a UTC time.
826
827 If \var{d} is aware, \var{d} is normalized to UTC time, by subtracting
828 \code{\var{d}.utcoffset()}, and a timetuple for the
829 normalized time is returned. \member{tm_isdst} is forced to 0.
830 Note that the result's \member{tm_year} member may be
831 \constant{MINYEAR}-1 or \constant{MAXYEAR}+1, if \var{d}.year was
832 \code{MINYEAR} or \code{MAXYEAR} and UTC adjustment spills over a
833 year boundary.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000834\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000835
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000836\begin{methoddesc}{toordinal}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000837 Return the proleptic Gregorian ordinal of the date. The same as
Tim Peters00372022003-01-09 04:10:05 +0000838 \code{self.date().toordinal()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000839\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000840
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000841\begin{methoddesc}{weekday}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000842 Return the day of the week as an integer, where Monday is 0 and
Tim Peters00372022003-01-09 04:10:05 +0000843 Sunday is 6. The same as \code{self.date().weekday()}.
Fred Drake436eadd2002-12-31 18:13:11 +0000844 See also \method{isoweekday()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000845\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000846
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000847\begin{methoddesc}{isoweekday}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000848 Return the day of the week as an integer, where Monday is 1 and
Tim Peters00372022003-01-09 04:10:05 +0000849 Sunday is 7. The same as \code{self.date().isoweekday)}.
Fred Drake436eadd2002-12-31 18:13:11 +0000850 See also \method{weekday()}, \method{isocalendar()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000851\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000852
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000853\begin{methoddesc}{isocalendar}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000854 Return a 3-tuple, (ISO year, ISO week number, ISO weekday). The
Tim Peters00372022003-01-09 04:10:05 +0000855 same as \code{self.date().isocalendar()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000856\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000857
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000858\begin{methoddesc}{isoformat}{sep='T'}
Fred Drake436eadd2002-12-31 18:13:11 +0000859 Return a string representing the date and time in ISO 8601 format,
860 YYYY-MM-DDTHH:MM:SS.mmmmmm
Tim Peters00372022003-01-09 04:10:05 +0000861 or, if \member{microsecond} is 0,
Fred Drake436eadd2002-12-31 18:13:11 +0000862 YYYY-MM-DDTHH:MM:SS
Tim Peters00372022003-01-09 04:10:05 +0000863
864 If \method{utcoffset()} does not return \code{None}, a 6-character
865 string is appended, giving the UTC offset in (signed) hours and
866 minutes:
867 YYYY-MM-DDTHH:MM:SS.mmmmmm+HH:MM
868 or, if \member{microsecond} is 0
869 YYYY-MM-DDTHH:MM:SS+HH:MM
870
Fred Drake436eadd2002-12-31 18:13:11 +0000871 The optional argument \var{sep} (default \code{'T'}) is a
872 one-character separator, placed between the date and time portions
873 of the result. For example,
Tim Peters00372022003-01-09 04:10:05 +0000874
875\begin{verbatim}
876>>> from datetime import tzinfo, timedelta, datetime
877>>> class TZ(tzinfo):
878... def utcoffset(self, dt): return timedelta(minutes=-399)
879...
880>>> datetime(2002, 12, 25, tzinfo=TZ()).isoformat(' ')
881'2002-12-25 00:00:00-06:39'
882\end{verbatim}
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000883\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000884
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000885\begin{methoddesc}{__str__}{}
Fred Drake436eadd2002-12-31 18:13:11 +0000886 For a \class{datetime} instance \var{d}, \code{str(\var{d})} is
887 equivalent to \code{\var{d}.isoformat(' ')}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000888\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000889
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000890\begin{methoddesc}{ctime}{}
Tim Peters00372022003-01-09 04:10:05 +0000891 Return a string representing the date and time, for example
892 \code{datetime(2002, 12, 4, 20, 30, 40).ctime() ==
893 'Wed Dec 4 20:30:40 2002'}.
Fred Drake436eadd2002-12-31 18:13:11 +0000894 \code{d.ctime()} is equivalent to
895 \code{time.ctime(time.mktime(d.timetuple()))} on platforms where
896 the native C \cfunction{ctime()} function (which
897 \function{time.ctime()} invokes, but which
898 \method{datetime.ctime()} does not invoke) conforms to the C
899 standard.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000900\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000901
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000902\begin{methoddesc}{strftime}{format}
Fred Drake436eadd2002-12-31 18:13:11 +0000903 Return a string representing the date and time, controlled by an
904 explicit format string. See the section on \method{strftime()}
905 behavior.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000906\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000907
908
Raymond Hettinger6005a342002-12-30 20:01:24 +0000909\subsection{\class{time} Objects \label{datetime-time}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000910
Tim Peters00372022003-01-09 04:10:05 +0000911A time object represents a (local) time of day, independent of any
912particular day, and subject to adjustment via a \class{tzinfo} object.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000913
Tim Peters00372022003-01-09 04:10:05 +0000914\begin{classdesc}{time}{hour=0, minute=0, second=0, microsecond=0,
915 tzinfo=None}
916 All arguments are optional. \var{tzinfo} may be \code{None}, or
917 an instance of a \class{tzinfo} subclass. The remaining arguments
918 may be ints or longs, in the following ranges:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000919
Tim Peters00372022003-01-09 04:10:05 +0000920 \begin{itemize}
921 \item \code{0 <= \var{hour} < 24}
922 \item \code{0 <= \var{minute} < 60}
923 \item \code{0 <= \var{second} < 60}
924 \item \code{0 <= \var{microsecond} < 1000000}.
925 \end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000926
Tim Peters00372022003-01-09 04:10:05 +0000927 If an argument outside those ranges is given,
928 \exception{ValueError} is raised.
Fred Drake0f8e5432002-12-31 18:31:48 +0000929\end{classdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000930
931Class attributes:
932
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000933\begin{memberdesc}{min}
Fred Drake436eadd2002-12-31 18:13:11 +0000934 The earliest representable \class{time}, \code{time(0, 0, 0, 0)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000935\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000936
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000937\begin{memberdesc}{max}
Fred Drake436eadd2002-12-31 18:13:11 +0000938 The latest representable \class{time}, \code{time(23, 59, 59, 999999)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000939\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000940
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000941\begin{memberdesc}{resolution}
Fred Drake436eadd2002-12-31 18:13:11 +0000942 The smallest possible difference between non-equal \class{time}
943 objects, \code{timedelta(microseconds=1)}, although note that
944 arithmetic on \class{time} objects is not supported.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000945\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000946
947Instance attributes (read-only):
948
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000949\begin{memberdesc}{hour}
Fred Drake436eadd2002-12-31 18:13:11 +0000950 In \code{range(24)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000951\end{memberdesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000952
Tim Petersbad8ff02002-12-30 20:52:32 +0000953\begin{memberdesc}{minute}
Fred Drake436eadd2002-12-31 18:13:11 +0000954 In \code{range(60)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000955\end{memberdesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000956
Tim Petersbad8ff02002-12-30 20:52:32 +0000957\begin{memberdesc}{second}
Fred Drake436eadd2002-12-31 18:13:11 +0000958 In \code{range(60)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000959\end{memberdesc}
Fred Drake436eadd2002-12-31 18:13:11 +0000960
Tim Petersbad8ff02002-12-30 20:52:32 +0000961\begin{memberdesc}{microsecond}
Fred Drake436eadd2002-12-31 18:13:11 +0000962 In \code{range(1000000)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000963\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000964
Tim Peters00372022003-01-09 04:10:05 +0000965\begin{memberdesc}{tzinfo}
966 The object passed as the tzinfo argument to the \class{time}
967 constructor, or \code{None} if none was passed.
968\end{memberdesc}
969
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000970Supported operations:
971
Fred Drakebbdb2502002-12-23 18:58:06 +0000972\begin{itemize}
973 \item
Tim Peters00372022003-01-09 04:10:05 +0000974 comparison of \class{time} to \class{time},
975 where \var{a} is considered less than \var{b} when \var{a} precedes
976 \var{b} in time. If one comparand is naive and the other is aware,
977 \exception{TypeError} is raised. If both comparands are aware, and
978 have the same \member{tzinfo} member, the common \member{tzinfo}
979 member is ignored and the base times are compared. If both
980 comparands are aware and have different \member{tzinfo} members,
981 the comparands are first adjusted by subtracting their UTC offsets
982 (obtained from \code{self.utcoffset()}).
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000983
Fred Drakebbdb2502002-12-23 18:58:06 +0000984 \item
985 hash, use as dict key
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000986
Fred Drakebbdb2502002-12-23 18:58:06 +0000987 \item
988 efficient pickling
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000989
Fred Drakebbdb2502002-12-23 18:58:06 +0000990 \item
Tim Peters00372022003-01-09 04:10:05 +0000991 in Boolean contexts, a \class{time} object is considered to be
992 true if and only if, after converting it to minutes and
993 subtracting \method{utcoffset()} (or \code{0} if that's
994 \code{None}), the result is non-zero.
Fred Drakebbdb2502002-12-23 18:58:06 +0000995\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000996
997Instance methods:
998
Tim Peters00372022003-01-09 04:10:05 +0000999\begin{methoddesc}{replace}(hour=, minute=, second=, microsecond=, tzinfo=)
1000 Return a \class{time} with the same value, except for those members given
1001 new values by whichever keyword arguments are specified. Note that
1002 \code{tzinfo=None} can be specified to create a naive \class{time} from
1003 an aware \class{time}, without conversion of the time members.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001004\end{methoddesc}
Tim Peters12bf3392002-12-24 05:41:27 +00001005
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001006\begin{methoddesc}{isoformat}{}
Fred Drake436eadd2002-12-31 18:13:11 +00001007 Return a string representing the time in ISO 8601 format,
1008 HH:MM:SS.mmmmmm
Tim Peters00372022003-01-09 04:10:05 +00001009 or, if self.microsecond is 0,
Fred Drake436eadd2002-12-31 18:13:11 +00001010 HH:MM:SS
Tim Peters00372022003-01-09 04:10:05 +00001011 If \method{utcoffset()} does not return \code{None}, a 6-character
1012 string is appended, giving the UTC offset in (signed) hours and
1013 minutes:
1014 HH:MM:SS.mmmmmm+HH:MM
1015 or, if self.microsecond is 0,
1016 HH:MM:SS+HH:MM
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001017\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001018
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001019\begin{methoddesc}{__str__}{}
Fred Drake436eadd2002-12-31 18:13:11 +00001020 For a time \var{t}, \code{str(\var{t})} is equivalent to
1021 \code{\var{t}.isoformat()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001022\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001023
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001024\begin{methoddesc}{strftime}{format}
Fred Drake436eadd2002-12-31 18:13:11 +00001025 Return a string representing the time, controlled by an explicit
1026 format string. See the section on \method{strftime()} behavior.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001027\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001028
Tim Peters00372022003-01-09 04:10:05 +00001029\begin{methoddesc}{utcoffset}{}
1030 If \member{tzinfo} is \code{None}, returns \code{None}, else
Tim Petersf196a0a2003-01-22 04:45:50 +00001031 returns \code{\var{self}.tzinfo.utcoffset(None)}, and
1032 raises an exception if the latter doesn't return \code{None} or
1033 a \class{timedelta} object representing a whole number of minutes
1034 with magnitude less than one day.
Tim Peters00372022003-01-09 04:10:05 +00001035\end{methoddesc}
1036
1037\begin{methoddesc}{dst}{}
1038 If \member{tzinfo} is \code{None}, returns \code{None}, else
Tim Petersf196a0a2003-01-22 04:45:50 +00001039 returns \code{\var{self}.tzinfo.dst(None)}, and
1040 raises an exception if the latter doesn't return \code{None}, or
1041 a \class{timedelta} object representing a whole number of minutes
1042 with magnitude less than one day.
Tim Peters00372022003-01-09 04:10:05 +00001043\end{methoddesc}
1044
1045\begin{methoddesc}{tzname}{}
1046 If \member{tzinfo} is \code{None}, returns \code{None}, else
Tim Petersf196a0a2003-01-22 04:45:50 +00001047 returns \code{\var{self}.tzinfo.tzname(None)}, or
1048 raises an exception if the latter doesn't return \code{None} or
1049 a string object.
Tim Peters00372022003-01-09 04:10:05 +00001050\end{methoddesc}
1051
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001052
Raymond Hettinger6005a342002-12-30 20:01:24 +00001053\subsection{\class{tzinfo} Objects \label{datetime-tzinfo}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001054
Fred Drakebbdb2502002-12-23 18:58:06 +00001055\class{tzinfo} is an abstract base clase, meaning that this class
1056should not be instantiated directly. You need to derive a concrete
1057subclass, and (at least) supply implementations of the standard
1058\class{tzinfo} methods needed by the \class{datetime} methods you
Tim Petersbad8ff02002-12-30 20:52:32 +00001059use. The \module{datetime} module does not supply any concrete
Fred Drakebbdb2502002-12-23 18:58:06 +00001060subclasses of \class{tzinfo}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001061
Fred Drakebbdb2502002-12-23 18:58:06 +00001062An instance of (a concrete subclass of) \class{tzinfo} can be passed
Tim Peters00372022003-01-09 04:10:05 +00001063to the constructors for \class{datetime} and \class{time} objects.
1064The latter objects view their members as being in local time, and the
Fred Drakebbdb2502002-12-23 18:58:06 +00001065\class{tzinfo} object supports methods revealing offset of local time
1066from UTC, the name of the time zone, and DST offset, all relative to a
1067date or time object passed to them.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001068
Tim Petersbad8ff02002-12-30 20:52:32 +00001069Special requirement for pickling: A \class{tzinfo} subclass must have an
Tim Peters2483b612002-12-24 16:30:58 +00001070\method{__init__} method that can be called with no arguments, else it
1071can be pickled but possibly not unpickled again. This is a technical
1072requirement that may be relaxed in the future.
1073
Fred Drakebbdb2502002-12-23 18:58:06 +00001074A concrete subclass of \class{tzinfo} may need to implement the
1075following methods. Exactly which methods are needed depends on the
Tim Petersbad8ff02002-12-30 20:52:32 +00001076uses made of aware \module{datetime} objects. If in doubt, simply
1077implement all of them.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001078
Tim Petersbad8ff02002-12-30 20:52:32 +00001079\begin{methoddesc}{utcoffset}{self, dt}
Fred Drake436eadd2002-12-31 18:13:11 +00001080 Return offset of local time from UTC, in minutes east of UTC. If
1081 local time is west of UTC, this should be negative. Note that this
1082 is intended to be the total offset from UTC; for example, if a
1083 \class{tzinfo} object represents both time zone and DST adjustments,
1084 \method{utcoffset()} should return their sum. If the UTC offset
1085 isn't known, return \code{None}. Else the value returned must be
Tim Peters397301e2003-01-02 21:28:08 +00001086 a \class{timedelta} object specifying a whole number of minutes in the
1087 range -1439 to 1439 inclusive (1440 = 24*60; the magnitude of the offset
1088 must be less than one day). Most implementations of
1089 \method{utcoffset()} will probably look like one of these two:
Fred Drake436eadd2002-12-31 18:13:11 +00001090
Tim Petersbad8ff02002-12-30 20:52:32 +00001091\begin{verbatim}
Tim Petersf3615152003-01-01 21:51:37 +00001092 return CONSTANT # fixed-offset class
Fred Drake436eadd2002-12-31 18:13:11 +00001093 return CONSTANT + self.dst(dt) # daylight-aware class
Guido van Rossum8e7ec7c2002-12-31 04:39:05 +00001094\end{verbatim}
Tim Peters710fb152003-01-02 19:35:54 +00001095
1096 If \method{utcoffset()} does not return \code{None},
1097 \method{dst()} should not return \code{None} either.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001098
Tim Peters327098a2003-01-20 22:54:38 +00001099 The default implementation of \method{utcoffset()} raises
1100 \exception{NotImplementedError}.
1101\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001102
Tim Petersbad8ff02002-12-30 20:52:32 +00001103\begin{methoddesc}{dst}{self, dt}
Tim Peters00372022003-01-09 04:10:05 +00001104 Return the daylight saving time (DST) adjustment, in minutes east of
Tim Petersb01c39b2003-01-21 16:44:27 +00001105 UTC, or \code{None} if DST information isn't known. Return
1106 \code{timedelta(0)} if DST is not in effect.
Tim Peters397301e2003-01-02 21:28:08 +00001107 If DST is in effect, return the offset as a
Fred Drake436eadd2002-12-31 18:13:11 +00001108 \class{timedelta} object (see \method{utcoffset()} for details).
1109 Note that DST offset, if applicable, has
1110 already been added to the UTC offset returned by
1111 \method{utcoffset()}, so there's no need to consult \method{dst()}
Tim Peters327098a2003-01-20 22:54:38 +00001112 unless you're interested in obtaining DST info separately. For
Tim Peters00372022003-01-09 04:10:05 +00001113 example, \method{datetime.timetuple()} calls its \member{tzinfo}
Fred Drake436eadd2002-12-31 18:13:11 +00001114 member's \method{dst()} method to determine how the
Tim Petersf3615152003-01-01 21:51:37 +00001115 \member{tm_isdst} flag should be set, and
Tim Petersf196a0a2003-01-22 04:45:50 +00001116 \method{tzinfo.fromutc()} calls \method{dst()} to account for
Tim Petersf3615152003-01-01 21:51:37 +00001117 DST changes when crossing time zones.
1118
1119 An instance \var{tz} of a \class{tzinfo} subclass that models both
1120 standard and daylight times must be consistent in this sense:
1121
Tim Petersf196a0a2003-01-22 04:45:50 +00001122 \code{\var{tz}.utcoffset(\var{dt}) - \var{tz}.dst(\var{dt})}
Tim Petersf3615152003-01-01 21:51:37 +00001123
Tim Peters00372022003-01-09 04:10:05 +00001124 must return the same result for every \class{datetime} \var{dt}
Tim Petersf196a0a2003-01-22 04:45:50 +00001125 with \code{\var{dt}.tzinfo==\var{tz}} For sane \class{tzinfo}
1126 subclasses, this expression yields the time zone's "standard offset",
1127 which should not depend on the date or the time, but only on geographic
1128 location. The implementation of \method{datetime.astimezone()} relies
1129 on this, but cannot detect violations; it's the programmer's
1130 responsibility to ensure it. If a \class{tzinfo} subclass cannot
1131 guarantee this, it may be able to override the default implementation
1132 of \method{tzinfo.fromutc()} to work correctly with \method{astimezone()}
1133 regardless.
1134
1135 Most implementations of \method{dst()} will probably look like one
1136 of these two:
1137
1138\begin{verbatim}
1139 return timedelta(0) # a fixed-offset class: doesn't account for DST
1140
1141 or
1142
1143 # Code to set dston and dstoff to the time zone's DST transition
1144 # times based on the input dt.year, and expressed in standard local
1145 # time. Then
1146
1147 if dston <= dt.replace(tzinfo=None) < dstoff:
1148 return timedelta(hours=1)
1149 else:
1150 return timedelta(0)
1151\end{verbatim}
Tim Petersf3615152003-01-01 21:51:37 +00001152
Tim Peters327098a2003-01-20 22:54:38 +00001153 The default implementation of \method{dst()} raises
1154 \exception{NotImplementedError}.
1155\end{methoddesc}
1156
Tim Peters710fb152003-01-02 19:35:54 +00001157\begin{methoddesc}{tzname}{self, dt}
Tim Petersf196a0a2003-01-22 04:45:50 +00001158 Return the time zone name corresponding to the \class{datetime}
1159 object \var{dt}, as a string.
1160 Nothing about string names is defined by the
1161 \module{datetime} module, and there's no requirement that it mean
1162 anything in particular. For example, "GMT", "UTC", "-500", "-5:00",
1163 "EDT", "US/Eastern", "America/New York" are all valid replies. Return
Tim Peters710fb152003-01-02 19:35:54 +00001164 \code{None} if a string name isn't known. Note that this is a method
Tim Petersf196a0a2003-01-22 04:45:50 +00001165 rather than a fixed string primarily because some \class{tzinfo}
1166 subclasses will wish to return different names depending on the specific
1167 value of \var{dt} passed, especially if the \class{tzinfo} class is
Tim Peters710fb152003-01-02 19:35:54 +00001168 accounting for daylight time.
Tim Peters710fb152003-01-02 19:35:54 +00001169
Tim Peters327098a2003-01-20 22:54:38 +00001170 The default implementation of \method{tzname()} raises
1171 \exception{NotImplementedError}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001172\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001173
Tim Peters00372022003-01-09 04:10:05 +00001174These methods are called by a \class{datetime} or \class{time} object,
1175in response to their methods of the same names. A \class{datetime}
1176object passes itself as the argument, and a \class{time} object passes
Tim Petersbad8ff02002-12-30 20:52:32 +00001177\code{None} as the argument. A \class{tzinfo} subclass's methods should
1178therefore be prepared to accept a \var{dt} argument of \code{None}, or of
Tim Peters00372022003-01-09 04:10:05 +00001179class \class{datetime}.
Tim Petersbad8ff02002-12-30 20:52:32 +00001180
1181When \code{None} is passed, it's up to the class designer to decide the
1182best response. For example, returning \code{None} is appropriate if the
Tim Peters00372022003-01-09 04:10:05 +00001183class wishes to say that time objects don't participate in the
Tim Petersf196a0a2003-01-22 04:45:50 +00001184\class{tzinfo} protocols. It may be more useful for \code{utcoffset(None)}
Tim Peters327098a2003-01-20 22:54:38 +00001185to return the standard UTC offset, as there is no other convention for
1186discovering the standard offset.
Tim Petersbad8ff02002-12-30 20:52:32 +00001187
Tim Peters00372022003-01-09 04:10:05 +00001188When a \class{datetime} object is passed in response to a
1189\class{datetime} method, \code{dt.tzinfo} is the same object as
Tim Petersbad8ff02002-12-30 20:52:32 +00001190\var{self}. \class{tzinfo} methods can rely on this, unless
1191user code calls \class{tzinfo} methods directly. The intent is that
1192the \class{tzinfo} methods interpret \var{dt} as being in local time,
Tim Peters327098a2003-01-20 22:54:38 +00001193and not need worry about objects in other timezones.
Tim Petersbad8ff02002-12-30 20:52:32 +00001194
Tim Petersf196a0a2003-01-22 04:45:50 +00001195There is one more \class{tzinfo} method that a subclass may wish to
1196override:
1197
1198\begin{methoddesc}{fromutc}{self, dt}
1199 This is called from the default \class{datetime.astimezone()}
1200 implementation. When called from that, \code{\var{dt}.tzinfo} is
1201 \var{self}, and \var{dt}'s date and time members are to be viewed as
1202 expressing a UTC time. The purpose of \method{fromutc()} is to
1203 adjust the date and time members, returning an equivalent datetime in
1204 \var{self}'s local time.
1205
1206 Most \class{tzinfo} subclasses should be able to inherit the default
1207 \method{fromutc()} implementation without problems. It's strong enough
1208 to handle fixed-offset time zones, and time zones accounting for both
1209 standard and daylight time, and the latter even if the DST transition
1210 times differ in different years. An example of a time zone the default
1211 \method{fromutc()} implementation may not handle correctly in all cases
1212 is one where the standard offset (from UTC) depends on the specific date
1213 and time passed, which can happen for political reasons.
1214 The default implementations of \method{astimezone()} and
1215 \method{fromutc()} may not produce the result you want if the result is
1216 one of the hours straddling the moment the standard offset changes.
1217
1218 Skipping code for error cases, the default \method{fromutc()}
1219 implementation acts like:
1220
1221 \begin{verbatim}
1222 def fromutc(self, dt):
1223 # raise ValueError error if dt.tzinfo is not self
1224 dtoff = dt.utcoffset()
1225 dtdst = dt.dst()
1226 # raise ValueError if dtoff is None or dtdst is None
1227 delta = dtoff - dtdst # this is self's standard offset
1228 if delta:
1229 dt += delta # convert to standard local time
1230 dtdst = dt.dst()
1231 # raise ValueError if dtdst is None
1232 if dtdst:
1233 return dt + dtdst
1234 else:
1235 return dt
1236 \end{verbatim}
1237\end{methoddesc}
1238
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001239Example \class{tzinfo} classes:
1240
Fred Drakebbdb2502002-12-23 18:58:06 +00001241\verbatiminput{tzinfo-examples.py}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001242
Tim Peters327098a2003-01-20 22:54:38 +00001243Note that there are unavoidable subtleties twice per year in a
1244\class{tzinfo}
Tim Petersadf64202003-01-04 06:03:15 +00001245subclass accounting for both standard and daylight time, at the DST
1246transition points. For concreteness, consider US Eastern (UTC -0500),
1247where EDT begins the minute after 1:59 (EST) on the first Sunday in
1248April, and ends the minute after 1:59 (EDT) on the last Sunday in October:
1249
1250\begin{verbatim}
1251 UTC 3:MM 4:MM 5:MM 6:MM 7:MM 8:MM
1252 EST 22:MM 23:MM 0:MM 1:MM 2:MM 3:MM
1253 EDT 23:MM 0:MM 1:MM 2:MM 3:MM 4:MM
1254
1255 start 22:MM 23:MM 0:MM 1:MM 3:MM 4:MM
1256
1257 end 23:MM 0:MM 1:MM 1:MM 2:MM 3:MM
1258\end{verbatim}
1259
1260When DST starts (the "start" line), the local wall clock leaps from 1:59
1261to 3:00. A wall time of the form 2:MM doesn't really make sense on that
Tim Peters75a6e3b2003-01-04 18:17:36 +00001262day, so \code{astimezone(Eastern)} won't deliver a result with
1263\code{hour==2} on the
Tim Peters327098a2003-01-20 22:54:38 +00001264day DST begins. In order for \method{astimezone()} to make this
Tim Petersf196a0a2003-01-22 04:45:50 +00001265guarantee, the \method{rzinfo.dst()} method must consider times
Tim Peters327098a2003-01-20 22:54:38 +00001266in the "missing hour" (2:MM for Eastern) to be in daylight time.
Tim Petersadf64202003-01-04 06:03:15 +00001267
1268When DST ends (the "end" line), there's a potentially worse problem:
Tim Peters327098a2003-01-20 22:54:38 +00001269there's an hour that can't be spelled unambiguously in local wall time:
1270the last hour of daylight time. In Eastern, that's times of
1271the form 5:MM UTC on the day daylight time ends. The local wall clock
Tim Petersadf64202003-01-04 06:03:15 +00001272leaps from 1:59 (daylight time) back to 1:00 (standard time) again.
Tim Peters327098a2003-01-20 22:54:38 +00001273Local times of the form 1:MM are ambiguous. \method{astimezone()} mimics
1274the local clock's behavior by mapping two adjacent UTC hours into the
1275same local hour then. In the Eastern example, UTC times of the form
12765:MM and 6:MM both map to 1:MM when converted to Eastern. In order for
Tim Petersf196a0a2003-01-22 04:45:50 +00001277\method{astimezone()} to make this guarantee, the \method{tzinfo.dst()}
1278method must consider times in the "repeated hour" to be in
Tim Peters327098a2003-01-20 22:54:38 +00001279standard time. This is easily arranged, as in the example, by expressing
1280DST switch times in the time zone's standard local time.
Tim Petersadf64202003-01-04 06:03:15 +00001281
Tim Peters327098a2003-01-20 22:54:38 +00001282Applications that can't bear such ambiguities should avoid using hybrid
1283\class{tzinfo} subclasses; there are no ambiguities when using UTC, or
1284any other fixed-offset \class{tzinfo} subclass (such as a class
1285representing only EST (fixed offset -5 hours), or only EDT (fixed offset
1286-4 hours)).
Tim Petersadf64202003-01-04 06:03:15 +00001287
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001288
Tim Peters29fb9c72002-12-23 22:21:52 +00001289\subsection{\method{strftime()} Behavior}
1290
Tim Peters00372022003-01-09 04:10:05 +00001291\class{date}, \class{datetime}, and \class{time}
1292objects all support a \code{strftime(\var{format})}
Tim Peters29fb9c72002-12-23 22:21:52 +00001293method, to create a string representing the time under the control of
1294an explicit format string. Broadly speaking,
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001295\code{d.strftime(fmt)}
Tim Peters29fb9c72002-12-23 22:21:52 +00001296acts like the \refmodule{time} module's
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001297\code{time.strftime(fmt, d.timetuple())}
Tim Peters29fb9c72002-12-23 22:21:52 +00001298although not all objects support a \method{timetuple()} method.
1299
Tim Peters00372022003-01-09 04:10:05 +00001300For \class{time} objects, the format codes for
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001301year, month, and day should not be used, as time objects have no such
1302values. If they're used anyway, \code{1900} is substituted for the
1303year, and \code{0} for the month and day.
Tim Peters29fb9c72002-12-23 22:21:52 +00001304
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001305For \class{date} objects, the format codes for hours, minutes, and
1306seconds should not be used, as \class{date} objects have no such
1307values. If they're used anyway, \code{0} is substituted for them.
Tim Peters29fb9c72002-12-23 22:21:52 +00001308
Fred Drakea37e5cc2002-12-30 21:26:42 +00001309For a naive object, the \code{\%z} and \code{\%Z} format codes are
Tim Peters29fb9c72002-12-23 22:21:52 +00001310replaced by empty strings.
1311
1312For an aware object:
1313
1314\begin{itemize}
1315 \item[\code{\%z}]
1316 \method{utcoffset()} is transformed into a 5-character string of
1317 the form +HHMM or -HHMM, where HH is a 2-digit string giving the
1318 number of UTC offset hours, and MM is a 2-digit string giving the
1319 number of UTC offset minutes. For example, if
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001320 \method{utcoffset()} returns \code{timedelta(hours=-3, minutes=-30)},
Tim Peters1cff9fc2002-12-24 16:25:29 +00001321 \code{\%z} is replaced with the string \code{'-0330'}.
Tim Peters29fb9c72002-12-23 22:21:52 +00001322
1323 \item[\code{\%Z}]
1324 If \method{tzname()} returns \code{None}, \code{\%Z} is replaced
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001325 by an empty string. Otherwise \code{\%Z} is replaced by the returned
Tim Peters29fb9c72002-12-23 22:21:52 +00001326 value, which must be a string.
1327\end{itemize}
1328
1329The full set of format codes supported varies across platforms,
1330because Python calls the platform C library's \function{strftime()}
1331function, and platform variations are common. The documentation for
1332Python's \refmodule{time} module lists the format codes that the C
1333standard (1989 version) requires, and those work on all platforms
1334with a standard C implementation. Note that the 1999 version of the
1335C standard added additional format codes.
1336
1337The exact range of years for which \method{strftime()} works also
1338varies across platforms. Regardless of platform, years before 1900
1339cannot be used.
1340
1341
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001342\begin{comment}
1343
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001344\subsection{C API}
1345
1346Struct typedefs:
1347
1348 PyDateTime_Date
1349 PyDateTime_DateTime
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001350 PyDateTime_Time
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001351 PyDateTime_Delta
1352 PyDateTime_TZInfo
1353
1354Type-check macros:
1355
1356 PyDate_Check(op)
1357 PyDate_CheckExact(op)
1358
1359 PyDateTime_Check(op)
1360 PyDateTime_CheckExact(op)
1361
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001362 PyTime_Check(op)
1363 PyTime_CheckExact(op)
1364
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001365 PyDelta_Check(op)
1366 PyDelta_CheckExact(op)
1367
1368 PyTZInfo_Check(op)
Raymond Hettingercbd6cd22002-12-31 16:30:49 +00001369 PyTZInfo_CheckExact(op)
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001370
1371Accessor macros:
1372
1373All objects are immutable, so accessors are read-only. All macros
1374return ints:
1375
Tim Petersf196a0a2003-01-22 04:45:50 +00001376 For \class{date} and \class{datetime} instances:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001377 PyDateTime_GET_YEAR(o)
1378 PyDateTime_GET_MONTH(o)
1379 PyDateTime_GET_DAY(o)
1380
Tim Petersf196a0a2003-01-22 04:45:50 +00001381 For \class{datetime} instances:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001382 PyDateTime_DATE_GET_HOUR(o)
1383 PyDateTime_DATE_GET_MINUTE(o)
1384 PyDateTime_DATE_GET_SECOND(o)
1385 PyDateTime_DATE_GET_MICROSECOND(o)
1386
Tim Petersf196a0a2003-01-22 04:45:50 +00001387 For \class{time} instances:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001388 PyDateTime_TIME_GET_HOUR(o)
1389 PyDateTime_TIME_GET_MINUTE(o)
1390 PyDateTime_TIME_GET_SECOND(o)
1391 PyDateTime_TIME_GET_MICROSECOND(o)
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001392
1393\end{comment}