blob: 772355bb319bc7b3aad395e4565d3f01f51b0dd7 [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
Raymond Hettinger6005a342002-12-30 20:01:24 +0000136\begin{classdesc}{timedelta}{days=0, seconds=0, microseconds=0,
137 milliseconds=0, minutes=0, hours=0, weeks=0}
Fred Drakebbdb2502002-12-23 18:58:06 +0000138A \class{timedelta} object represents a duration, the difference
139between two dates or times.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000140
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000141 All arguments are optional. Arguments may be ints, longs, or floats,
142 and may be positive or negative.
143
Raymond Hettinger6005a342002-12-30 20:01:24 +0000144 Only \var{days}, \var{seconds} and \var{microseconds} are stored
145 internally. Arguments are converted to those units:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000146
Raymond Hettinger6005a342002-12-30 20:01:24 +0000147\begin{verbatim}
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000148 A millisecond is converted to 1000 microseconds.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000149 A minute is converted to 60 seconds.
150 An hour is converted to 3600 seconds.
151 A week is converted to 7 days.
Raymond Hettinger6005a342002-12-30 20:01:24 +0000152\end{verbatim}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000153
154 and days, seconds and microseconds are then normalized so that the
155 representation is unique, with
156
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000157\begin{itemize}
158 \item \code{0 <= \var{microseconds} < 1000000}
159 \item \code{0 <= \var{seconds} < 3600*24} (the number of seconds in one day)
160 \item \code{-999999999 <= \var{days} <= 999999999}
161\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000162
163 If any argument is a float, and there are fractional microseconds,
164 the fractional microseconds left over from all arguments are combined
165 and their sum is rounded to the nearest microsecond. If no
Guido van Rossum8e7ec7c2002-12-31 04:39:05 +0000166 argument is a float, the conversion and normalization processes
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000167 are exact (no information is lost).
168
169 If the normalized value of days lies outside the indicated range,
Fred Drakebbdb2502002-12-23 18:58:06 +0000170 \exception{OverflowError} is raised.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000171
172 Note that normalization of negative values may be surprising at first.
173 For example,
174
Fred Drakebbdb2502002-12-23 18:58:06 +0000175\begin{verbatim}
176>>> d = timedelta(microseconds=-1)
177>>> (d.days, d.seconds, d.microseconds)
178(-1, 86399, 999999)
179\end{verbatim}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000180
Raymond Hettinger6005a342002-12-30 20:01:24 +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
Raymond Hettinger6005a342002-12-30 20:01:24 +0000185\begin{tableii}{c|l}{code}{Attribute}{Value}
186 \lineii{min}{The most negative \class{timedelta} object,
Raymond Hettingercbd6cd22002-12-31 16:30:49 +0000187 \code{timedelta(-999999999)}}
Raymond Hettinger6005a342002-12-30 20:01:24 +0000188 \lineii{max}{The most positive \class{timedelta} object,
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000189 timedelta(days=999999999, hours=23, minutes=59, seconds=59,
Raymond Hettinger6005a342002-12-30 20:01:24 +0000190 microseconds=999999)}
191 \lineii{resolution}{The smallest possible difference between non-equal
Raymond Hettingercbd6cd22002-12-31 16:30:49 +0000192 \class{timedelta} objects, \code{timedelta(microseconds=1)}}
Raymond Hettinger6005a342002-12-30 20:01:24 +0000193\end{tableii}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000194
Fred Drakea37e5cc2002-12-30 21:26:42 +0000195Note that, because of normalization, \code{timedelta.max} \textgreater
196\code{-timedelta.min}. \code{-timedelta.max} is not representable as
197a \class{timedelta} object.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000198
199Instance attributes (read-only):
200
Raymond Hettinger6005a342002-12-30 20:01:24 +0000201\begin{tableii}{c|l}{code}{Attribute}{Value}
202 \lineii{days}{Between -999999999 and 999999999 inclusive}
203 \lineii{seconds}{Between 0 and 86399 inclusive}
204 \lineii{microseconds}{Between 0 and 999999 inclusive}
205\end{tableii}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000206
207Supported operations:
208
Raymond Hettinger6005a342002-12-30 20:01:24 +0000209\begin{tableiii}{c|l|c}{code}{Operation}{Result}{Notes}
Fred Drake9bdeee42002-12-30 20:35:32 +0000210 \lineiii{\var{t1} = \var{t2} + \var{t3}}
Tim Peters95322982002-12-31 16:01:47 +0000211 {Sum of \var{t2} and \var{t3}.
Fred Drake9bdeee42002-12-30 20:35:32 +0000212 Afterwards \var{t1}-\var{t2} == \var{t3} and \var{t1}-\var{t3}
213 == \var{t2} are true.}
214 {(1)}
215 \lineiii{\var{t1} = \var{t2} - \var{t3}}
216 {Difference of \var{t2} and \var{t3}. Afterwards \var{t1} ==
217 \var{t2} - \var{t3} and \var{t2} == \var{t1} + \var{t3} are
218 true.}
219 {(1)}
220 \lineiii{\var{t1} = \var{t2} * \var{i} or \var{t1} = \var{i} * \var{t2}}
221 {Delta multiplied by an integer or long.
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000222 Afterwards \var{t1} // i == \var{t2} is true, provided \code{i != 0}.
Fred Drake9bdeee42002-12-30 20:35:32 +0000223 In general, \var{t1} * i == \var{t1} * (i-1) + \var{t1} is true.}
224 {(1)}
225 \lineiii{\var{t1} = \var{t2} // \var{i}}
226 {The floor is computed and the remainder (if any) is thrown away.}
227 {(2)}
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000228 \lineiii{+\var{t1}}
Raymond Hettingercbd6cd22002-12-31 16:30:49 +0000229 {Returns a \class{timedelta} object with the same value.}
230 {}
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000231 \lineiii{-\var{t1}}
Raymond Hettingercbd6cd22002-12-31 16:30:49 +0000232 {equivalent to \class{timedelta}(-\var{t1.days}, -\var{t1.seconds},
233 -\var{t1.microseconds}),and to \var{t1}* -1.}
234 {(1)(3)}
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000235 \lineiii{abs(\var{t})}
236 {equivalent to +\var{t} when \code{t.days >= 0}, and to -\var{t} when
237 \code{t.days < 0}.}
238 {(1)}
239
Tim Peters95322982002-12-31 16:01:47 +0000240
Fred Drake9bdeee42002-12-30 20:35:32 +0000241\end{tableiii}
Raymond Hettinger6005a342002-12-30 20:01:24 +0000242\noindent
243Notes:
244
245\begin{description}
246\item[(1)]
247This is exact, but may overflow.
248
249\item[(2)]
250Division by 0 raises \exception{ZeroDivisionError}.
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000251
252\item[(3)]
Raymond Hettingercbd6cd22002-12-31 16:30:49 +0000253-\var{timedelta.max} is not representable as a \class{timedelta} object.
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000254
Raymond Hettinger6005a342002-12-30 20:01:24 +0000255\end{description}
256
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000257In addition to the operations listed above \class{timedelta} objects
258support certain additions and subtractions with \class{date},
259\class{datetime}, and \class{datimetz} objects (see below).
Raymond Hettinger6005a342002-12-30 20:01:24 +0000260
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000261Comparisons of \class{timedelta} objects are supported with the
262\class{timedelta} object representing the smaller duration considered
263to be the smaller timedelta.
Raymond Hettinger6005a342002-12-30 20:01:24 +0000264
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000265\class{timedelta} objects are hashable (usable as dictionary key),
266support efficient pickling, and in Boolean contexts, a \class{timedelta}
267object is considered to be true if and only if it isn't equal to
268\code{timedelta(0)}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000269
270
Raymond Hettinger6005a342002-12-30 20:01:24 +0000271\subsection{\class{date} Objects \label{datetime-date}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000272
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000273A \class{date} object represents a date (year, month and day) in an idealized
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000274calendar, the current Gregorian calendar indefinitely extended in both
275directions. January 1 of year 1 is called day number 1, January 2 of year
2761 is called day number 2, and so on. This matches the definition of the
277"proleptic Gregorian" calendar in Dershowitz and Reingold's book
Fred Drakea37e5cc2002-12-30 21:26:42 +0000278\citetitle{Calendrical Calculations}, where it's the base calendar for all
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000279computations. See the book for algorithms for converting between
280proleptic Gregorian ordinals and many other calendar systems.
281
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000282\begin{funcdesc}{date}{year, month, day}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000283
284 All arguments are required. Arguments may be ints or longs, in the
285 following ranges:
286
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000287\begin{itemize}
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000288 \item \code{MINYEAR <= \var{year} <= MAXYEAR}
289 \item \code{1 <= \var{month} <= 12}
290 \item \code{1 <= \var{day} <= number of days in the given month and year}
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000291\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000292
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000293If an argument outside those ranges is given, \exception{ValueError}
294is raised.
295\end{funcdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000296
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000297Other constructors, all class methods:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000298
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000299\begin{methoddesc}{today}{}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000300 Return the current local date. This is equivalent to
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000301 \code{date.fromtimestamp(time.time())}.
302\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000303
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000304\begin{methoddesc}{fromtimestamp}{timestamp}
Fred Drakebbdb2502002-12-23 18:58:06 +0000305 Return the local date corresponding to the POSIX timestamp, such
306 as is returned by \function{time.time()}. This may raise
307 \exception{ValueError}, if the timestamp is out of the range of
308 values supported by the platform C \cfunction{localtime()}
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000309 function. It's common for this to be restricted to years from 1970
Fred Drakebbdb2502002-12-23 18:58:06 +0000310 through 2038.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000311\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000312
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000313\begin{methoddesc}{fromordinal}{ordinal}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000314 Return the date corresponding to the proleptic Gregorian ordinal,
Fred Drakebbdb2502002-12-23 18:58:06 +0000315 where January 1 of year 1 has ordinal 1. \exception{ValueError}
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000316 is raised unless \code{1 <= \var{ordinal} <= date.max.toordinal()}. For any
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000317 date \var{d}, \code{date.fromordinal(\var{d}.toordinal()) == \var{d}}.
318\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000319
320Class attributes:
321
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000322\begin{memberdesc}{min}
Fred Drakebbdb2502002-12-23 18:58:06 +0000323 The earliest representable date, \code{date(MINYEAR, 1, 1)}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000324\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000325
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000326\begin{memberdesc}{max}
Fred Drakebbdb2502002-12-23 18:58:06 +0000327 The latest representable date, \code{date(MAXYEAR, 12, 31)}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000328\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000329
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000330\begin{memberdesc}{resolution}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000331 The smallest possible difference between non-equal date
Fred Drakebbdb2502002-12-23 18:58:06 +0000332 objects, \code{timedelta(days=1)}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000333\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000334
335Instance attributes (read-only):
336
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000337\begin{memberdesc}{year}
338Between \constant{MINYEAR} and \constant{MAXYEAR} inclusive
339\end{memberdesc}
340
341\begin{memberdesc}{month}
342Between 1 and 12 inclusive.
343\end{memberdesc}
344
345\begin{memberdesc}{day}
346Between 1 and the number of days in the given month
347 of the given year.
348\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000349
350Supported operations:
351
Fred Drakebbdb2502002-12-23 18:58:06 +0000352\begin{itemize}
353 \item
354 date1 + timedelta -> date2
355 timedelta + date1 -> date2
356 date2 is timedelta.days days removed from the date1, moving forward
357 in time if timedelta.days > 0, or backward if timedetla.days < 0.
358 date2 - date1 == timedelta.days after. timedelta.seconds and
359 timedelta.microseconds are ignored. \exception{OverflowError} is
360 raised if date2.year would be smaller than \constant{MINYEAR} or
361 larger than \constant{MAXYEAR}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000362
Fred Drakebbdb2502002-12-23 18:58:06 +0000363 \item
364 date1 - timedelta -> date2
365 Computes the date2 such that date2 + timedelta == date1. This
366 isn't quite equivalent to date1 + (-timedelta), because -timedelta
367 in isolation can overflow in cases where date1 - timedelta does
368 not. timedelta.seconds and timedelta.microseconds are ignored.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000369
Fred Drakebbdb2502002-12-23 18:58:06 +0000370 \item
371 date1 - date2 -> timedelta
372 This is exact, and cannot overflow. timedelta.seconds and
373 timedelta.microseconds are 0, and date2 + timedelta == date1
374 after.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000375
Fred Drakebbdb2502002-12-23 18:58:06 +0000376 \item
377 comparison of date to date, where date1 is considered less than
378 date2 when date1 precedes date2 in time. In other words,
379 date1 < date2 if and only if date1.toordinal() < date2.toordinal().
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000380
Fred Drakebbdb2502002-12-23 18:58:06 +0000381 \item
382 hash, use as dict key
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000383
Fred Drakebbdb2502002-12-23 18:58:06 +0000384 \item
385 efficient pickling
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000386
Fred Drakebbdb2502002-12-23 18:58:06 +0000387 \item
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000388 in Boolean contexts, all \class{date} objects are considered to be true
Fred Drakebbdb2502002-12-23 18:58:06 +0000389\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000390
391Instance methods:
392
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000393\begin{methoddesc}{replace}{year, month, day}
Tim Peters12bf3392002-12-24 05:41:27 +0000394 Return a date with the same value, except for those fields given
395 new values by whichever keyword arguments are specified. For
396 example, if \code{d == date(2002, 12, 31)}, then
397 \code{d.replace(day=26) == date(2000, 12, 26)}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000398\end{methoddesc}
Tim Peters12bf3392002-12-24 05:41:27 +0000399
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000400\begin{methoddesc}{timetuple}{}
Fred Drakebbdb2502002-12-23 18:58:06 +0000401 Return a 9-element tuple of the form returned by
402 \function{time.localtime()}. The hours, minutes and seconds are
403 0, and the DST flag is -1.
Fred Drakea37e5cc2002-12-30 21:26:42 +0000404 \code{\var{d}.timetuple()} is equivalent to
405 \code{(\var{d}.year, \var{d}.month, \var{d}.day,
406 0, 0, 0, \# h, m, s
407 \var{d}.weekday(), \# 0 is Monday
408 \var{d}.toordinal() - date(\var{d}.year, 1, 1).toordinal() + 1,
409 \# day of year
410 -1)}
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000411\end{methoddesc}
412\begin{methoddesc}{toordinal}{}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000413 Return the proleptic Gregorian ordinal of the date, where January 1
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000414 of year 1 has ordinal 1. For any \class{date} object \var{d},
Fred Drakebbdb2502002-12-23 18:58:06 +0000415 \code{date.fromordinal(\var{d}.toordinal()) == \var{d}}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000416\end{methoddesc}
417\begin{methoddesc}{weekday}{}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000418 Return the day of the week as an integer, where Monday is 0 and
419 Sunday is 6. For example, date(2002, 12, 4).weekday() == 2, a
420 Wednesday.
Fred Drakebbdb2502002-12-23 18:58:06 +0000421 See also \method{isoweekday()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000422\end{methoddesc}
423\begin{methoddesc}{isoweekday}{}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000424 Return the day of the week as an integer, where Monday is 1 and
425 Sunday is 7. For example, date(2002, 12, 4).isoweekday() == 3, a
426 Wednesday.
Fred Drakebbdb2502002-12-23 18:58:06 +0000427 See also \method{weekday()}, \method{isocalendar()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000428\end{methoddesc}
429\begin{methoddesc}{isocalendar}{}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000430 Return a 3-tuple, (ISO year, ISO week number, ISO weekday).
431
432 The ISO calendar is a widely used variant of the Gregorian calendar.
Fred Drakebbdb2502002-12-23 18:58:06 +0000433 See \url{http://www.phys.uu.nl/~vgent/calendar/isocalendar.htm}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000434 for a good explanation.
435
436 The ISO year consists of 52 or 53 full weeks, and where a week starts
437 on a Monday and ends on a Sunday. The first week of an ISO year is
438 the first (Gregorian) calendar week of a year containing a Thursday.
439 This is called week number 1, and the ISO year of that Thursday is
440 the same as its Gregorian year.
441
442 For example, 2004 begins on a Thursday, so the first week of ISO
443 year 2004 begins on Monday, 29 Dec 2003 and ends on Sunday, 4 Jan
444 2004, so that
445
446 date(2003, 12, 29).isocalendar() == (2004, 1, 1)
447 date(2004, 1, 4).isocalendar() == (2004, 1, 7)
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000448\end{methoddesc}
449\begin{methoddesc}{isoformat}{}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000450 Return a string representing the date in ISO 8601 format,
451 'YYYY-MM-DD'. For example,
452 date(2002, 12, 4).isoformat() == '2002-12-04'.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000453\end{methoddesc}
454\begin{methoddesc}{__str__}{}
Fred Drakebbdb2502002-12-23 18:58:06 +0000455 For a date \var{d}, \code{str(\var{d})} is equivalent to
456 \code{\var{d}.isoformat()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000457\end{methoddesc}
458\begin{methoddesc}{ctime}{}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000459 Return a string representing the date, for example
460 date(2002, 12, 4).ctime() == 'Wed Dec 4 00:00:00 2002'.
Fred Drakea37e5cc2002-12-30 21:26:42 +0000461 \code{\var{d}.ctime()} is equivalent to
462 \code{time.ctime(time.mktime(\var{d}.timetuple()))}
Fred Drakebbdb2502002-12-23 18:58:06 +0000463 on platforms where the native C \cfunction{ctime()} function
464 (which \function{time.ctime()} invokes, but which
465 \method{date.ctime()} does not invoke) conforms to the C standard.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000466\end{methoddesc}
467\begin{methoddesc}{strftime}{format}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000468 Return a string representing the date, controlled by an explicit
469 format string. Format codes referring to hours, minutes or seconds
Fred Drakebbdb2502002-12-23 18:58:06 +0000470 will see 0 values.
471 See the section on \method{strftime()} behavior.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000472\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000473
474
Raymond Hettinger6005a342002-12-30 20:01:24 +0000475\subsection{\class{datetime} Objects \label{datetime-datetime}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000476
Fred Drakebbdb2502002-12-23 18:58:06 +0000477A \class{datetime} object is a single object containing all the
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000478information from a \class{date} object and a time object. Like a
479\class{date} object, \class{datetime} assumes the current Gregorian
480calendar extended in both directions; like a time object,
481\class{datetime} assumes there are exactly 3600*24 seconds in every
482day.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000483
Guido van Rossum8e7ec7c2002-12-31 04:39:05 +0000484\begin{funcdesc}{datetime}{year, month, day,
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000485 hour=0, minute=0, second=0, microsecond=0}
486The year, month and day arguments are required. Arguments may be ints
487or longs, in the following ranges:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000488
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000489\begin{itemize}
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000490 \item \code{\member{MINYEAR} <= \var{year} <= \member{MAXYEAR}}
491 \item \code{1 <= \var{month} <= 12}
492 \item \code{1 <= \var{day} <= number of days in the given month and year}
493 \item \code{0 <= \var{hour} < 24}
494 \item \code{0 <= \var{minute} < 60}
495 \item \code{0 <= \var{second} < 60}
496 \item \code{0 <= \var{microsecond} < 1000000}
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000497\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000498
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000499If an argument outside those ranges is given,
500\exception{ValueError} is raised.
501\end{funcdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000502
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000503Other constructors, all class methods:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000504
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000505\begin{methoddesc}{today}{}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000506 Return the current local datetime. This is equivalent to
Fred Drakebbdb2502002-12-23 18:58:06 +0000507 \code{datetime.fromtimestamp(time.time())}.
508 See also \method{now()}, \method{fromtimestamp()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000509\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000510
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000511\begin{methoddesc}{now}{}
Fred Drakebbdb2502002-12-23 18:58:06 +0000512 Return the current local datetime. This is like \method{today()},
513 but, if possible, supplies more precision than can be gotten from
514 going through a \function{time.time()} timestamp (for example,
515 this may be possible on platforms that supply the C
516 \cfunction{gettimeofday()} function).
517 See also \method{today()}, \method{utcnow()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000518\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000519
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000520\begin{methoddesc}{utcnow}{}
Fred Drakebbdb2502002-12-23 18:58:06 +0000521 Return the current UTC datetime. This is like \method{now()}, but
522 returns the current UTC date and time.
523 See also \method{now()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000524\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000525
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000526\begin{methoddesc}{fromtimestamp}{timestamp}
Fred Drakebbdb2502002-12-23 18:58:06 +0000527 Return the local \class{datetime} corresponding to the \POSIX{}
528 timestamp, such as is returned by \function{time.time()}. This
529 may raise \exception{ValueError}, if the timestamp is out of the
530 range of values supported by the platform C
531 \cfunction{localtime()} function. It's common for this to be
532 restricted to years in 1970 through 2038.
533 See also \method{utcfromtimestamp()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000534\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000535
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000536\begin{methoddesc}{utcfromtimestamp}{timestamp}
Fred Drakebbdb2502002-12-23 18:58:06 +0000537 Return the UTC \class{datetime} corresponding to the \POSIX{}
538 timestamp. This may raise \exception{ValueError}, if the
539 timestamp is out of the range of values supported by the platform
540 C \cfunction{gmtime()} function. It's common for this to be
541 restricted to years in 1970 through 2038.
542 See also \method{fromtimestamp()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000543\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000544
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000545\begin{methoddesc}{fromordinal}{ordinal}
Fred Drakebbdb2502002-12-23 18:58:06 +0000546 Return the \class{datetime} corresponding to the proleptic
547 Gregorian ordinal, where January 1 of year 1 has ordinal 1.
548 \exception{ValueError} is raised unless 1 <= ordinal <=
549 datetime.max.toordinal(). The hour, minute, second and
550 microsecond of the result are all 0.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000551\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000552
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000553\begin{methoddesc}{combine}{date, time}
Fred Drakebbdb2502002-12-23 18:58:06 +0000554 Return a new \class{datetime} object whose date components are
Fred Drakea37e5cc2002-12-30 21:26:42 +0000555 equal to the given \class{date} object's, and whose time
556 components are equal to the given time object's. For any
557 \class{datetime} object \var{d}, \code{\var{d} ==
558 datetime.combine(\var{d}.date(), \var{d}.time())}. If date is a
559 \class{datetime} or \class{datetimetz} object, its time components
560 are ignored. If date is \class{datetimetz} object, its
561 \member{tzinfo} component is also ignored. If time is a
562 \class{timetz} object, its \member{tzinfo} component is ignored.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000563\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000564
565Class attributes:
566
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000567\begin{memberdesc}{min}
568 The earliest representable \class{datetime},
569 \code{datetime(MINYEAR, 1, 1)}.
570\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000571
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000572\begin{memberdesc}{max}
573 The latest representable \class{datetime},
574 \code{datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999)}.
575\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000576
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000577\begin{memberdesc}{resolution}
578 The smallest possible difference between non-equal \class{datetime}
579 objects, \code{timedelta(microseconds=1)}.
580\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000581
582Instance attributes (read-only):
583
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000584\begin{memberdesc}{year}
585Between \constant{MINYEAR} and \constant{MAXYEAR} inclusive
586\end{memberdesc}
587
588\begin{memberdesc}{month}
589Between 1 and 12 inclusive
590\end{memberdesc}
591
592\begin{memberdesc}{day}
Fred Drakea37e5cc2002-12-30 21:26:42 +0000593Between 1 and the number of days in the given month of the given year.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000594\end{memberdesc}
595
596\begin{memberdesc}{hour}
597In \code{range(24)}.
598\end{memberdesc}
599
600\begin{memberdesc}{minute}
601In \code{range(60)}.
602\end{memberdesc}
603
604\begin{memberdesc}{second}
605In \code{range(60)}.
606\end{memberdesc}
607
608\begin{memberdesc}{microsecond}
609In \code{range(1000000)}.
610\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000611
612Supported operations:
613
Fred Drakebbdb2502002-12-23 18:58:06 +0000614\begin{itemize}
615 \item
616 datetime1 + timedelta -> datetime2
617 timedelta + datetime1 -> datetime2
618 datetime2 is a duration of timedelta removed from datetime1, moving
619 forward in time if timedelta.days > 0, or backward if
620 timedelta.days < 0. datetime2 - datetime1 == timedelta after.
621 \exception{OverflowError} is raised if datetime2.year would be
622 smaller than \constant{MINYEAR} or larger than \constant{MAXYEAR}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000623
Fred Drakebbdb2502002-12-23 18:58:06 +0000624 \item
625 datetime1 - timedelta -> datetime2
626 Computes the datetime2 such that datetime2 + timedelta == datetime1.
627 This isn't quite equivalent to datetime1 + (-timedelta), because
628 -timedelta in isolation can overflow in cases where
629 datetime1 - timedelta does not.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000630
Fred Drakebbdb2502002-12-23 18:58:06 +0000631 \item
632 datetime1 - datetime2 -> timedelta
633 This is exact, and cannot overflow.
634 datetime2 + timedelta == datetime1 after.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000635
Fred Drakebbdb2502002-12-23 18:58:06 +0000636 \item
637 comparison of \class{datetime} to datetime, where datetime1 is
638 considered less than datetime2 when datetime1 precedes datetime2
639 in time.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000640
Fred Drakebbdb2502002-12-23 18:58:06 +0000641 \item
642 hash, use as dict key
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000643
Fred Drakebbdb2502002-12-23 18:58:06 +0000644 \item
645 efficient pickling
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000646
Fred Drakebbdb2502002-12-23 18:58:06 +0000647 \item
648 in Boolean contexts, all \class{datetime} objects are considered
649 to be true
650\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000651
652Instance methods:
653
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000654\begin{methoddesc}{date}{}
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000655 Return \class{date} object with same year, month and day.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000656\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000657
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000658\begin{methoddesc}{time}{}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000659 Return time object with same hour, minute, second and microsecond.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000660\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000661
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000662\begin{methoddesc}{replace}{year=, month=, day=, hour=, minute=, second=, microsecond=}
Tim Peters12bf3392002-12-24 05:41:27 +0000663 Return a datetime with the same value, except for those fields given
664 new values by whichever keyword arguments are specified.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000665\end{methoddesc}
Tim Peters12bf3392002-12-24 05:41:27 +0000666
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000667\begin{methoddesc}{astimezone}{tz}
Tim Peters80475bb2002-12-25 07:40:55 +0000668 Return a \class{datetimetz} with the same date and time fields, and
Tim Peters276a8f32002-12-27 21:41:32 +0000669 with \member{tzinfo} member \var{tz}. \var{tz} must be \code{None},
670 or an instance of a \class{tzinfo} subclass.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000671\end{methoddesc}
Tim Peters80475bb2002-12-25 07:40:55 +0000672
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000673\begin{methoddesc}{timetuple}{}
Fred Drakebbdb2502002-12-23 18:58:06 +0000674 Return a 9-element tuple of the form returned by
675 \function{time.localtime()}.
Fred Drakea37e5cc2002-12-30 21:26:42 +0000676 The DST flag is -1. \code{\var{d}.timetuple()} is equivalent to
677 \code{(\var{d}.year, \var{d}.month, \var{d}.day,
678 \var{d}.hour, \var{d}.minute, \var{d}.second,
679 \var{d}.weekday(), \# 0 is Monday
680 \var{d}.toordinal() - date(\var{d}.year, 1, 1).toordinal() + 1,
681 \# day of year
682 -1)}
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000683\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000684
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000685\begin{methoddesc}{toordinal}{}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000686 Return the proleptic Gregorian ordinal of the date. The same as
Fred Drakebbdb2502002-12-23 18:58:06 +0000687 \method{date.toordinal()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000688\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000689
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000690\begin{methoddesc}{weekday}{}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000691 Return the day of the week as an integer, where Monday is 0 and
Fred Drakebbdb2502002-12-23 18:58:06 +0000692 Sunday is 6. The same as \method{date.weekday()}.
693 See also \method{isoweekday()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000694\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000695
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000696\begin{methoddesc}{isoweekday}{}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000697 Return the day of the week as an integer, where Monday is 1 and
Fred Drakebbdb2502002-12-23 18:58:06 +0000698 Sunday is 7. The same as \method{date.isoweekday()}.
699 See also \method{weekday()}, \method{isocalendar()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000700\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000701
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000702\begin{methoddesc}{isocalendar}{}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000703 Return a 3-tuple, (ISO year, ISO week number, ISO weekday). The
Fred Drakebbdb2502002-12-23 18:58:06 +0000704 same as \method{date.isocalendar()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000705\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000706
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000707\begin{methoddesc}{isoformat}{sep='T'}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000708 Return a string representing the date and time in ISO 8601 format,
709 YYYY-MM-DDTHH:MM:SS.mmmmmm
710 or, if self.microsecond is 0,
711 YYYY-MM-DDTHH:MM:SS
Fred Drakebbdb2502002-12-23 18:58:06 +0000712 The optional argument \var{sep} (default \code{'T'}) is a
713 one-character separator, placed between the date and time portions
714 of the result. For example,
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000715 datetime(2002, 12, 4, 1, 2, 3, 4).isoformat(' ') ==
716 '2002-12-04 01:02:03.000004'
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000717\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000718
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000719\begin{methoddesc}{__str__}{}
Fred Drakebbdb2502002-12-23 18:58:06 +0000720 For a \class{datetime} instance \var{d}, \code{str(\var{d})} is
721 equivalent to \code{\var{d}.isoformat(' ')}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000722\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000723
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000724\begin{methoddesc}{ctime}{}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000725 Return a string representing the date, for example
726 datetime(2002, 12, 4, 20, 30, 40).ctime() == 'Wed Dec 4 20:30:40 2002'.
Fred Drakebbdb2502002-12-23 18:58:06 +0000727 \code{d.ctime()} is equivalent to
728 \code{time.ctime(time.mktime(d.timetuple()))} on platforms where
729 the native C \cfunction{ctime()} function (which
730 \function{time.ctime()} invokes, but which
731 \method{datetime.ctime()} does not invoke) conforms to the C
732 standard.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000733\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000734
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000735\begin{methoddesc}{strftime}{format}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000736 Return a string representing the date and time, controlled by an
Fred Drakebbdb2502002-12-23 18:58:06 +0000737 explicit format string. See the section on \method{strftime()}
738 behavior.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000739\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000740
741
Raymond Hettinger6005a342002-12-30 20:01:24 +0000742\subsection{\class{time} Objects \label{datetime-time}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000743
Raymond Hettingercbd6cd22002-12-31 16:30:49 +0000744A \class{time} object represents an idealized time of day, independent
745of day and timezone.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000746
Raymond Hettingercbd6cd22002-12-31 16:30:49 +0000747\begin{funcdesc}{hour=0, minute=0, second=0, microsecond=0}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000748
Raymond Hettingercbd6cd22002-12-31 16:30:49 +0000749All arguments are optional. They may be ints or longs, in the
750following ranges:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000751
Raymond Hettingercbd6cd22002-12-31 16:30:49 +0000752\begin{itemize}
753 \item \code{0 <= \var{hour} < 24}
754 \item \code{0 <= \var{minute} < 60}
755 \item \code{0 <= \var{second} < 60}
756 \item \code{0 <= \var{microsecond} < 1000000}
757\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000758
Raymond Hettingercbd6cd22002-12-31 16:30:49 +0000759If an argument outside those ranges is given, \exception{ValueError} is
760raised.
761\end{funcdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000762
763Class attributes:
764
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000765\begin{memberdesc}{min}
766 The earliest representable \class{time}, \code{time(0, 0, 0, 0)}.
767\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000768
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000769\begin{memberdesc}{max}
770 The latest representable \class{time}, \code{time(23, 59, 59, 999999)}.
771\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000772
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000773\begin{memberdesc}{resolution}
774 The smallest possible difference between non-equal \class{time}
775 objects, \code{timedelta(microseconds=1)}, although note that
776 arithmetic on \class{time} objects is not supported.
777\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000778
779Instance attributes (read-only):
780
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000781\begin{memberdesc}{hour}
782In \code{range(24)}.
783\end{memberdesc}
Tim Petersbad8ff02002-12-30 20:52:32 +0000784\begin{memberdesc}{minute}
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000785In \code{range(60)}.
786\end{memberdesc}
Tim Petersbad8ff02002-12-30 20:52:32 +0000787\begin{memberdesc}{second}
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000788In \code{range(60)}.
789\end{memberdesc}
Tim Petersbad8ff02002-12-30 20:52:32 +0000790\begin{memberdesc}{microsecond}
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000791In \code{range(1000000)}.
792\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000793
794Supported operations:
795
Fred Drakebbdb2502002-12-23 18:58:06 +0000796\begin{itemize}
797 \item
798 comparison of time to time, where time1 is considered
799 less than time2 when time1 precedes time2 in time.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000800
Fred Drakebbdb2502002-12-23 18:58:06 +0000801 \item
802 hash, use as dict key
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000803
Fred Drakebbdb2502002-12-23 18:58:06 +0000804 \item
805 efficient pickling
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000806
Fred Drakebbdb2502002-12-23 18:58:06 +0000807 \item
808 in Boolean contexts, a time object is considered to be true
Fred Drakea37e5cc2002-12-30 21:26:42 +0000809 if and only if it isn't equal to \code{time(0)}
Fred Drakebbdb2502002-12-23 18:58:06 +0000810\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000811
812Instance methods:
813
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000814\begin{methoddesc}{replace}{hour=, minute=, second=, microsecond=}
Tim Peters12bf3392002-12-24 05:41:27 +0000815 Return a time with the same value, except for those fields given
816 new values by whichever keyword arguments are specified.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000817\end{methoddesc}
Tim Peters12bf3392002-12-24 05:41:27 +0000818
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000819\begin{methoddesc}{isoformat}{}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000820 Return a string representing the time in ISO 8601 format,
821 HH:MM:SS.mmmmmm
822 or, if self.microsecond is 0
823 HH:MM:SS
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000824\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000825
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000826\begin{methoddesc}{__str__}{}
Fred Drakebbdb2502002-12-23 18:58:06 +0000827 For a time \var{t}, \code{str(\var{t})} is equivalent to
828 \code{\var{t}.isoformat()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000829\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000830
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000831\begin{methoddesc}{strftime}{format}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000832 Return a string representing the time, controlled by an explicit
Fred Drakebbdb2502002-12-23 18:58:06 +0000833 format string. See the section on \method{strftime()} behavior.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000834\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000835
836
Raymond Hettinger6005a342002-12-30 20:01:24 +0000837\subsection{\class{tzinfo} Objects \label{datetime-tzinfo}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000838
Fred Drakebbdb2502002-12-23 18:58:06 +0000839\class{tzinfo} is an abstract base clase, meaning that this class
840should not be instantiated directly. You need to derive a concrete
841subclass, and (at least) supply implementations of the standard
842\class{tzinfo} methods needed by the \class{datetime} methods you
Tim Petersbad8ff02002-12-30 20:52:32 +0000843use. The \module{datetime} module does not supply any concrete
Fred Drakebbdb2502002-12-23 18:58:06 +0000844subclasses of \class{tzinfo}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000845
Fred Drakebbdb2502002-12-23 18:58:06 +0000846An instance of (a concrete subclass of) \class{tzinfo} can be passed
847to the constructors for \class{datetimetz} and \class{timetz} objects.
848The latter objects view their fields as being in local time, and the
849\class{tzinfo} object supports methods revealing offset of local time
850from UTC, the name of the time zone, and DST offset, all relative to a
851date or time object passed to them.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000852
Tim Petersbad8ff02002-12-30 20:52:32 +0000853Special requirement for pickling: A \class{tzinfo} subclass must have an
Tim Peters2483b612002-12-24 16:30:58 +0000854\method{__init__} method that can be called with no arguments, else it
855can be pickled but possibly not unpickled again. This is a technical
856requirement that may be relaxed in the future.
857
Fred Drakebbdb2502002-12-23 18:58:06 +0000858A concrete subclass of \class{tzinfo} may need to implement the
859following methods. Exactly which methods are needed depends on the
Tim Petersbad8ff02002-12-30 20:52:32 +0000860uses made of aware \module{datetime} objects. If in doubt, simply
861implement all of them.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000862
Tim Petersbad8ff02002-12-30 20:52:32 +0000863\begin{methoddesc}{utcoffset}{self, dt}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000864 Return offset of local time from UTC, in minutes east of UTC. If
865 local time is west of UTC, this should be negative. Note that this
866 is intended to be the total offset from UTC; for example, if a
867 \class{tzinfo} object represents both time zone and DST adjustments,
Fred Drakebbdb2502002-12-23 18:58:06 +0000868 \method{utcoffset()} should return their sum. If the UTC offset
869 isn't known, return \code{None}. Else the value returned must be
870 an integer, in the range -1439 to 1439 inclusive (1440 = 24*60;
Tim Peters1cff9fc2002-12-24 16:25:29 +0000871 the magnitude of the offset must be less than one day), or a
872 \class{timedelta} object representing a whole number of minutes
Tim Petersbad8ff02002-12-30 20:52:32 +0000873 in the same range. Most implementations of \method{utcoffset()}
874 will probably look like:
875\begin{verbatim}
876 return CONSTANT # fixed-offset class
877 return CONSTANT + self.dst(dt) # daylight-aware class
Guido van Rossum8e7ec7c2002-12-31 04:39:05 +0000878\end{verbatim}
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000879\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000880
Tim Petersbad8ff02002-12-30 20:52:32 +0000881\begin{methoddesc}{tzname}{self, dt}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000882 Return the timezone name corresponding to the \class{datetime} represented
883 by dt, as a string. Nothing about string names is defined by the
884 \module{datetime} module, and there's no requirement that it mean anything
885 in particular. For example, "GMT", "UTC", "-500", "-5:00", "EDT",
886 "US/Eastern", "America/New York" are all valid replies. Return
Fred Drakebbdb2502002-12-23 18:58:06 +0000887 \code{None} if a string name isn't known. Note that this is a method
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000888 rather than a fixed string primarily because some \class{tzinfo} objects
889 will wish to return different names depending on the specific value
890 of dt passed, especially if the \class{tzinfo} class is accounting for DST.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000891\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000892
Tim Petersbad8ff02002-12-30 20:52:32 +0000893\begin{methoddesc}{dst}{self, dt}
Fred Drakebbdb2502002-12-23 18:58:06 +0000894 Return the DST offset, in minutes east of UTC, or \code{None} if
895 DST information isn't known. Return 0 if DST is not in effect.
Tim Peters1cff9fc2002-12-24 16:25:29 +0000896 If DST is in effect, return the offset as an integer or
897 \class{timedelta} object (see \method{utcoffset()} for details).
898 Note that DST offset, if applicable, has
Fred Drakebbdb2502002-12-23 18:58:06 +0000899 already been added to the UTC offset returned by
900 \method{utcoffset()}, so there's no need to consult \method{dst()}
901 unless you're interested in displaying DST info separately. For
Tim Peters1cff9fc2002-12-24 16:25:29 +0000902 example, \method{datetimetz.timetuple()} calls its \member{tzinfo}
903 member's \method{dst()} method to determine how the
Fred Drakebbdb2502002-12-23 18:58:06 +0000904 \member{tm_isdst} flag should be set.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000905\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000906
Tim Petersbad8ff02002-12-30 20:52:32 +0000907These methods are called by a \class{datetimetz} or \class{timetz} object,
908in response to their methods of the same names. A \class{datetimetz}
909object passes itself as the argument, and a \class{timetz} object passes
910\code{None} as the argument. A \class{tzinfo} subclass's methods should
911therefore be prepared to accept a \var{dt} argument of \code{None}, or of
912class \class{datetimetz}.
913
914When \code{None} is passed, it's up to the class designer to decide the
915best response. For example, returning \code{None} is appropriate if the
916class wishes to say that timetz objects don't participate in the
917\class{tzinfo} protocol. In other applications, it may be more useful
Raymond Hettingerc5f5f872002-12-31 14:26:54 +0000918for \code{utcoffset(None)} to return the standard UTC offset.
Tim Petersbad8ff02002-12-30 20:52:32 +0000919
920When a \class{datetimetz} object is passed in response to a
921\class{datetimetz} method, \code{dt.tzinfo} is the same object as
922\var{self}. \class{tzinfo} methods can rely on this, unless
923user code calls \class{tzinfo} methods directly. The intent is that
924the \class{tzinfo} methods interpret \var{dt} as being in local time,
925and not need to worry about objects in other timezones.
926
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000927Example \class{tzinfo} classes:
928
Fred Drakebbdb2502002-12-23 18:58:06 +0000929\verbatiminput{tzinfo-examples.py}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000930
931
Raymond Hettinger6005a342002-12-30 20:01:24 +0000932\subsection{\class{timetz} Objects \label{datetime-timetz}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000933
934A time object represents a (local) time of day, independent of any
935particular day, and subject to adjustment via a \class{tzinfo} object.
936
937Constructor:
938
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000939\begin{funcdesc}{time}{hour=0, minute=0, second=0, microsecond=0, tzinfo=None}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000940
Fred Drakebbdb2502002-12-23 18:58:06 +0000941 All arguments are optional. \var{tzinfo} may be \code{None}, or
942 an instance of a \class{tzinfo} subclass. The remaining arguments
943 may be ints or longs, in the following ranges:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000944
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000945\begin{itemize}
946 \item \code{0 <= \var{hour} < 24}
947 \item \code{0 <= \var{minute} < 60}
948 \item \code{0 <= \var{second} < 60}
949 \item \code{0 <= \var{microsecond} < 1000000}.
950\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000951
Fred Drakebbdb2502002-12-23 18:58:06 +0000952 If an argument outside those ranges is given,
953 \exception{ValueError} is raised.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000954\end{funcdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000955
956Class attributes:
957
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000958\begin{memberdesc}{min}
959 The earliest representable time, \code{timetz(0, 0, 0, 0)}.
960\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000961
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000962\begin{memberdesc}{max}
963 The latest representable time, \code{timetz(23, 59, 59, 999999)}.
964\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000965
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000966\begin{memberdesc}{resolution}
967 The smallest possible difference between non-equal \class{timetz}
968 objects, \code{timedelta(microseconds=1)}, although note that
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000969 arithmetic on \class{timetz} objects is not supported.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000970\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000971
972Instance attributes (read-only):
973
974 .hour in range(24)
975 .minute in range(60)
976 .second in range(60)
977 .microsecond in range(1000000)
978 .tzinfo the object passed as the tzinfo argument to the
Fred Drakebbdb2502002-12-23 18:58:06 +0000979 \class{timetz} constructor, or \code{None} if none
980 was passed.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000981
982Supported operations:
983
Fred Drakebbdb2502002-12-23 18:58:06 +0000984\begin{itemize}
985 \item
Tim Peters60c76e42002-12-27 00:41:11 +0000986 comparison of \class{timetz} to \class{time} or \class{timetz},
987 where \var{a} is considered less than \var{b} when \var{a} precedes
Fred Drakea37e5cc2002-12-30 21:26:42 +0000988 \var{b} in time. If one comparand is naive and the other is aware,
Tim Peters60c76e42002-12-27 00:41:11 +0000989 \exception{TypeError} is raised. If both comparands are aware, and
990 have the same \member{tzinfo} member, the common \member{tzinfo}
991 member is ignored and the base times are compared. If both
992 comparands are aware and have different \member{tzinfo} members,
993 the comparands are first adjusted by subtracting their UTC offsets
994 (obtained from \code{self.utcoffset()}).
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000995
Fred Drakebbdb2502002-12-23 18:58:06 +0000996 \item
997 hash, use as dict key
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000998
Fred Drakebbdb2502002-12-23 18:58:06 +0000999 \item
1000 pickling
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001001
Fred Drakebbdb2502002-12-23 18:58:06 +00001002 \item
1003 in Boolean contexts, a \class{timetz} object is considered to be
1004 true if and only if, after converting it to minutes and
1005 subtracting \method{utcoffset()} (or \code{0} if that's
1006 \code{None}), the result is non-zero.
1007\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001008
1009Instance methods:
1010
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001011\begin{methoddesc}{replace}(hour=, minute=, second=, microsecond=, tzinfo=)
1012 Return a \class{timetz} with the same value, except for those fields given
Tim Peters12bf3392002-12-24 05:41:27 +00001013 new values by whichever keyword arguments are specified. Note that
Fred Drakea37e5cc2002-12-30 21:26:42 +00001014 \code{tzinfo=None} can be specified to create a naive \class{timetz} from an
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001015 aware \class{timetz}.
1016\end{methoddesc}
Tim Peters12bf3392002-12-24 05:41:27 +00001017
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001018\begin{methoddesc}{isoformat}{}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001019 Return a string representing the time in ISO 8601 format,
1020 HH:MM:SS.mmmmmm
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001021 or, if self.microsecond is 0,
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001022 HH:MM:SS
Fred Drakebbdb2502002-12-23 18:58:06 +00001023 If \method{utcoffset()} does not return \code{None}, a 6-character
1024 string is appended, giving the UTC offset in (signed) hours and
1025 minutes:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001026 HH:MM:SS.mmmmmm+HH:MM
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001027 or, if self.microsecond is 0,
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001028 HH:MM:SS+HH:MM
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001029\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001030
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001031\begin{methoddesc}{__str__}{}
Fred Drakebbdb2502002-12-23 18:58:06 +00001032 For a \class{timetz} \var{t}, \code{str(\var{t})} is equivalent to
1033 \code{\var{t}.isoformat()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001034\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001035
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001036\begin{methoddesc}{strftime}{format}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001037 Return a string representing the time, controlled by an explicit
Fred Drakebbdb2502002-12-23 18:58:06 +00001038 format string. See the section on \method{strftime()} behavior.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001039\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001040
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001041\begin{methoddesc}{utcoffset}{}
Fred Drakebbdb2502002-12-23 18:58:06 +00001042 If \member{tzinfo} is \code{None}, returns \code{None}, else
Tim Peters1cff9fc2002-12-24 16:25:29 +00001043 \code{tzinfo.utcoffset(self)} converted to a \class{timedelta}
1044 object.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001045\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001046
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001047\begin{methoddesc}{tzname}{}
Fred Drakebbdb2502002-12-23 18:58:06 +00001048 If \member{tzinfo} is \code{None}, returns \code{None}, else
1049 \code{tzinfo.tzname(self)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001050\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001051
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001052\begin{methoddesc}{dst}{}
Fred Drakebbdb2502002-12-23 18:58:06 +00001053 If \member{tzinfo} is \code{None}, returns \code{None}, else
Tim Peters1cff9fc2002-12-24 16:25:29 +00001054 \code{tzinfo.dst(self)} converted to a \class{timedelta} object.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001055\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001056
1057
1058
Raymond Hettinger6005a342002-12-30 20:01:24 +00001059\subsection{ \class{datetimetz} Objects \label{datetime-datetimetz}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001060
Fred Drakebbdb2502002-12-23 18:58:06 +00001061\begin{notice}[warning]
1062 I think this is \emph{still} missing some methods from the
1063 Python implementation.
1064\end{notice}
1065
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001066A \class{datetimetz} object is a single object containing all the information
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001067from a \class{date} object and a \class{timetz} object.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001068
1069Constructor:
1070
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001071\begin{funcdesc}{datetimetz}{year, month, day,
1072 hour=0, minute=0, second=0, microsecond=0, tzinfo=None}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001073
Fred Drakebbdb2502002-12-23 18:58:06 +00001074 The year, month and day arguments are required. \var{tzinfo} may
1075 be \code{None}, or an instance of a \class{tzinfo} subclass. The
1076 remaining arguments may be ints or longs, in the following ranges:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001077
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001078\begin{itemize}
1079 \item \code{MINYEAR <= \var{year} <= MAXYEAR}
1080 \item \code{1 <= \var{month} <= 12}
1081 \item \code{1 <= \var{day} <= number of days in the given month and year}
1082 \item \code{0 <= \var{hour} < 24}
1083 \item \code{0 <= \var{minute} < 60}
1084 \item \code{0 <= \var{second} < 60}
1085 \item \code{0 <= \var{microsecond} < 1000000}
1086\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001087
Fred Drakebbdb2502002-12-23 18:58:06 +00001088 If an argument outside those ranges is given,
1089 \exception{ValueError} is raised.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001090\end{funcdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001091
1092Other constructors (class methods):
1093
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001094\begin{funcdesc}{today}{}
1095 \methodline{utcnow}{}
1096 \methodline{utcfromtimestamp}{timestamp}
1097 \methodline{fromordinal}{ordinal}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001098
Fred Drakebbdb2502002-12-23 18:58:06 +00001099 These are the same as the \class{datetime} class methods of the
1100 same names, except that they construct a \class{datetimetz}
1101 object, with tzinfo \code{None}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001102\end{funcdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001103
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001104\begin{funcdesc}{now}{\optional{tzinfo=None}}
1105 \methodline{fromtimestamp}{timestamp\optional{, tzinfo=None}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001106
1107 These are the same as the \class{datetime} class methods of the same names,
1108 except that they accept an additional, optional tzinfo argument, and
1109 construct a \class{datetimetz} object with that \class{tzinfo} object attached.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001110\end{funcdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001111
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001112\begin{funcdesc}{combine}{date, time}
Fred Drakebbdb2502002-12-23 18:58:06 +00001113 This is the same as \method{datetime.combine()}, except that it constructs
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001114 a \class{datetimetz} object, and, if the time object is of type timetz,
1115 the \class{datetimetz} object has the same \class{tzinfo} object as the time object.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001116\end{funcdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001117
1118Class attributes:
1119
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001120\begin{memberdesc}{min}
1121 The earliest representable \class{datetimetz},
1122 \code{datetimetz(MINYEAR, 1, 1)}.
1123\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001124
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001125\begin{memberdesc}{max}
1126 The latest representable \class{datetime},
1127 \code{datetimetz(MAXYEAR, 12, 31, 23, 59, 59, 999999)}.
1128\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001129
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001130\begin{memberdesc}{resolution}
1131 The smallest possible difference between non-equal \class{datetimetz}
1132 objects, \code{timedelta(microseconds=1)}.
1133\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001134
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001135Instance attributes, all read-only:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001136
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001137\begin{memberdesc}{year}
1138Between MINYEAR and MAXYEAR inclusive
1139\end{memberdesc}
1140\begin{memberdesc}{month}
1141Between 1 and 12 inclusive
1142\end{memberdesc}
1143\begin{memberdesc}{day}
1144Between 1 and the number of days in the given month
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001145 of the given year
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001146\end{memberdesc}
1147\begin{memberdesc}{hour}
1148In \code{range(24)}.
1149\end{memberdesc}
1150\begin{memberdesc}{minute}
1151In \code{range(60)}.
1152\end{memberdesc}
1153\begin{memberdesc}{second}
1154In \code{range(60)}.
1155\end{memberdesc}
1156\begin{memberdesc}{microsecond}
1157In \code{range(1000000)}.
1158\end{memberdesc}
1159\begin{memberdesc}{tzinfo}
1160The object passed as the \var{tzinfo} argument to
Fred Drakebbdb2502002-12-23 18:58:06 +00001161 the \class{datetimetz} constructor, or \code{None}
1162 if none was passed.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001163\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001164
1165Supported operations:
1166
Fred Drakebbdb2502002-12-23 18:58:06 +00001167\begin{itemize}
1168 \item
1169 datetimetz1 + timedelta -> datetimetz2
1170 timedelta + datetimetz1 -> datetimetz2
Tim Peters60c76e42002-12-27 00:41:11 +00001171
Fred Drakebbdb2502002-12-23 18:58:06 +00001172 The same as addition of \class{datetime} objects, except that
1173 datetimetz2.tzinfo is set to datetimetz1.tzinfo.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001174
Fred Drakebbdb2502002-12-23 18:58:06 +00001175 \item
1176 datetimetz1 - timedelta -> datetimetz2
Tim Peters60c76e42002-12-27 00:41:11 +00001177
Fred Drakebbdb2502002-12-23 18:58:06 +00001178 The same as addition of \class{datetime} objects, except that
1179 datetimetz2.tzinfo is set to datetimetz1.tzinfo.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001180
Fred Drakebbdb2502002-12-23 18:58:06 +00001181 \item
1182 aware_datetimetz1 - aware_datetimetz2 -> timedelta
Fred Drakea37e5cc2002-12-30 21:26:42 +00001183 naive_datetimetz1 - naive_datetimetz2 -> timedelta
1184 naive_datetimetz1 - datetime2 -> timedelta
1185 datetime1 - naive_datetimetz2 -> timedelta
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001186
Tim Peters60c76e42002-12-27 00:41:11 +00001187 Subtraction of a \class{datetime} or \class{datetimetz}, from a
Fred Drakebbdb2502002-12-23 18:58:06 +00001188 \class{datetime} or \class{datetimetz}, is defined only if both
Fred Drakea37e5cc2002-12-30 21:26:42 +00001189 operands are naive, or if both are aware. If one is aware and the
1190 other is naive, \exception{TypeError} is raised.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001191
Fred Drakea37e5cc2002-12-30 21:26:42 +00001192 If both are naive, or both are aware and have the same \member{tzinfo}
Tim Peters60c76e42002-12-27 00:41:11 +00001193 member, subtraction acts as for \class{datetime} subtraction.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001194
Tim Peters60c76e42002-12-27 00:41:11 +00001195 If both are aware and have different \member{tzinfo} members,
1196 \code{a-b} acts as if \var{a} and \var{b} were first converted to UTC
1197 datetimes (by subtracting \code{a.utcoffset()} minutes from \var{a},
1198 and \code{b.utcoffset()} minutes from \var{b}), and then doing
Fred Drakebbdb2502002-12-23 18:58:06 +00001199 \class{datetime} subtraction, except that the implementation never
1200 overflows.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001201
Fred Drakebbdb2502002-12-23 18:58:06 +00001202 \item
Tim Peters60c76e42002-12-27 00:41:11 +00001203 comparison of \class{datetimetz} to \class{datetime} or
1204 \class{datetimetz}, where \var{a} is considered less than \var{b}
1205 when \var{a} precedes \var{b} in time. If one comparand is naive and
1206 the other is aware, \exception{TypeError} is raised. If both
1207 comparands are aware, and have the same \member{tzinfo} member,
1208 the common \member{tzinfo} member is ignored and the base datetimes
1209 are compared. If both comparands are aware and have different
1210 \member{tzinfo} members, the comparands are first adjusted by
1211 subtracting their UTC offsets (obtained from \code{self.utcoffset()}).
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001212
Fred Drakebbdb2502002-12-23 18:58:06 +00001213 \item
1214 hash, use as dict key
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001215
Fred Drakebbdb2502002-12-23 18:58:06 +00001216 \item
1217 efficient pickling
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001218
Fred Drakebbdb2502002-12-23 18:58:06 +00001219 \item
1220 in Boolean contexts, all \class{datetimetz} objects are considered to be
1221 true
1222\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001223
1224Instance methods:
1225
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001226\begin{methoddesc}{date}{}
1227 \methodline{time}{}
1228 \methodline{toordinal}{}
1229 \methodline{weekday}{}
1230 \methodline{isoweekday}{}
1231 \methodline{isocalendar}{}
1232 \methodline{ctime}{}
1233 \methodline{__str__}{}
1234 \methodline{strftime}{format}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001235
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001236These are the same as the \class{datetime} methods of the same names.
1237\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001238
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001239\begin{methoddesc}{timetz}{}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001240 Return \class{timetz} object with same hour, minute, second, microsecond,
1241 and tzinfo.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001242\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001243
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001244\begin{methoddesc}{replace}{year=, month=, day=, hour=, minute=, second=, microsecond=,
1245 tzinfo=}
Tim Peters12bf3392002-12-24 05:41:27 +00001246 Return a datetimetz with the same value, except for those fields given
1247 new values by whichever keyword arguments are specified. Note that
1248 \code{tzinfo=None} can be specified to create a naive datetimetz from
1249 an aware datetimetz.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001250\end{methoddesc}
Tim Peters12bf3392002-12-24 05:41:27 +00001251
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001252\begin{methoddesc}{astimezone}{tz}
Tim Peters80475bb2002-12-25 07:40:55 +00001253 Return a \class{datetimetz} with new tzinfo member \var{tz}. \var{tz}
Tim Peters276a8f32002-12-27 21:41:32 +00001254 must be \code{None}, or an instance of a \class{tzinfo} subclass. If
1255 \var{tz} is \code{None}, self is naive, or
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001256 \code{tz.utcoffset(self)} returns \code{None},
Tim Peters80475bb2002-12-25 07:40:55 +00001257 \code{self.astimezone(tz)} is equivalent to
1258 \code{self.replace(tzinfo=tz)}: a new timezone object is attached
1259 without any conversion of date or time fields. If self is aware and
1260 \code{tz.utcoffset(self)} does not return \code{None}, the date and
1261 time fields are adjusted so that the result is local time in timezone
Tim Peters95322982002-12-31 16:01:47 +00001262 tz, representing the same UTC time as self.
1263 XXX [The treatment of endcases remains unclear: for DST-aware
1264 classes, one hour per year has two spellings in local time, and
1265 another hour has no spelling in local time.] XXX
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001266\end{methoddesc}
Tim Peters80475bb2002-12-25 07:40:55 +00001267
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001268\begin{methoddesc}{utcoffset}{}
Fred Drakebbdb2502002-12-23 18:58:06 +00001269 If \member{tzinfo} is \code{None}, returns \code{None}, else
Tim Peters1cff9fc2002-12-24 16:25:29 +00001270 \code{tzinfo.utcoffset(self)} converted to a \class{timedelta}
1271 object.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001272\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001273
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001274\begin{methoddesc}{tzname}{}
Fred Drakebbdb2502002-12-23 18:58:06 +00001275 If \member{tzinfo} is \code{None}, returns \code{None}, else
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001276 returns \code{tzinfo.tzname(self)}.
1277\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001278
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001279\begin{methoddesc}{dst}{}
Fred Drakebbdb2502002-12-23 18:58:06 +00001280 If \member{tzinfo} is \code{None}, returns \code{None}, else
Tim Peters1cff9fc2002-12-24 16:25:29 +00001281 \code{tzinfo.dst(self)} converted to a \class{timedelta}
1282 object.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001283\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001284
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001285\begin{methoddesc}{timetuple}{}
Fred Drakebbdb2502002-12-23 18:58:06 +00001286 Like \function{datetime.timetuple()}, but sets the
1287 \member{tm_isdst} flag according to the \method{dst()} method: if
1288 \method{dst()} returns \code{None}, \member{tm_isdst} is set to
1289 \code{-1}; else if \method{dst()} returns a non-zero value,
1290 \member{tm_isdst} is set to \code{1}; else \code{tm_isdst} is set
1291 to \code{0}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001292\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001293
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001294\begin{methoddesc}{utctimetuple}{}
Fred Drakea37e5cc2002-12-30 21:26:42 +00001295 If \class{datetimetz} instance \var{d} is naive, this is the same as
Fred Drakebbdb2502002-12-23 18:58:06 +00001296 \code{\var{d}.timetuple()} except that \member{tm_isdst} is forced to 0
1297 regardless of what \code{d.dst()} returns. DST is never in effect
1298 for a UTC time.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001299
Fred Drakebbdb2502002-12-23 18:58:06 +00001300 If \var{d} is aware, \var{d} is normalized to UTC time, by subtracting
1301 \code{\var{d}.utcoffset()} minutes, and a timetuple for the
1302 normalized time is returned. \member{tm_isdst} is forced to 0.
1303 Note that the result's \member{tm_year} field may be
1304 \constant{MINYEAR}-1 or \constant{MAXYEAR}+1, if \var{d}.year was
1305 \code{MINYEAR} or \code{MAXYEAR} and UTC adjustment spills over a
1306 year boundary.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001307\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001308
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001309\begin{methoddesc}{isoformat}{sep='T'}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001310 Return a string representing the date and time in ISO 8601 format,
1311 YYYY-MM-DDTHH:MM:SS.mmmmmm
Fred Drakebbdb2502002-12-23 18:58:06 +00001312 or, if \member{microsecond} is 0,
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001313 YYYY-MM-DDTHH:MM:SS
1314
Fred Drakebbdb2502002-12-23 18:58:06 +00001315 If \method{utcoffset()} does not return \code{None}, a 6-character
1316 string is appended, giving the UTC offset in (signed) hours and
1317 minutes:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001318 YYYY-MM-DDTHH:MM:SS.mmmmmm+HH:MM
Fred Drakebbdb2502002-12-23 18:58:06 +00001319 or, if \member{microsecond} is 0
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001320 YYYY-MM-DDTHH:MM:SS+HH:MM
1321
Fred Drakebbdb2502002-12-23 18:58:06 +00001322 The optional argument \var{sep} (default \code{'T'}) is a
1323 one-character separator, placed between the date and time portions
1324 of the result. For example,
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001325
Fred Drakebbdb2502002-12-23 18:58:06 +00001326\begin{verbatim}
1327>>> from datetime import *
1328>>> class TZ(tzinfo):
1329... def utcoffset(self, dt): return -399
1330...
1331>>> datetimetz(2002, 12, 25, tzinfo=TZ()).isoformat(' ')
1332'2002-12-25 00:00:00-06:39'
1333\end{verbatim}
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001334\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001335
Fred Drakebbdb2502002-12-23 18:58:06 +00001336\code{str(\var{d})} is equivalent to \code{\var{d}.isoformat(' ')}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001337
1338
Tim Peters29fb9c72002-12-23 22:21:52 +00001339\subsection{\method{strftime()} Behavior}
1340
1341\class{date}, \class{datetime}, \class{datetimetz}, \class{time},
1342and \class{timetz} objects all support a \code{strftime(\var{format})}
1343method, to create a string representing the time under the control of
1344an explicit format string. Broadly speaking,
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001345\code{d.strftime(fmt)}
Tim Peters29fb9c72002-12-23 22:21:52 +00001346acts like the \refmodule{time} module's
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001347\code{time.strftime(fmt, d.timetuple())}
Tim Peters29fb9c72002-12-23 22:21:52 +00001348although not all objects support a \method{timetuple()} method.
1349
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001350For \class{time} and \class{timetz} objects, the format codes for
1351year, month, and day should not be used, as time objects have no such
1352values. If they're used anyway, \code{1900} is substituted for the
1353year, and \code{0} for the month and day.
Tim Peters29fb9c72002-12-23 22:21:52 +00001354
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001355For \class{date} objects, the format codes for hours, minutes, and
1356seconds should not be used, as \class{date} objects have no such
1357values. If they're used anyway, \code{0} is substituted for them.
Tim Peters29fb9c72002-12-23 22:21:52 +00001358
Fred Drakea37e5cc2002-12-30 21:26:42 +00001359For a naive object, the \code{\%z} and \code{\%Z} format codes are
Tim Peters29fb9c72002-12-23 22:21:52 +00001360replaced by empty strings.
1361
1362For an aware object:
1363
1364\begin{itemize}
1365 \item[\code{\%z}]
1366 \method{utcoffset()} is transformed into a 5-character string of
1367 the form +HHMM or -HHMM, where HH is a 2-digit string giving the
1368 number of UTC offset hours, and MM is a 2-digit string giving the
1369 number of UTC offset minutes. For example, if
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001370 \method{utcoffset()} returns \code{timedelta(hours=-3, minutes=-30)},
Tim Peters1cff9fc2002-12-24 16:25:29 +00001371 \code{\%z} is replaced with the string \code{'-0330'}.
Tim Peters29fb9c72002-12-23 22:21:52 +00001372
1373 \item[\code{\%Z}]
1374 If \method{tzname()} returns \code{None}, \code{\%Z} is replaced
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001375 by an empty string. Otherwise \code{\%Z} is replaced by the returned
Tim Peters29fb9c72002-12-23 22:21:52 +00001376 value, which must be a string.
1377\end{itemize}
1378
1379The full set of format codes supported varies across platforms,
1380because Python calls the platform C library's \function{strftime()}
1381function, and platform variations are common. The documentation for
1382Python's \refmodule{time} module lists the format codes that the C
1383standard (1989 version) requires, and those work on all platforms
1384with a standard C implementation. Note that the 1999 version of the
1385C standard added additional format codes.
1386
1387The exact range of years for which \method{strftime()} works also
1388varies across platforms. Regardless of platform, years before 1900
1389cannot be used.
1390
1391
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001392\begin{comment}
1393
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001394\subsection{C API}
1395
1396Struct typedefs:
1397
1398 PyDateTime_Date
1399 PyDateTime_DateTime
1400 PyDateTime_DateTimeTZ
1401 PyDateTime_Time
1402 PyDateTime_TimeTZ
1403 PyDateTime_Delta
1404 PyDateTime_TZInfo
1405
1406Type-check macros:
1407
1408 PyDate_Check(op)
1409 PyDate_CheckExact(op)
1410
1411 PyDateTime_Check(op)
1412 PyDateTime_CheckExact(op)
1413
1414 PyDateTimeTZ_Check(op)
1415 PyDateTimeTZ_CheckExact(op)
1416
1417 PyTime_Check(op)
1418 PyTime_CheckExact(op)
1419
1420 PyTimeTZ_Check(op)
1421 PyTimeTZ_CheckExact(op)
1422
1423 PyDelta_Check(op)
1424 PyDelta_CheckExact(op)
1425
1426 PyTZInfo_Check(op)
Raymond Hettingercbd6cd22002-12-31 16:30:49 +00001427 PyTZInfo_CheckExact(op)
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001428
1429Accessor macros:
1430
1431All objects are immutable, so accessors are read-only. All macros
1432return ints:
1433
Tim Peters1cff9fc2002-12-24 16:25:29 +00001434 For \class{date}, \class{datetime}, and \class{datetimetz} instances:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001435 PyDateTime_GET_YEAR(o)
1436 PyDateTime_GET_MONTH(o)
1437 PyDateTime_GET_DAY(o)
1438
1439 For \class{datetime} and \class{datetimetz} instances:
1440 PyDateTime_DATE_GET_HOUR(o)
1441 PyDateTime_DATE_GET_MINUTE(o)
1442 PyDateTime_DATE_GET_SECOND(o)
1443 PyDateTime_DATE_GET_MICROSECOND(o)
1444
Tim Peters1cff9fc2002-12-24 16:25:29 +00001445 For \class{time} and \class{timetz} instances:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001446 PyDateTime_TIME_GET_HOUR(o)
1447 PyDateTime_TIME_GET_MINUTE(o)
1448 PyDateTime_TIME_GET_SECOND(o)
1449 PyDateTime_TIME_GET_MICROSECOND(o)
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001450
1451\end{comment}