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