Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 1 | % XXX what order should the types be discussed in? |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 2 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 3 | \section{\module{datetime} --- |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 4 | Basic date and time types} |
| 5 | |
| 6 | \declaremodule{builtin}{datetime} |
| 7 | \modulesynopsis{Basic date and time types.} |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 8 | \moduleauthor{Tim Peters}{tim@zope.com} |
| 9 | \sectionauthor{Tim Peters}{tim@zope.com} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 10 | \sectionauthor{A.M. Kuchling}{amk@amk.ca} |
Raymond Hettinger | 6005a34 | 2002-12-30 20:01:24 +0000 | [diff] [blame] | 11 | |
| 12 | \versionadded{2.3} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 13 | |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 14 | |
| 15 | The \module{datetime} module supplies classes for manipulating dates |
| 16 | and times in both simple and complex ways. While date and time |
| 17 | arithmetic is supported, the focus of the implementation is on |
Andrew M. Kuchling | 9b44571 | 2003-01-09 13:46:30 +0000 | [diff] [blame] | 18 | efficient member extraction for output formatting and manipulation. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 19 | |
Fred Drake | a37e5cc | 2002-12-30 21:26:42 +0000 | [diff] [blame] | 20 | There are two kinds of date and time objects: ``naive'' and ``aware''. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 21 | This distinction refers to whether the object has any notion of time |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 22 | zone, daylight saving time, or other kind of algorithmic or political |
| 23 | time adjustment. Whether a naive \class{datetime} object represents |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 24 | Coordinated Universal Time (UTC), local time, or time in some other |
| 25 | timezone is purely up to the program, just like it's up to the program |
Fred Drake | a37e5cc | 2002-12-30 21:26:42 +0000 | [diff] [blame] | 26 | whether a particular number represents meters, miles, or mass. Naive |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 27 | \class{datetime} objects are easy to understand and to work with, at |
| 28 | the cost of ignoring some aspects of reality. |
| 29 | |
Andrew M. Kuchling | 9b44571 | 2003-01-09 13:46:30 +0000 | [diff] [blame] | 30 | For applications requiring more, \class{datetime} and \class{time} |
| 31 | objects have an optional time zone information member, |
Tim Peters | 327098a | 2003-01-20 22:54:38 +0000 | [diff] [blame] | 32 | \member{tzinfo}, that can contain an instance of a subclass of |
Andrew M. Kuchling | 9b44571 | 2003-01-09 13:46:30 +0000 | [diff] [blame] | 33 | the abstract \class{tzinfo} class. These \class{tzinfo} objects |
| 34 | capture information about the offset from UTC time, the time zone |
| 35 | name, and whether Daylight Saving Time is in effect. Note that no |
| 36 | concrete \class{tzinfo} classes are supplied by the \module{datetime} |
Andrew M. Kuchling | 570e358 | 2003-02-05 21:15:38 +0000 | [diff] [blame] | 37 | module. Supporting timezones at whatever level of detail is required |
| 38 | is up to the application. The rules for time adjustment across the |
| 39 | world are more political than rational, and there is no standard |
Andrew M. Kuchling | 9b44571 | 2003-01-09 13:46:30 +0000 | [diff] [blame] | 40 | suitable for every application. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 41 | |
| 42 | The \module{datetime} module exports the following constants: |
| 43 | |
| 44 | \begin{datadesc}{MINYEAR} |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 45 | The smallest year number allowed in a \class{date} or |
| 46 | \class{datetime} object. \constant{MINYEAR} |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 47 | is \code{1}. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 48 | \end{datadesc} |
| 49 | |
| 50 | \begin{datadesc}{MAXYEAR} |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 51 | The largest year number allowed in a \class{date} or \class{datetime} |
| 52 | object. \constant{MAXYEAR} is \code{9999}. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 53 | \end{datadesc} |
| 54 | |
Raymond Hettinger | 6005a34 | 2002-12-30 20:01:24 +0000 | [diff] [blame] | 55 | \begin{seealso} |
Raymond Hettinger | 6222958 | 2002-12-31 16:37:03 +0000 | [diff] [blame] | 56 | \seemodule{calendar}{General calendar related functions.} |
Raymond Hettinger | 6005a34 | 2002-12-30 20:01:24 +0000 | [diff] [blame] | 57 | \seemodule{time}{Time access and conversions.} |
| 58 | \end{seealso} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 59 | |
| 60 | \subsection{Available Types} |
| 61 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 62 | \begin{classdesc*}{date} |
Fred Drake | a37e5cc | 2002-12-30 21:26:42 +0000 | [diff] [blame] | 63 | An idealized naive date, assuming the current Gregorian calendar |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 64 | always was, and always will be, in effect. |
| 65 | Attributes: \member{year}, \member{month}, and \member{day}. |
| 66 | \end{classdesc*} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 67 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 68 | \begin{classdesc*}{time} |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 69 | An idealized time, independent of any particular day, assuming |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 70 | that every day has exactly 24*60*60 seconds (there is no notion |
| 71 | of "leap seconds" here). |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 72 | Attributes: \member{hour}, \member{minute}, \member{second}, |
| 73 | \member{microsecond}, and \member{tzinfo}. |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 74 | \end{classdesc*} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 75 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 76 | \begin{classdesc*}{datetime} |
Andrew M. Kuchling | 9b44571 | 2003-01-09 13:46:30 +0000 | [diff] [blame] | 77 | A combination of a date and a time. |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 78 | Attributes: \member{year}, \member{month}, \member{day}, |
| 79 | \member{hour}, \member{minute}, \member{second}, |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 80 | \member{microsecond}, and \member{tzinfo}. |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 81 | \end{classdesc*} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 82 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 83 | \begin{classdesc*}{timedelta} |
Andrew M. Kuchling | 9b44571 | 2003-01-09 13:46:30 +0000 | [diff] [blame] | 84 | A duration expressing the difference between two \class{date}, |
| 85 | \class{time}, or \class{datetime} instances to microsecond |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 86 | resolution. |
| 87 | \end{classdesc*} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 88 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 89 | \begin{classdesc*}{tzinfo} |
| 90 | An abstract base class for time zone information objects. These |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 91 | are used by the \class{datetime} and \class{time} classes to |
Andrew M. Kuchling | 9b44571 | 2003-01-09 13:46:30 +0000 | [diff] [blame] | 92 | provide a customizable notion of time adjustment (for example, to |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 93 | account for time zone and/or daylight saving time). |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 94 | \end{classdesc*} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 95 | |
| 96 | Objects of these types are immutable. |
| 97 | |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 98 | Objects of the \class{date} type are always naive. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 99 | |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 100 | An object \var{d} of type \class{time} or \class{datetime} may be |
Fred Drake | a37e5cc | 2002-12-30 21:26:42 +0000 | [diff] [blame] | 101 | naive or aware. \var{d} is aware if \code{\var{d}.tzinfo} is not |
Andrew M. Kuchling | 9b44571 | 2003-01-09 13:46:30 +0000 | [diff] [blame] | 102 | \code{None} and \code{\var{d}.tzinfo.utcoffset(\var{d})} does not return |
Fred Drake | a37e5cc | 2002-12-30 21:26:42 +0000 | [diff] [blame] | 103 | \code{None}. If \code{\var{d}.tzinfo} is \code{None}, or if |
| 104 | \code{\var{d}.tzinfo} is not \code{None} but |
| 105 | \code{\var{d}.tzinfo.utcoffset(\var{d})} returns \code{None}, \var{d} |
| 106 | is naive. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 107 | |
Fred Drake | a37e5cc | 2002-12-30 21:26:42 +0000 | [diff] [blame] | 108 | The distinction between naive and aware doesn't apply to |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 109 | \code{timedelta} objects. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 110 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 111 | Subclass relationships: |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 112 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 113 | \begin{verbatim} |
| 114 | object |
| 115 | timedelta |
| 116 | tzinfo |
| 117 | time |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 118 | date |
| 119 | datetime |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 120 | \end{verbatim} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 121 | |
Raymond Hettinger | 6005a34 | 2002-12-30 20:01:24 +0000 | [diff] [blame] | 122 | \subsection{\class{timedelta} Objects \label{datetime-timedelta}} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 123 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 124 | A \class{timedelta} object represents a duration, the difference |
| 125 | between two dates or times. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 126 | |
Fred Drake | 0f8e543 | 2002-12-31 18:31:48 +0000 | [diff] [blame] | 127 | \begin{classdesc}{timedelta}{days=0, seconds=0, microseconds=0, |
| 128 | milliseconds=0, minutes=0, hours=0, weeks=0} |
Raymond Hettinger | f621232 | 2002-12-31 17:24:50 +0000 | [diff] [blame] | 129 | |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 130 | All arguments are optional. Arguments may be ints, longs, or floats, |
| 131 | and may be positive or negative. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 132 | |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 133 | Only \var{days}, \var{seconds} and \var{microseconds} are stored |
| 134 | internally. Arguments are converted to those units: |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 135 | |
Andrew M. Kuchling | 9b44571 | 2003-01-09 13:46:30 +0000 | [diff] [blame] | 136 | \begin{itemize} |
| 137 | \item A millisecond is converted to 1000 microseconds. |
| 138 | \item A minute is converted to 60 seconds. |
| 139 | \item An hour is converted to 3600 seconds. |
| 140 | \item A week is converted to 7 days. |
| 141 | \end{itemize} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 142 | |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 143 | and days, seconds and microseconds are then normalized so that the |
| 144 | representation is unique, with |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 145 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 146 | \begin{itemize} |
| 147 | \item \code{0 <= \var{microseconds} < 1000000} |
| 148 | \item \code{0 <= \var{seconds} < 3600*24} (the number of seconds in one day) |
| 149 | \item \code{-999999999 <= \var{days} <= 999999999} |
| 150 | \end{itemize} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 151 | |
Andrew M. Kuchling | 9b44571 | 2003-01-09 13:46:30 +0000 | [diff] [blame] | 152 | If any argument is a float and there are fractional microseconds, |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 153 | the fractional microseconds left over from all arguments are combined |
| 154 | and their sum is rounded to the nearest microsecond. If no |
| 155 | argument is a float, the conversion and normalization processes |
| 156 | are exact (no information is lost). |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 157 | |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 158 | If the normalized value of days lies outside the indicated range, |
| 159 | \exception{OverflowError} is raised. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 160 | |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 161 | Note that normalization of negative values may be surprising at first. |
| 162 | For example, |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 163 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 164 | \begin{verbatim} |
| 165 | >>> d = timedelta(microseconds=-1) |
| 166 | >>> (d.days, d.seconds, d.microseconds) |
| 167 | (-1, 86399, 999999) |
| 168 | \end{verbatim} |
Fred Drake | 0f8e543 | 2002-12-31 18:31:48 +0000 | [diff] [blame] | 169 | \end{classdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 170 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 171 | Class attributes are: |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 172 | |
Fred Drake | 0f8e543 | 2002-12-31 18:31:48 +0000 | [diff] [blame] | 173 | \begin{memberdesc}{min} |
| 174 | The most negative \class{timedelta} object, |
| 175 | \code{timedelta(-999999999)}. |
| 176 | \end{memberdesc} |
| 177 | |
| 178 | \begin{memberdesc}{max} |
| 179 | The most positive \class{timedelta} object, |
| 180 | \code{timedelta(days=999999999, hours=23, minutes=59, seconds=59, |
| 181 | microseconds=999999)}. |
| 182 | \end{memberdesc} |
| 183 | |
| 184 | \begin{memberdesc}{resolution} |
| 185 | The smallest possible difference between non-equal |
| 186 | \class{timedelta} objects, \code{timedelta(microseconds=1)}. |
| 187 | \end{memberdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 188 | |
Fred Drake | a37e5cc | 2002-12-30 21:26:42 +0000 | [diff] [blame] | 189 | Note that, because of normalization, \code{timedelta.max} \textgreater |
| 190 | \code{-timedelta.min}. \code{-timedelta.max} is not representable as |
| 191 | a \class{timedelta} object. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 192 | |
| 193 | Instance attributes (read-only): |
| 194 | |
Raymond Hettinger | 6005a34 | 2002-12-30 20:01:24 +0000 | [diff] [blame] | 195 | \begin{tableii}{c|l}{code}{Attribute}{Value} |
| 196 | \lineii{days}{Between -999999999 and 999999999 inclusive} |
| 197 | \lineii{seconds}{Between 0 and 86399 inclusive} |
| 198 | \lineii{microseconds}{Between 0 and 999999 inclusive} |
| 199 | \end{tableii} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 200 | |
| 201 | Supported operations: |
| 202 | |
Andrew M. Kuchling | 9b44571 | 2003-01-09 13:46:30 +0000 | [diff] [blame] | 203 | % XXX this table is too wide! |
Andrew M. Kuchling | 570e358 | 2003-02-05 21:15:38 +0000 | [diff] [blame] | 204 | \begin{tableii}{c|l}{code}{Operation}{Result} |
| 205 | \lineii{\var{t1} = \var{t2} + \var{t3}} |
Tim Peters | 9532298 | 2002-12-31 16:01:47 +0000 | [diff] [blame] | 206 | {Sum of \var{t2} and \var{t3}. |
Fred Drake | 9bdeee4 | 2002-12-30 20:35:32 +0000 | [diff] [blame] | 207 | Afterwards \var{t1}-\var{t2} == \var{t3} and \var{t1}-\var{t3} |
Andrew M. Kuchling | 570e358 | 2003-02-05 21:15:38 +0000 | [diff] [blame] | 208 | == \var{t2} are true. |
| 209 | (1)} |
| 210 | \lineii{\var{t1} = \var{t2} - \var{t3}} |
| 211 | {Difference of \var{t2} and \var{t3}. |
| 212 | Afterwards \var{t1} == \var{t2} - \var{t3} and \var{t2} == \var{t1} + \var{t3} are |
| 213 | true. |
| 214 | (1)} |
| 215 | \lineii{\var{t1} = \var{t2} * \var{i} or \var{t1} = \var{i} * \var{t2}} |
Fred Drake | 9bdeee4 | 2002-12-30 20:35:32 +0000 | [diff] [blame] | 216 | {Delta multiplied by an integer or long. |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 217 | Afterwards \var{t1} // i == \var{t2} is true, |
Andrew M. Kuchling | 570e358 | 2003-02-05 21:15:38 +0000 | [diff] [blame] | 218 | provided \code{i != 0}.} |
| 219 | \lineii{}{In general, \var{t1} * i == \var{t1} * (i-1) + \var{t1} is true. |
| 220 | (1)} |
| 221 | \lineii{\var{t1} = \var{t2} // \var{i}} |
| 222 | {The floor is computed and the remainder (if any) is thrown away. |
| 223 | (3)} |
| 224 | \lineii{+\var{t1}} |
| 225 | {Returns a \class{timedelta} object with the same value. |
| 226 | (2)} |
| 227 | \lineii{-\var{t1}} |
Raymond Hettinger | cbd6cd2 | 2002-12-31 16:30:49 +0000 | [diff] [blame] | 228 | {equivalent to \class{timedelta}(-\var{t1.days}, -\var{t1.seconds}, |
Andrew M. Kuchling | 570e358 | 2003-02-05 21:15:38 +0000 | [diff] [blame] | 229 | -\var{t1.microseconds}), and to \var{t1}* -1. |
| 230 | (1)(4)} |
| 231 | \lineii{abs(\var{t})} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 232 | {equivalent to +\var{t} when \code{t.days >= 0}, and to |
Andrew M. Kuchling | 570e358 | 2003-02-05 21:15:38 +0000 | [diff] [blame] | 233 | -\var{t} when \code{t.days < 0}. |
| 234 | (2)} |
| 235 | \end{tableii} |
Raymond Hettinger | 6005a34 | 2002-12-30 20:01:24 +0000 | [diff] [blame] | 236 | \noindent |
| 237 | Notes: |
| 238 | |
| 239 | \begin{description} |
| 240 | \item[(1)] |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 241 | This is exact, but may overflow. |
Raymond Hettinger | 6005a34 | 2002-12-30 20:01:24 +0000 | [diff] [blame] | 242 | |
| 243 | \item[(2)] |
Tim Peters | 397301e | 2003-01-02 21:28:08 +0000 | [diff] [blame] | 244 | This is exact, and cannot overflow. |
Raymond Hettinger | c5f5f87 | 2002-12-31 14:26:54 +0000 | [diff] [blame] | 245 | |
| 246 | \item[(3)] |
Tim Peters | 397301e | 2003-01-02 21:28:08 +0000 | [diff] [blame] | 247 | Division by 0 raises \exception{ZeroDivisionError}. |
| 248 | |
| 249 | \item[(4)] |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 250 | -\var{timedelta.max} is not representable as a \class{timedelta} object. |
Raymond Hettinger | 6005a34 | 2002-12-30 20:01:24 +0000 | [diff] [blame] | 251 | \end{description} |
| 252 | |
Raymond Hettinger | c5f5f87 | 2002-12-31 14:26:54 +0000 | [diff] [blame] | 253 | In addition to the operations listed above \class{timedelta} objects |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 254 | support certain additions and subtractions with \class{date} and |
| 255 | \class{datetime} objects (see below). |
Raymond Hettinger | 6005a34 | 2002-12-30 20:01:24 +0000 | [diff] [blame] | 256 | |
Raymond Hettinger | c5f5f87 | 2002-12-31 14:26:54 +0000 | [diff] [blame] | 257 | Comparisons of \class{timedelta} objects are supported with the |
| 258 | \class{timedelta} object representing the smaller duration considered |
| 259 | to be the smaller timedelta. |
Tim Peters | 07534a6 | 2003-02-07 22:50:28 +0000 | [diff] [blame] | 260 | In order to stop mixed-type comparisons from falling back to the |
| 261 | default comparison by object address, when a \class{timedelta} object is |
| 262 | compared to an object of a different type, \exception{TypeError} is |
| 263 | raised unless the comparison is \code{==} or \code{!=}. The latter |
| 264 | cases return \constant{False} or \constant{True}, respectively. |
Raymond Hettinger | 6005a34 | 2002-12-30 20:01:24 +0000 | [diff] [blame] | 265 | |
Andrew M. Kuchling | 9b44571 | 2003-01-09 13:46:30 +0000 | [diff] [blame] | 266 | \class{timedelta} objects are hashable (usable as dictionary keys), |
Raymond Hettinger | c5f5f87 | 2002-12-31 14:26:54 +0000 | [diff] [blame] | 267 | support efficient pickling, and in Boolean contexts, a \class{timedelta} |
| 268 | object is considered to be true if and only if it isn't equal to |
| 269 | \code{timedelta(0)}. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 270 | |
| 271 | |
Raymond Hettinger | 6005a34 | 2002-12-30 20:01:24 +0000 | [diff] [blame] | 272 | \subsection{\class{date} Objects \label{datetime-date}} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 273 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 274 | A \class{date} object represents a date (year, month and day) in an idealized |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 275 | calendar, the current Gregorian calendar indefinitely extended in both |
| 276 | directions. January 1 of year 1 is called day number 1, January 2 of year |
| 277 | 1 is called day number 2, and so on. This matches the definition of the |
| 278 | "proleptic Gregorian" calendar in Dershowitz and Reingold's book |
Fred Drake | a37e5cc | 2002-12-30 21:26:42 +0000 | [diff] [blame] | 279 | \citetitle{Calendrical Calculations}, where it's the base calendar for all |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 280 | computations. See the book for algorithms for converting between |
| 281 | proleptic Gregorian ordinals and many other calendar systems. |
| 282 | |
Fred Drake | 0f8e543 | 2002-12-31 18:31:48 +0000 | [diff] [blame] | 283 | \begin{classdesc}{date}{year, month, day} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 284 | All arguments are required. Arguments may be ints or longs, in the |
| 285 | following ranges: |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 286 | |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 287 | \begin{itemize} |
| 288 | \item \code{MINYEAR <= \var{year} <= MAXYEAR} |
| 289 | \item \code{1 <= \var{month} <= 12} |
| 290 | \item \code{1 <= \var{day} <= number of days in the given month and year} |
| 291 | \end{itemize} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 292 | |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 293 | If an argument outside those ranges is given, \exception{ValueError} |
| 294 | is raised. |
Fred Drake | 0f8e543 | 2002-12-31 18:31:48 +0000 | [diff] [blame] | 295 | \end{classdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 296 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 297 | Other constructors, all class methods: |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 298 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 299 | \begin{methoddesc}{today}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 300 | Return the current local date. This is equivalent to |
| 301 | \code{date.fromtimestamp(time.time())}. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 302 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 303 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 304 | \begin{methoddesc}{fromtimestamp}{timestamp} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 305 | Return the local date corresponding to the POSIX timestamp, such |
| 306 | as is returned by \function{time.time()}. This may raise |
| 307 | \exception{ValueError}, if the timestamp is out of the range of |
| 308 | values supported by the platform C \cfunction{localtime()} |
| 309 | function. It's common for this to be restricted to years from 1970 |
Tim Peters | 75a6e3b | 2003-01-04 18:17:36 +0000 | [diff] [blame] | 310 | through 2038. Note that on non-POSIX systems that include leap |
| 311 | seconds in their notion of a timestamp, leap seconds are ignored by |
| 312 | \method{fromtimestamp()}. |
Andrew M. Kuchling | 0f0e6b9 | 2003-01-09 12:51:50 +0000 | [diff] [blame] | 313 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 314 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 315 | \begin{methoddesc}{fromordinal}{ordinal} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 316 | Return the date corresponding to the proleptic Gregorian ordinal, |
| 317 | where January 1 of year 1 has ordinal 1. \exception{ValueError} is |
| 318 | raised unless \code{1 <= \var{ordinal} <= date.max.toordinal()}. |
| 319 | For any date \var{d}, \code{date.fromordinal(\var{d}.toordinal()) == |
| 320 | \var{d}}. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 321 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 322 | |
| 323 | Class attributes: |
| 324 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 325 | \begin{memberdesc}{min} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 326 | The earliest representable date, \code{date(MINYEAR, 1, 1)}. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 327 | \end{memberdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 328 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 329 | \begin{memberdesc}{max} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 330 | The latest representable date, \code{date(MAXYEAR, 12, 31)}. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 331 | \end{memberdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 332 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 333 | \begin{memberdesc}{resolution} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 334 | The smallest possible difference between non-equal date |
| 335 | objects, \code{timedelta(days=1)}. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 336 | \end{memberdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 337 | |
| 338 | Instance attributes (read-only): |
| 339 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 340 | \begin{memberdesc}{year} |
Andrew M. Kuchling | 9b44571 | 2003-01-09 13:46:30 +0000 | [diff] [blame] | 341 | Between \constant{MINYEAR} and \constant{MAXYEAR} inclusive. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 342 | \end{memberdesc} |
| 343 | |
| 344 | \begin{memberdesc}{month} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 345 | Between 1 and 12 inclusive. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 346 | \end{memberdesc} |
| 347 | |
| 348 | \begin{memberdesc}{day} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 349 | Between 1 and the number of days in the given month of the given |
| 350 | year. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 351 | \end{memberdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 352 | |
| 353 | Supported operations: |
| 354 | |
Andrew M. Kuchling | 9b44571 | 2003-01-09 13:46:30 +0000 | [diff] [blame] | 355 | % XXX rewrite to be a table |
Andrew M. Kuchling | 570e358 | 2003-02-05 21:15:38 +0000 | [diff] [blame] | 356 | \begin{tableii}{c|l}{code}{Operation}{Result} |
| 357 | \lineii{\var{date2} = \var{date1} + \var{timedelta}} |
| 358 | {\var{date2} is \code{\var{timedelta}.days} days removed from |
| 359 | \var{date1}. (1)} |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 360 | |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 361 | |
Andrew M. Kuchling | 570e358 | 2003-02-05 21:15:38 +0000 | [diff] [blame] | 362 | \lineii{\var{date2} = \var{date1} - \var{timedelta}} |
| 363 | {Computes \var{date2} such that \code{\var{date2} + \var{timedelta} |
| 364 | == \var{date1}}. (2)} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 365 | |
Andrew M. Kuchling | 570e358 | 2003-02-05 21:15:38 +0000 | [diff] [blame] | 366 | \lineii{\var{timedelta} = \var{date1} - \var{date2}} |
| 367 | {(3)} |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 368 | |
Andrew M. Kuchling | 570e358 | 2003-02-05 21:15:38 +0000 | [diff] [blame] | 369 | \lineii{\var{date1}<\var{date2}} |
| 370 | {\var{date1} is considered less than \var{date2} when \var{date1} |
| 371 | precedes \var{date2} in time. (4)} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 372 | |
Andrew M. Kuchling | 570e358 | 2003-02-05 21:15:38 +0000 | [diff] [blame] | 373 | \end{tableii} |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 374 | |
Andrew M. Kuchling | 570e358 | 2003-02-05 21:15:38 +0000 | [diff] [blame] | 375 | Notes: |
| 376 | \begin{description} |
| 377 | |
| 378 | \item[(1)] |
| 379 | \var{date2} is moved forward in time if \code{\var{timedelta}.days |
| 380 | > 0}, or backward if \code{\var{timedelta}.days < 0}. Afterward |
| 381 | \code{\var{date2} - \var{date1} == \var{timedelta}.days}. |
| 382 | \code{\var{timedelta}.seconds} and |
| 383 | \code{\var{timedelta}.microseconds} are ignored. |
| 384 | \exception{OverflowError} is raised if \code{\var{date2}.year} |
| 385 | would be smaller than \constant{MINYEAR} or larger than |
| 386 | \constant{MAXYEAR}. |
| 387 | |
| 388 | \item[(2)] |
| 389 | This isn't quite equivalent to date1 + |
| 390 | (-timedelta), because -timedelta in isolation can overflow in cases |
| 391 | where date1 - timedelta does not. \code{\var{timedelta}.seconds} |
| 392 | and \code{\var{timedelta}.microseconds} are ignored. |
| 393 | |
| 394 | \item[(3)] |
| 395 | This is exact, and cannot overflow. timedelta.seconds and |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 396 | timedelta.microseconds are 0, and date2 + timedelta == date1 |
| 397 | after. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 398 | |
Andrew M. Kuchling | 570e358 | 2003-02-05 21:15:38 +0000 | [diff] [blame] | 399 | \item[(4)] |
| 400 | In other words, \code{date1 < date2} |
| 401 | if and only if \code{\var{date1}.toordinal() < |
| 402 | \var{date2}.toordinal()}. |
| 403 | In order to stop comparison from falling back to the default |
| 404 | scheme of comparing object addresses, date comparison |
| 405 | normally raises \exception{TypeError} if the other comparand |
| 406 | isn't also a \class{date} object. However, \code{NotImplemented} |
| 407 | is returned instead if the other comparand has a |
| 408 | \method{timetuple} attribute. This hook gives other kinds of |
| 409 | date objects a chance at implementing mixed-type comparison. |
Tim Peters | 07534a6 | 2003-02-07 22:50:28 +0000 | [diff] [blame] | 410 | If not, when a \class{date} object is |
| 411 | compared to an object of a different type, \exception{TypeError} is |
| 412 | raised unless the comparison is \code{==} or \code{!=}. The latter |
| 413 | cases return \constant{False} or \constant{True}, respectively. |
Andrew M. Kuchling | 570e358 | 2003-02-05 21:15:38 +0000 | [diff] [blame] | 414 | |
| 415 | \end{description} |
Tim Peters | 8d81a01 | 2003-01-24 22:36:34 +0000 | [diff] [blame] | 416 | |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 417 | |
Andrew M. Kuchling | 570e358 | 2003-02-05 21:15:38 +0000 | [diff] [blame] | 418 | Dates can be used as dictionary keys. In Boolean contexts, all |
| 419 | \class{date} objects are considered to be true. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 420 | |
| 421 | Instance methods: |
| 422 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 423 | \begin{methoddesc}{replace}{year, month, day} |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 424 | Return a date with the same value, except for those members given |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 425 | new values by whichever keyword arguments are specified. For |
| 426 | example, if \code{d == date(2002, 12, 31)}, then |
| 427 | \code{d.replace(day=26) == date(2000, 12, 26)}. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 428 | \end{methoddesc} |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 429 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 430 | \begin{methoddesc}{timetuple}{} |
Martin v. Löwis | ef36537 | 2003-09-04 18:29:53 +0000 | [diff] [blame] | 431 | Return a \class{time.struct_time} such as returned by |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 432 | \function{time.localtime()}. The hours, minutes and seconds are |
| 433 | 0, and the DST flag is -1. |
| 434 | \code{\var{d}.timetuple()} is equivalent to |
Martin v. Löwis | ef36537 | 2003-09-04 18:29:53 +0000 | [diff] [blame] | 435 | \code{time.struct_time((\var{d}.year, \var{d}.month, \var{d}.day, |
Andrew M. Kuchling | 570e358 | 2003-02-05 21:15:38 +0000 | [diff] [blame] | 436 | 0, 0, 0, |
| 437 | \var{d}.weekday(), |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 438 | \var{d}.toordinal() - date(\var{d}.year, 1, 1).toordinal() + 1, |
Martin v. Löwis | ef36537 | 2003-09-04 18:29:53 +0000 | [diff] [blame] | 439 | -1))} |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 440 | \end{methoddesc} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 441 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 442 | \begin{methoddesc}{toordinal}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 443 | Return the proleptic Gregorian ordinal of the date, where January 1 |
| 444 | of year 1 has ordinal 1. For any \class{date} object \var{d}, |
| 445 | \code{date.fromordinal(\var{d}.toordinal()) == \var{d}}. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 446 | \end{methoddesc} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 447 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 448 | \begin{methoddesc}{weekday}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 449 | Return the day of the week as an integer, where Monday is 0 and |
Andrew M. Kuchling | 570e358 | 2003-02-05 21:15:38 +0000 | [diff] [blame] | 450 | Sunday is 6. For example, \code{date(2002, 12, 4).weekday() == 2}, a |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 451 | Wednesday. |
| 452 | See also \method{isoweekday()}. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 453 | \end{methoddesc} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 454 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 455 | \begin{methoddesc}{isoweekday}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 456 | Return the day of the week as an integer, where Monday is 1 and |
Andrew M. Kuchling | 570e358 | 2003-02-05 21:15:38 +0000 | [diff] [blame] | 457 | Sunday is 7. For example, \code{date(2002, 12, 4).isoweekday() == 3}, a |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 458 | Wednesday. |
| 459 | See also \method{weekday()}, \method{isocalendar()}. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 460 | \end{methoddesc} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 461 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 462 | \begin{methoddesc}{isocalendar}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 463 | Return a 3-tuple, (ISO year, ISO week number, ISO weekday). |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 464 | |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 465 | The ISO calendar is a widely used variant of the Gregorian calendar. |
| 466 | See \url{http://www.phys.uu.nl/~vgent/calendar/isocalendar.htm} |
| 467 | for a good explanation. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 468 | |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 469 | The ISO year consists of 52 or 53 full weeks, and where a week starts |
| 470 | on a Monday and ends on a Sunday. The first week of an ISO year is |
| 471 | the first (Gregorian) calendar week of a year containing a Thursday. |
| 472 | This is called week number 1, and the ISO year of that Thursday is |
| 473 | the same as its Gregorian year. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 474 | |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 475 | For example, 2004 begins on a Thursday, so the first week of ISO |
| 476 | year 2004 begins on Monday, 29 Dec 2003 and ends on Sunday, 4 Jan |
| 477 | 2004, so that |
Andrew M. Kuchling | 570e358 | 2003-02-05 21:15:38 +0000 | [diff] [blame] | 478 | \code{date(2003, 12, 29).isocalendar() == (2004, 1, 1)} |
| 479 | and |
| 480 | \code{date(2004, 1, 4).isocalendar() == (2004, 1, 7)}. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 481 | \end{methoddesc} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 482 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 483 | \begin{methoddesc}{isoformat}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 484 | Return a string representing the date in ISO 8601 format, |
| 485 | 'YYYY-MM-DD'. For example, |
Andrew M. Kuchling | 570e358 | 2003-02-05 21:15:38 +0000 | [diff] [blame] | 486 | \code{date(2002, 12, 4).isoformat() == '2002-12-04'}. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 487 | \end{methoddesc} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 488 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 489 | \begin{methoddesc}{__str__}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 490 | For a date \var{d}, \code{str(\var{d})} is equivalent to |
| 491 | \code{\var{d}.isoformat()}. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 492 | \end{methoddesc} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 493 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 494 | \begin{methoddesc}{ctime}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 495 | Return a string representing the date, for example |
| 496 | date(2002, 12, 4).ctime() == 'Wed Dec 4 00:00:00 2002'. |
| 497 | \code{\var{d}.ctime()} is equivalent to |
| 498 | \code{time.ctime(time.mktime(\var{d}.timetuple()))} |
| 499 | on platforms where the native C \cfunction{ctime()} function |
| 500 | (which \function{time.ctime()} invokes, but which |
| 501 | \method{date.ctime()} does not invoke) conforms to the C standard. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 502 | \end{methoddesc} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 503 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 504 | \begin{methoddesc}{strftime}{format} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 505 | Return a string representing the date, controlled by an explicit |
| 506 | format string. Format codes referring to hours, minutes or seconds |
| 507 | will see 0 values. |
| 508 | See the section on \method{strftime()} behavior. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 509 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 510 | |
| 511 | |
Raymond Hettinger | 6005a34 | 2002-12-30 20:01:24 +0000 | [diff] [blame] | 512 | \subsection{\class{datetime} Objects \label{datetime-datetime}} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 513 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 514 | A \class{datetime} object is a single object containing all the |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 515 | information from a \class{date} object and a \class{time} object. Like a |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 516 | \class{date} object, \class{datetime} assumes the current Gregorian |
| 517 | calendar extended in both directions; like a time object, |
| 518 | \class{datetime} assumes there are exactly 3600*24 seconds in every |
| 519 | day. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 520 | |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 521 | Constructor: |
| 522 | |
Fred Drake | 0f8e543 | 2002-12-31 18:31:48 +0000 | [diff] [blame] | 523 | \begin{classdesc}{datetime}{year, month, day, |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 524 | hour=0, minute=0, second=0, microsecond=0, |
| 525 | tzinfo=None} |
| 526 | The year, month and day arguments are required. \var{tzinfo} may |
| 527 | be \code{None}, or an instance of a \class{tzinfo} subclass. The |
| 528 | remaining arguments may be ints or longs, in the following ranges: |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 529 | |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 530 | \begin{itemize} |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 531 | \item \code{MINYEAR <= \var{year} <= MAXYEAR} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 532 | \item \code{1 <= \var{month} <= 12} |
| 533 | \item \code{1 <= \var{day} <= number of days in the given month and year} |
| 534 | \item \code{0 <= \var{hour} < 24} |
| 535 | \item \code{0 <= \var{minute} < 60} |
| 536 | \item \code{0 <= \var{second} < 60} |
| 537 | \item \code{0 <= \var{microsecond} < 1000000} |
| 538 | \end{itemize} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 539 | |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 540 | If an argument outside those ranges is given, |
| 541 | \exception{ValueError} is raised. |
Fred Drake | 0f8e543 | 2002-12-31 18:31:48 +0000 | [diff] [blame] | 542 | \end{classdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 543 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 544 | Other constructors, all class methods: |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 545 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 546 | \begin{methoddesc}{today}{} |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 547 | Return the current local datetime, with \member{tzinfo} \code{None}. |
| 548 | This is equivalent to |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 549 | \code{datetime.fromtimestamp(time.time())}. |
| 550 | See also \method{now()}, \method{fromtimestamp()}. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 551 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 552 | |
Tim Peters | 10cadce | 2003-01-23 19:58:02 +0000 | [diff] [blame] | 553 | \begin{methoddesc}{now(tz=None)}{} |
| 554 | Return the current local date and time. If optional argument |
| 555 | \var{tz} is \code{None} or not specified, this is like |
| 556 | \method{today()}, but, if possible, supplies more precision than can |
| 557 | be gotten from going through a \function{time.time()} timestamp (for |
| 558 | example, this may be possible on platforms supplying the C |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 559 | \cfunction{gettimeofday()} function). |
Tim Peters | 10cadce | 2003-01-23 19:58:02 +0000 | [diff] [blame] | 560 | |
| 561 | Else \var{tz} must be an instance of a class \class{tzinfo} subclass, |
Tim Peters | 2a44a8d | 2003-01-23 20:53:10 +0000 | [diff] [blame] | 562 | and the current date and time are converted to \var{tz}'s time |
Tim Peters | 10cadce | 2003-01-23 19:58:02 +0000 | [diff] [blame] | 563 | zone. In this case the result is equivalent to |
Tim Peters | 2a44a8d | 2003-01-23 20:53:10 +0000 | [diff] [blame] | 564 | \code{\var{tz}.fromutc(datetime.utcnow().replace(tzinfo=\var{tz}))}. |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 565 | See also \method{today()}, \method{utcnow()}. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 566 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 567 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 568 | \begin{methoddesc}{utcnow}{} |
Tim Peters | 10cadce | 2003-01-23 19:58:02 +0000 | [diff] [blame] | 569 | Return the current UTC date and time, with \member{tzinfo} \code{None}. |
Tim Peters | 2a44a8d | 2003-01-23 20:53:10 +0000 | [diff] [blame] | 570 | This is like \method{now()}, but returns the current UTC date and time, |
Tim Peters | 10cadce | 2003-01-23 19:58:02 +0000 | [diff] [blame] | 571 | as a naive \class{datetime} object. |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 572 | See also \method{now()}. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 573 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 574 | |
Tim Peters | 2a44a8d | 2003-01-23 20:53:10 +0000 | [diff] [blame] | 575 | \begin{methoddesc}{fromtimestamp}{timestamp, tz=None} |
| 576 | Return the local date and time corresponding to the \POSIX{} |
| 577 | timestamp, such as is returned by \function{time.time()}. |
| 578 | If optional argument \var{tz} is \code{None} or not specified, the |
| 579 | timestamp is converted to the platform's local date and time, and |
| 580 | the returned \class{datetime} object is naive. |
| 581 | |
| 582 | Else \var{tz} must be an instance of a class \class{tzinfo} subclass, |
| 583 | and the timestamp is converted to \var{tz}'s time zone. In this case |
| 584 | the result is equivalent to |
| 585 | \code{\var{tz}.fromutc(datetime.utcfromtimestamp(\var{timestamp}).replace(tzinfo=\var{tz}))}. |
| 586 | |
| 587 | \method{fromtimestamp()} may raise \exception{ValueError}, if the |
| 588 | timestamp is out of the range of values supported by the platform C |
Neal Norwitz | daae327 | 2003-01-25 21:08:30 +0000 | [diff] [blame] | 589 | \cfunction{localtime()} or \cfunction{gmtime()} functions. It's common |
Tim Peters | 2a44a8d | 2003-01-23 20:53:10 +0000 | [diff] [blame] | 590 | for this to be restricted to years in 1970 through 2038. |
Tim Peters | 75a6e3b | 2003-01-04 18:17:36 +0000 | [diff] [blame] | 591 | Note that on non-POSIX systems that include leap seconds in their |
| 592 | notion of a timestamp, leap seconds are ignored by |
| 593 | \method{fromtimestamp()}, and then it's possible to have two timestamps |
| 594 | differing by a second that yield identical \class{datetime} objects. |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 595 | See also \method{utcfromtimestamp()}. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 596 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 597 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 598 | \begin{methoddesc}{utcfromtimestamp}{timestamp} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 599 | Return the UTC \class{datetime} corresponding to the \POSIX{} |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 600 | timestamp, with \member{tzinfo} \code{None}. |
| 601 | This may raise \exception{ValueError}, if the |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 602 | timestamp is out of the range of values supported by the platform |
| 603 | C \cfunction{gmtime()} function. It's common for this to be |
| 604 | restricted to years in 1970 through 2038. |
| 605 | See also \method{fromtimestamp()}. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 606 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 607 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 608 | \begin{methoddesc}{fromordinal}{ordinal} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 609 | Return the \class{datetime} corresponding to the proleptic |
| 610 | Gregorian ordinal, where January 1 of year 1 has ordinal 1. |
Andrew M. Kuchling | 570e358 | 2003-02-05 21:15:38 +0000 | [diff] [blame] | 611 | \exception{ValueError} is raised unless \code{1 <= ordinal <= |
| 612 | datetime.max.toordinal()}. The hour, minute, second and |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 613 | microsecond of the result are all 0, |
| 614 | and \member{tzinfo} is \code{None}. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 615 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 616 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 617 | \begin{methoddesc}{combine}{date, time} |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 618 | Return a new \class{datetime} object whose date members are |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 619 | equal to the given \class{date} object's, and whose time |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 620 | and \member{tzinfo} members are equal to the given \class{time} object's. |
| 621 | For any \class{datetime} object \var{d}, \code{\var{d} == |
| 622 | datetime.combine(\var{d}.date(), \var{d}.timetz())}. If date is a |
| 623 | \class{datetime} object, its time and \member{tzinfo} members are |
| 624 | ignored. |
| 625 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 626 | |
| 627 | Class attributes: |
| 628 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 629 | \begin{memberdesc}{min} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 630 | The earliest representable \class{datetime}, |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 631 | \code{datetime(MINYEAR, 1, 1, tzinfo=None)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 632 | \end{memberdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 633 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 634 | \begin{memberdesc}{max} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 635 | The latest representable \class{datetime}, |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 636 | \code{datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999, tzinfo=None)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 637 | \end{memberdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 638 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 639 | \begin{memberdesc}{resolution} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 640 | The smallest possible difference between non-equal \class{datetime} |
| 641 | objects, \code{timedelta(microseconds=1)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 642 | \end{memberdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 643 | |
| 644 | Instance attributes (read-only): |
| 645 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 646 | \begin{memberdesc}{year} |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 647 | Between \constant{MINYEAR} and \constant{MAXYEAR} inclusive. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 648 | \end{memberdesc} |
| 649 | |
| 650 | \begin{memberdesc}{month} |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 651 | Between 1 and 12 inclusive. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 652 | \end{memberdesc} |
| 653 | |
| 654 | \begin{memberdesc}{day} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 655 | Between 1 and the number of days in the given month of the given |
| 656 | year. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 657 | \end{memberdesc} |
| 658 | |
| 659 | \begin{memberdesc}{hour} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 660 | In \code{range(24)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 661 | \end{memberdesc} |
| 662 | |
| 663 | \begin{memberdesc}{minute} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 664 | In \code{range(60)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 665 | \end{memberdesc} |
| 666 | |
| 667 | \begin{memberdesc}{second} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 668 | In \code{range(60)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 669 | \end{memberdesc} |
| 670 | |
| 671 | \begin{memberdesc}{microsecond} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 672 | In \code{range(1000000)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 673 | \end{memberdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 674 | |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 675 | \begin{memberdesc}{tzinfo} |
| 676 | The object passed as the \var{tzinfo} argument to the |
| 677 | \class{datetime} constructor, or \code{None} if none was passed. |
| 678 | \end{memberdesc} |
| 679 | |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 680 | Supported operations: |
| 681 | |
Andrew M. Kuchling | 570e358 | 2003-02-05 21:15:38 +0000 | [diff] [blame] | 682 | \begin{tableii}{c|l}{code}{Operation}{Result} |
| 683 | \lineii{\var{datetime2} = \var{datetime1} + \var{timedelta}}{(1)} |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 684 | |
Andrew M. Kuchling | 570e358 | 2003-02-05 21:15:38 +0000 | [diff] [blame] | 685 | \lineii{\var{datetime2} = \var{datetime1} - \var{timedelta}}{(2)} |
| 686 | |
| 687 | \lineii{\var{timedelta} = \var{datetime1} - \var{datetime2}}{(3)} |
| 688 | |
| 689 | \lineii{\var{datetime1} < \var{datetime2}} |
| 690 | {Compares \class{datetime} to \class{datetime}. |
| 691 | (4)} |
| 692 | |
| 693 | \end{tableii} |
| 694 | |
| 695 | \begin{description} |
| 696 | |
| 697 | \item[(1)] |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 698 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 699 | datetime2 is a duration of timedelta removed from datetime1, moving |
Andrew M. Kuchling | 570e358 | 2003-02-05 21:15:38 +0000 | [diff] [blame] | 700 | forward in time if \code{\var{timedelta}.days} > 0, or backward if |
| 701 | \code{\var{timedelta}.days} < 0. The result has the same \member{tzinfo} member |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 702 | as the input datetime, and datetime2 - datetime1 == timedelta after. |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 703 | \exception{OverflowError} is raised if datetime2.year would be |
| 704 | smaller than \constant{MINYEAR} or larger than \constant{MAXYEAR}. |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 705 | Note that no time zone adjustments are done even if the input is an |
| 706 | aware object. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 707 | |
Andrew M. Kuchling | 570e358 | 2003-02-05 21:15:38 +0000 | [diff] [blame] | 708 | \item[(2)] |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 709 | Computes the datetime2 such that datetime2 + timedelta == datetime1. |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 710 | As for addition, the result has the same \member{tzinfo} member |
| 711 | as the input datetime, and no time zone adjustments are done even |
| 712 | if the input is aware. |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 713 | This isn't quite equivalent to datetime1 + (-timedelta), because |
| 714 | -timedelta in isolation can overflow in cases where |
| 715 | datetime1 - timedelta does not. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 716 | |
Andrew M. Kuchling | 570e358 | 2003-02-05 21:15:38 +0000 | [diff] [blame] | 717 | \item[(3)] |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 718 | Subtraction of a \class{datetime} from a |
| 719 | \class{datetime} is defined only if both |
| 720 | operands are naive, or if both are aware. If one is aware and the |
| 721 | other is naive, \exception{TypeError} is raised. |
| 722 | |
| 723 | If both are naive, or both are aware and have the same \member{tzinfo} |
| 724 | member, the \member{tzinfo} members are ignored, and the result is |
| 725 | a \class{timedelta} object \var{t} such that |
| 726 | \code{\var{datetime2} + \var{t} == \var{datetime1}}. No time zone |
| 727 | adjustments are done in this case. |
| 728 | |
| 729 | If both are aware and have different \member{tzinfo} members, |
| 730 | \code{a-b} acts as if \var{a} and \var{b} were first converted to |
| 731 | naive UTC datetimes first. The result is |
| 732 | \code{(\var{a}.replace(tzinfo=None) - \var{a}.utcoffset()) - |
| 733 | (\var{b}.replace(tzinfo=None) - \var{b}.utcoffset())} |
| 734 | except that the implementation never overflows. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 735 | |
Andrew M. Kuchling | 570e358 | 2003-02-05 21:15:38 +0000 | [diff] [blame] | 736 | \item[(4)] |
| 737 | |
| 738 | \var{datetime1} is considered less than \var{datetime2} |
| 739 | when \var{datetime1} precedes \var{datetime2} in time. |
| 740 | |
| 741 | If one comparand is naive and |
| 742 | the other is aware, \exception{TypeError} is raised. If both |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 743 | comparands are aware, and have the same \member{tzinfo} member, |
| 744 | the common \member{tzinfo} member is ignored and the base datetimes |
| 745 | are compared. If both comparands are aware and have different |
| 746 | \member{tzinfo} members, the comparands are first adjusted by |
| 747 | subtracting their UTC offsets (obtained from \code{self.utcoffset()}). |
Tim Peters | 8d81a01 | 2003-01-24 22:36:34 +0000 | [diff] [blame] | 748 | \note{In order to stop comparison from falling back to the default |
| 749 | scheme of comparing object addresses, datetime comparison |
| 750 | normally raises \exception{TypeError} if the other comparand |
| 751 | isn't also a \class{datetime} object. However, |
| 752 | \code{NotImplemented} is returned instead if the other comparand |
| 753 | has a \method{timetuple} attribute. This hook gives other |
| 754 | kinds of date objects a chance at implementing mixed-type |
Tim Peters | 07534a6 | 2003-02-07 22:50:28 +0000 | [diff] [blame] | 755 | comparison. If not, when a \class{datetime} object is |
| 756 | compared to an object of a different type, \exception{TypeError} |
| 757 | is raised unless the comparison is \code{==} or \code{!=}. The |
| 758 | latter cases return \constant{False} or \constant{True}, |
| 759 | respectively.} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 760 | |
Andrew M. Kuchling | 570e358 | 2003-02-05 21:15:38 +0000 | [diff] [blame] | 761 | \end{description} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 762 | |
Andrew M. Kuchling | 570e358 | 2003-02-05 21:15:38 +0000 | [diff] [blame] | 763 | \class{datetime} objects can be used as dictionary keys. In Boolean |
| 764 | contexts, all \class{datetime} objects are considered to be true. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 765 | |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 766 | |
| 767 | Instance methods: |
| 768 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 769 | \begin{methoddesc}{date}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 770 | Return \class{date} object with same year, month and day. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 771 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 772 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 773 | \begin{methoddesc}{time}{} |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 774 | Return \class{time} object with same hour, minute, second and microsecond. |
| 775 | \member{tzinfo} is \code{None}. See also method \method{timetz()}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 776 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 777 | |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 778 | \begin{methoddesc}{timetz}{} |
| 779 | Return \class{time} object with same hour, minute, second, microsecond, |
| 780 | and tzinfo members. See also method \method{time()}. |
| 781 | \end{methoddesc} |
| 782 | |
| 783 | \begin{methoddesc}{replace}{year=, month=, day=, hour=, minute=, second=, |
| 784 | microsecond=, tzinfo=} |
| 785 | Return a datetime with the same members, except for those members given |
| 786 | new values by whichever keyword arguments are specified. Note that |
| 787 | \code{tzinfo=None} can be specified to create a naive datetime from |
| 788 | an aware datetime with no conversion of date and time members. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 789 | \end{methoddesc} |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 790 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 791 | \begin{methoddesc}{astimezone}{tz} |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 792 | Return a \class{datetime} object with new \member{tzinfo} member |
Tim Peters | f196a0a | 2003-01-22 04:45:50 +0000 | [diff] [blame] | 793 | \var{tz}, adjusting the date and time members so the result is the |
| 794 | same UTC time as \var{self}, but in \var{tz}'s local time. |
| 795 | |
| 796 | \var{tz} must be an instance of a \class{tzinfo} subclass, and its |
| 797 | \method{utcoffset()} and \method{dst()} methods must not return |
| 798 | \code{None}. \var{self} must be aware (\code{\var{self}.tzinfo} must |
| 799 | not be \code{None}, and \code{\var{self}.utcoffset()} must not return |
| 800 | \code{None}). |
| 801 | |
Andrew M. Kuchling | 570e358 | 2003-02-05 21:15:38 +0000 | [diff] [blame] | 802 | If \code{\var{self}.tzinfo} is \var{tz}, |
Tim Peters | f196a0a | 2003-01-22 04:45:50 +0000 | [diff] [blame] | 803 | \code{\var{self}.astimezone(\var{tz})} is equal to \var{self}: no |
| 804 | adjustment of date or time members is performed. |
| 805 | Else the result is local time in time zone \var{tz}, representing the |
| 806 | same UTC time as \var{self}: after \code{\var{astz} = |
| 807 | \var{dt}.astimezone(\var{tz})}, |
| 808 | \code{\var{astz} - \var{astz}.utcoffset()} will usually have the same |
| 809 | date and time members as \code{\var{dt} - \var{dt}.utcoffset()}. |
| 810 | The discussion of class \class{tzinfo} explains the cases at Daylight |
| 811 | Saving Time transition boundaries where this cannot be achieved (an issue |
| 812 | only if \var{tz} models both standard and daylight time). |
| 813 | |
| 814 | If you merely want to attach a time zone object \var{tz} to a |
| 815 | datetime \var{dt} without adjustment of date and time members, |
| 816 | use \code{\var{dt}.replace(tzinfo=\var{tz})}. If |
| 817 | you merely want to remove the time zone object from an aware datetime |
| 818 | \var{dt} without conversion of date and time members, use |
| 819 | \code{\var{dt}.replace(tzinfo=None)}. |
| 820 | |
| 821 | Note that the default \method{tzinfo.fromutc()} method can be overridden |
Raymond Hettinger | 1c90a67 | 2003-09-06 05:36:13 +0000 | [diff] [blame] | 822 | in a \class{tzinfo} subclass to affect the result returned by |
Tim Peters | f196a0a | 2003-01-22 04:45:50 +0000 | [diff] [blame] | 823 | \method{astimezone()}. Ignoring error cases, \method{astimezone()} |
| 824 | acts like: |
| 825 | |
| 826 | \begin{verbatim} |
| 827 | def astimezone(self, tz): |
| 828 | if self.tzinfo is tz: |
| 829 | return self |
| 830 | # Convert self to UTC, and attach the new time zone object. |
| 831 | utc = (self - self.utcoffset()).replace(tzinfo=tz) |
| 832 | # Convert from UTC to tz's local time. |
| 833 | return tz.fromutc(utc) |
| 834 | \end{verbatim} |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 835 | \end{methoddesc} |
| 836 | |
| 837 | \begin{methoddesc}{utcoffset}{} |
| 838 | If \member{tzinfo} is \code{None}, returns \code{None}, else |
Tim Peters | f196a0a | 2003-01-22 04:45:50 +0000 | [diff] [blame] | 839 | returns \code{\var{self}.tzinfo.utcoffset(\var{self})}, and |
| 840 | raises an exception if the latter doesn't return \code{None}, or |
| 841 | a \class{timedelta} object representing a whole number of minutes |
| 842 | with magnitude less than one day. |
| 843 | \end{methoddesc} |
| 844 | |
| 845 | \begin{methoddesc}{dst}{} |
| 846 | If \member{tzinfo} is \code{None}, returns \code{None}, else |
| 847 | returns \code{\var{self}.tzinfo.dst(\var{self})}, and |
| 848 | raises an exception if the latter doesn't return \code{None}, or |
| 849 | a \class{timedelta} object representing a whole number of minutes |
| 850 | with magnitude less than one day. |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 851 | \end{methoddesc} |
| 852 | |
| 853 | \begin{methoddesc}{tzname}{} |
| 854 | If \member{tzinfo} is \code{None}, returns \code{None}, else |
Tim Peters | f196a0a | 2003-01-22 04:45:50 +0000 | [diff] [blame] | 855 | returns \code{\var{self}.tzinfo.tzname(\var{self})}, |
| 856 | raises an exception if the latter doesn't return \code{None} or |
| 857 | a string object, |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 858 | \end{methoddesc} |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 859 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 860 | \begin{methoddesc}{timetuple}{} |
Martin v. Löwis | ef36537 | 2003-09-04 18:29:53 +0000 | [diff] [blame] | 861 | Return a \class{time.struct_time} such as returned by |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 862 | \function{time.localtime()}. |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 863 | \code{\var{d}.timetuple()} is equivalent to |
Martin v. Löwis | ef36537 | 2003-09-04 18:29:53 +0000 | [diff] [blame] | 864 | \code{time.struct_time((\var{d}.year, \var{d}.month, \var{d}.day, |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 865 | \var{d}.hour, \var{d}.minute, \var{d}.second, |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 866 | \var{d}.weekday(), |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 867 | \var{d}.toordinal() - date(\var{d}.year, 1, 1).toordinal() + 1, |
Martin v. Löwis | ef36537 | 2003-09-04 18:29:53 +0000 | [diff] [blame] | 868 | dst))} |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 869 | The \member{tm_isdst} flag of the result is set according to |
| 870 | the \method{dst()} method: \member{tzinfo} is \code{None} or |
| 871 | \method{dst()} returns \code{None}, |
| 872 | \member{tm_isdst} is set to \code{-1}; else if \method{dst()} returns |
| 873 | a non-zero value, \member{tm_isdst} is set to \code{1}; |
| 874 | else \code{tm_isdst} is set to \code{0}. |
| 875 | \end{methoddesc} |
| 876 | |
| 877 | \begin{methoddesc}{utctimetuple}{} |
| 878 | If \class{datetime} instance \var{d} is naive, this is the same as |
| 879 | \code{\var{d}.timetuple()} except that \member{tm_isdst} is forced to 0 |
| 880 | regardless of what \code{d.dst()} returns. DST is never in effect |
| 881 | for a UTC time. |
| 882 | |
| 883 | If \var{d} is aware, \var{d} is normalized to UTC time, by subtracting |
Martin v. Löwis | ef36537 | 2003-09-04 18:29:53 +0000 | [diff] [blame] | 884 | \code{\var{d}.utcoffset()}, and a \class{time.struct_time} for the |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 885 | normalized time is returned. \member{tm_isdst} is forced to 0. |
| 886 | Note that the result's \member{tm_year} member may be |
| 887 | \constant{MINYEAR}-1 or \constant{MAXYEAR}+1, if \var{d}.year was |
| 888 | \code{MINYEAR} or \code{MAXYEAR} and UTC adjustment spills over a |
| 889 | year boundary. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 890 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 891 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 892 | \begin{methoddesc}{toordinal}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 893 | Return the proleptic Gregorian ordinal of the date. The same as |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 894 | \code{self.date().toordinal()}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 895 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 896 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 897 | \begin{methoddesc}{weekday}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 898 | Return the day of the week as an integer, where Monday is 0 and |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 899 | Sunday is 6. The same as \code{self.date().weekday()}. |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 900 | See also \method{isoweekday()}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 901 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 902 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 903 | \begin{methoddesc}{isoweekday}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 904 | Return the day of the week as an integer, where Monday is 1 and |
Raymond Hettinger | eca984f | 2003-05-10 04:21:08 +0000 | [diff] [blame] | 905 | Sunday is 7. The same as \code{self.date().isoweekday()}. |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 906 | See also \method{weekday()}, \method{isocalendar()}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 907 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 908 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 909 | \begin{methoddesc}{isocalendar}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 910 | Return a 3-tuple, (ISO year, ISO week number, ISO weekday). The |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 911 | same as \code{self.date().isocalendar()}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 912 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 913 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 914 | \begin{methoddesc}{isoformat}{sep='T'} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 915 | Return a string representing the date and time in ISO 8601 format, |
| 916 | YYYY-MM-DDTHH:MM:SS.mmmmmm |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 917 | or, if \member{microsecond} is 0, |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 918 | YYYY-MM-DDTHH:MM:SS |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 919 | |
| 920 | If \method{utcoffset()} does not return \code{None}, a 6-character |
| 921 | string is appended, giving the UTC offset in (signed) hours and |
| 922 | minutes: |
| 923 | YYYY-MM-DDTHH:MM:SS.mmmmmm+HH:MM |
| 924 | or, if \member{microsecond} is 0 |
| 925 | YYYY-MM-DDTHH:MM:SS+HH:MM |
| 926 | |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 927 | The optional argument \var{sep} (default \code{'T'}) is a |
| 928 | one-character separator, placed between the date and time portions |
| 929 | of the result. For example, |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 930 | |
| 931 | \begin{verbatim} |
| 932 | >>> from datetime import tzinfo, timedelta, datetime |
| 933 | >>> class TZ(tzinfo): |
| 934 | ... def utcoffset(self, dt): return timedelta(minutes=-399) |
| 935 | ... |
| 936 | >>> datetime(2002, 12, 25, tzinfo=TZ()).isoformat(' ') |
| 937 | '2002-12-25 00:00:00-06:39' |
| 938 | \end{verbatim} |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 939 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 940 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 941 | \begin{methoddesc}{__str__}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 942 | For a \class{datetime} instance \var{d}, \code{str(\var{d})} is |
| 943 | equivalent to \code{\var{d}.isoformat(' ')}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 944 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 945 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 946 | \begin{methoddesc}{ctime}{} |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 947 | Return a string representing the date and time, for example |
| 948 | \code{datetime(2002, 12, 4, 20, 30, 40).ctime() == |
| 949 | 'Wed Dec 4 20:30:40 2002'}. |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 950 | \code{d.ctime()} is equivalent to |
| 951 | \code{time.ctime(time.mktime(d.timetuple()))} on platforms where |
| 952 | the native C \cfunction{ctime()} function (which |
| 953 | \function{time.ctime()} invokes, but which |
| 954 | \method{datetime.ctime()} does not invoke) conforms to the C |
| 955 | standard. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 956 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 957 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 958 | \begin{methoddesc}{strftime}{format} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 959 | Return a string representing the date and time, controlled by an |
| 960 | explicit format string. See the section on \method{strftime()} |
| 961 | behavior. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 962 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 963 | |
| 964 | |
Raymond Hettinger | 6005a34 | 2002-12-30 20:01:24 +0000 | [diff] [blame] | 965 | \subsection{\class{time} Objects \label{datetime-time}} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 966 | |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 967 | A time object represents a (local) time of day, independent of any |
| 968 | particular day, and subject to adjustment via a \class{tzinfo} object. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 969 | |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 970 | \begin{classdesc}{time}{hour=0, minute=0, second=0, microsecond=0, |
| 971 | tzinfo=None} |
| 972 | All arguments are optional. \var{tzinfo} may be \code{None}, or |
| 973 | an instance of a \class{tzinfo} subclass. The remaining arguments |
| 974 | may be ints or longs, in the following ranges: |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 975 | |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 976 | \begin{itemize} |
| 977 | \item \code{0 <= \var{hour} < 24} |
| 978 | \item \code{0 <= \var{minute} < 60} |
| 979 | \item \code{0 <= \var{second} < 60} |
| 980 | \item \code{0 <= \var{microsecond} < 1000000}. |
| 981 | \end{itemize} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 982 | |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 983 | If an argument outside those ranges is given, |
| 984 | \exception{ValueError} is raised. |
Fred Drake | 0f8e543 | 2002-12-31 18:31:48 +0000 | [diff] [blame] | 985 | \end{classdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 986 | |
| 987 | Class attributes: |
| 988 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 989 | \begin{memberdesc}{min} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 990 | The earliest representable \class{time}, \code{time(0, 0, 0, 0)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 991 | \end{memberdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 992 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 993 | \begin{memberdesc}{max} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 994 | The latest representable \class{time}, \code{time(23, 59, 59, 999999)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 995 | \end{memberdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 996 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 997 | \begin{memberdesc}{resolution} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 998 | The smallest possible difference between non-equal \class{time} |
| 999 | objects, \code{timedelta(microseconds=1)}, although note that |
| 1000 | arithmetic on \class{time} objects is not supported. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1001 | \end{memberdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1002 | |
| 1003 | Instance attributes (read-only): |
| 1004 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1005 | \begin{memberdesc}{hour} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1006 | In \code{range(24)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1007 | \end{memberdesc} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1008 | |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1009 | \begin{memberdesc}{minute} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1010 | In \code{range(60)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1011 | \end{memberdesc} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1012 | |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1013 | \begin{memberdesc}{second} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1014 | In \code{range(60)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1015 | \end{memberdesc} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1016 | |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1017 | \begin{memberdesc}{microsecond} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1018 | In \code{range(1000000)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1019 | \end{memberdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1020 | |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 1021 | \begin{memberdesc}{tzinfo} |
| 1022 | The object passed as the tzinfo argument to the \class{time} |
| 1023 | constructor, or \code{None} if none was passed. |
| 1024 | \end{memberdesc} |
| 1025 | |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1026 | Supported operations: |
| 1027 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 1028 | \begin{itemize} |
| 1029 | \item |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 1030 | comparison of \class{time} to \class{time}, |
| 1031 | where \var{a} is considered less than \var{b} when \var{a} precedes |
| 1032 | \var{b} in time. If one comparand is naive and the other is aware, |
| 1033 | \exception{TypeError} is raised. If both comparands are aware, and |
| 1034 | have the same \member{tzinfo} member, the common \member{tzinfo} |
| 1035 | member is ignored and the base times are compared. If both |
| 1036 | comparands are aware and have different \member{tzinfo} members, |
| 1037 | the comparands are first adjusted by subtracting their UTC offsets |
| 1038 | (obtained from \code{self.utcoffset()}). |
Tim Peters | 07534a6 | 2003-02-07 22:50:28 +0000 | [diff] [blame] | 1039 | In order to stop mixed-type comparisons from falling back to the |
| 1040 | default comparison by object address, when a \class{time} object is |
| 1041 | compared to an object of a different type, \exception{TypeError} is |
| 1042 | raised unless the comparison is \code{==} or \code{!=}. The latter |
| 1043 | cases return \constant{False} or \constant{True}, respectively. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1044 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 1045 | \item |
| 1046 | hash, use as dict key |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1047 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 1048 | \item |
| 1049 | efficient pickling |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1050 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 1051 | \item |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 1052 | in Boolean contexts, a \class{time} object is considered to be |
| 1053 | true if and only if, after converting it to minutes and |
| 1054 | subtracting \method{utcoffset()} (or \code{0} if that's |
| 1055 | \code{None}), the result is non-zero. |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 1056 | \end{itemize} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1057 | |
| 1058 | Instance methods: |
| 1059 | |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 1060 | \begin{methoddesc}{replace}(hour=, minute=, second=, microsecond=, tzinfo=) |
| 1061 | Return a \class{time} with the same value, except for those members given |
| 1062 | new values by whichever keyword arguments are specified. Note that |
| 1063 | \code{tzinfo=None} can be specified to create a naive \class{time} from |
| 1064 | an aware \class{time}, without conversion of the time members. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1065 | \end{methoddesc} |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 1066 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1067 | \begin{methoddesc}{isoformat}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1068 | Return a string representing the time in ISO 8601 format, |
| 1069 | HH:MM:SS.mmmmmm |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 1070 | or, if self.microsecond is 0, |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1071 | HH:MM:SS |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 1072 | If \method{utcoffset()} does not return \code{None}, a 6-character |
| 1073 | string is appended, giving the UTC offset in (signed) hours and |
| 1074 | minutes: |
| 1075 | HH:MM:SS.mmmmmm+HH:MM |
| 1076 | or, if self.microsecond is 0, |
| 1077 | HH:MM:SS+HH:MM |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1078 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1079 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1080 | \begin{methoddesc}{__str__}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1081 | For a time \var{t}, \code{str(\var{t})} is equivalent to |
| 1082 | \code{\var{t}.isoformat()}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1083 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1084 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1085 | \begin{methoddesc}{strftime}{format} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1086 | Return a string representing the time, controlled by an explicit |
| 1087 | format string. See the section on \method{strftime()} behavior. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1088 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1089 | |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 1090 | \begin{methoddesc}{utcoffset}{} |
| 1091 | If \member{tzinfo} is \code{None}, returns \code{None}, else |
Tim Peters | f196a0a | 2003-01-22 04:45:50 +0000 | [diff] [blame] | 1092 | returns \code{\var{self}.tzinfo.utcoffset(None)}, and |
| 1093 | raises an exception if the latter doesn't return \code{None} or |
| 1094 | a \class{timedelta} object representing a whole number of minutes |
| 1095 | with magnitude less than one day. |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 1096 | \end{methoddesc} |
| 1097 | |
| 1098 | \begin{methoddesc}{dst}{} |
| 1099 | If \member{tzinfo} is \code{None}, returns \code{None}, else |
Tim Peters | f196a0a | 2003-01-22 04:45:50 +0000 | [diff] [blame] | 1100 | returns \code{\var{self}.tzinfo.dst(None)}, and |
| 1101 | raises an exception if the latter doesn't return \code{None}, or |
| 1102 | a \class{timedelta} object representing a whole number of minutes |
| 1103 | with magnitude less than one day. |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 1104 | \end{methoddesc} |
| 1105 | |
| 1106 | \begin{methoddesc}{tzname}{} |
| 1107 | If \member{tzinfo} is \code{None}, returns \code{None}, else |
Tim Peters | f196a0a | 2003-01-22 04:45:50 +0000 | [diff] [blame] | 1108 | returns \code{\var{self}.tzinfo.tzname(None)}, or |
| 1109 | raises an exception if the latter doesn't return \code{None} or |
| 1110 | a string object. |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 1111 | \end{methoddesc} |
| 1112 | |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1113 | |
Raymond Hettinger | 6005a34 | 2002-12-30 20:01:24 +0000 | [diff] [blame] | 1114 | \subsection{\class{tzinfo} Objects \label{datetime-tzinfo}} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1115 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 1116 | \class{tzinfo} is an abstract base clase, meaning that this class |
| 1117 | should not be instantiated directly. You need to derive a concrete |
| 1118 | subclass, and (at least) supply implementations of the standard |
| 1119 | \class{tzinfo} methods needed by the \class{datetime} methods you |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1120 | use. The \module{datetime} module does not supply any concrete |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 1121 | subclasses of \class{tzinfo}. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1122 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 1123 | An instance of (a concrete subclass of) \class{tzinfo} can be passed |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 1124 | to the constructors for \class{datetime} and \class{time} objects. |
| 1125 | The latter objects view their members as being in local time, and the |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 1126 | \class{tzinfo} object supports methods revealing offset of local time |
| 1127 | from UTC, the name of the time zone, and DST offset, all relative to a |
| 1128 | date or time object passed to them. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1129 | |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1130 | Special requirement for pickling: A \class{tzinfo} subclass must have an |
Tim Peters | 2483b61 | 2002-12-24 16:30:58 +0000 | [diff] [blame] | 1131 | \method{__init__} method that can be called with no arguments, else it |
| 1132 | can be pickled but possibly not unpickled again. This is a technical |
| 1133 | requirement that may be relaxed in the future. |
| 1134 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 1135 | A concrete subclass of \class{tzinfo} may need to implement the |
| 1136 | following methods. Exactly which methods are needed depends on the |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1137 | uses made of aware \module{datetime} objects. If in doubt, simply |
| 1138 | implement all of them. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1139 | |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1140 | \begin{methoddesc}{utcoffset}{self, dt} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1141 | Return offset of local time from UTC, in minutes east of UTC. If |
| 1142 | local time is west of UTC, this should be negative. Note that this |
| 1143 | is intended to be the total offset from UTC; for example, if a |
| 1144 | \class{tzinfo} object represents both time zone and DST adjustments, |
| 1145 | \method{utcoffset()} should return their sum. If the UTC offset |
| 1146 | isn't known, return \code{None}. Else the value returned must be |
Tim Peters | 397301e | 2003-01-02 21:28:08 +0000 | [diff] [blame] | 1147 | a \class{timedelta} object specifying a whole number of minutes in the |
| 1148 | range -1439 to 1439 inclusive (1440 = 24*60; the magnitude of the offset |
| 1149 | must be less than one day). Most implementations of |
| 1150 | \method{utcoffset()} will probably look like one of these two: |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1151 | |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1152 | \begin{verbatim} |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 1153 | return CONSTANT # fixed-offset class |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1154 | return CONSTANT + self.dst(dt) # daylight-aware class |
Guido van Rossum | 8e7ec7c | 2002-12-31 04:39:05 +0000 | [diff] [blame] | 1155 | \end{verbatim} |
Tim Peters | 710fb15 | 2003-01-02 19:35:54 +0000 | [diff] [blame] | 1156 | |
| 1157 | If \method{utcoffset()} does not return \code{None}, |
| 1158 | \method{dst()} should not return \code{None} either. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1159 | |
Tim Peters | 327098a | 2003-01-20 22:54:38 +0000 | [diff] [blame] | 1160 | The default implementation of \method{utcoffset()} raises |
| 1161 | \exception{NotImplementedError}. |
| 1162 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1163 | |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1164 | \begin{methoddesc}{dst}{self, dt} |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 1165 | Return the daylight saving time (DST) adjustment, in minutes east of |
Tim Peters | b01c39b | 2003-01-21 16:44:27 +0000 | [diff] [blame] | 1166 | UTC, or \code{None} if DST information isn't known. Return |
| 1167 | \code{timedelta(0)} if DST is not in effect. |
Tim Peters | 397301e | 2003-01-02 21:28:08 +0000 | [diff] [blame] | 1168 | If DST is in effect, return the offset as a |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1169 | \class{timedelta} object (see \method{utcoffset()} for details). |
| 1170 | Note that DST offset, if applicable, has |
| 1171 | already been added to the UTC offset returned by |
| 1172 | \method{utcoffset()}, so there's no need to consult \method{dst()} |
Tim Peters | 327098a | 2003-01-20 22:54:38 +0000 | [diff] [blame] | 1173 | unless you're interested in obtaining DST info separately. For |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 1174 | example, \method{datetime.timetuple()} calls its \member{tzinfo} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1175 | member's \method{dst()} method to determine how the |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 1176 | \member{tm_isdst} flag should be set, and |
Tim Peters | f196a0a | 2003-01-22 04:45:50 +0000 | [diff] [blame] | 1177 | \method{tzinfo.fromutc()} calls \method{dst()} to account for |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 1178 | DST changes when crossing time zones. |
| 1179 | |
| 1180 | An instance \var{tz} of a \class{tzinfo} subclass that models both |
| 1181 | standard and daylight times must be consistent in this sense: |
| 1182 | |
Tim Peters | f196a0a | 2003-01-22 04:45:50 +0000 | [diff] [blame] | 1183 | \code{\var{tz}.utcoffset(\var{dt}) - \var{tz}.dst(\var{dt})} |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 1184 | |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 1185 | must return the same result for every \class{datetime} \var{dt} |
Tim Peters | f196a0a | 2003-01-22 04:45:50 +0000 | [diff] [blame] | 1186 | with \code{\var{dt}.tzinfo==\var{tz}} For sane \class{tzinfo} |
| 1187 | subclasses, this expression yields the time zone's "standard offset", |
| 1188 | which should not depend on the date or the time, but only on geographic |
| 1189 | location. The implementation of \method{datetime.astimezone()} relies |
| 1190 | on this, but cannot detect violations; it's the programmer's |
| 1191 | responsibility to ensure it. If a \class{tzinfo} subclass cannot |
| 1192 | guarantee this, it may be able to override the default implementation |
| 1193 | of \method{tzinfo.fromutc()} to work correctly with \method{astimezone()} |
| 1194 | regardless. |
| 1195 | |
| 1196 | Most implementations of \method{dst()} will probably look like one |
| 1197 | of these two: |
| 1198 | |
| 1199 | \begin{verbatim} |
| 1200 | return timedelta(0) # a fixed-offset class: doesn't account for DST |
| 1201 | |
| 1202 | or |
| 1203 | |
| 1204 | # Code to set dston and dstoff to the time zone's DST transition |
| 1205 | # times based on the input dt.year, and expressed in standard local |
| 1206 | # time. Then |
| 1207 | |
| 1208 | if dston <= dt.replace(tzinfo=None) < dstoff: |
| 1209 | return timedelta(hours=1) |
| 1210 | else: |
| 1211 | return timedelta(0) |
| 1212 | \end{verbatim} |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 1213 | |
Tim Peters | 327098a | 2003-01-20 22:54:38 +0000 | [diff] [blame] | 1214 | The default implementation of \method{dst()} raises |
| 1215 | \exception{NotImplementedError}. |
| 1216 | \end{methoddesc} |
| 1217 | |
Tim Peters | 710fb15 | 2003-01-02 19:35:54 +0000 | [diff] [blame] | 1218 | \begin{methoddesc}{tzname}{self, dt} |
Tim Peters | f196a0a | 2003-01-22 04:45:50 +0000 | [diff] [blame] | 1219 | Return the time zone name corresponding to the \class{datetime} |
| 1220 | object \var{dt}, as a string. |
| 1221 | Nothing about string names is defined by the |
| 1222 | \module{datetime} module, and there's no requirement that it mean |
| 1223 | anything in particular. For example, "GMT", "UTC", "-500", "-5:00", |
| 1224 | "EDT", "US/Eastern", "America/New York" are all valid replies. Return |
Tim Peters | 710fb15 | 2003-01-02 19:35:54 +0000 | [diff] [blame] | 1225 | \code{None} if a string name isn't known. Note that this is a method |
Tim Peters | f196a0a | 2003-01-22 04:45:50 +0000 | [diff] [blame] | 1226 | rather than a fixed string primarily because some \class{tzinfo} |
| 1227 | subclasses will wish to return different names depending on the specific |
| 1228 | value of \var{dt} passed, especially if the \class{tzinfo} class is |
Tim Peters | 710fb15 | 2003-01-02 19:35:54 +0000 | [diff] [blame] | 1229 | accounting for daylight time. |
Tim Peters | 710fb15 | 2003-01-02 19:35:54 +0000 | [diff] [blame] | 1230 | |
Tim Peters | 327098a | 2003-01-20 22:54:38 +0000 | [diff] [blame] | 1231 | The default implementation of \method{tzname()} raises |
| 1232 | \exception{NotImplementedError}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1233 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1234 | |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 1235 | These methods are called by a \class{datetime} or \class{time} object, |
| 1236 | in response to their methods of the same names. A \class{datetime} |
| 1237 | object passes itself as the argument, and a \class{time} object passes |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1238 | \code{None} as the argument. A \class{tzinfo} subclass's methods should |
| 1239 | therefore be prepared to accept a \var{dt} argument of \code{None}, or of |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 1240 | class \class{datetime}. |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1241 | |
| 1242 | When \code{None} is passed, it's up to the class designer to decide the |
| 1243 | best response. For example, returning \code{None} is appropriate if the |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 1244 | class wishes to say that time objects don't participate in the |
Tim Peters | f196a0a | 2003-01-22 04:45:50 +0000 | [diff] [blame] | 1245 | \class{tzinfo} protocols. It may be more useful for \code{utcoffset(None)} |
Tim Peters | 327098a | 2003-01-20 22:54:38 +0000 | [diff] [blame] | 1246 | to return the standard UTC offset, as there is no other convention for |
| 1247 | discovering the standard offset. |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1248 | |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 1249 | When a \class{datetime} object is passed in response to a |
| 1250 | \class{datetime} method, \code{dt.tzinfo} is the same object as |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1251 | \var{self}. \class{tzinfo} methods can rely on this, unless |
| 1252 | user code calls \class{tzinfo} methods directly. The intent is that |
| 1253 | the \class{tzinfo} methods interpret \var{dt} as being in local time, |
Tim Peters | 327098a | 2003-01-20 22:54:38 +0000 | [diff] [blame] | 1254 | and not need worry about objects in other timezones. |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 1255 | |
Tim Peters | f196a0a | 2003-01-22 04:45:50 +0000 | [diff] [blame] | 1256 | There is one more \class{tzinfo} method that a subclass may wish to |
| 1257 | override: |
| 1258 | |
| 1259 | \begin{methoddesc}{fromutc}{self, dt} |
| 1260 | This is called from the default \class{datetime.astimezone()} |
| 1261 | implementation. When called from that, \code{\var{dt}.tzinfo} is |
| 1262 | \var{self}, and \var{dt}'s date and time members are to be viewed as |
| 1263 | expressing a UTC time. The purpose of \method{fromutc()} is to |
| 1264 | adjust the date and time members, returning an equivalent datetime in |
| 1265 | \var{self}'s local time. |
| 1266 | |
| 1267 | Most \class{tzinfo} subclasses should be able to inherit the default |
| 1268 | \method{fromutc()} implementation without problems. It's strong enough |
| 1269 | to handle fixed-offset time zones, and time zones accounting for both |
| 1270 | standard and daylight time, and the latter even if the DST transition |
| 1271 | times differ in different years. An example of a time zone the default |
| 1272 | \method{fromutc()} implementation may not handle correctly in all cases |
| 1273 | is one where the standard offset (from UTC) depends on the specific date |
| 1274 | and time passed, which can happen for political reasons. |
| 1275 | The default implementations of \method{astimezone()} and |
| 1276 | \method{fromutc()} may not produce the result you want if the result is |
| 1277 | one of the hours straddling the moment the standard offset changes. |
| 1278 | |
| 1279 | Skipping code for error cases, the default \method{fromutc()} |
| 1280 | implementation acts like: |
| 1281 | |
| 1282 | \begin{verbatim} |
| 1283 | def fromutc(self, dt): |
| 1284 | # raise ValueError error if dt.tzinfo is not self |
| 1285 | dtoff = dt.utcoffset() |
| 1286 | dtdst = dt.dst() |
| 1287 | # raise ValueError if dtoff is None or dtdst is None |
| 1288 | delta = dtoff - dtdst # this is self's standard offset |
| 1289 | if delta: |
| 1290 | dt += delta # convert to standard local time |
| 1291 | dtdst = dt.dst() |
| 1292 | # raise ValueError if dtdst is None |
| 1293 | if dtdst: |
| 1294 | return dt + dtdst |
| 1295 | else: |
| 1296 | return dt |
| 1297 | \end{verbatim} |
| 1298 | \end{methoddesc} |
| 1299 | |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1300 | Example \class{tzinfo} classes: |
| 1301 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 1302 | \verbatiminput{tzinfo-examples.py} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1303 | |
Tim Peters | 327098a | 2003-01-20 22:54:38 +0000 | [diff] [blame] | 1304 | Note that there are unavoidable subtleties twice per year in a |
| 1305 | \class{tzinfo} |
Tim Peters | adf6420 | 2003-01-04 06:03:15 +0000 | [diff] [blame] | 1306 | subclass accounting for both standard and daylight time, at the DST |
| 1307 | transition points. For concreteness, consider US Eastern (UTC -0500), |
| 1308 | where EDT begins the minute after 1:59 (EST) on the first Sunday in |
| 1309 | April, and ends the minute after 1:59 (EDT) on the last Sunday in October: |
| 1310 | |
| 1311 | \begin{verbatim} |
| 1312 | UTC 3:MM 4:MM 5:MM 6:MM 7:MM 8:MM |
| 1313 | EST 22:MM 23:MM 0:MM 1:MM 2:MM 3:MM |
| 1314 | EDT 23:MM 0:MM 1:MM 2:MM 3:MM 4:MM |
| 1315 | |
| 1316 | start 22:MM 23:MM 0:MM 1:MM 3:MM 4:MM |
| 1317 | |
| 1318 | end 23:MM 0:MM 1:MM 1:MM 2:MM 3:MM |
| 1319 | \end{verbatim} |
| 1320 | |
| 1321 | When DST starts (the "start" line), the local wall clock leaps from 1:59 |
| 1322 | to 3:00. A wall time of the form 2:MM doesn't really make sense on that |
Tim Peters | 75a6e3b | 2003-01-04 18:17:36 +0000 | [diff] [blame] | 1323 | day, so \code{astimezone(Eastern)} won't deliver a result with |
| 1324 | \code{hour==2} on the |
Tim Peters | 327098a | 2003-01-20 22:54:38 +0000 | [diff] [blame] | 1325 | day DST begins. In order for \method{astimezone()} to make this |
Tim Peters | f196a0a | 2003-01-22 04:45:50 +0000 | [diff] [blame] | 1326 | guarantee, the \method{rzinfo.dst()} method must consider times |
Tim Peters | 327098a | 2003-01-20 22:54:38 +0000 | [diff] [blame] | 1327 | in the "missing hour" (2:MM for Eastern) to be in daylight time. |
Tim Peters | adf6420 | 2003-01-04 06:03:15 +0000 | [diff] [blame] | 1328 | |
| 1329 | When DST ends (the "end" line), there's a potentially worse problem: |
Tim Peters | 327098a | 2003-01-20 22:54:38 +0000 | [diff] [blame] | 1330 | there's an hour that can't be spelled unambiguously in local wall time: |
| 1331 | the last hour of daylight time. In Eastern, that's times of |
| 1332 | the form 5:MM UTC on the day daylight time ends. The local wall clock |
Tim Peters | adf6420 | 2003-01-04 06:03:15 +0000 | [diff] [blame] | 1333 | leaps from 1:59 (daylight time) back to 1:00 (standard time) again. |
Tim Peters | 327098a | 2003-01-20 22:54:38 +0000 | [diff] [blame] | 1334 | Local times of the form 1:MM are ambiguous. \method{astimezone()} mimics |
| 1335 | the local clock's behavior by mapping two adjacent UTC hours into the |
| 1336 | same local hour then. In the Eastern example, UTC times of the form |
| 1337 | 5:MM and 6:MM both map to 1:MM when converted to Eastern. In order for |
Tim Peters | f196a0a | 2003-01-22 04:45:50 +0000 | [diff] [blame] | 1338 | \method{astimezone()} to make this guarantee, the \method{tzinfo.dst()} |
| 1339 | method must consider times in the "repeated hour" to be in |
Tim Peters | 327098a | 2003-01-20 22:54:38 +0000 | [diff] [blame] | 1340 | standard time. This is easily arranged, as in the example, by expressing |
| 1341 | DST switch times in the time zone's standard local time. |
Tim Peters | adf6420 | 2003-01-04 06:03:15 +0000 | [diff] [blame] | 1342 | |
Tim Peters | 327098a | 2003-01-20 22:54:38 +0000 | [diff] [blame] | 1343 | Applications that can't bear such ambiguities should avoid using hybrid |
| 1344 | \class{tzinfo} subclasses; there are no ambiguities when using UTC, or |
| 1345 | any other fixed-offset \class{tzinfo} subclass (such as a class |
| 1346 | representing only EST (fixed offset -5 hours), or only EDT (fixed offset |
| 1347 | -4 hours)). |
Tim Peters | adf6420 | 2003-01-04 06:03:15 +0000 | [diff] [blame] | 1348 | |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1349 | |
Tim Peters | 29fb9c7 | 2002-12-23 22:21:52 +0000 | [diff] [blame] | 1350 | \subsection{\method{strftime()} Behavior} |
| 1351 | |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 1352 | \class{date}, \class{datetime}, and \class{time} |
| 1353 | objects all support a \code{strftime(\var{format})} |
Tim Peters | 29fb9c7 | 2002-12-23 22:21:52 +0000 | [diff] [blame] | 1354 | method, to create a string representing the time under the control of |
| 1355 | an explicit format string. Broadly speaking, |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 1356 | \code{d.strftime(fmt)} |
Tim Peters | 29fb9c7 | 2002-12-23 22:21:52 +0000 | [diff] [blame] | 1357 | acts like the \refmodule{time} module's |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 1358 | \code{time.strftime(fmt, d.timetuple())} |
Tim Peters | 29fb9c7 | 2002-12-23 22:21:52 +0000 | [diff] [blame] | 1359 | although not all objects support a \method{timetuple()} method. |
| 1360 | |
Tim Peters | 0037202 | 2003-01-09 04:10:05 +0000 | [diff] [blame] | 1361 | For \class{time} objects, the format codes for |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 1362 | year, month, and day should not be used, as time objects have no such |
| 1363 | values. If they're used anyway, \code{1900} is substituted for the |
| 1364 | year, and \code{0} for the month and day. |
Tim Peters | 29fb9c7 | 2002-12-23 22:21:52 +0000 | [diff] [blame] | 1365 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 1366 | For \class{date} objects, the format codes for hours, minutes, and |
| 1367 | seconds should not be used, as \class{date} objects have no such |
| 1368 | values. If they're used anyway, \code{0} is substituted for them. |
Tim Peters | 29fb9c7 | 2002-12-23 22:21:52 +0000 | [diff] [blame] | 1369 | |
Fred Drake | a37e5cc | 2002-12-30 21:26:42 +0000 | [diff] [blame] | 1370 | For a naive object, the \code{\%z} and \code{\%Z} format codes are |
Tim Peters | 29fb9c7 | 2002-12-23 22:21:52 +0000 | [diff] [blame] | 1371 | replaced by empty strings. |
| 1372 | |
| 1373 | For an aware object: |
| 1374 | |
| 1375 | \begin{itemize} |
| 1376 | \item[\code{\%z}] |
| 1377 | \method{utcoffset()} is transformed into a 5-character string of |
| 1378 | the form +HHMM or -HHMM, where HH is a 2-digit string giving the |
| 1379 | number of UTC offset hours, and MM is a 2-digit string giving the |
| 1380 | number of UTC offset minutes. For example, if |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 1381 | \method{utcoffset()} returns \code{timedelta(hours=-3, minutes=-30)}, |
Tim Peters | 1cff9fc | 2002-12-24 16:25:29 +0000 | [diff] [blame] | 1382 | \code{\%z} is replaced with the string \code{'-0330'}. |
Tim Peters | 29fb9c7 | 2002-12-23 22:21:52 +0000 | [diff] [blame] | 1383 | |
| 1384 | \item[\code{\%Z}] |
| 1385 | If \method{tzname()} returns \code{None}, \code{\%Z} is replaced |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1386 | by an empty string. Otherwise \code{\%Z} is replaced by the returned |
Tim Peters | 29fb9c7 | 2002-12-23 22:21:52 +0000 | [diff] [blame] | 1387 | value, which must be a string. |
| 1388 | \end{itemize} |
| 1389 | |
| 1390 | The full set of format codes supported varies across platforms, |
| 1391 | because Python calls the platform C library's \function{strftime()} |
| 1392 | function, and platform variations are common. The documentation for |
| 1393 | Python's \refmodule{time} module lists the format codes that the C |
| 1394 | standard (1989 version) requires, and those work on all platforms |
| 1395 | with a standard C implementation. Note that the 1999 version of the |
| 1396 | C standard added additional format codes. |
| 1397 | |
| 1398 | The exact range of years for which \method{strftime()} works also |
| 1399 | varies across platforms. Regardless of platform, years before 1900 |
| 1400 | cannot be used. |
Fred Drake | c5528b1 | 2003-07-02 14:44:55 +0000 | [diff] [blame] | 1401 | |