blob: b061d90853947708732e6a90edb71e29fa103cfd [file] [log] [blame]
Fred Drakebbdb2502002-12-23 18:58:06 +00001\section{\module{datetime} ---
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00002 Basic date and time types}
3
4\declaremodule{builtin}{datetime}
5\modulesynopsis{Basic date and time types.}
Fred Drakebbdb2502002-12-23 18:58:06 +00006\moduleauthor{Tim Peters}{tim@zope.com}
7\sectionauthor{Tim Peters}{tim@zope.com}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00008\sectionauthor{A.M. Kuchling}{amk@amk.ca}
9
Fred Drakebbdb2502002-12-23 18:58:06 +000010\newcommand{\Naive}{Na\"ive}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000011\newcommand{\naive}{na\"ive}
12
13The \module{datetime} module supplies classes for manipulating dates
14and times in both simple and complex ways. While date and time
15arithmetic is supported, the focus of the implementation is on
16efficient field extraction, for output formatting and manipulation.
17
18There are two kinds of date and time objects: ``\naive'' and ``aware''.
19This distinction refers to whether the object has any notion of time
20zone, daylight savings time, or other kind of algorithmic or political
21time adjustment. Whether a \naive\ \class{datetime} object represents
22Coordinated Universal Time (UTC), local time, or time in some other
23timezone is purely up to the program, just like it's up to the program
24whether a particular number represents meters, miles, or mass. \Naive\
25\class{datetime} objects are easy to understand and to work with, at
26the cost of ignoring some aspects of reality.
27
28For applications requiring more, ``aware'' \class{datetime} subclasses add an
29optional time zone information object to the basic \naive\ classes.
30These \class{tzinfo} objects capture information about the offset from
31UTC time, the time zone name, and whether Daylight Savings Time is in
32effect. Note that no concrete \class{tzinfo} classes are supplied by
33the \module{datetime} module. Instead, they provide a framework for
34incorporating the level of detail an app may require. The rules for
35time adjustment across the world are more political than rational, and
36there is no standard suitable for every app.
37
38The \module{datetime} module exports the following constants:
39
40\begin{datadesc}{MINYEAR}
Fred Drakebbdb2502002-12-23 18:58:06 +000041 The smallest year number allowed in a \class{date},
42 \class{datetime}, or \class{datetimetz} object. \constant{MINYEAR}
43 is \code{1}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000044\end{datadesc}
45
46\begin{datadesc}{MAXYEAR}
Fred Drakebbdb2502002-12-23 18:58:06 +000047 The largest year number allowed in a \class{date}, \class{datetime},
48 or \class{datetimetz} object. \constant{MAXYEAR} is \code{9999}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000049\end{datadesc}
50
51
52\subsection{Available Types}
53
Fred Drakebbdb2502002-12-23 18:58:06 +000054\begin{classdesc*}{date}
55 An idealized \naive\ date, assuming the current Gregorian calendar
56 always was, and always will be, in effect.
57 Attributes: \member{year}, \member{month}, and \member{day}.
58\end{classdesc*}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000059
Fred Drakebbdb2502002-12-23 18:58:06 +000060\begin{classdesc*}{time}
61 An idealized \naive\ time, independent of any particular day, assuming
62 that every day has exactly 24*60*60 seconds (there is no notion
63 of "leap seconds" here).
64 Attributes: \member{hour}, \member{minute}, \member{second}, and
65 \member{microsecond}
66\end{classdesc*}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000067
Fred Drakebbdb2502002-12-23 18:58:06 +000068\begin{classdesc*}{datetime}
69 A combination of a \naive\ date and a \naive\ time.
70 Attributes: \member{year}, \member{month}, \member{day},
71 \member{hour}, \member{minute}, \member{second},
72 and \member{microsecond}.
73\end{classdesc*}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000074
Fred Drakebbdb2502002-12-23 18:58:06 +000075\begin{classdesc*}{timedelta}
76 A duration, expressing the difference between two \class{date},
77 \class{time}, or \class{datetime} instances, to microsecond
78 resolution.
79\end{classdesc*}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000080
Fred Drakebbdb2502002-12-23 18:58:06 +000081\begin{classdesc*}{tzinfo}
82 An abstract base class for time zone information objects. These
83 are used by the \class{datetimetz} and \class{timetz} classes to
84 provided a customizable notion of time adjustment (for example, to
85 account for time zone and/or daylight savings time).
86\end{classdesc*}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000087
Fred Drakebbdb2502002-12-23 18:58:06 +000088\begin{classdesc*}{timetz}
89 An aware subclass of \class{time}, supporting a customizable notion of
90 time adjustment.
91\end{classdesc*}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000092
Fred Drakebbdb2502002-12-23 18:58:06 +000093\begin{classdesc*}{datetimetz}
94 An aware subclass of \class{datetime}, supporting a customizable notion of
95 time adjustment.
96\end{classdesc*}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +000097
98Objects of these types are immutable.
99
Fred Drakebbdb2502002-12-23 18:58:06 +0000100Objects of the \class{date}, \class{datetime}, and \class{time} types
101are always \naive.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000102
Fred Drakebbdb2502002-12-23 18:58:06 +0000103An object \code{D} of type \class{timetz} or \class{datetimetz} may be
104\naive\ or aware. \code{D} is aware if \code{D.tzinfo} is not
105\code{None}, and \code{D.tzinfo.utcoffset(D)} does not return
106\code{None}. If \code{D.tzinfo} is \code{None}, or if \code{D.tzinfo}
107is not \code{None} but \code{D.tzinfo.utcoffset(D)} returns
108\code{None}, \code{D} is \naive.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000109
Fred Drakebbdb2502002-12-23 18:58:06 +0000110The distinction between \naive\ and aware doesn't apply to
111\code{timedelta} objects.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000112
Fred Drakebbdb2502002-12-23 18:58:06 +0000113Subclass relationships:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000114
Fred Drakebbdb2502002-12-23 18:58:06 +0000115\begin{verbatim}
116object
117 timedelta
118 tzinfo
119 time
120 timetz
121 date
122 datetime
123 datetimetz
124\end{verbatim}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000125
Fred Drakebbdb2502002-12-23 18:58:06 +0000126\subsection{\class{timedelta} \label{datetime-timedelta}}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000127
Fred Drakebbdb2502002-12-23 18:58:06 +0000128A \class{timedelta} object represents a duration, the difference
129between two dates or times.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000130
131Constructor:
132
133 timedelta(days=0, seconds=0, microseconds=0,
Fred Drakebbdb2502002-12-23 18:58:06 +0000134 \# The following should only be used as keyword args:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000135 milliseconds=0, minutes=0, hours=0, weeks=0)
136
137 All arguments are optional. Arguments may be ints, longs, or floats,
138 and may be positive or negative.
139
140 Only days, seconds and microseconds are stored internally. Arguments
141 are converted to those units:
142
143 A millisecond is converted 1000 microseconds.
144 A minute is converted to 60 seconds.
145 An hour is converted to 3600 seconds.
146 A week is converted to 7 days.
147
148 and days, seconds and microseconds are then normalized so that the
149 representation is unique, with
150
151 0 <= microseconds < 1000000
152 0 <= seconds < 3600*24 (the number of seconds in one day)
153 -999999999 <= days <= 999999999
154
155 If any argument is a float, and there are fractional microseconds,
156 the fractional microseconds left over from all arguments are combined
157 and their sum is rounded to the nearest microsecond. If no
158 argument is a flost, the conversion and normalization processes
159 are exact (no information is lost).
160
161 If the normalized value of days lies outside the indicated range,
Fred Drakebbdb2502002-12-23 18:58:06 +0000162 \exception{OverflowError} is raised.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000163
164 Note that normalization of negative values may be surprising at first.
165 For example,
166
Fred Drakebbdb2502002-12-23 18:58:06 +0000167\begin{verbatim}
168>>> d = timedelta(microseconds=-1)
169>>> (d.days, d.seconds, d.microseconds)
170(-1, 86399, 999999)
171\end{verbatim}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000172
173
174Class attributes:
175
176 .min
177 The most negative timedelta object, timedelta(-999999999).
178
179 .max
180 The most positive timedelta object,
181 timedelta(days=999999999, hours=23, minutes=59, seconds=59,
182 microseconds=999999)
183
184 .resolution
185 The smallest possible difference between non-equal timedelta
Fred Drakebbdb2502002-12-23 18:58:06 +0000186 objects, \code{timedelta(microseconds=1)}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000187
188 Note that, because of normalization, timedelta.max > -timedelta.min.
189 -timedelta.max is not representable as a timedelta object.
190
191Instance attributes (read-only):
192
193 .days between -999999999 and 999999999 inclusive
194 .seconds between 0 and 86399 inclusive
195 .microseconds between 0 and 999999 inclusive
196
197Supported operations:
198
Fred Drakebbdb2502002-12-23 18:58:06 +0000199\begin{itemize}
200 \item
201 timedelta + timedelta -> timedelta
202 This is exact, but may overflow. After
203 t1 = t2 + t3
204 t1-t2 == t3 and t1-t3 == t2 are true.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000205
Fred Drakebbdb2502002-12-23 18:58:06 +0000206 \item
207 timedelta - timedelta -> timedelta
208 This is exact, but may overflow. After
209 t1 = t2 - t3
210 t2 == t1 + t3 is true.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000211
Fred Drakebbdb2502002-12-23 18:58:06 +0000212 \item
213 timedelta * (int or long) -> timedelta
214 (int or long) * timedelta -> timedelta
215 This is exact, but may overflow. After
216 t1 = t2 * i
217 t1 // i == t2 is true, provided i != 0. In general,
218 t * i == t * (i-1) + t
219 is true.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000220
Fred Drakebbdb2502002-12-23 18:58:06 +0000221 \item
222 timedelta // (int or long) -> timedelta
223 The floor is computed and the remainder (if any) is thrown away.
224 Division by 0 raises \exception{ZeroDivisionError}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000225
Fred Drakebbdb2502002-12-23 18:58:06 +0000226 \item
227 certain additions and subtractions with date, datetime, and datimetz
228 objects (see below)
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000229
Fred Drakebbdb2502002-12-23 18:58:06 +0000230 \item
231 +timedelta -> timedelta
232 Returns a timedelta object with the same value.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000233
Fred Drakebbdb2502002-12-23 18:58:06 +0000234 \item
235 -timedelta -> timedelta
236 -t is equivalent to timedelta(-t.days, -t.seconds, -t.microseconds),
237 and to t*-1. This is exact, but may overflow (for example,
238 -timedelta.max is not representable as a timedelta object).
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000239
Fred Drakebbdb2502002-12-23 18:58:06 +0000240 \item
241 abs(timedelta) -> timedelta
242 abs(t) is equivalent to +t when t.days >= 0, and to -t when
243 t.days < 0. This is exact, and cannot overflow.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000244
Fred Drakebbdb2502002-12-23 18:58:06 +0000245 \item
246 comparison of timedelta to timedelta; the timedelta representing
247 the smaller duration is considered to be the smaller timedelta
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000248
Fred Drakebbdb2502002-12-23 18:58:06 +0000249 \item
250 hash, use as dict key
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000251
Fred Drakebbdb2502002-12-23 18:58:06 +0000252 \item
253 efficient pickling
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000254
Fred Drakebbdb2502002-12-23 18:58:06 +0000255 \item
256 in Boolean contexts, a timedelta object is considred to be true
257 if and only if it isn't equal to \code{timedelta(0)}
258\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000259
260
261\subsection{\class{date} \label{datetime-date}}
262
263A date object represents a date (year, month and day) in an idealized
264calendar, the current Gregorian calendar indefinitely extended in both
265directions. January 1 of year 1 is called day number 1, January 2 of year
2661 is called day number 2, and so on. This matches the definition of the
267"proleptic Gregorian" calendar in Dershowitz and Reingold's book
268"Calendrical Calculations", where it's the base calendar for all
269computations. See the book for algorithms for converting between
270proleptic Gregorian ordinals and many other calendar systems.
271
272Constructor:
273
274 date(year, month, day)
275
276 All arguments are required. Arguments may be ints or longs, in the
277 following ranges:
278
279 MINYEAR <= year <= MAXYEAR
280 1 <= month <= 12
281 1 <= day <= number of days in the given month and year
282
Fred Drakebbdb2502002-12-23 18:58:06 +0000283 If an argument outside those ranges is given,
284 \exception{ValueError} is raised.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000285
286Other constructors (class methods):
287
288 - today()
289 Return the current local date. This is equivalent to
290 date.fromtimestamp(time.time()).
291
292 - fromtimestamp(timestamp)
Fred Drakebbdb2502002-12-23 18:58:06 +0000293 Return the local date corresponding to the POSIX timestamp, such
294 as is returned by \function{time.time()}. This may raise
295 \exception{ValueError}, if the timestamp is out of the range of
296 values supported by the platform C \cfunction{localtime()}
297 function. It's common for this to be restricted to years in 1970
298 through 2038.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000299
300 - fromordinal(ordinal)
301 Return the date corresponding to the proleptic Gregorian ordinal,
Fred Drakebbdb2502002-12-23 18:58:06 +0000302 where January 1 of year 1 has ordinal 1. \exception{ValueError}
303 is raised unless 1 <= ordinal <= date.max.toordinal(). For any
304 date d, date.fromordinal(d.toordinal()) == d.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000305
306Class attributes:
307
308 .min
Fred Drakebbdb2502002-12-23 18:58:06 +0000309 The earliest representable date, \code{date(MINYEAR, 1, 1)}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000310
311 .max
Fred Drakebbdb2502002-12-23 18:58:06 +0000312 The latest representable date, \code{date(MAXYEAR, 12, 31)}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000313
314 .resolution
315 The smallest possible difference between non-equal date
Fred Drakebbdb2502002-12-23 18:58:06 +0000316 objects, \code{timedelta(days=1)}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000317
318Instance attributes (read-only):
319
Fred Drakebbdb2502002-12-23 18:58:06 +0000320 .year between \constant{MINYEAR} and \constant{MAXYEAR} inclusive
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000321 .month between 1 and 12 inclusive
322 .day between 1 and the number of days in the given month
323 of the given year
324
325Supported operations:
326
Fred Drakebbdb2502002-12-23 18:58:06 +0000327\begin{itemize}
328 \item
329 date1 + timedelta -> date2
330 timedelta + date1 -> date2
331 date2 is timedelta.days days removed from the date1, moving forward
332 in time if timedelta.days > 0, or backward if timedetla.days < 0.
333 date2 - date1 == timedelta.days after. timedelta.seconds and
334 timedelta.microseconds are ignored. \exception{OverflowError} is
335 raised if date2.year would be smaller than \constant{MINYEAR} or
336 larger than \constant{MAXYEAR}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000337
Fred Drakebbdb2502002-12-23 18:58:06 +0000338 \item
339 date1 - timedelta -> date2
340 Computes the date2 such that date2 + timedelta == date1. This
341 isn't quite equivalent to date1 + (-timedelta), because -timedelta
342 in isolation can overflow in cases where date1 - timedelta does
343 not. timedelta.seconds and timedelta.microseconds are ignored.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000344
Fred Drakebbdb2502002-12-23 18:58:06 +0000345 \item
346 date1 - date2 -> timedelta
347 This is exact, and cannot overflow. timedelta.seconds and
348 timedelta.microseconds are 0, and date2 + timedelta == date1
349 after.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000350
Fred Drakebbdb2502002-12-23 18:58:06 +0000351 \item
352 comparison of date to date, where date1 is considered less than
353 date2 when date1 precedes date2 in time. In other words,
354 date1 < date2 if and only if date1.toordinal() < date2.toordinal().
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000355
Fred Drakebbdb2502002-12-23 18:58:06 +0000356 \item
357 hash, use as dict key
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000358
Fred Drakebbdb2502002-12-23 18:58:06 +0000359 \item
360 efficient pickling
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000361
Fred Drakebbdb2502002-12-23 18:58:06 +0000362 \item
363 in Boolean contexts, all date objects are considered to be true
364\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000365
366Instance methods:
367
Tim Peters12bf3392002-12-24 05:41:27 +0000368 - replace(year=, month=, day=)
369 Return a date with the same value, except for those fields given
370 new values by whichever keyword arguments are specified. For
371 example, if \code{d == date(2002, 12, 31)}, then
372 \code{d.replace(day=26) == date(2000, 12, 26)}.
373
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000374 - timetuple()
Fred Drakebbdb2502002-12-23 18:58:06 +0000375 Return a 9-element tuple of the form returned by
376 \function{time.localtime()}. The hours, minutes and seconds are
377 0, and the DST flag is -1.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000378 d.timetuple() is equivalent to
379 (d.year, d.month, d.day,
Fred Drakebbdb2502002-12-23 18:58:06 +0000380 0, 0, 0, \# h, m, s
381 d.weekday(), \# 0 is Monday
382 d.toordinal() - date(d.year, 1, 1).toordinal() + 1, \# day of year
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000383 -1)
384
385 - toordinal()
386 Return the proleptic Gregorian ordinal of the date, where January 1
Fred Drakebbdb2502002-12-23 18:58:06 +0000387 of year 1 has ordinal 1. For any date object \var{d},
388 \code{date.fromordinal(\var{d}.toordinal()) == \var{d}}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000389
390 - weekday()
391 Return the day of the week as an integer, where Monday is 0 and
392 Sunday is 6. For example, date(2002, 12, 4).weekday() == 2, a
393 Wednesday.
Fred Drakebbdb2502002-12-23 18:58:06 +0000394 See also \method{isoweekday()}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000395
396 - isoweekday()
397 Return the day of the week as an integer, where Monday is 1 and
398 Sunday is 7. For example, date(2002, 12, 4).isoweekday() == 3, a
399 Wednesday.
Fred Drakebbdb2502002-12-23 18:58:06 +0000400 See also \method{weekday()}, \method{isocalendar()}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000401
402 - isocalendar()
403 Return a 3-tuple, (ISO year, ISO week number, ISO weekday).
404
405 The ISO calendar is a widely used variant of the Gregorian calendar.
Fred Drakebbdb2502002-12-23 18:58:06 +0000406 See \url{http://www.phys.uu.nl/~vgent/calendar/isocalendar.htm}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000407 for a good explanation.
408
409 The ISO year consists of 52 or 53 full weeks, and where a week starts
410 on a Monday and ends on a Sunday. The first week of an ISO year is
411 the first (Gregorian) calendar week of a year containing a Thursday.
412 This is called week number 1, and the ISO year of that Thursday is
413 the same as its Gregorian year.
414
415 For example, 2004 begins on a Thursday, so the first week of ISO
416 year 2004 begins on Monday, 29 Dec 2003 and ends on Sunday, 4 Jan
417 2004, so that
418
419 date(2003, 12, 29).isocalendar() == (2004, 1, 1)
420 date(2004, 1, 4).isocalendar() == (2004, 1, 7)
421
422 - isoformat()
423 Return a string representing the date in ISO 8601 format,
424 'YYYY-MM-DD'. For example,
425 date(2002, 12, 4).isoformat() == '2002-12-04'.
426
427 - __str__()
Fred Drakebbdb2502002-12-23 18:58:06 +0000428 For a date \var{d}, \code{str(\var{d})} is equivalent to
429 \code{\var{d}.isoformat()}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000430
431 - ctime()
432 Return a string representing the date, for example
433 date(2002, 12, 4).ctime() == 'Wed Dec 4 00:00:00 2002'.
434 d.ctime() is equivalent to time.ctime(time.mktime(d.timetuple()))
Fred Drakebbdb2502002-12-23 18:58:06 +0000435 on platforms where the native C \cfunction{ctime()} function
436 (which \function{time.ctime()} invokes, but which
437 \method{date.ctime()} does not invoke) conforms to the C standard.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000438
439 - strftime(format)
440 Return a string representing the date, controlled by an explicit
441 format string. Format codes referring to hours, minutes or seconds
Fred Drakebbdb2502002-12-23 18:58:06 +0000442 will see 0 values.
443 See the section on \method{strftime()} behavior.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000444
445
446\subsection{\class{datetime} \label{datetime-datetime}}
447
Fred Drakebbdb2502002-12-23 18:58:06 +0000448A \class{datetime} object is a single object containing all the
449information from a date object and a time object. Like a date object,
450\class{datetime} assumes the current Gregorian calendar extended in
451both directions; like a time object, \class{datetime} assumes there
452are exactly 3600*24 seconds in every day.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000453
454Constructor:
455
456 datetime(year, month, day,
457 hour=0, minute=0, second=0, microsecond=0)
458
459 The year, month and day arguments are required. Arguments may be ints
460 or longs, in the following ranges:
461
462 MINYEAR <= year <= MAXYEAR
463 1 <= month <= 12
464 1 <= day <= number of days in the given month and year
465 0 <= hour < 24
466 0 <= minute < 60
467 0 <= second < 60
468 0 <= microsecond < 1000000
469
Fred Drakebbdb2502002-12-23 18:58:06 +0000470 If an argument outside those ranges is given,
471 \exception{ValueError} is raised.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000472
473Other constructors (class methods):
474
475 - today()
476 Return the current local datetime. This is equivalent to
Fred Drakebbdb2502002-12-23 18:58:06 +0000477 \code{datetime.fromtimestamp(time.time())}.
478 See also \method{now()}, \method{fromtimestamp()}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000479
480 - now()
Fred Drakebbdb2502002-12-23 18:58:06 +0000481 Return the current local datetime. This is like \method{today()},
482 but, if possible, supplies more precision than can be gotten from
483 going through a \function{time.time()} timestamp (for example,
484 this may be possible on platforms that supply the C
485 \cfunction{gettimeofday()} function).
486 See also \method{today()}, \method{utcnow()}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000487
488 - utcnow()
Fred Drakebbdb2502002-12-23 18:58:06 +0000489 Return the current UTC datetime. This is like \method{now()}, but
490 returns the current UTC date and time.
491 See also \method{now()}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000492
493 - fromtimestamp(timestamp)
Fred Drakebbdb2502002-12-23 18:58:06 +0000494 Return the local \class{datetime} corresponding to the \POSIX{}
495 timestamp, such as is returned by \function{time.time()}. This
496 may raise \exception{ValueError}, if the timestamp is out of the
497 range of values supported by the platform C
498 \cfunction{localtime()} function. It's common for this to be
499 restricted to years in 1970 through 2038.
500 See also \method{utcfromtimestamp()}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000501
502 - utcfromtimestamp(timestamp)
Fred Drakebbdb2502002-12-23 18:58:06 +0000503 Return the UTC \class{datetime} corresponding to the \POSIX{}
504 timestamp. This may raise \exception{ValueError}, if the
505 timestamp is out of the range of values supported by the platform
506 C \cfunction{gmtime()} function. It's common for this to be
507 restricted to years in 1970 through 2038.
508 See also \method{fromtimestamp()}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000509
510 - fromordinal(ordinal)
Fred Drakebbdb2502002-12-23 18:58:06 +0000511 Return the \class{datetime} corresponding to the proleptic
512 Gregorian ordinal, where January 1 of year 1 has ordinal 1.
513 \exception{ValueError} is raised unless 1 <= ordinal <=
514 datetime.max.toordinal(). The hour, minute, second and
515 microsecond of the result are all 0.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000516
517 - combine(date, time)
Fred Drakebbdb2502002-12-23 18:58:06 +0000518 Return a new \class{datetime} object whose date components are
519 equal to the given date object's, and whose time components are
520 equal to the given time object's. For any \class{datetime} object
521 d, d == datetime.combine(d.date(), d.time()).
522 If date is a \class{datetime} or \class{datetimetz} object, its
523 time components are ignored. If date is \class{datetimetz}
524 object, its \member{tzinfo} component is also ignored. If time is
525 a \class{timetz} object, its \member{tzinfo} component is ignored.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000526
527Class attributes:
528
529 .min
530 The earliest representable datetime,
531 datetime(MINYEAR, 1, 1).
532
533 .max
534 The latest representable datetime,
535 datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999).
536
537 .resolution
538 The smallest possible difference between non-equal datetime
539 objects, timedelta(microseconds=1).
540
541Instance attributes (read-only):
542
Fred Drakebbdb2502002-12-23 18:58:06 +0000543 .year between \constant{MINYEAR} and \constant{MAXYEAR} inclusive
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000544 .month between 1 and 12 inclusive
545 .day between 1 and the number of days in the given month
546 of the given year
547 .hour in range(24)
548 .minute in range(60)
549 .second in range(60)
550 .microsecond in range(1000000)
551
552Supported operations:
553
Fred Drakebbdb2502002-12-23 18:58:06 +0000554\begin{itemize}
555 \item
556 datetime1 + timedelta -> datetime2
557 timedelta + datetime1 -> datetime2
558 datetime2 is a duration of timedelta removed from datetime1, moving
559 forward in time if timedelta.days > 0, or backward if
560 timedelta.days < 0. datetime2 - datetime1 == timedelta after.
561 \exception{OverflowError} is raised if datetime2.year would be
562 smaller than \constant{MINYEAR} or larger than \constant{MAXYEAR}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000563
Fred Drakebbdb2502002-12-23 18:58:06 +0000564 \item
565 datetime1 - timedelta -> datetime2
566 Computes the datetime2 such that datetime2 + timedelta == datetime1.
567 This isn't quite equivalent to datetime1 + (-timedelta), because
568 -timedelta in isolation can overflow in cases where
569 datetime1 - timedelta does not.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000570
Fred Drakebbdb2502002-12-23 18:58:06 +0000571 \item
572 datetime1 - datetime2 -> timedelta
573 This is exact, and cannot overflow.
574 datetime2 + timedelta == datetime1 after.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000575
Fred Drakebbdb2502002-12-23 18:58:06 +0000576 \item
577 comparison of \class{datetime} to datetime, where datetime1 is
578 considered less than datetime2 when datetime1 precedes datetime2
579 in time.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000580
Fred Drakebbdb2502002-12-23 18:58:06 +0000581 \item
582 hash, use as dict key
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000583
Fred Drakebbdb2502002-12-23 18:58:06 +0000584 \item
585 efficient pickling
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000586
Fred Drakebbdb2502002-12-23 18:58:06 +0000587 \item
588 in Boolean contexts, all \class{datetime} objects are considered
589 to be true
590\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000591
592Instance methods:
593
594 - date()
595 Return date object with same year, month and day.
596
597 - time()
598 Return time object with same hour, minute, second and microsecond.
599
Tim Peters12bf3392002-12-24 05:41:27 +0000600 - replace(year=, month=, day=, hour=, minute=, second=, microsecond=)
601 Return a datetime with the same value, except for those fields given
602 new values by whichever keyword arguments are specified.
603
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000604 - timetuple()
Fred Drakebbdb2502002-12-23 18:58:06 +0000605 Return a 9-element tuple of the form returned by
606 \function{time.localtime()}.
607 The DST flag is -1. \code{d.timetuple()} is equivalent to
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000608 (d.year, d.month, d.day,
609 d.hour, d.minute, d.second,
Fred Drakebbdb2502002-12-23 18:58:06 +0000610 d.weekday(), \# 0 is Monday
611 d.toordinal() - date(d.year, 1, 1).toordinal() + 1, \# day of year
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000612 -1)
613
614 - toordinal()
615 Return the proleptic Gregorian ordinal of the date. The same as
Fred Drakebbdb2502002-12-23 18:58:06 +0000616 \method{date.toordinal()}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000617
618 - weekday()
619 Return the day of the week as an integer, where Monday is 0 and
Fred Drakebbdb2502002-12-23 18:58:06 +0000620 Sunday is 6. The same as \method{date.weekday()}.
621 See also \method{isoweekday()}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000622
623 - isoweekday()
624 Return the day of the week as an integer, where Monday is 1 and
Fred Drakebbdb2502002-12-23 18:58:06 +0000625 Sunday is 7. The same as \method{date.isoweekday()}.
626 See also \method{weekday()}, \method{isocalendar()}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000627
628 - isocalendar()
629 Return a 3-tuple, (ISO year, ISO week number, ISO weekday). The
Fred Drakebbdb2502002-12-23 18:58:06 +0000630 same as \method{date.isocalendar()}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000631
632 - isoformat(sep='T')
633 Return a string representing the date and time in ISO 8601 format,
634 YYYY-MM-DDTHH:MM:SS.mmmmmm
635 or, if self.microsecond is 0,
636 YYYY-MM-DDTHH:MM:SS
Fred Drakebbdb2502002-12-23 18:58:06 +0000637 The optional argument \var{sep} (default \code{'T'}) is a
638 one-character separator, placed between the date and time portions
639 of the result. For example,
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000640 datetime(2002, 12, 4, 1, 2, 3, 4).isoformat(' ') ==
641 '2002-12-04 01:02:03.000004'
642
643 - __str__()
Fred Drakebbdb2502002-12-23 18:58:06 +0000644 For a \class{datetime} instance \var{d}, \code{str(\var{d})} is
645 equivalent to \code{\var{d}.isoformat(' ')}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000646
647 - ctime()
648 Return a string representing the date, for example
649 datetime(2002, 12, 4, 20, 30, 40).ctime() == 'Wed Dec 4 20:30:40 2002'.
Fred Drakebbdb2502002-12-23 18:58:06 +0000650 \code{d.ctime()} is equivalent to
651 \code{time.ctime(time.mktime(d.timetuple()))} on platforms where
652 the native C \cfunction{ctime()} function (which
653 \function{time.ctime()} invokes, but which
654 \method{datetime.ctime()} does not invoke) conforms to the C
655 standard.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000656
657 - strftime(format)
658 Return a string representing the date and time, controlled by an
Fred Drakebbdb2502002-12-23 18:58:06 +0000659 explicit format string. See the section on \method{strftime()}
660 behavior.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000661
662
663\subsection{\class{time} \label{datetime-time}}
664
665A time object represents an idealized time of day, independent of day
666and timezone.
667
668Constructor:
669
670 time(hour=0, minute=0, second=0, microsecond=0)
671
672 All arguments are optional. They may be ints or longs, in the
673 following ranges:
674
675 0 <= hour < 24
676 0 <= minute < 60
677 0 <= second < 60
678 0 <= microsecond < 1000000
679
Fred Drakebbdb2502002-12-23 18:58:06 +0000680 If an argument outside those ranges is given,
681 \exception{ValueError} is raised.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000682
683Class attributes:
684
685 .min
686 The earliest representable time, time(0, 0, 0, 0).
687
688 .max
689 The latest representable time, time(23, 59, 59, 999999).
690
691 .resolution
692 The smallest possible difference between non-equal time
693 objects, timedelta(microseconds=1), although note that
694 arithmetic on time objects is not supported.
695
696Instance attributes (read-only):
697
698 .hour in range(24)
699 .minute in range(60)
700 .second in range(60)
701 .microsecond in range(1000000)
702
703Supported operations:
704
Fred Drakebbdb2502002-12-23 18:58:06 +0000705\begin{itemize}
706 \item
707 comparison of time to time, where time1 is considered
708 less than time2 when time1 precedes time2 in time.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000709
Fred Drakebbdb2502002-12-23 18:58:06 +0000710 \item
711 hash, use as dict key
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000712
Fred Drakebbdb2502002-12-23 18:58:06 +0000713 \item
714 efficient pickling
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000715
Fred Drakebbdb2502002-12-23 18:58:06 +0000716 \item
717 in Boolean contexts, a time object is considered to be true
718 if and only if it isn't equal to time(0)
719\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000720
721Instance methods:
722
Tim Peters12bf3392002-12-24 05:41:27 +0000723 - replace(hour=, minute=, second=, microsecond=)
724 Return a time with the same value, except for those fields given
725 new values by whichever keyword arguments are specified.
726
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000727 - isoformat()
728 Return a string representing the time in ISO 8601 format,
729 HH:MM:SS.mmmmmm
730 or, if self.microsecond is 0
731 HH:MM:SS
732
733 - __str__()
Fred Drakebbdb2502002-12-23 18:58:06 +0000734 For a time \var{t}, \code{str(\var{t})} is equivalent to
735 \code{\var{t}.isoformat()}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000736
737 - strftime(format)
738 Return a string representing the time, controlled by an explicit
Fred Drakebbdb2502002-12-23 18:58:06 +0000739 format string. See the section on \method{strftime()} behavior.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000740
741
742\subsection{\class{tzinfo} \label{datetime-tzinfo}}
743
Fred Drakebbdb2502002-12-23 18:58:06 +0000744\class{tzinfo} is an abstract base clase, meaning that this class
745should not be instantiated directly. You need to derive a concrete
746subclass, and (at least) supply implementations of the standard
747\class{tzinfo} methods needed by the \class{datetime} methods you
748use. The \module{datetime} module does not supply any concrete
749subclasses of \class{tzinfo}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000750
Fred Drakebbdb2502002-12-23 18:58:06 +0000751An instance of (a concrete subclass of) \class{tzinfo} can be passed
752to the constructors for \class{datetimetz} and \class{timetz} objects.
753The latter objects view their fields as being in local time, and the
754\class{tzinfo} object supports methods revealing offset of local time
755from UTC, the name of the time zone, and DST offset, all relative to a
756date or time object passed to them.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000757
Tim Peters2483b612002-12-24 16:30:58 +0000758Special requirement for pickling: A tzinfo subclass must have an
759\method{__init__} method that can be called with no arguments, else it
760can be pickled but possibly not unpickled again. This is a technical
761requirement that may be relaxed in the future.
762
Fred Drakebbdb2502002-12-23 18:58:06 +0000763A concrete subclass of \class{tzinfo} may need to implement the
764following methods. Exactly which methods are needed depends on the
Tim Peters2483b612002-12-24 16:30:58 +0000765uses made of aware \module{datetime} objects; if in doubt, simply
Fred Drakebbdb2502002-12-23 18:58:06 +0000766implement all of them. The methods are called by a \class{datetimetz}
767or \class{timetz} object, passing itself as the argument. A
768\class{tzinfo} subclass's methods should be prepared to accept a dt
769argument of \code{None} or of type \class{timetz} or
770\class{datetimetz}. If is not \code{None}, and dt.tzinfo is not
771\code{None} and not equal to self, an exception should be raised.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000772
773 - utcoffset(dt)
774 Return offset of local time from UTC, in minutes east of UTC. If
775 local time is west of UTC, this should be negative. Note that this
776 is intended to be the total offset from UTC; for example, if a
777 \class{tzinfo} object represents both time zone and DST adjustments,
Fred Drakebbdb2502002-12-23 18:58:06 +0000778 \method{utcoffset()} should return their sum. If the UTC offset
779 isn't known, return \code{None}. Else the value returned must be
780 an integer, in the range -1439 to 1439 inclusive (1440 = 24*60;
Tim Peters1cff9fc2002-12-24 16:25:29 +0000781 the magnitude of the offset must be less than one day), or a
782 \class{timedelta} object representing a whole number of minutes
783 in the same range.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000784
785 - tzname(dt)
786 Return the timezone name corresponding to the \class{datetime} represented
787 by dt, as a string. Nothing about string names is defined by the
788 \module{datetime} module, and there's no requirement that it mean anything
789 in particular. For example, "GMT", "UTC", "-500", "-5:00", "EDT",
790 "US/Eastern", "America/New York" are all valid replies. Return
Fred Drakebbdb2502002-12-23 18:58:06 +0000791 \code{None} if a string name isn't known. Note that this is a method
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000792 rather than a fixed string primarily because some \class{tzinfo} objects
793 will wish to return different names depending on the specific value
794 of dt passed, especially if the \class{tzinfo} class is accounting for DST.
795
796 - dst(dt)
Fred Drakebbdb2502002-12-23 18:58:06 +0000797 Return the DST offset, in minutes east of UTC, or \code{None} if
798 DST information isn't known. Return 0 if DST is not in effect.
Tim Peters1cff9fc2002-12-24 16:25:29 +0000799 If DST is in effect, return the offset as an integer or
800 \class{timedelta} object (see \method{utcoffset()} for details).
801 Note that DST offset, if applicable, has
Fred Drakebbdb2502002-12-23 18:58:06 +0000802 already been added to the UTC offset returned by
803 \method{utcoffset()}, so there's no need to consult \method{dst()}
804 unless you're interested in displaying DST info separately. For
Tim Peters1cff9fc2002-12-24 16:25:29 +0000805 example, \method{datetimetz.timetuple()} calls its \member{tzinfo}
806 member's \method{dst()} method to determine how the
Fred Drakebbdb2502002-12-23 18:58:06 +0000807 \member{tm_isdst} flag should be set.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000808
809Example \class{tzinfo} classes:
810
Fred Drakebbdb2502002-12-23 18:58:06 +0000811\verbatiminput{tzinfo-examples.py}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000812
813
814\subsection{\class{timetz} \label{datetime-timetz}}
815
816A time object represents a (local) time of day, independent of any
817particular day, and subject to adjustment via a \class{tzinfo} object.
818
819Constructor:
820
821 time(hour=0, minute=0, second=0, microsecond=0, tzinfo=None)
822
Fred Drakebbdb2502002-12-23 18:58:06 +0000823 All arguments are optional. \var{tzinfo} may be \code{None}, or
824 an instance of a \class{tzinfo} subclass. The remaining arguments
825 may be ints or longs, in the following ranges:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000826
827 0 <= hour < 24
828 0 <= minute < 60
829 0 <= second < 60
830 0 <= microsecond < 1000000
831
Fred Drakebbdb2502002-12-23 18:58:06 +0000832 If an argument outside those ranges is given,
833 \exception{ValueError} is raised.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000834
835Class attributes:
836
837 .min
838 The earliest representable time, timetz(0, 0, 0, 0).
839
840 .max
841 The latest representable time, timetz(23, 59, 59, 999999).
842
843 .resolution
844 The smallest possible difference between non-equal timetz
845 objects, timedelta(microseconds=1), although note that
846 arithmetic on \class{timetz} objects is not supported.
847
848Instance attributes (read-only):
849
850 .hour in range(24)
851 .minute in range(60)
852 .second in range(60)
853 .microsecond in range(1000000)
854 .tzinfo the object passed as the tzinfo argument to the
Fred Drakebbdb2502002-12-23 18:58:06 +0000855 \class{timetz} constructor, or \code{None} if none
856 was passed.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000857
858Supported operations:
859
Fred Drakebbdb2502002-12-23 18:58:06 +0000860\begin{itemize}
861 \item
862 comparison of \class{timetz} to timetz, where timetz1 is considered
863 less than timetz2 when timetz1 precedes timetz2 in time, and
864 where the \class{timetz} objects are first adjusted by subtracting
865 their UTC offsets (obtained from \method{utcoffset()}).
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000866
Fred Drakebbdb2502002-12-23 18:58:06 +0000867 \item
868 hash, use as dict key
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000869
Fred Drakebbdb2502002-12-23 18:58:06 +0000870 \item
871 pickling
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000872
Fred Drakebbdb2502002-12-23 18:58:06 +0000873 \item
874 in Boolean contexts, a \class{timetz} object is considered to be
875 true if and only if, after converting it to minutes and
876 subtracting \method{utcoffset()} (or \code{0} if that's
877 \code{None}), the result is non-zero.
878\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000879
880Instance methods:
881
Tim Peters12bf3392002-12-24 05:41:27 +0000882 - replace(hour=, minute=, second=, microsecond=, tzinfo=)
883 Return a timetz with the same value, except for those fields given
884 new values by whichever keyword arguments are specified. Note that
885 \code{tzinfo=None} can be specified to create a naive timetz from an
886 aware timetz.
887
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000888 - isoformat()
889 Return a string representing the time in ISO 8601 format,
890 HH:MM:SS.mmmmmm
891 or, if self.microsecond is 0
892 HH:MM:SS
Fred Drakebbdb2502002-12-23 18:58:06 +0000893 If \method{utcoffset()} does not return \code{None}, a 6-character
894 string is appended, giving the UTC offset in (signed) hours and
895 minutes:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000896 HH:MM:SS.mmmmmm+HH:MM
897 or, if self.microsecond is 0
898 HH:MM:SS+HH:MM
899
900 - __str__()
Fred Drakebbdb2502002-12-23 18:58:06 +0000901 For a \class{timetz} \var{t}, \code{str(\var{t})} is equivalent to
902 \code{\var{t}.isoformat()}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000903
904 - strftime(format)
905 Return a string representing the time, controlled by an explicit
Fred Drakebbdb2502002-12-23 18:58:06 +0000906 format string. See the section on \method{strftime()} behavior.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000907
908 - utcoffset()
Fred Drakebbdb2502002-12-23 18:58:06 +0000909 If \member{tzinfo} is \code{None}, returns \code{None}, else
Tim Peters1cff9fc2002-12-24 16:25:29 +0000910 \code{tzinfo.utcoffset(self)} converted to a \class{timedelta}
911 object.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000912
913 - tzname():
Fred Drakebbdb2502002-12-23 18:58:06 +0000914 If \member{tzinfo} is \code{None}, returns \code{None}, else
915 \code{tzinfo.tzname(self)}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000916
917 - dst()
Fred Drakebbdb2502002-12-23 18:58:06 +0000918 If \member{tzinfo} is \code{None}, returns \code{None}, else
Tim Peters1cff9fc2002-12-24 16:25:29 +0000919 \code{tzinfo.dst(self)} converted to a \class{timedelta} object.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000920
921
922
923\subsection{ \class{datetimetz} \label{datetime-datetimetz}}
924
Fred Drakebbdb2502002-12-23 18:58:06 +0000925\begin{notice}[warning]
926 I think this is \emph{still} missing some methods from the
927 Python implementation.
928\end{notice}
929
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000930A \class{datetimetz} object is a single object containing all the information
931from a date object and a \class{timetz} object.
932
933Constructor:
934
935 datetimetz(year, month, day,
936 hour=0, minute=0, second=0, microsecond=0, tzinfo=None)
937
Fred Drakebbdb2502002-12-23 18:58:06 +0000938 The year, month and day arguments are required. \var{tzinfo} may
939 be \code{None}, or an instance of a \class{tzinfo} subclass. The
940 remaining arguments may be ints or longs, in the following ranges:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000941
942 MINYEAR <= year <= MAXYEAR
943 1 <= month <= 12
944 1 <= day <= number of days in the given month and year
945 0 <= hour < 24
946 0 <= minute < 60
947 0 <= second < 60
948 0 <= microsecond < 1000000
949
Fred Drakebbdb2502002-12-23 18:58:06 +0000950 If an argument outside those ranges is given,
951 \exception{ValueError} is raised.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000952
953Other constructors (class methods):
954
955 - today()
956 utcnow()
957 utcfromtimestamp(timestamp)
958 fromordinal(ordinal)
959
Fred Drakebbdb2502002-12-23 18:58:06 +0000960 These are the same as the \class{datetime} class methods of the
961 same names, except that they construct a \class{datetimetz}
962 object, with tzinfo \code{None}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000963
964 - now([tzinfo=None])
965 fromtimestamp(timestamp[, tzinfo=None])
966
967 These are the same as the \class{datetime} class methods of the same names,
968 except that they accept an additional, optional tzinfo argument, and
969 construct a \class{datetimetz} object with that \class{tzinfo} object attached.
970
971 - combine(date, time)
Fred Drakebbdb2502002-12-23 18:58:06 +0000972 This is the same as \method{datetime.combine()}, except that it constructs
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +0000973 a \class{datetimetz} object, and, if the time object is of type timetz,
974 the \class{datetimetz} object has the same \class{tzinfo} object as the time object.
975
976Class attributes:
977
978 .min
979 The earliest representable datetimetz,
980 datetimetz(MINYEAR, 1, 1).
981
982 .max
983 The latest representable datetime,
984 datetimetz(MAXYEAR, 12, 31, 23, 59, 59, 999999).
985
986 .resolution
987 The smallest possible difference between non-equal datetimetz
988 objects, timedelta(microseconds=1).
989
990Instance attributes (read-only):
991
992 .year between MINYEAR and MAXYEAR inclusive
993 .month between 1 and 12 inclusive
994 .day between 1 and the number of days in the given month
995 of the given year
996 .hour in range(24)
997 .minute in range(60)
998 .second in range(60)
999 .microsecond in range(1000000)
Fred Drakebbdb2502002-12-23 18:58:06 +00001000 .tzinfo the object passed as the \var{tzinfo} argument to
1001 the \class{datetimetz} constructor, or \code{None}
1002 if none was passed.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001003
1004Supported operations:
1005
Fred Drakebbdb2502002-12-23 18:58:06 +00001006\begin{itemize}
1007 \item
1008 datetimetz1 + timedelta -> datetimetz2
1009 timedelta + datetimetz1 -> datetimetz2
1010 The same as addition of \class{datetime} objects, except that
1011 datetimetz2.tzinfo is set to datetimetz1.tzinfo.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001012
Fred Drakebbdb2502002-12-23 18:58:06 +00001013 \item
1014 datetimetz1 - timedelta -> datetimetz2
1015 The same as addition of \class{datetime} objects, except that
1016 datetimetz2.tzinfo is set to datetimetz1.tzinfo.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001017
Fred Drakebbdb2502002-12-23 18:58:06 +00001018 \item
1019 aware_datetimetz1 - aware_datetimetz2 -> timedelta
1020 \naive\_datetimetz1 - \naive\_datetimetz2 -> timedelta
1021 \naive\_datetimetz1 - datetime2 -> timedelta
1022 datetime1 - \naive\_datetimetz2 -> timedelta
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001023
Fred Drakebbdb2502002-12-23 18:58:06 +00001024 \item
1025 Subtraction of a \class{datetime} or datetimetz, from a
1026 \class{datetime} or \class{datetimetz}, is defined only if both
1027 operands are \naive, or if both are aware. If one is aware and
1028 the other is \naive, \exception{TypeError} is raised.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001029
Fred Drakebbdb2502002-12-23 18:58:06 +00001030 \item
1031 If both are \naive, subtraction acts as for \class{datetime}
1032 subtraction.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001033
Fred Drakebbdb2502002-12-23 18:58:06 +00001034 \item
1035 If both are aware \class{datetimetz} objects, a-b acts as if a and b were
Tim Peters1cff9fc2002-12-24 16:25:29 +00001036 first converted to UTC datetimes (by subtracting \code{a.utcoffset()}
1037 minutes from a, and \code{b.utcoffset()} minutes from b), and then doing
Fred Drakebbdb2502002-12-23 18:58:06 +00001038 \class{datetime} subtraction, except that the implementation never
1039 overflows.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001040
Fred Drakebbdb2502002-12-23 18:58:06 +00001041 \item
1042 Comparison of \class{datetimetz} to \class{datetime} or datetimetz. As for
1043 subtraction, comparison is defined only if both operands are
1044 \naive\ or both are aware. If both are \naive, comparison is as
1045 for \class{datetime} objects with the same date and time components.
1046 If both are aware, comparison acts as if both were converted to
1047 UTC datetimes first, except the the implementation never
1048 overflows. If one comparand is \naive\ and the other aware,
1049 \exception{TypeError} is raised.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001050
Fred Drakebbdb2502002-12-23 18:58:06 +00001051 \item
1052 hash, use as dict key
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001053
Fred Drakebbdb2502002-12-23 18:58:06 +00001054 \item
1055 efficient pickling
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001056
Fred Drakebbdb2502002-12-23 18:58:06 +00001057 \item
1058 in Boolean contexts, all \class{datetimetz} objects are considered to be
1059 true
1060\end{itemize}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001061
1062Instance methods:
1063
1064 - date()
1065 time()
1066 toordinal()
1067 weekday()
1068 isoweekday()
1069 isocalendar()
1070 ctime()
1071 __str__()
1072 strftime(format)
1073
1074 These are the same as the \class{datetime} methods of the same names.
1075
1076 - timetz()
1077 Return \class{timetz} object with same hour, minute, second, microsecond,
1078 and tzinfo.
1079
Tim Peters12bf3392002-12-24 05:41:27 +00001080 - replace(year=, month=, day=, hour=, minute=, second=, microsecond=,
1081 tzinfo=)
1082 Return a datetimetz with the same value, except for those fields given
1083 new values by whichever keyword arguments are specified. Note that
1084 \code{tzinfo=None} can be specified to create a naive datetimetz from
1085 an aware datetimetz.
1086
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001087 - utcoffset()
Fred Drakebbdb2502002-12-23 18:58:06 +00001088 If \member{tzinfo} is \code{None}, returns \code{None}, else
Tim Peters1cff9fc2002-12-24 16:25:29 +00001089 \code{tzinfo.utcoffset(self)} converted to a \class{timedelta}
1090 object.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001091
Tim Peters12bf3392002-12-24 05:41:27 +00001092 - tzname()
Fred Drakebbdb2502002-12-23 18:58:06 +00001093 If \member{tzinfo} is \code{None}, returns \code{None}, else
1094 \code{tzinfo.tzname(self)}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001095
1096 - dst()
Fred Drakebbdb2502002-12-23 18:58:06 +00001097 If \member{tzinfo} is \code{None}, returns \code{None}, else
Tim Peters1cff9fc2002-12-24 16:25:29 +00001098 \code{tzinfo.dst(self)} converted to a \class{timedelta}
1099 object.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001100
1101 - timetuple()
Fred Drakebbdb2502002-12-23 18:58:06 +00001102 Like \function{datetime.timetuple()}, but sets the
1103 \member{tm_isdst} flag according to the \method{dst()} method: if
1104 \method{dst()} returns \code{None}, \member{tm_isdst} is set to
1105 \code{-1}; else if \method{dst()} returns a non-zero value,
1106 \member{tm_isdst} is set to \code{1}; else \code{tm_isdst} is set
1107 to \code{0}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001108
1109 - utctimetuple()
Fred Drakebbdb2502002-12-23 18:58:06 +00001110 If \class{datetimetz} instance \var{d} is \naive, this is the same as
1111 \code{\var{d}.timetuple()} except that \member{tm_isdst} is forced to 0
1112 regardless of what \code{d.dst()} returns. DST is never in effect
1113 for a UTC time.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001114
Fred Drakebbdb2502002-12-23 18:58:06 +00001115 If \var{d} is aware, \var{d} is normalized to UTC time, by subtracting
1116 \code{\var{d}.utcoffset()} minutes, and a timetuple for the
1117 normalized time is returned. \member{tm_isdst} is forced to 0.
1118 Note that the result's \member{tm_year} field may be
1119 \constant{MINYEAR}-1 or \constant{MAXYEAR}+1, if \var{d}.year was
1120 \code{MINYEAR} or \code{MAXYEAR} and UTC adjustment spills over a
1121 year boundary.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001122
1123 - isoformat(sep='T')
1124 Return a string representing the date and time in ISO 8601 format,
1125 YYYY-MM-DDTHH:MM:SS.mmmmmm
Fred Drakebbdb2502002-12-23 18:58:06 +00001126 or, if \member{microsecond} is 0,
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001127 YYYY-MM-DDTHH:MM:SS
1128
Fred Drakebbdb2502002-12-23 18:58:06 +00001129 If \method{utcoffset()} does not return \code{None}, a 6-character
1130 string is appended, giving the UTC offset in (signed) hours and
1131 minutes:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001132 YYYY-MM-DDTHH:MM:SS.mmmmmm+HH:MM
Fred Drakebbdb2502002-12-23 18:58:06 +00001133 or, if \member{microsecond} is 0
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001134 YYYY-MM-DDTHH:MM:SS+HH:MM
1135
Fred Drakebbdb2502002-12-23 18:58:06 +00001136 The optional argument \var{sep} (default \code{'T'}) is a
1137 one-character separator, placed between the date and time portions
1138 of the result. For example,
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001139
Fred Drakebbdb2502002-12-23 18:58:06 +00001140\begin{verbatim}
1141>>> from datetime import *
1142>>> class TZ(tzinfo):
1143... def utcoffset(self, dt): return -399
1144...
1145>>> datetimetz(2002, 12, 25, tzinfo=TZ()).isoformat(' ')
1146'2002-12-25 00:00:00-06:39'
1147\end{verbatim}
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001148
Fred Drakebbdb2502002-12-23 18:58:06 +00001149\code{str(\var{d})} is equivalent to \code{\var{d}.isoformat(' ')}.
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001150
1151
Tim Peters29fb9c72002-12-23 22:21:52 +00001152\subsection{\method{strftime()} Behavior}
1153
1154\class{date}, \class{datetime}, \class{datetimetz}, \class{time},
1155and \class{timetz} objects all support a \code{strftime(\var{format})}
1156method, to create a string representing the time under the control of
1157an explicit format string. Broadly speaking,
1158\begin{verbatim}
1159d.strftime(fmt)
1160\end{verbatim}
1161acts like the \refmodule{time} module's
1162\begin{verbatim}
1163time.strftime(fmt, d.timetuple())
1164\end{verbatim}
1165although not all objects support a \method{timetuple()} method.
1166
1167For \class{time} and \class{timetz} objects, format codes for year,
1168month, and day should not be used, as time objects have no such values.
1169\code{1900} is used for the year, and \code{0} for the month and day.
1170
1171For \class{date} objects, format codes for hours, minutes, and seconds
1172should not be used, as date objects have no such values. \code{0} is
1173used instead.
1174
1175For a \naive\ object, the \code{\%z} and \code{\%Z} format codes are
1176replaced by empty strings.
1177
1178For an aware object:
1179
1180\begin{itemize}
1181 \item[\code{\%z}]
1182 \method{utcoffset()} is transformed into a 5-character string of
1183 the form +HHMM or -HHMM, where HH is a 2-digit string giving the
1184 number of UTC offset hours, and MM is a 2-digit string giving the
1185 number of UTC offset minutes. For example, if
Tim Peters1cff9fc2002-12-24 16:25:29 +00001186 \method{utcoffset()} returns \code{timedelta(hours=-3, minutes=-30}},
1187 \code{\%z} is replaced with the string \code{'-0330'}.
Tim Peters29fb9c72002-12-23 22:21:52 +00001188
1189 \item[\code{\%Z}]
1190 If \method{tzname()} returns \code{None}, \code{\%Z} is replaced
1191 by an empty string. Else \code{\%Z} is replaced by the returned
1192 value, which must be a string.
1193\end{itemize}
1194
1195The full set of format codes supported varies across platforms,
1196because Python calls the platform C library's \function{strftime()}
1197function, and platform variations are common. The documentation for
1198Python's \refmodule{time} module lists the format codes that the C
1199standard (1989 version) requires, and those work on all platforms
1200with a standard C implementation. Note that the 1999 version of the
1201C standard added additional format codes.
1202
1203The exact range of years for which \method{strftime()} works also
1204varies across platforms. Regardless of platform, years before 1900
1205cannot be used.
1206
1207
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001208\subsection{C API}
1209
1210Struct typedefs:
1211
1212 PyDateTime_Date
1213 PyDateTime_DateTime
1214 PyDateTime_DateTimeTZ
1215 PyDateTime_Time
1216 PyDateTime_TimeTZ
1217 PyDateTime_Delta
1218 PyDateTime_TZInfo
1219
1220Type-check macros:
1221
1222 PyDate_Check(op)
1223 PyDate_CheckExact(op)
1224
1225 PyDateTime_Check(op)
1226 PyDateTime_CheckExact(op)
1227
1228 PyDateTimeTZ_Check(op)
1229 PyDateTimeTZ_CheckExact(op)
1230
1231 PyTime_Check(op)
1232 PyTime_CheckExact(op)
1233
1234 PyTimeTZ_Check(op)
1235 PyTimeTZ_CheckExact(op)
1236
1237 PyDelta_Check(op)
1238 PyDelta_CheckExact(op)
1239
1240 PyTZInfo_Check(op)
1241 PyTZInfo_CheckExact(op
1242
1243Accessor macros:
1244
1245All objects are immutable, so accessors are read-only. All macros
1246return ints:
1247
Tim Peters1cff9fc2002-12-24 16:25:29 +00001248 For \class{date}, \class{datetime}, and \class{datetimetz} instances:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001249 PyDateTime_GET_YEAR(o)
1250 PyDateTime_GET_MONTH(o)
1251 PyDateTime_GET_DAY(o)
1252
1253 For \class{datetime} and \class{datetimetz} instances:
1254 PyDateTime_DATE_GET_HOUR(o)
1255 PyDateTime_DATE_GET_MINUTE(o)
1256 PyDateTime_DATE_GET_SECOND(o)
1257 PyDateTime_DATE_GET_MICROSECOND(o)
1258
Tim Peters1cff9fc2002-12-24 16:25:29 +00001259 For \class{time} and \class{timetz} instances:
Andrew M. Kuchlingca2623a2002-12-18 14:59:11 +00001260 PyDateTime_TIME_GET_HOUR(o)
1261 PyDateTime_TIME_GET_MINUTE(o)
1262 PyDateTime_TIME_GET_SECOND(o)
1263 PyDateTime_TIME_GET_MICROSECOND(o)