blob: ecec11d4fba96e15557d99a348220fb022835648 [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}
55 \seemodule{calendar}{General calandar related functions.}
56 \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
166 argument is a flost, the conversion and normalization processes
167 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,
187 \code{timedelta(-999999999)}}
188 \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
192 \class{timedelta} objects, \code{timedelta(microseconds=1)}}
193\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}}
211 {Sum of \var{t2} and \var{t3}.
212 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.
222 Afterwards \var{t1} // i == \var{t2} is true, provided i != 0.
223 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)}
228\end{tableiii}
Raymond Hettinger6005a342002-12-30 20:01:24 +0000229\noindent
230Notes:
231
232\begin{description}
233\item[(1)]
234This is exact, but may overflow.
235
236\item[(2)]
237Division by 0 raises \exception{ZeroDivisionError}.
238\end{description}
239
240
241
242
Fred Drakebbdb2502002-12-23 18:58:06 +0000243\begin{itemize}
244 \item
Fred Drakebbdb2502002-12-23 18:58:06 +0000245 certain additions and subtractions with date, datetime, and datimetz
246 objects (see below)
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000247
Fred Drakebbdb2502002-12-23 18:58:06 +0000248 \item
249 +timedelta -> timedelta
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000250 Returns a \class{timedelta} object with the same value.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000251
Fred Drakebbdb2502002-12-23 18:58:06 +0000252 \item
253 -timedelta -> timedelta
254 -t is equivalent to timedelta(-t.days, -t.seconds, -t.microseconds),
255 and to t*-1. This is exact, but may overflow (for example,
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000256 -timedelta.max is not representable as a \class{timedelta} object).
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000257
Fred Drakebbdb2502002-12-23 18:58:06 +0000258 \item
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000259 \code{abs(timedelta) -> timedelta}:
260 \code{abs(t)} is equivalent to +t when \code{t.days >= 0}, and to -t when
261 \code{t.days < 0}. This is exact, and cannot overflow.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000262
Fred Drakebbdb2502002-12-23 18:58:06 +0000263 \item
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000264 comparison of \class{timedelta} to timedelta; the \class{timedelta} representing
Fred Drakebbdb2502002-12-23 18:58:06 +0000265 the smaller duration is considered to be the smaller timedelta
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000266
Fred Drakebbdb2502002-12-23 18:58:06 +0000267 \item
268 hash, use as dict key
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000269
Fred Drakebbdb2502002-12-23 18:58:06 +0000270 \item
271 efficient pickling
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000272
Fred Drakebbdb2502002-12-23 18:58:06 +0000273 \item
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000274 in Boolean contexts, a \class{timedelta} object is considered to be true
Fred Drakebbdb2502002-12-23 18:58:06 +0000275 if and only if it isn't equal to \code{timedelta(0)}
276\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000277
278
Raymond Hettinger6005a342002-12-30 20:01:24 +0000279\subsection{\class{date} Objects \label{datetime-date}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000280
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000281A \class{date} object represents a date (year, month and day) in an idealized
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000282calendar, the current Gregorian calendar indefinitely extended in both
283directions. January 1 of year 1 is called day number 1, January 2 of year
2841 is called day number 2, and so on. This matches the definition of the
285"proleptic Gregorian" calendar in Dershowitz and Reingold's book
Fred Drakea37e5cc2002-12-30 21:26:42 +0000286\citetitle{Calendrical Calculations}, where it's the base calendar for all
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000287computations. See the book for algorithms for converting between
288proleptic Gregorian ordinals and many other calendar systems.
289
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000290\begin{funcdesc}{date}{year, month, day}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000291
292 All arguments are required. Arguments may be ints or longs, in the
293 following ranges:
294
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000295\begin{itemize}
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000296 \item \code{MINYEAR <= \var{year} <= MAXYEAR}
297 \item \code{1 <= \var{month} <= 12}
298 \item \code{1 <= \var{day} <= number of days in the given month and year}
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000299\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000300
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000301If an argument outside those ranges is given, \exception{ValueError}
302is raised.
303\end{funcdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000304
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000305Other constructors, all class methods:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000306
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000307\begin{methoddesc}{today}{}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000308 Return the current local date. This is equivalent to
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000309 \code{date.fromtimestamp(time.time())}.
310\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000311
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000312\begin{methoddesc}{fromtimestamp}{timestamp}
Fred Drakebbdb2502002-12-23 18:58:06 +0000313 Return the local date corresponding to the POSIX timestamp, such
314 as is returned by \function{time.time()}. This may raise
315 \exception{ValueError}, if the timestamp is out of the range of
316 values supported by the platform C \cfunction{localtime()}
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000317 function. It's common for this to be restricted to years from 1970
Fred Drakebbdb2502002-12-23 18:58:06 +0000318 through 2038.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000319\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000320
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000321\begin{methoddesc}{fromordinal}{ordinal}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000322 Return the date corresponding to the proleptic Gregorian ordinal,
Fred Drakebbdb2502002-12-23 18:58:06 +0000323 where January 1 of year 1 has ordinal 1. \exception{ValueError}
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000324 is raised unless \code{1 <= \var{ordinal} <= date.max.toordinal()}. For any
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000325 date \var{d}, \code{date.fromordinal(\var{d}.toordinal()) == \var{d}}.
326\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000327
328Class attributes:
329
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000330\begin{memberdesc}{min}
Fred Drakebbdb2502002-12-23 18:58:06 +0000331 The earliest representable date, \code{date(MINYEAR, 1, 1)}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000332\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000333
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000334\begin{memberdesc}{max}
Fred Drakebbdb2502002-12-23 18:58:06 +0000335 The latest representable date, \code{date(MAXYEAR, 12, 31)}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000336\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000337
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000338\begin{memberdesc}{resolution}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000339 The smallest possible difference between non-equal date
Fred Drakebbdb2502002-12-23 18:58:06 +0000340 objects, \code{timedelta(days=1)}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000341\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000342
343Instance attributes (read-only):
344
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000345\begin{memberdesc}{year}
346Between \constant{MINYEAR} and \constant{MAXYEAR} inclusive
347\end{memberdesc}
348
349\begin{memberdesc}{month}
350Between 1 and 12 inclusive.
351\end{memberdesc}
352
353\begin{memberdesc}{day}
354Between 1 and the number of days in the given month
355 of the given year.
356\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000357
358Supported operations:
359
Fred Drakebbdb2502002-12-23 18:58:06 +0000360\begin{itemize}
361 \item
362 date1 + timedelta -> date2
363 timedelta + date1 -> date2
364 date2 is timedelta.days days removed from the date1, moving forward
365 in time if timedelta.days > 0, or backward if timedetla.days < 0.
366 date2 - date1 == timedelta.days after. timedelta.seconds and
367 timedelta.microseconds are ignored. \exception{OverflowError} is
368 raised if date2.year would be smaller than \constant{MINYEAR} or
369 larger than \constant{MAXYEAR}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000370
Fred Drakebbdb2502002-12-23 18:58:06 +0000371 \item
372 date1 - timedelta -> date2
373 Computes the date2 such that date2 + timedelta == date1. This
374 isn't quite equivalent to date1 + (-timedelta), because -timedelta
375 in isolation can overflow in cases where date1 - timedelta does
376 not. timedelta.seconds and timedelta.microseconds are ignored.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000377
Fred Drakebbdb2502002-12-23 18:58:06 +0000378 \item
379 date1 - date2 -> timedelta
380 This is exact, and cannot overflow. timedelta.seconds and
381 timedelta.microseconds are 0, and date2 + timedelta == date1
382 after.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000383
Fred Drakebbdb2502002-12-23 18:58:06 +0000384 \item
385 comparison of date to date, where date1 is considered less than
386 date2 when date1 precedes date2 in time. In other words,
387 date1 < date2 if and only if date1.toordinal() < date2.toordinal().
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000388
Fred Drakebbdb2502002-12-23 18:58:06 +0000389 \item
390 hash, use as dict key
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000391
Fred Drakebbdb2502002-12-23 18:58:06 +0000392 \item
393 efficient pickling
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000394
Fred Drakebbdb2502002-12-23 18:58:06 +0000395 \item
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000396 in Boolean contexts, all \class{date} objects are considered to be true
Fred Drakebbdb2502002-12-23 18:58:06 +0000397\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000398
399Instance methods:
400
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000401\begin{methoddesc}{replace}{year, month, day}
Tim Peters12bf3392002-12-24 05:41:27 +0000402 Return a date with the same value, except for those fields given
403 new values by whichever keyword arguments are specified. For
404 example, if \code{d == date(2002, 12, 31)}, then
405 \code{d.replace(day=26) == date(2000, 12, 26)}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000406\end{methoddesc}
Tim Peters12bf3392002-12-24 05:41:27 +0000407
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000408\begin{methoddesc}{timetuple}{}
Fred Drakebbdb2502002-12-23 18:58:06 +0000409 Return a 9-element tuple of the form returned by
410 \function{time.localtime()}. The hours, minutes and seconds are
411 0, and the DST flag is -1.
Fred Drakea37e5cc2002-12-30 21:26:42 +0000412 \code{\var{d}.timetuple()} is equivalent to
413 \code{(\var{d}.year, \var{d}.month, \var{d}.day,
414 0, 0, 0, \# h, m, s
415 \var{d}.weekday(), \# 0 is Monday
416 \var{d}.toordinal() - date(\var{d}.year, 1, 1).toordinal() + 1,
417 \# day of year
418 -1)}
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000419\end{methoddesc}
420\begin{methoddesc}{toordinal}{}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000421 Return the proleptic Gregorian ordinal of the date, where January 1
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000422 of year 1 has ordinal 1. For any \class{date} object \var{d},
Fred Drakebbdb2502002-12-23 18:58:06 +0000423 \code{date.fromordinal(\var{d}.toordinal()) == \var{d}}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000424\end{methoddesc}
425\begin{methoddesc}{weekday}{}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000426 Return the day of the week as an integer, where Monday is 0 and
427 Sunday is 6. For example, date(2002, 12, 4).weekday() == 2, a
428 Wednesday.
Fred Drakebbdb2502002-12-23 18:58:06 +0000429 See also \method{isoweekday()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000430\end{methoddesc}
431\begin{methoddesc}{isoweekday}{}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000432 Return the day of the week as an integer, where Monday is 1 and
433 Sunday is 7. For example, date(2002, 12, 4).isoweekday() == 3, a
434 Wednesday.
Fred Drakebbdb2502002-12-23 18:58:06 +0000435 See also \method{weekday()}, \method{isocalendar()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000436\end{methoddesc}
437\begin{methoddesc}{isocalendar}{}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000438 Return a 3-tuple, (ISO year, ISO week number, ISO weekday).
439
440 The ISO calendar is a widely used variant of the Gregorian calendar.
Fred Drakebbdb2502002-12-23 18:58:06 +0000441 See \url{http://www.phys.uu.nl/~vgent/calendar/isocalendar.htm}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000442 for a good explanation.
443
444 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.
449
450 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
453
454 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}
457\begin{methoddesc}{isoformat}{}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000458 Return a string representing the date in ISO 8601 format,
459 'YYYY-MM-DD'. For example,
460 date(2002, 12, 4).isoformat() == '2002-12-04'.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000461\end{methoddesc}
462\begin{methoddesc}{__str__}{}
Fred Drakebbdb2502002-12-23 18:58:06 +0000463 For a date \var{d}, \code{str(\var{d})} is equivalent to
464 \code{\var{d}.isoformat()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000465\end{methoddesc}
466\begin{methoddesc}{ctime}{}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000467 Return a string representing the date, for example
468 date(2002, 12, 4).ctime() == 'Wed Dec 4 00:00:00 2002'.
Fred Drakea37e5cc2002-12-30 21:26:42 +0000469 \code{\var{d}.ctime()} is equivalent to
470 \code{time.ctime(time.mktime(\var{d}.timetuple()))}
Fred Drakebbdb2502002-12-23 18:58:06 +0000471 on platforms where the native C \cfunction{ctime()} function
472 (which \function{time.ctime()} invokes, but which
473 \method{date.ctime()} does not invoke) conforms to the C standard.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000474\end{methoddesc}
475\begin{methoddesc}{strftime}{format}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000476 Return a string representing the date, controlled by an explicit
477 format string. Format codes referring to hours, minutes or seconds
Fred Drakebbdb2502002-12-23 18:58:06 +0000478 will see 0 values.
479 See the section on \method{strftime()} behavior.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000480\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000481
482
Raymond Hettinger6005a342002-12-30 20:01:24 +0000483\subsection{\class{datetime} Objects \label{datetime-datetime}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000484
Fred Drakebbdb2502002-12-23 18:58:06 +0000485A \class{datetime} object is a single object containing all the
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000486information from a \class{date} object and a time object. Like a
487\class{date} object, \class{datetime} assumes the current Gregorian
488calendar extended in both directions; like a time object,
489\class{datetime} assumes there are exactly 3600*24 seconds in every
490day.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000491
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000492\begin{funcdesc}datetime{year, month, day,
493 hour=0, minute=0, second=0, microsecond=0}
494The year, month and day arguments are required. Arguments may be ints
495or longs, in the following ranges:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000496
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000497\begin{itemize}
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000498 \item \code{\member{MINYEAR} <= \var{year} <= \member{MAXYEAR}}
499 \item \code{1 <= \var{month} <= 12}
500 \item \code{1 <= \var{day} <= number of days in the given month and year}
501 \item \code{0 <= \var{hour} < 24}
502 \item \code{0 <= \var{minute} < 60}
503 \item \code{0 <= \var{second} < 60}
504 \item \code{0 <= \var{microsecond} < 1000000}
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000505\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000506
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000507If an argument outside those ranges is given,
508\exception{ValueError} is raised.
509\end{funcdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000510
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000511Other constructors, all class methods:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000512
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000513\begin{methoddesc}{today}{}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000514 Return the current local datetime. This is equivalent to
Fred Drakebbdb2502002-12-23 18:58:06 +0000515 \code{datetime.fromtimestamp(time.time())}.
516 See also \method{now()}, \method{fromtimestamp()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000517\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000518
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000519\begin{methoddesc}{now}{}
Fred Drakebbdb2502002-12-23 18:58:06 +0000520 Return the current local datetime. This is like \method{today()},
521 but, if possible, supplies more precision than can be gotten from
522 going through a \function{time.time()} timestamp (for example,
523 this may be possible on platforms that supply the C
524 \cfunction{gettimeofday()} function).
525 See also \method{today()}, \method{utcnow()}.
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}{utcnow}{}
Fred Drakebbdb2502002-12-23 18:58:06 +0000529 Return the current UTC datetime. This is like \method{now()}, but
530 returns the current UTC date and time.
531 See also \method{now()}.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000532\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000533
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000534\begin{methoddesc}{fromtimestamp}{timestamp}
Fred Drakebbdb2502002-12-23 18:58:06 +0000535 Return the local \class{datetime} corresponding to the \POSIX{}
536 timestamp, such as is returned by \function{time.time()}. This
537 may raise \exception{ValueError}, if the timestamp is out of the
538 range of values supported by the platform C
539 \cfunction{localtime()} function. It's common for this to be
540 restricted to years in 1970 through 2038.
541 See also \method{utcfromtimestamp()}.
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}{utcfromtimestamp}{timestamp}
Fred Drakebbdb2502002-12-23 18:58:06 +0000545 Return the UTC \class{datetime} corresponding to the \POSIX{}
546 timestamp. This may raise \exception{ValueError}, if the
547 timestamp is out of the range of values supported by the platform
548 C \cfunction{gmtime()} function. It's common for this to be
549 restricted to years in 1970 through 2038.
550 See also \method{fromtimestamp()}.
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}{fromordinal}{ordinal}
Fred Drakebbdb2502002-12-23 18:58:06 +0000554 Return the \class{datetime} corresponding to the proleptic
555 Gregorian ordinal, where January 1 of year 1 has ordinal 1.
556 \exception{ValueError} is raised unless 1 <= ordinal <=
557 datetime.max.toordinal(). The hour, minute, second and
558 microsecond of the result are all 0.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000559\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000560
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000561\begin{methoddesc}{combine}{date, time}
Fred Drakebbdb2502002-12-23 18:58:06 +0000562 Return a new \class{datetime} object whose date components are
Fred Drakea37e5cc2002-12-30 21:26:42 +0000563 equal to the given \class{date} object's, and whose time
564 components are equal to the given time object's. For any
565 \class{datetime} object \var{d}, \code{\var{d} ==
566 datetime.combine(\var{d}.date(), \var{d}.time())}. If date is a
567 \class{datetime} or \class{datetimetz} object, its time components
568 are ignored. If date is \class{datetimetz} object, its
569 \member{tzinfo} component is also ignored. If time is a
570 \class{timetz} object, its \member{tzinfo} component is ignored.
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000571\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000572
573Class attributes:
574
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000575\begin{memberdesc}{min}
576 The earliest representable \class{datetime},
577 \code{datetime(MINYEAR, 1, 1)}.
578\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000579
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000580\begin{memberdesc}{max}
581 The latest representable \class{datetime},
582 \code{datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999)}.
583\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000584
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000585\begin{memberdesc}{resolution}
586 The smallest possible difference between non-equal \class{datetime}
587 objects, \code{timedelta(microseconds=1)}.
588\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000589
590Instance attributes (read-only):
591
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000592\begin{memberdesc}{year}
593Between \constant{MINYEAR} and \constant{MAXYEAR} inclusive
594\end{memberdesc}
595
596\begin{memberdesc}{month}
597Between 1 and 12 inclusive
598\end{memberdesc}
599
600\begin{memberdesc}{day}
Fred Drakea37e5cc2002-12-30 21:26:42 +0000601Between 1 and the number of days in the given month of the given year.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000602\end{memberdesc}
603
604\begin{memberdesc}{hour}
605In \code{range(24)}.
606\end{memberdesc}
607
608\begin{memberdesc}{minute}
609In \code{range(60)}.
610\end{memberdesc}
611
612\begin{memberdesc}{second}
613In \code{range(60)}.
614\end{memberdesc}
615
616\begin{memberdesc}{microsecond}
617In \code{range(1000000)}.
618\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000619
620Supported operations:
621
Fred Drakebbdb2502002-12-23 18:58:06 +0000622\begin{itemize}
623 \item
624 datetime1 + timedelta -> datetime2
625 timedelta + datetime1 -> datetime2
626 datetime2 is a duration of timedelta removed from datetime1, moving
627 forward in time if timedelta.days > 0, or backward if
628 timedelta.days < 0. datetime2 - datetime1 == timedelta after.
629 \exception{OverflowError} is raised if datetime2.year would be
630 smaller than \constant{MINYEAR} or larger than \constant{MAXYEAR}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000631
Fred Drakebbdb2502002-12-23 18:58:06 +0000632 \item
633 datetime1 - timedelta -> datetime2
634 Computes the datetime2 such that datetime2 + timedelta == datetime1.
635 This isn't quite equivalent to datetime1 + (-timedelta), because
636 -timedelta in isolation can overflow in cases where
637 datetime1 - timedelta does not.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000638
Fred Drakebbdb2502002-12-23 18:58:06 +0000639 \item
640 datetime1 - datetime2 -> timedelta
641 This is exact, and cannot overflow.
642 datetime2 + timedelta == datetime1 after.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000643
Fred Drakebbdb2502002-12-23 18:58:06 +0000644 \item
645 comparison of \class{datetime} to datetime, where datetime1 is
646 considered less than datetime2 when datetime1 precedes datetime2
647 in time.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000648
Fred Drakebbdb2502002-12-23 18:58:06 +0000649 \item
650 hash, use as dict key
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000651
Fred Drakebbdb2502002-12-23 18:58:06 +0000652 \item
653 efficient pickling
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000654
Fred Drakebbdb2502002-12-23 18:58:06 +0000655 \item
656 in Boolean contexts, all \class{datetime} objects are considered
657 to be true
658\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000659
660Instance methods:
661
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000662\begin{methoddesc}{date}{}
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +0000663 Return \class{date} object with same year, month and day.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000664\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000665
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000666\begin{methoddesc}{time}{}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000667 Return time object with same hour, minute, second and microsecond.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000668\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000669
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000670\begin{methoddesc}{replace}{year=, month=, day=, hour=, minute=, second=, microsecond=}
Tim Peters12bf3392002-12-24 05:41:27 +0000671 Return a datetime with the same value, except for those fields given
672 new values by whichever keyword arguments are specified.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000673\end{methoddesc}
Tim Peters12bf3392002-12-24 05:41:27 +0000674
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000675\begin{methoddesc}{astimezone}{tz}
Tim Peters80475bb2002-12-25 07:40:55 +0000676 Return a \class{datetimetz} with the same date and time fields, and
Tim Peters276a8f32002-12-27 21:41:32 +0000677 with \member{tzinfo} member \var{tz}. \var{tz} must be \code{None},
678 or an instance of a \class{tzinfo} subclass.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000679\end{methoddesc}
Tim Peters80475bb2002-12-25 07:40:55 +0000680
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000681\begin{methoddesc}{timetuple}{}
Fred Drakebbdb2502002-12-23 18:58:06 +0000682 Return a 9-element tuple of the form returned by
683 \function{time.localtime()}.
Fred Drakea37e5cc2002-12-30 21:26:42 +0000684 The DST flag is -1. \code{\var{d}.timetuple()} is equivalent to
685 \code{(\var{d}.year, \var{d}.month, \var{d}.day,
686 \var{d}.hour, \var{d}.minute, \var{d}.second,
687 \var{d}.weekday(), \# 0 is Monday
688 \var{d}.toordinal() - date(\var{d}.year, 1, 1).toordinal() + 1,
689 \# day of year
690 -1)}
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000691\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000692
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000693\begin{methoddesc}{toordinal}{}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000694 Return the proleptic Gregorian ordinal of the date. The same as
Fred Drakebbdb2502002-12-23 18:58:06 +0000695 \method{date.toordinal()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000696\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000697
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000698\begin{methoddesc}{weekday}{}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000699 Return the day of the week as an integer, where Monday is 0 and
Fred Drakebbdb2502002-12-23 18:58:06 +0000700 Sunday is 6. The same as \method{date.weekday()}.
701 See also \method{isoweekday()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000702\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000703
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000704\begin{methoddesc}{isoweekday}{}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000705 Return the day of the week as an integer, where Monday is 1 and
Fred Drakebbdb2502002-12-23 18:58:06 +0000706 Sunday is 7. The same as \method{date.isoweekday()}.
707 See also \method{weekday()}, \method{isocalendar()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000708\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000709
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000710\begin{methoddesc}{isocalendar}{}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000711 Return a 3-tuple, (ISO year, ISO week number, ISO weekday). The
Fred Drakebbdb2502002-12-23 18:58:06 +0000712 same as \method{date.isocalendar()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000713\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000714
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000715\begin{methoddesc}{isoformat}{sep='T'}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000716 Return a string representing the date and time in ISO 8601 format,
717 YYYY-MM-DDTHH:MM:SS.mmmmmm
718 or, if self.microsecond is 0,
719 YYYY-MM-DDTHH:MM:SS
Fred Drakebbdb2502002-12-23 18:58:06 +0000720 The optional argument \var{sep} (default \code{'T'}) is a
721 one-character separator, placed between the date and time portions
722 of the result. For example,
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000723 datetime(2002, 12, 4, 1, 2, 3, 4).isoformat(' ') ==
724 '2002-12-04 01:02:03.000004'
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000725\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000726
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000727\begin{methoddesc}{__str__}{}
Fred Drakebbdb2502002-12-23 18:58:06 +0000728 For a \class{datetime} instance \var{d}, \code{str(\var{d})} is
729 equivalent to \code{\var{d}.isoformat(' ')}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000730\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000731
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000732\begin{methoddesc}{ctime}{}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000733 Return a string representing the date, for example
734 datetime(2002, 12, 4, 20, 30, 40).ctime() == 'Wed Dec 4 20:30:40 2002'.
Fred Drakebbdb2502002-12-23 18:58:06 +0000735 \code{d.ctime()} is equivalent to
736 \code{time.ctime(time.mktime(d.timetuple()))} on platforms where
737 the native C \cfunction{ctime()} function (which
738 \function{time.ctime()} invokes, but which
739 \method{datetime.ctime()} does not invoke) conforms to the C
740 standard.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000741\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000742
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000743\begin{methoddesc}{strftime}{format}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000744 Return a string representing the date and time, controlled by an
Fred Drakebbdb2502002-12-23 18:58:06 +0000745 explicit format string. See the section on \method{strftime()}
746 behavior.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000747\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000748
749
Raymond Hettinger6005a342002-12-30 20:01:24 +0000750\subsection{\class{time} Objects \label{datetime-time}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000751
752A time object represents an idealized time of day, independent of day
753and timezone.
754
755Constructor:
756
757 time(hour=0, minute=0, second=0, microsecond=0)
758
759 All arguments are optional. They may be ints or longs, in the
760 following ranges:
761
762 0 <= hour < 24
763 0 <= minute < 60
764 0 <= second < 60
765 0 <= microsecond < 1000000
766
Fred Drakebbdb2502002-12-23 18:58:06 +0000767 If an argument outside those ranges is given,
768 \exception{ValueError} is raised.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000769
770Class attributes:
771
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000772\begin{memberdesc}{min}
773 The earliest representable \class{time}, \code{time(0, 0, 0, 0)}.
774\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000775
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000776\begin{memberdesc}{max}
777 The latest representable \class{time}, \code{time(23, 59, 59, 999999)}.
778\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000779
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000780\begin{memberdesc}{resolution}
781 The smallest possible difference between non-equal \class{time}
782 objects, \code{timedelta(microseconds=1)}, although note that
783 arithmetic on \class{time} objects is not supported.
784\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000785
786Instance attributes (read-only):
787
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000788\begin{memberdesc}{hour}
789In \code{range(24)}.
790\end{memberdesc}
Tim Petersbad8ff02002-12-30 20:52:32 +0000791\begin{memberdesc}{minute}
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000792In \code{range(60)}.
793\end{memberdesc}
Tim Petersbad8ff02002-12-30 20:52:32 +0000794\begin{memberdesc}{second}
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000795In \code{range(60)}.
796\end{memberdesc}
Tim Petersbad8ff02002-12-30 20:52:32 +0000797\begin{memberdesc}{microsecond}
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000798In \code{range(1000000)}.
799\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000800
801Supported operations:
802
Fred Drakebbdb2502002-12-23 18:58:06 +0000803\begin{itemize}
804 \item
805 comparison of time to time, where time1 is considered
806 less than time2 when time1 precedes time2 in time.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000807
Fred Drakebbdb2502002-12-23 18:58:06 +0000808 \item
809 hash, use as dict key
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000810
Fred Drakebbdb2502002-12-23 18:58:06 +0000811 \item
812 efficient pickling
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000813
Fred Drakebbdb2502002-12-23 18:58:06 +0000814 \item
815 in Boolean contexts, a time object is considered to be true
Fred Drakea37e5cc2002-12-30 21:26:42 +0000816 if and only if it isn't equal to \code{time(0)}
Fred Drakebbdb2502002-12-23 18:58:06 +0000817\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000818
819Instance methods:
820
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000821\begin{methoddesc}{replace}{hour=, minute=, second=, microsecond=}
Tim Peters12bf3392002-12-24 05:41:27 +0000822 Return a time with the same value, except for those fields given
823 new values by whichever keyword arguments are specified.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000824\end{methoddesc}
Tim Peters12bf3392002-12-24 05:41:27 +0000825
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000826\begin{methoddesc}{isoformat}{}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000827 Return a string representing the time in ISO 8601 format,
828 HH:MM:SS.mmmmmm
829 or, if self.microsecond is 0
830 HH:MM:SS
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000831\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000832
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000833\begin{methoddesc}{__str__}{}
Fred Drakebbdb2502002-12-23 18:58:06 +0000834 For a time \var{t}, \code{str(\var{t})} is equivalent to
835 \code{\var{t}.isoformat()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000836\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000837
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000838\begin{methoddesc}{strftime}{format}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000839 Return a string representing the time, controlled by an explicit
Fred Drakebbdb2502002-12-23 18:58:06 +0000840 format string. See the section on \method{strftime()} behavior.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000841\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000842
843
Raymond Hettinger6005a342002-12-30 20:01:24 +0000844\subsection{\class{tzinfo} Objects \label{datetime-tzinfo}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000845
Fred Drakebbdb2502002-12-23 18:58:06 +0000846\class{tzinfo} is an abstract base clase, meaning that this class
847should not be instantiated directly. You need to derive a concrete
848subclass, and (at least) supply implementations of the standard
849\class{tzinfo} methods needed by the \class{datetime} methods you
Tim Petersbad8ff02002-12-30 20:52:32 +0000850use. The \module{datetime} module does not supply any concrete
Fred Drakebbdb2502002-12-23 18:58:06 +0000851subclasses of \class{tzinfo}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000852
Fred Drakebbdb2502002-12-23 18:58:06 +0000853An instance of (a concrete subclass of) \class{tzinfo} can be passed
854to the constructors for \class{datetimetz} and \class{timetz} objects.
855The latter objects view their fields as being in local time, and the
856\class{tzinfo} object supports methods revealing offset of local time
857from UTC, the name of the time zone, and DST offset, all relative to a
858date or time object passed to them.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000859
Tim Petersbad8ff02002-12-30 20:52:32 +0000860Special requirement for pickling: A \class{tzinfo} subclass must have an
Tim Peters2483b612002-12-24 16:30:58 +0000861\method{__init__} method that can be called with no arguments, else it
862can be pickled but possibly not unpickled again. This is a technical
863requirement that may be relaxed in the future.
864
Fred Drakebbdb2502002-12-23 18:58:06 +0000865A concrete subclass of \class{tzinfo} may need to implement the
866following methods. Exactly which methods are needed depends on the
Tim Petersbad8ff02002-12-30 20:52:32 +0000867uses made of aware \module{datetime} objects. If in doubt, simply
868implement all of them.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000869
Tim Petersbad8ff02002-12-30 20:52:32 +0000870\begin{methoddesc}{utcoffset}{self, dt}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000871 Return offset of local time from UTC, in minutes east of UTC. If
872 local time is west of UTC, this should be negative. Note that this
873 is intended to be the total offset from UTC; for example, if a
874 \class{tzinfo} object represents both time zone and DST adjustments,
Fred Drakebbdb2502002-12-23 18:58:06 +0000875 \method{utcoffset()} should return their sum. If the UTC offset
876 isn't known, return \code{None}. Else the value returned must be
877 an integer, in the range -1439 to 1439 inclusive (1440 = 24*60;
Tim Peters1cff9fc2002-12-24 16:25:29 +0000878 the magnitude of the offset must be less than one day), or a
879 \class{timedelta} object representing a whole number of minutes
Tim Petersbad8ff02002-12-30 20:52:32 +0000880 in the same range. Most implementations of \method{utcoffset()}
881 will probably look like:
882\begin{verbatim}
883 return CONSTANT # fixed-offset class
884 return CONSTANT + self.dst(dt) # daylight-aware class
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000885\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000886
Tim Petersbad8ff02002-12-30 20:52:32 +0000887\begin{methoddesc}{tzname}{self, dt}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000888 Return the timezone name corresponding to the \class{datetime} represented
889 by dt, as a string. Nothing about string names is defined by the
890 \module{datetime} module, and there's no requirement that it mean anything
891 in particular. For example, "GMT", "UTC", "-500", "-5:00", "EDT",
892 "US/Eastern", "America/New York" are all valid replies. Return
Fred Drakebbdb2502002-12-23 18:58:06 +0000893 \code{None} if a string name isn't known. Note that this is a method
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000894 rather than a fixed string primarily because some \class{tzinfo} objects
895 will wish to return different names depending on the specific value
896 of dt passed, especially if the \class{tzinfo} class is accounting for DST.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000897\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000898
Tim Petersbad8ff02002-12-30 20:52:32 +0000899\begin{methoddesc}{dst}{self, dt}
Fred Drakebbdb2502002-12-23 18:58:06 +0000900 Return the DST offset, in minutes east of UTC, or \code{None} if
901 DST information isn't known. Return 0 if DST is not in effect.
Tim Peters1cff9fc2002-12-24 16:25:29 +0000902 If DST is in effect, return the offset as an integer or
903 \class{timedelta} object (see \method{utcoffset()} for details).
904 Note that DST offset, if applicable, has
Fred Drakebbdb2502002-12-23 18:58:06 +0000905 already been added to the UTC offset returned by
906 \method{utcoffset()}, so there's no need to consult \method{dst()}
907 unless you're interested in displaying DST info separately. For
Tim Peters1cff9fc2002-12-24 16:25:29 +0000908 example, \method{datetimetz.timetuple()} calls its \member{tzinfo}
909 member's \method{dst()} method to determine how the
Fred Drakebbdb2502002-12-23 18:58:06 +0000910 \member{tm_isdst} flag should be set.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000911\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000912
Tim Petersbad8ff02002-12-30 20:52:32 +0000913These methods are called by a \class{datetimetz} or \class{timetz} object,
914in response to their methods of the same names. A \class{datetimetz}
915object passes itself as the argument, and a \class{timetz} object passes
916\code{None} as the argument. A \class{tzinfo} subclass's methods should
917therefore be prepared to accept a \var{dt} argument of \code{None}, or of
918class \class{datetimetz}.
919
920When \code{None} is passed, it's up to the class designer to decide the
921best response. For example, returning \code{None} is appropriate if the
922class wishes to say that timetz objects don't participate in the
923\class{tzinfo} protocol. In other applications, it may be more useful
924for \code{utcoffset(None}} to return the standard UTC offset.
925
926When a \class{datetimetz} object is passed in response to a
927\class{datetimetz} method, \code{dt.tzinfo} is the same object as
928\var{self}. \class{tzinfo} methods can rely on this, unless
929user code calls \class{tzinfo} methods directly. The intent is that
930the \class{tzinfo} methods interpret \var{dt} as being in local time,
931and not need to worry about objects in other timezones.
932
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000933Example \class{tzinfo} classes:
934
Fred Drakebbdb2502002-12-23 18:58:06 +0000935\verbatiminput{tzinfo-examples.py}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000936
937
Raymond Hettinger6005a342002-12-30 20:01:24 +0000938\subsection{\class{timetz} Objects \label{datetime-timetz}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000939
940A time object represents a (local) time of day, independent of any
941particular day, and subject to adjustment via a \class{tzinfo} object.
942
943Constructor:
944
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000945\begin{funcdesc}{time}{hour=0, minute=0, second=0, microsecond=0, tzinfo=None}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000946
Fred Drakebbdb2502002-12-23 18:58:06 +0000947 All arguments are optional. \var{tzinfo} may be \code{None}, or
948 an instance of a \class{tzinfo} subclass. The remaining arguments
949 may be ints or longs, in the following ranges:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000950
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000951\begin{itemize}
952 \item \code{0 <= \var{hour} < 24}
953 \item \code{0 <= \var{minute} < 60}
954 \item \code{0 <= \var{second} < 60}
955 \item \code{0 <= \var{microsecond} < 1000000}.
956\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000957
Fred Drakebbdb2502002-12-23 18:58:06 +0000958 If an argument outside those ranges is given,
959 \exception{ValueError} is raised.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000960\end{funcdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000961
962Class attributes:
963
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000964\begin{memberdesc}{min}
965 The earliest representable time, \code{timetz(0, 0, 0, 0)}.
966\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000967
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000968\begin{memberdesc}{max}
969 The latest representable time, \code{timetz(23, 59, 59, 999999)}.
970\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000971
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000972\begin{memberdesc}{resolution}
973 The smallest possible difference between non-equal \class{timetz}
974 objects, \code{timedelta(microseconds=1)}, although note that
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000975 arithmetic on \class{timetz} objects is not supported.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +0000976\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000977
978Instance attributes (read-only):
979
980 .hour in range(24)
981 .minute in range(60)
982 .second in range(60)
983 .microsecond in range(1000000)
984 .tzinfo the object passed as the tzinfo argument to the
Fred Drakebbdb2502002-12-23 18:58:06 +0000985 \class{timetz} constructor, or \code{None} if none
986 was passed.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000987
988Supported operations:
989
Fred Drakebbdb2502002-12-23 18:58:06 +0000990\begin{itemize}
991 \item
Tim Peters60c76e42002-12-27 00:41:11 +0000992 comparison of \class{timetz} to \class{time} or \class{timetz},
993 where \var{a} is considered less than \var{b} when \var{a} precedes
Fred Drakea37e5cc2002-12-30 21:26:42 +0000994 \var{b} in time. If one comparand is naive and the other is aware,
Tim Peters60c76e42002-12-27 00:41:11 +0000995 \exception{TypeError} is raised. If both comparands are aware, and
996 have the same \member{tzinfo} member, the common \member{tzinfo}
997 member is ignored and the base times are compared. If both
998 comparands are aware and have different \member{tzinfo} members,
999 the comparands are first adjusted by subtracting their UTC offsets
1000 (obtained from \code{self.utcoffset()}).
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001001
Fred Drakebbdb2502002-12-23 18:58:06 +00001002 \item
1003 hash, use as dict key
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001004
Fred Drakebbdb2502002-12-23 18:58:06 +00001005 \item
1006 pickling
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001007
Fred Drakebbdb2502002-12-23 18:58:06 +00001008 \item
1009 in Boolean contexts, a \class{timetz} object is considered to be
1010 true if and only if, after converting it to minutes and
1011 subtracting \method{utcoffset()} (or \code{0} if that's
1012 \code{None}), the result is non-zero.
1013\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001014
1015Instance methods:
1016
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001017\begin{methoddesc}{replace}(hour=, minute=, second=, microsecond=, tzinfo=)
1018 Return a \class{timetz} with the same value, except for those fields given
Tim Peters12bf3392002-12-24 05:41:27 +00001019 new values by whichever keyword arguments are specified. Note that
Fred Drakea37e5cc2002-12-30 21:26:42 +00001020 \code{tzinfo=None} can be specified to create a naive \class{timetz} from an
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001021 aware \class{timetz}.
1022\end{methoddesc}
Tim Peters12bf3392002-12-24 05:41:27 +00001023
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001024\begin{methoddesc}{isoformat}{}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001025 Return a string representing the time in ISO 8601 format,
1026 HH:MM:SS.mmmmmm
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
Fred Drakebbdb2502002-12-23 18:58:06 +00001029 If \method{utcoffset()} does not return \code{None}, a 6-character
1030 string is appended, giving the UTC offset in (signed) hours and
1031 minutes:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001032 HH:MM:SS.mmmmmm+HH:MM
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001033 or, if self.microsecond is 0,
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001034 HH:MM:SS+HH:MM
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001035\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001036
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001037\begin{methoddesc}{__str__}{}
Fred Drakebbdb2502002-12-23 18:58:06 +00001038 For a \class{timetz} \var{t}, \code{str(\var{t})} is equivalent to
1039 \code{\var{t}.isoformat()}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001040\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001041
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001042\begin{methoddesc}{strftime}{format}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001043 Return a string representing the time, controlled by an explicit
Fred Drakebbdb2502002-12-23 18:58:06 +00001044 format string. See the section on \method{strftime()} behavior.
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}{utcoffset}{}
Fred Drakebbdb2502002-12-23 18:58:06 +00001048 If \member{tzinfo} is \code{None}, returns \code{None}, else
Tim Peters1cff9fc2002-12-24 16:25:29 +00001049 \code{tzinfo.utcoffset(self)} converted to a \class{timedelta}
1050 object.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001051\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001052
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001053\begin{methoddesc}{tzname}{}
Fred Drakebbdb2502002-12-23 18:58:06 +00001054 If \member{tzinfo} is \code{None}, returns \code{None}, else
1055 \code{tzinfo.tzname(self)}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001056\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001057
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001058\begin{methoddesc}{dst}{}
Fred Drakebbdb2502002-12-23 18:58:06 +00001059 If \member{tzinfo} is \code{None}, returns \code{None}, else
Tim Peters1cff9fc2002-12-24 16:25:29 +00001060 \code{tzinfo.dst(self)} converted to a \class{timedelta} object.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001061\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001062
1063
1064
Raymond Hettinger6005a342002-12-30 20:01:24 +00001065\subsection{ \class{datetimetz} Objects \label{datetime-datetimetz}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001066
Fred Drakebbdb2502002-12-23 18:58:06 +00001067\begin{notice}[warning]
1068 I think this is \emph{still} missing some methods from the
1069 Python implementation.
1070\end{notice}
1071
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001072A \class{datetimetz} object is a single object containing all the information
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001073from a \class{date} object and a \class{timetz} object.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001074
1075Constructor:
1076
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001077\begin{funcdesc}{datetimetz}{year, month, day,
1078 hour=0, minute=0, second=0, microsecond=0, tzinfo=None}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001079
Fred Drakebbdb2502002-12-23 18:58:06 +00001080 The year, month and day arguments are required. \var{tzinfo} may
1081 be \code{None}, or an instance of a \class{tzinfo} subclass. The
1082 remaining arguments may be ints or longs, in the following ranges:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001083
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001084\begin{itemize}
1085 \item \code{MINYEAR <= \var{year} <= MAXYEAR}
1086 \item \code{1 <= \var{month} <= 12}
1087 \item \code{1 <= \var{day} <= number of days in the given month and year}
1088 \item \code{0 <= \var{hour} < 24}
1089 \item \code{0 <= \var{minute} < 60}
1090 \item \code{0 <= \var{second} < 60}
1091 \item \code{0 <= \var{microsecond} < 1000000}
1092\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001093
Fred Drakebbdb2502002-12-23 18:58:06 +00001094 If an argument outside those ranges is given,
1095 \exception{ValueError} is raised.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001096\end{funcdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001097
1098Other constructors (class methods):
1099
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001100\begin{funcdesc}{today}{}
1101 \methodline{utcnow}{}
1102 \methodline{utcfromtimestamp}{timestamp}
1103 \methodline{fromordinal}{ordinal}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001104
Fred Drakebbdb2502002-12-23 18:58:06 +00001105 These are the same as the \class{datetime} class methods of the
1106 same names, except that they construct a \class{datetimetz}
1107 object, with tzinfo \code{None}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001108\end{funcdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001109
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001110\begin{funcdesc}{now}{\optional{tzinfo=None}}
1111 \methodline{fromtimestamp}{timestamp\optional{, tzinfo=None}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001112
1113 These are the same as the \class{datetime} class methods of the same names,
1114 except that they accept an additional, optional tzinfo argument, and
1115 construct a \class{datetimetz} object with that \class{tzinfo} object attached.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001116\end{funcdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001117
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001118\begin{funcdesc}{combine}{date, time}
Fred Drakebbdb2502002-12-23 18:58:06 +00001119 This is the same as \method{datetime.combine()}, except that it constructs
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001120 a \class{datetimetz} object, and, if the time object is of type timetz,
1121 the \class{datetimetz} object has the same \class{tzinfo} object as the time object.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001122\end{funcdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001123
1124Class attributes:
1125
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001126\begin{memberdesc}{min}
1127 The earliest representable \class{datetimetz},
1128 \code{datetimetz(MINYEAR, 1, 1)}.
1129\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001130
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001131\begin{memberdesc}{max}
1132 The latest representable \class{datetime},
1133 \code{datetimetz(MAXYEAR, 12, 31, 23, 59, 59, 999999)}.
1134\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001135
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001136\begin{memberdesc}{resolution}
1137 The smallest possible difference between non-equal \class{datetimetz}
1138 objects, \code{timedelta(microseconds=1)}.
1139\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001140
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001141Instance attributes, all read-only:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001142
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001143\begin{memberdesc}{year}
1144Between MINYEAR and MAXYEAR inclusive
1145\end{memberdesc}
1146\begin{memberdesc}{month}
1147Between 1 and 12 inclusive
1148\end{memberdesc}
1149\begin{memberdesc}{day}
1150Between 1 and the number of days in the given month
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001151 of the given year
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001152\end{memberdesc}
1153\begin{memberdesc}{hour}
1154In \code{range(24)}.
1155\end{memberdesc}
1156\begin{memberdesc}{minute}
1157In \code{range(60)}.
1158\end{memberdesc}
1159\begin{memberdesc}{second}
1160In \code{range(60)}.
1161\end{memberdesc}
1162\begin{memberdesc}{microsecond}
1163In \code{range(1000000)}.
1164\end{memberdesc}
1165\begin{memberdesc}{tzinfo}
1166The object passed as the \var{tzinfo} argument to
Fred Drakebbdb2502002-12-23 18:58:06 +00001167 the \class{datetimetz} constructor, or \code{None}
1168 if none was passed.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001169\end{memberdesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001170
1171Supported operations:
1172
Fred Drakebbdb2502002-12-23 18:58:06 +00001173\begin{itemize}
1174 \item
1175 datetimetz1 + timedelta -> datetimetz2
1176 timedelta + datetimetz1 -> 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 datetimetz1 - timedelta -> datetimetz2
Tim Peters60c76e42002-12-27 00:41:11 +00001183
Fred Drakebbdb2502002-12-23 18:58:06 +00001184 The same as addition of \class{datetime} objects, except that
1185 datetimetz2.tzinfo is set to datetimetz1.tzinfo.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001186
Fred Drakebbdb2502002-12-23 18:58:06 +00001187 \item
1188 aware_datetimetz1 - aware_datetimetz2 -> timedelta
Fred Drakea37e5cc2002-12-30 21:26:42 +00001189 naive_datetimetz1 - naive_datetimetz2 -> timedelta
1190 naive_datetimetz1 - datetime2 -> timedelta
1191 datetime1 - naive_datetimetz2 -> timedelta
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001192
Tim Peters60c76e42002-12-27 00:41:11 +00001193 Subtraction of a \class{datetime} or \class{datetimetz}, from a
Fred Drakebbdb2502002-12-23 18:58:06 +00001194 \class{datetime} or \class{datetimetz}, is defined only if both
Fred Drakea37e5cc2002-12-30 21:26:42 +00001195 operands are naive, or if both are aware. If one is aware and the
1196 other is naive, \exception{TypeError} is raised.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001197
Fred Drakea37e5cc2002-12-30 21:26:42 +00001198 If both are naive, or both are aware and have the same \member{tzinfo}
Tim Peters60c76e42002-12-27 00:41:11 +00001199 member, subtraction acts as for \class{datetime} subtraction.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001200
Tim Peters60c76e42002-12-27 00:41:11 +00001201 If both are aware and have different \member{tzinfo} members,
1202 \code{a-b} acts as if \var{a} and \var{b} were first converted to UTC
1203 datetimes (by subtracting \code{a.utcoffset()} minutes from \var{a},
1204 and \code{b.utcoffset()} minutes from \var{b}), and then doing
Fred Drakebbdb2502002-12-23 18:58:06 +00001205 \class{datetime} subtraction, except that the implementation never
1206 overflows.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001207
Fred Drakebbdb2502002-12-23 18:58:06 +00001208 \item
Tim Peters60c76e42002-12-27 00:41:11 +00001209 comparison of \class{datetimetz} to \class{datetime} or
1210 \class{datetimetz}, where \var{a} is considered less than \var{b}
1211 when \var{a} precedes \var{b} in time. If one comparand is naive and
1212 the other is aware, \exception{TypeError} is raised. If both
1213 comparands are aware, and have the same \member{tzinfo} member,
1214 the common \member{tzinfo} member is ignored and the base datetimes
1215 are compared. If both comparands are aware and have different
1216 \member{tzinfo} members, the comparands are first adjusted by
1217 subtracting their UTC offsets (obtained from \code{self.utcoffset()}).
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001218
Fred Drakebbdb2502002-12-23 18:58:06 +00001219 \item
1220 hash, use as dict key
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001221
Fred Drakebbdb2502002-12-23 18:58:06 +00001222 \item
1223 efficient pickling
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001224
Fred Drakebbdb2502002-12-23 18:58:06 +00001225 \item
1226 in Boolean contexts, all \class{datetimetz} objects are considered to be
1227 true
1228\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001229
1230Instance methods:
1231
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001232\begin{methoddesc}{date}{}
1233 \methodline{time}{}
1234 \methodline{toordinal}{}
1235 \methodline{weekday}{}
1236 \methodline{isoweekday}{}
1237 \methodline{isocalendar}{}
1238 \methodline{ctime}{}
1239 \methodline{__str__}{}
1240 \methodline{strftime}{format}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001241
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001242These are the same as the \class{datetime} methods of the same names.
1243\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001244
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001245\begin{methoddesc}{timetz}{}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001246 Return \class{timetz} object with same hour, minute, second, microsecond,
1247 and tzinfo.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001248\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001249
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001250\begin{methoddesc}{replace}{year=, month=, day=, hour=, minute=, second=, microsecond=,
1251 tzinfo=}
Tim Peters12bf3392002-12-24 05:41:27 +00001252 Return a datetimetz with the same value, except for those fields given
1253 new values by whichever keyword arguments are specified. Note that
1254 \code{tzinfo=None} can be specified to create a naive datetimetz from
1255 an aware datetimetz.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001256\end{methoddesc}
Tim Peters12bf3392002-12-24 05:41:27 +00001257
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001258\begin{methoddesc}{astimezone}{tz}
Tim Peters80475bb2002-12-25 07:40:55 +00001259 Return a \class{datetimetz} with new tzinfo member \var{tz}. \var{tz}
Tim Peters276a8f32002-12-27 21:41:32 +00001260 must be \code{None}, or an instance of a \class{tzinfo} subclass. If
1261 \var{tz} is \code{None}, self is naive, or
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001262 \code{tz.utcoffset(self)} returns \code{None},
Tim Peters80475bb2002-12-25 07:40:55 +00001263 \code{self.astimezone(tz)} is equivalent to
1264 \code{self.replace(tzinfo=tz)}: a new timezone object is attached
1265 without any conversion of date or time fields. If self is aware and
1266 \code{tz.utcoffset(self)} does not return \code{None}, the date and
1267 time fields are adjusted so that the result is local time in timezone
1268 tz, representing the same UTC time as self. \code{self.astimezone(tz)}
1269 is then equivalent to
1270 \begin{verbatim}
1271 (self - (self.utcoffset() - tz.utcoffset(self)).replace(tzinfo=tz)
1272 \end{verbatim}
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001273 where the result of \code{tz.utcoffset(self)} is converted to a
Tim Peters80475bb2002-12-25 07:40:55 +00001274 \class{timedelta} if it's an integer.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001275\end{methoddesc}
Tim Peters80475bb2002-12-25 07:40:55 +00001276
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001277\begin{methoddesc}{utcoffset}{}
Fred Drakebbdb2502002-12-23 18:58:06 +00001278 If \member{tzinfo} is \code{None}, returns \code{None}, else
Tim Peters1cff9fc2002-12-24 16:25:29 +00001279 \code{tzinfo.utcoffset(self)} converted to a \class{timedelta}
1280 object.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001281\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001282
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001283\begin{methoddesc}{tzname}{}
Fred Drakebbdb2502002-12-23 18:58:06 +00001284 If \member{tzinfo} is \code{None}, returns \code{None}, else
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001285 returns \code{tzinfo.tzname(self)}.
1286\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001287
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001288\begin{methoddesc}{dst}{}
Fred Drakebbdb2502002-12-23 18:58:06 +00001289 If \member{tzinfo} is \code{None}, returns \code{None}, else
Tim Peters1cff9fc2002-12-24 16:25:29 +00001290 \code{tzinfo.dst(self)} converted to a \class{timedelta}
1291 object.
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}{timetuple}{}
Fred Drakebbdb2502002-12-23 18:58:06 +00001295 Like \function{datetime.timetuple()}, but sets the
1296 \member{tm_isdst} flag according to the \method{dst()} method: if
1297 \method{dst()} returns \code{None}, \member{tm_isdst} is set to
1298 \code{-1}; else if \method{dst()} returns a non-zero value,
1299 \member{tm_isdst} is set to \code{1}; else \code{tm_isdst} is set
1300 to \code{0}.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001301\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001302
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001303\begin{methoddesc}{utctimetuple}{}
Fred Drakea37e5cc2002-12-30 21:26:42 +00001304 If \class{datetimetz} instance \var{d} is naive, this is the same as
Fred Drakebbdb2502002-12-23 18:58:06 +00001305 \code{\var{d}.timetuple()} except that \member{tm_isdst} is forced to 0
1306 regardless of what \code{d.dst()} returns. DST is never in effect
1307 for a UTC time.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001308
Fred Drakebbdb2502002-12-23 18:58:06 +00001309 If \var{d} is aware, \var{d} is normalized to UTC time, by subtracting
1310 \code{\var{d}.utcoffset()} minutes, and a timetuple for the
1311 normalized time is returned. \member{tm_isdst} is forced to 0.
1312 Note that the result's \member{tm_year} field may be
1313 \constant{MINYEAR}-1 or \constant{MAXYEAR}+1, if \var{d}.year was
1314 \code{MINYEAR} or \code{MAXYEAR} and UTC adjustment spills over a
1315 year boundary.
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001316\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001317
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001318\begin{methoddesc}{isoformat}{sep='T'}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001319 Return a string representing the date and time in ISO 8601 format,
1320 YYYY-MM-DDTHH:MM:SS.mmmmmm
Fred Drakebbdb2502002-12-23 18:58:06 +00001321 or, if \member{microsecond} is 0,
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001322 YYYY-MM-DDTHH:MM:SS
1323
Fred Drakebbdb2502002-12-23 18:58:06 +00001324 If \method{utcoffset()} does not return \code{None}, a 6-character
1325 string is appended, giving the UTC offset in (signed) hours and
1326 minutes:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001327 YYYY-MM-DDTHH:MM:SS.mmmmmm+HH:MM
Fred Drakebbdb2502002-12-23 18:58:06 +00001328 or, if \member{microsecond} is 0
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001329 YYYY-MM-DDTHH:MM:SS+HH:MM
1330
Fred Drakebbdb2502002-12-23 18:58:06 +00001331 The optional argument \var{sep} (default \code{'T'}) is a
1332 one-character separator, placed between the date and time portions
1333 of the result. For example,
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001334
Fred Drakebbdb2502002-12-23 18:58:06 +00001335\begin{verbatim}
1336>>> from datetime import *
1337>>> class TZ(tzinfo):
1338... def utcoffset(self, dt): return -399
1339...
1340>>> datetimetz(2002, 12, 25, tzinfo=TZ()).isoformat(' ')
1341'2002-12-25 00:00:00-06:39'
1342\end{verbatim}
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001343\end{methoddesc}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001344
Fred Drakebbdb2502002-12-23 18:58:06 +00001345\code{str(\var{d})} is equivalent to \code{\var{d}.isoformat(' ')}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001346
1347
Tim Peters29fb9c72002-12-23 22:21:52 +00001348\subsection{\method{strftime()} Behavior}
1349
1350\class{date}, \class{datetime}, \class{datetimetz}, \class{time},
1351and \class{timetz} objects all support a \code{strftime(\var{format})}
1352method, to create a string representing the time under the control of
1353an explicit format string. Broadly speaking,
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001354\code{d.strftime(fmt)}
Tim Peters29fb9c72002-12-23 22:21:52 +00001355acts like the \refmodule{time} module's
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001356\code{time.strftime(fmt, d.timetuple())}
Tim Peters29fb9c72002-12-23 22:21:52 +00001357although not all objects support a \method{timetuple()} method.
1358
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001359For \class{time} and \class{timetz} objects, the format codes for
1360year, month, and day should not be used, as time objects have no such
1361values. If they're used anyway, \code{1900} is substituted for the
1362year, and \code{0} for the month and day.
Tim Peters29fb9c72002-12-23 22:21:52 +00001363
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001364For \class{date} objects, the format codes for hours, minutes, and
1365seconds should not be used, as \class{date} objects have no such
1366values. If they're used anyway, \code{0} is substituted for them.
Tim Peters29fb9c72002-12-23 22:21:52 +00001367
Fred Drakea37e5cc2002-12-30 21:26:42 +00001368For a naive object, the \code{\%z} and \code{\%Z} format codes are
Tim Peters29fb9c72002-12-23 22:21:52 +00001369replaced by empty strings.
1370
1371For an aware object:
1372
1373\begin{itemize}
1374 \item[\code{\%z}]
1375 \method{utcoffset()} is transformed into a 5-character string of
1376 the form +HHMM or -HHMM, where HH is a 2-digit string giving the
1377 number of UTC offset hours, and MM is a 2-digit string giving the
1378 number of UTC offset minutes. For example, if
Andrew M. Kuchlingc97868e2002-12-30 03:06:45 +00001379 \method{utcoffset()} returns \code{timedelta(hours=-3, minutes=-30)},
Tim Peters1cff9fc2002-12-24 16:25:29 +00001380 \code{\%z} is replaced with the string \code{'-0330'}.
Tim Peters29fb9c72002-12-23 22:21:52 +00001381
1382 \item[\code{\%Z}]
1383 If \method{tzname()} returns \code{None}, \code{\%Z} is replaced
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001384 by an empty string. Otherwise \code{\%Z} is replaced by the returned
Tim Peters29fb9c72002-12-23 22:21:52 +00001385 value, which must be a string.
1386\end{itemize}
1387
1388The full set of format codes supported varies across platforms,
1389because Python calls the platform C library's \function{strftime()}
1390function, and platform variations are common. The documentation for
1391Python's \refmodule{time} module lists the format codes that the C
1392standard (1989 version) requires, and those work on all platforms
1393with a standard C implementation. Note that the 1999 version of the
1394C standard added additional format codes.
1395
1396The exact range of years for which \method{strftime()} works also
1397varies across platforms. Regardless of platform, years before 1900
1398cannot be used.
1399
1400
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001401\begin{comment}
1402
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001403\subsection{C API}
1404
1405Struct typedefs:
1406
1407 PyDateTime_Date
1408 PyDateTime_DateTime
1409 PyDateTime_DateTimeTZ
1410 PyDateTime_Time
1411 PyDateTime_TimeTZ
1412 PyDateTime_Delta
1413 PyDateTime_TZInfo
1414
1415Type-check macros:
1416
1417 PyDate_Check(op)
1418 PyDate_CheckExact(op)
1419
1420 PyDateTime_Check(op)
1421 PyDateTime_CheckExact(op)
1422
1423 PyDateTimeTZ_Check(op)
1424 PyDateTimeTZ_CheckExact(op)
1425
1426 PyTime_Check(op)
1427 PyTime_CheckExact(op)
1428
1429 PyTimeTZ_Check(op)
1430 PyTimeTZ_CheckExact(op)
1431
1432 PyDelta_Check(op)
1433 PyDelta_CheckExact(op)
1434
1435 PyTZInfo_Check(op)
1436 PyTZInfo_CheckExact(op
1437
1438Accessor macros:
1439
1440All objects are immutable, so accessors are read-only. All macros
1441return ints:
1442
Tim Peters1cff9fc2002-12-24 16:25:29 +00001443 For \class{date}, \class{datetime}, and \class{datetimetz} instances:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001444 PyDateTime_GET_YEAR(o)
1445 PyDateTime_GET_MONTH(o)
1446 PyDateTime_GET_DAY(o)
1447
1448 For \class{datetime} and \class{datetimetz} instances:
1449 PyDateTime_DATE_GET_HOUR(o)
1450 PyDateTime_DATE_GET_MINUTE(o)
1451 PyDateTime_DATE_GET_SECOND(o)
1452 PyDateTime_DATE_GET_MICROSECOND(o)
1453
Tim Peters1cff9fc2002-12-24 16:25:29 +00001454 For \class{time} and \class{timetz} instances:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001455 PyDateTime_TIME_GET_HOUR(o)
1456 PyDateTime_TIME_GET_MINUTE(o)
1457 PyDateTime_TIME_GET_SECOND(o)
1458 PyDateTime_TIME_GET_MICROSECOND(o)
Andrew M. Kuchlingfa918582002-12-30 14:20:16 +00001459
1460\end{comment}