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 |
| 317 | through 2038. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 318 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 319 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 320 | \begin{methoddesc}{fromordinal}{ordinal} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 321 | Return the date corresponding to the proleptic Gregorian ordinal, |
| 322 | where January 1 of year 1 has ordinal 1. \exception{ValueError} is |
| 323 | raised unless \code{1 <= \var{ordinal} <= date.max.toordinal()}. |
| 324 | For any date \var{d}, \code{date.fromordinal(\var{d}.toordinal()) == |
| 325 | \var{d}}. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 326 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 327 | |
| 328 | Class attributes: |
| 329 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 330 | \begin{memberdesc}{min} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 331 | The earliest representable date, \code{date(MINYEAR, 1, 1)}. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 332 | \end{memberdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 333 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 334 | \begin{memberdesc}{max} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 335 | The latest representable date, \code{date(MAXYEAR, 12, 31)}. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 336 | \end{memberdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 337 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 338 | \begin{memberdesc}{resolution} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 339 | The smallest possible difference between non-equal date |
| 340 | objects, \code{timedelta(days=1)}. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 341 | \end{memberdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 342 | |
| 343 | Instance attributes (read-only): |
| 344 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 345 | \begin{memberdesc}{year} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 346 | Between \constant{MINYEAR} and \constant{MAXYEAR} inclusive |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 347 | \end{memberdesc} |
| 348 | |
| 349 | \begin{memberdesc}{month} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 350 | Between 1 and 12 inclusive. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 351 | \end{memberdesc} |
| 352 | |
| 353 | \begin{memberdesc}{day} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 354 | Between 1 and the number of days in the given month of the given |
| 355 | year. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 356 | \end{memberdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 357 | |
| 358 | Supported operations: |
| 359 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 360 | \begin{itemize} |
| 361 | \item |
| 362 | date1 + timedelta -> date2 |
| 363 | timedelta + date1 -> date2 |
| 364 | date2 is timedelta.days days removed from the date1, moving forward |
| 365 | in time if timedelta.days > 0, or backward if timedetla.days < 0. |
| 366 | date2 - date1 == timedelta.days after. timedelta.seconds and |
| 367 | timedelta.microseconds are ignored. \exception{OverflowError} is |
| 368 | raised if date2.year would be smaller than \constant{MINYEAR} or |
| 369 | larger than \constant{MAXYEAR}. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 370 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 371 | \item |
| 372 | date1 - timedelta -> date2 |
| 373 | Computes the date2 such that date2 + timedelta == date1. This |
| 374 | isn't quite equivalent to date1 + (-timedelta), because -timedelta |
| 375 | in isolation can overflow in cases where date1 - timedelta does |
| 376 | not. timedelta.seconds and timedelta.microseconds are ignored. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 377 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 378 | \item |
| 379 | date1 - date2 -> timedelta |
| 380 | This is exact, and cannot overflow. timedelta.seconds and |
| 381 | timedelta.microseconds are 0, and date2 + timedelta == date1 |
| 382 | after. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 383 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 384 | \item |
| 385 | comparison of date to date, where date1 is considered less than |
| 386 | date2 when date1 precedes date2 in time. In other words, |
| 387 | date1 < date2 if and only if date1.toordinal() < date2.toordinal(). |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 388 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 389 | \item |
| 390 | hash, use as dict key |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 391 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 392 | \item |
| 393 | efficient pickling |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 394 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 395 | \item |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 396 | in Boolean contexts, all \class{date} objects are considered to be true |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 397 | \end{itemize} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 398 | |
| 399 | Instance methods: |
| 400 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 401 | \begin{methoddesc}{replace}{year, month, day} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 402 | Return a date with the same value, except for those fields given |
| 403 | new values by whichever keyword arguments are specified. For |
| 404 | example, if \code{d == date(2002, 12, 31)}, then |
| 405 | \code{d.replace(day=26) == date(2000, 12, 26)}. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 406 | \end{methoddesc} |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 407 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 408 | \begin{methoddesc}{timetuple}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 409 | Return a 9-element tuple of the form returned by |
| 410 | \function{time.localtime()}. The hours, minutes and seconds are |
| 411 | 0, and the DST flag is -1. |
| 412 | \code{\var{d}.timetuple()} is equivalent to |
| 413 | \code{(\var{d}.year, \var{d}.month, \var{d}.day, |
| 414 | 0, 0, 0, \# h, m, s |
| 415 | \var{d}.weekday(), \# 0 is Monday |
| 416 | \var{d}.toordinal() - date(\var{d}.year, 1, 1).toordinal() + 1, |
| 417 | \# day of year |
| 418 | -1)} |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 419 | \end{methoddesc} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 420 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 421 | \begin{methoddesc}{toordinal}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 422 | Return the proleptic Gregorian ordinal of the date, where January 1 |
| 423 | of year 1 has ordinal 1. For any \class{date} object \var{d}, |
| 424 | \code{date.fromordinal(\var{d}.toordinal()) == \var{d}}. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 425 | \end{methoddesc} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 426 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 427 | \begin{methoddesc}{weekday}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 428 | Return the day of the week as an integer, where Monday is 0 and |
| 429 | Sunday is 6. For example, date(2002, 12, 4).weekday() == 2, a |
| 430 | Wednesday. |
| 431 | See also \method{isoweekday()}. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 432 | \end{methoddesc} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 433 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 434 | \begin{methoddesc}{isoweekday}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 435 | Return the day of the week as an integer, where Monday is 1 and |
| 436 | Sunday is 7. For example, date(2002, 12, 4).isoweekday() == 3, a |
| 437 | Wednesday. |
| 438 | See also \method{weekday()}, \method{isocalendar()}. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 439 | \end{methoddesc} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 440 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 441 | \begin{methoddesc}{isocalendar}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 442 | Return a 3-tuple, (ISO year, ISO week number, ISO weekday). |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 443 | |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 444 | The ISO calendar is a widely used variant of the Gregorian calendar. |
| 445 | See \url{http://www.phys.uu.nl/~vgent/calendar/isocalendar.htm} |
| 446 | for a good explanation. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 447 | |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 448 | The ISO year consists of 52 or 53 full weeks, and where a week starts |
| 449 | on a Monday and ends on a Sunday. The first week of an ISO year is |
| 450 | the first (Gregorian) calendar week of a year containing a Thursday. |
| 451 | This is called week number 1, and the ISO year of that Thursday is |
| 452 | the same as its Gregorian year. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 453 | |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 454 | For example, 2004 begins on a Thursday, so the first week of ISO |
| 455 | year 2004 begins on Monday, 29 Dec 2003 and ends on Sunday, 4 Jan |
| 456 | 2004, so that |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 457 | |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 458 | date(2003, 12, 29).isocalendar() == (2004, 1, 1) |
| 459 | date(2004, 1, 4).isocalendar() == (2004, 1, 7) |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 460 | \end{methoddesc} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 461 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 462 | \begin{methoddesc}{isoformat}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 463 | Return a string representing the date in ISO 8601 format, |
| 464 | 'YYYY-MM-DD'. For example, |
| 465 | date(2002, 12, 4).isoformat() == '2002-12-04'. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 466 | \end{methoddesc} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 467 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 468 | \begin{methoddesc}{__str__}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 469 | For a date \var{d}, \code{str(\var{d})} is equivalent to |
| 470 | \code{\var{d}.isoformat()}. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 471 | \end{methoddesc} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 472 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 473 | \begin{methoddesc}{ctime}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 474 | Return a string representing the date, for example |
| 475 | date(2002, 12, 4).ctime() == 'Wed Dec 4 00:00:00 2002'. |
| 476 | \code{\var{d}.ctime()} is equivalent to |
| 477 | \code{time.ctime(time.mktime(\var{d}.timetuple()))} |
| 478 | on platforms where the native C \cfunction{ctime()} function |
| 479 | (which \function{time.ctime()} invokes, but which |
| 480 | \method{date.ctime()} does not invoke) conforms to the C standard. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 481 | \end{methoddesc} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 482 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 483 | \begin{methoddesc}{strftime}{format} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 484 | Return a string representing the date, controlled by an explicit |
| 485 | format string. Format codes referring to hours, minutes or seconds |
| 486 | will see 0 values. |
| 487 | See the section on \method{strftime()} behavior. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 488 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 489 | |
| 490 | |
Raymond Hettinger | 6005a34 | 2002-12-30 20:01:24 +0000 | [diff] [blame] | 491 | \subsection{\class{datetime} Objects \label{datetime-datetime}} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 492 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 493 | A \class{datetime} object is a single object containing all the |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 494 | information from a \class{date} object and a time object. Like a |
| 495 | \class{date} object, \class{datetime} assumes the current Gregorian |
| 496 | calendar extended in both directions; like a time object, |
| 497 | \class{datetime} assumes there are exactly 3600*24 seconds in every |
| 498 | day. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 499 | |
Fred Drake | 0f8e543 | 2002-12-31 18:31:48 +0000 | [diff] [blame] | 500 | \begin{classdesc}{datetime}{year, month, day, |
| 501 | hour=0, minute=0, second=0, microsecond=0} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 502 | The year, month and day arguments are required. Arguments may be |
| 503 | ints or longs, in the following ranges: |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 504 | |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 505 | \begin{itemize} |
| 506 | \item \code{\member{MINYEAR} <= \var{year} <= \member{MAXYEAR}} |
| 507 | \item \code{1 <= \var{month} <= 12} |
| 508 | \item \code{1 <= \var{day} <= number of days in the given month and year} |
| 509 | \item \code{0 <= \var{hour} < 24} |
| 510 | \item \code{0 <= \var{minute} < 60} |
| 511 | \item \code{0 <= \var{second} < 60} |
| 512 | \item \code{0 <= \var{microsecond} < 1000000} |
| 513 | \end{itemize} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 514 | |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 515 | If an argument outside those ranges is given, \exception{ValueError} |
| 516 | is raised. |
Fred Drake | 0f8e543 | 2002-12-31 18:31:48 +0000 | [diff] [blame] | 517 | \end{classdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 518 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 519 | Other constructors, all class methods: |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 520 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 521 | \begin{methoddesc}{today}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 522 | Return the current local datetime. This is equivalent to |
| 523 | \code{datetime.fromtimestamp(time.time())}. |
| 524 | See also \method{now()}, \method{fromtimestamp()}. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 525 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 526 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 527 | \begin{methoddesc}{now}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 528 | Return the current local datetime. This is like \method{today()}, |
| 529 | but, if possible, supplies more precision than can be gotten from |
| 530 | going through a \function{time.time()} timestamp (for example, |
| 531 | this may be possible on platforms that supply the C |
| 532 | \cfunction{gettimeofday()} function). |
| 533 | See also \method{today()}, \method{utcnow()}. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 534 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 535 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 536 | \begin{methoddesc}{utcnow}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 537 | Return the current UTC datetime. This is like \method{now()}, but |
| 538 | returns the current UTC date and time. |
| 539 | See also \method{now()}. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 540 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 541 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 542 | \begin{methoddesc}{fromtimestamp}{timestamp} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 543 | Return the local \class{datetime} corresponding to the \POSIX{} |
| 544 | timestamp, such as is returned by \function{time.time()}. This |
| 545 | may raise \exception{ValueError}, if the timestamp is out of the |
| 546 | range of values supported by the platform C |
| 547 | \cfunction{localtime()} function. It's common for this to be |
| 548 | restricted to years in 1970 through 2038. |
| 549 | See also \method{utcfromtimestamp()}. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 550 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 551 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 552 | \begin{methoddesc}{utcfromtimestamp}{timestamp} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 553 | Return the UTC \class{datetime} corresponding to the \POSIX{} |
| 554 | timestamp. This may raise \exception{ValueError}, if the |
| 555 | timestamp is out of the range of values supported by the platform |
| 556 | C \cfunction{gmtime()} function. It's common for this to be |
| 557 | restricted to years in 1970 through 2038. |
| 558 | See also \method{fromtimestamp()}. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 559 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 560 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 561 | \begin{methoddesc}{fromordinal}{ordinal} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 562 | Return the \class{datetime} corresponding to the proleptic |
| 563 | Gregorian ordinal, where January 1 of year 1 has ordinal 1. |
| 564 | \exception{ValueError} is raised unless 1 <= ordinal <= |
| 565 | datetime.max.toordinal(). The hour, minute, second and |
| 566 | microsecond of the result are all 0. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 567 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 568 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 569 | \begin{methoddesc}{combine}{date, time} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 570 | Return a new \class{datetime} object whose date components are |
| 571 | equal to the given \class{date} object's, and whose time |
| 572 | components are equal to the given time object's. For any |
| 573 | \class{datetime} object \var{d}, \code{\var{d} == |
| 574 | datetime.combine(\var{d}.date(), \var{d}.time())}. If date is a |
| 575 | \class{datetime} or \class{datetimetz} object, its time components |
| 576 | are ignored. If date is \class{datetimetz} object, its |
| 577 | \member{tzinfo} component is also ignored. If time is a |
| 578 | \class{timetz} object, its \member{tzinfo} component is ignored. |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 579 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 580 | |
| 581 | Class attributes: |
| 582 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 583 | \begin{memberdesc}{min} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 584 | The earliest representable \class{datetime}, |
| 585 | \code{datetime(MINYEAR, 1, 1)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 586 | \end{memberdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 587 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 588 | \begin{memberdesc}{max} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 589 | The latest representable \class{datetime}, |
| 590 | \code{datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 591 | \end{memberdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 592 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 593 | \begin{memberdesc}{resolution} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 594 | The smallest possible difference between non-equal \class{datetime} |
| 595 | objects, \code{timedelta(microseconds=1)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 596 | \end{memberdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 597 | |
| 598 | Instance attributes (read-only): |
| 599 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 600 | \begin{memberdesc}{year} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 601 | Between \constant{MINYEAR} and \constant{MAXYEAR} inclusive |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 602 | \end{memberdesc} |
| 603 | |
| 604 | \begin{memberdesc}{month} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 605 | Between 1 and 12 inclusive |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 606 | \end{memberdesc} |
| 607 | |
| 608 | \begin{memberdesc}{day} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 609 | Between 1 and the number of days in the given month of the given |
| 610 | year. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 611 | \end{memberdesc} |
| 612 | |
| 613 | \begin{memberdesc}{hour} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 614 | In \code{range(24)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 615 | \end{memberdesc} |
| 616 | |
| 617 | \begin{memberdesc}{minute} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 618 | In \code{range(60)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 619 | \end{memberdesc} |
| 620 | |
| 621 | \begin{memberdesc}{second} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 622 | In \code{range(60)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 623 | \end{memberdesc} |
| 624 | |
| 625 | \begin{memberdesc}{microsecond} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 626 | In \code{range(1000000)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 627 | \end{memberdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 628 | |
| 629 | Supported operations: |
| 630 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 631 | \begin{itemize} |
| 632 | \item |
| 633 | datetime1 + timedelta -> datetime2 |
| 634 | timedelta + datetime1 -> datetime2 |
| 635 | datetime2 is a duration of timedelta removed from datetime1, moving |
| 636 | forward in time if timedelta.days > 0, or backward if |
| 637 | timedelta.days < 0. datetime2 - datetime1 == timedelta after. |
| 638 | \exception{OverflowError} is raised if datetime2.year would be |
| 639 | smaller than \constant{MINYEAR} or larger than \constant{MAXYEAR}. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 640 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 641 | \item |
| 642 | datetime1 - timedelta -> datetime2 |
| 643 | Computes the datetime2 such that datetime2 + timedelta == datetime1. |
| 644 | This isn't quite equivalent to datetime1 + (-timedelta), because |
| 645 | -timedelta in isolation can overflow in cases where |
| 646 | datetime1 - timedelta does not. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 647 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 648 | \item |
| 649 | datetime1 - datetime2 -> timedelta |
| 650 | This is exact, and cannot overflow. |
| 651 | datetime2 + timedelta == datetime1 after. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 652 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 653 | \item |
| 654 | comparison of \class{datetime} to datetime, where datetime1 is |
| 655 | considered less than datetime2 when datetime1 precedes datetime2 |
| 656 | in time. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 657 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 658 | \item |
| 659 | hash, use as dict key |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 660 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 661 | \item |
| 662 | efficient pickling |
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 | in Boolean contexts, all \class{datetime} objects are considered |
| 666 | to be true |
| 667 | \end{itemize} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 668 | |
| 669 | Instance methods: |
| 670 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 671 | \begin{methoddesc}{date}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 672 | Return \class{date} object with same year, month and day. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 673 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 674 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 675 | \begin{methoddesc}{time}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 676 | Return time object with same hour, minute, second and microsecond. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 677 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 678 | |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 679 | \begin{methoddesc}{replace}{year=, month=, day=, hour=, minute=, |
| 680 | second=, microsecond=} |
| 681 | Return a datetime with the same value, except for those fields given |
| 682 | new values by whichever keyword arguments are specified. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 683 | \end{methoddesc} |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 684 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 685 | \begin{methoddesc}{astimezone}{tz} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 686 | Return a \class{datetimetz} with the same date and time fields, and |
| 687 | with \member{tzinfo} member \var{tz}. \var{tz} must be \code{None}, |
| 688 | or an instance of a \class{tzinfo} subclass. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 689 | \end{methoddesc} |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 690 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 691 | \begin{methoddesc}{timetuple}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 692 | Return a 9-element tuple of the form returned by |
| 693 | \function{time.localtime()}. |
| 694 | The DST flag is -1. \code{\var{d}.timetuple()} is equivalent to |
| 695 | \code{(\var{d}.year, \var{d}.month, \var{d}.day, |
| 696 | \var{d}.hour, \var{d}.minute, \var{d}.second, |
| 697 | \var{d}.weekday(), \# 0 is Monday |
| 698 | \var{d}.toordinal() - date(\var{d}.year, 1, 1).toordinal() + 1, |
| 699 | \# day of year |
| 700 | -1)} |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 701 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 702 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 703 | \begin{methoddesc}{toordinal}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 704 | Return the proleptic Gregorian ordinal of the date. The same as |
| 705 | \method{date.toordinal()}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 706 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 707 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 708 | \begin{methoddesc}{weekday}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 709 | Return the day of the week as an integer, where Monday is 0 and |
| 710 | Sunday is 6. The same as \method{date.weekday()}. |
| 711 | See also \method{isoweekday()}. |
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}{isoweekday}{} |
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 1 and |
| 716 | Sunday is 7. The same as \method{date.isoweekday()}. |
| 717 | See also \method{weekday()}, \method{isocalendar()}. |
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}{isocalendar}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 721 | Return a 3-tuple, (ISO year, ISO week number, ISO weekday). The |
| 722 | same as \method{date.isocalendar()}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 723 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 724 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 725 | \begin{methoddesc}{isoformat}{sep='T'} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 726 | Return a string representing the date and time in ISO 8601 format, |
| 727 | YYYY-MM-DDTHH:MM:SS.mmmmmm |
| 728 | or, if self.microsecond is 0, |
| 729 | YYYY-MM-DDTHH:MM:SS |
| 730 | The optional argument \var{sep} (default \code{'T'}) is a |
| 731 | one-character separator, placed between the date and time portions |
| 732 | of the result. For example, |
| 733 | datetime(2002, 12, 4, 1, 2, 3, 4).isoformat(' ') == |
| 734 | '2002-12-04 01:02:03.000004' |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 735 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 736 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 737 | \begin{methoddesc}{__str__}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 738 | For a \class{datetime} instance \var{d}, \code{str(\var{d})} is |
| 739 | equivalent to \code{\var{d}.isoformat(' ')}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 740 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 741 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 742 | \begin{methoddesc}{ctime}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 743 | Return a string representing the date, for example |
| 744 | datetime(2002, 12, 4, 20, 30, 40).ctime() == 'Wed Dec 4 20:30:40 2002'. |
| 745 | \code{d.ctime()} is equivalent to |
| 746 | \code{time.ctime(time.mktime(d.timetuple()))} on platforms where |
| 747 | the native C \cfunction{ctime()} function (which |
| 748 | \function{time.ctime()} invokes, but which |
| 749 | \method{datetime.ctime()} does not invoke) conforms to the C |
| 750 | standard. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 751 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 752 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 753 | \begin{methoddesc}{strftime}{format} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 754 | Return a string representing the date and time, controlled by an |
| 755 | explicit format string. See the section on \method{strftime()} |
| 756 | behavior. |
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 | |
| 759 | |
Raymond Hettinger | 6005a34 | 2002-12-30 20:01:24 +0000 | [diff] [blame] | 760 | \subsection{\class{time} Objects \label{datetime-time}} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 761 | |
Raymond Hettinger | cbd6cd2 | 2002-12-31 16:30:49 +0000 | [diff] [blame] | 762 | A \class{time} object represents an idealized time of day, independent |
| 763 | of day and timezone. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 764 | |
Fred Drake | 0f8e543 | 2002-12-31 18:31:48 +0000 | [diff] [blame] | 765 | \begin{classdesc}{time}{hour=0, minute=0, second=0, microsecond=0} |
| 766 | All arguments are optional. They may be ints or longs, in the |
| 767 | following ranges: |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 768 | |
Raymond Hettinger | cbd6cd2 | 2002-12-31 16:30:49 +0000 | [diff] [blame] | 769 | \begin{itemize} |
| 770 | \item \code{0 <= \var{hour} < 24} |
| 771 | \item \code{0 <= \var{minute} < 60} |
| 772 | \item \code{0 <= \var{second} < 60} |
| 773 | \item \code{0 <= \var{microsecond} < 1000000} |
| 774 | \end{itemize} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 775 | |
Fred Drake | 0f8e543 | 2002-12-31 18:31:48 +0000 | [diff] [blame] | 776 | If an argument outside those ranges is given, \exception{ValueError} |
| 777 | is raised. |
| 778 | \end{classdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 779 | |
| 780 | Class attributes: |
| 781 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 782 | \begin{memberdesc}{min} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 783 | The earliest representable \class{time}, \code{time(0, 0, 0, 0)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 784 | \end{memberdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 785 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 786 | \begin{memberdesc}{max} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 787 | The latest representable \class{time}, \code{time(23, 59, 59, 999999)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 788 | \end{memberdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 789 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 790 | \begin{memberdesc}{resolution} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 791 | The smallest possible difference between non-equal \class{time} |
| 792 | objects, \code{timedelta(microseconds=1)}, although note that |
| 793 | arithmetic on \class{time} objects is not supported. |
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 | |
| 796 | Instance attributes (read-only): |
| 797 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 798 | \begin{memberdesc}{hour} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 799 | In \code{range(24)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 800 | \end{memberdesc} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 801 | |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 802 | \begin{memberdesc}{minute} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 803 | In \code{range(60)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 804 | \end{memberdesc} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 805 | |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 806 | \begin{memberdesc}{second} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 807 | In \code{range(60)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 808 | \end{memberdesc} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 809 | |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 810 | \begin{memberdesc}{microsecond} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 811 | In \code{range(1000000)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 812 | \end{memberdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 813 | |
| 814 | Supported operations: |
| 815 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 816 | \begin{itemize} |
| 817 | \item |
| 818 | comparison of time to time, where time1 is considered |
| 819 | less than time2 when time1 precedes time2 in time. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 820 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 821 | \item |
| 822 | hash, use as dict key |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 823 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 824 | \item |
| 825 | efficient pickling |
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 | in Boolean contexts, a time object is considered to be true |
Fred Drake | a37e5cc | 2002-12-30 21:26:42 +0000 | [diff] [blame] | 829 | if and only if it isn't equal to \code{time(0)} |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 830 | \end{itemize} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 831 | |
| 832 | Instance methods: |
| 833 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 834 | \begin{methoddesc}{replace}{hour=, minute=, second=, microsecond=} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 835 | Return a time with the same value, except for those fields given |
| 836 | new values by whichever keyword arguments are specified. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 837 | \end{methoddesc} |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 838 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 839 | \begin{methoddesc}{isoformat}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 840 | Return a string representing the time in ISO 8601 format, |
| 841 | HH:MM:SS.mmmmmm |
| 842 | or, if self.microsecond is 0 |
| 843 | HH:MM:SS |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 844 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 845 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 846 | \begin{methoddesc}{__str__}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 847 | For a time \var{t}, \code{str(\var{t})} is equivalent to |
| 848 | \code{\var{t}.isoformat()}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 849 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 850 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 851 | \begin{methoddesc}{strftime}{format} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 852 | Return a string representing the time, controlled by an explicit |
| 853 | format string. See the section on \method{strftime()} behavior. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 854 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 855 | |
| 856 | |
Raymond Hettinger | 6005a34 | 2002-12-30 20:01:24 +0000 | [diff] [blame] | 857 | \subsection{\class{tzinfo} Objects \label{datetime-tzinfo}} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 858 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 859 | \class{tzinfo} is an abstract base clase, meaning that this class |
| 860 | should not be instantiated directly. You need to derive a concrete |
| 861 | subclass, and (at least) supply implementations of the standard |
| 862 | \class{tzinfo} methods needed by the \class{datetime} methods you |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 863 | use. The \module{datetime} module does not supply any concrete |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 864 | subclasses of \class{tzinfo}. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 865 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 866 | An instance of (a concrete subclass of) \class{tzinfo} can be passed |
| 867 | to the constructors for \class{datetimetz} and \class{timetz} objects. |
| 868 | The latter objects view their fields as being in local time, and the |
| 869 | \class{tzinfo} object supports methods revealing offset of local time |
| 870 | from UTC, the name of the time zone, and DST offset, all relative to a |
| 871 | date or time object passed to them. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 872 | |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 873 | Special requirement for pickling: A \class{tzinfo} subclass must have an |
Tim Peters | 2483b61 | 2002-12-24 16:30:58 +0000 | [diff] [blame] | 874 | \method{__init__} method that can be called with no arguments, else it |
| 875 | can be pickled but possibly not unpickled again. This is a technical |
| 876 | requirement that may be relaxed in the future. |
| 877 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 878 | A concrete subclass of \class{tzinfo} may need to implement the |
| 879 | following methods. Exactly which methods are needed depends on the |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 880 | uses made of aware \module{datetime} objects. If in doubt, simply |
| 881 | implement all of them. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 882 | |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 883 | \begin{methoddesc}{utcoffset}{self, dt} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 884 | Return offset of local time from UTC, in minutes east of UTC. If |
| 885 | local time is west of UTC, this should be negative. Note that this |
| 886 | is intended to be the total offset from UTC; for example, if a |
| 887 | \class{tzinfo} object represents both time zone and DST adjustments, |
| 888 | \method{utcoffset()} should return their sum. If the UTC offset |
| 889 | isn't known, return \code{None}. Else the value returned must be |
Tim Peters | 397301e | 2003-01-02 21:28:08 +0000 | [diff] [blame] | 890 | a \class{timedelta} object specifying a whole number of minutes in the |
| 891 | range -1439 to 1439 inclusive (1440 = 24*60; the magnitude of the offset |
| 892 | must be less than one day). Most implementations of |
| 893 | \method{utcoffset()} will probably look like one of these two: |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 894 | |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 895 | \begin{verbatim} |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 896 | return CONSTANT # fixed-offset class |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 897 | return CONSTANT + self.dst(dt) # daylight-aware class |
Guido van Rossum | 8e7ec7c | 2002-12-31 04:39:05 +0000 | [diff] [blame] | 898 | \end{verbatim} |
Tim Peters | 710fb15 | 2003-01-02 19:35:54 +0000 | [diff] [blame] | 899 | |
| 900 | If \method{utcoffset()} does not return \code{None}, |
| 901 | \method{dst()} should not return \code{None} either. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 902 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 903 | |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 904 | |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 905 | \begin{methoddesc}{dst}{self, dt} |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 906 | Return the daylight savings time (DST) adjustment, in minutes east of |
| 907 | UTC, or \code{None} if DST information isn't known. Return \code{0} if |
| 908 | DST is not in effect. |
Tim Peters | 397301e | 2003-01-02 21:28:08 +0000 | [diff] [blame] | 909 | If DST is in effect, return the offset as a |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 910 | \class{timedelta} object (see \method{utcoffset()} for details). |
| 911 | Note that DST offset, if applicable, has |
| 912 | already been added to the UTC offset returned by |
| 913 | \method{utcoffset()}, so there's no need to consult \method{dst()} |
| 914 | unless you're interested in displaying DST info separately. For |
| 915 | example, \method{datetimetz.timetuple()} calls its \member{tzinfo} |
| 916 | member's \method{dst()} method to determine how the |
Tim Peters | f361515 | 2003-01-01 21:51:37 +0000 | [diff] [blame] | 917 | \member{tm_isdst} flag should be set, and |
| 918 | \method{datetimetz.astimezone()} calls \method{dst()} to account for |
| 919 | DST changes when crossing time zones. |
| 920 | |
| 921 | An instance \var{tz} of a \class{tzinfo} subclass that models both |
| 922 | standard and daylight times must be consistent in this sense: |
| 923 | |
| 924 | \code{tz.utcoffset(dt) - tz.dst(dt)} |
| 925 | |
| 926 | must return the same result for every \class{datetimetz} \var{dt} |
| 927 | in a given year with \code{dt.tzinfo==tz} For sane \class{tzinfo} |
| 928 | subclasses, this expression yields the time zone's "standard offset" |
| 929 | within the year, which should be the same across all days in the year. |
| 930 | The implementation of \method{datetimetz.astimezone()} relies on this, |
| 931 | but cannot detect violations; it's the programmer's responsibility to |
| 932 | ensure it. |
| 933 | |
Tim Peters | 710fb15 | 2003-01-02 19:35:54 +0000 | [diff] [blame] | 934 | \begin{methoddesc}{tzname}{self, dt} |
| 935 | Return the timezone name corresponding to the \class{datetime} represented |
| 936 | by \var{dt}, as a string. Nothing about string names is defined by the |
| 937 | \module{datetime} module, and there's no requirement that it mean anything |
| 938 | in particular. For example, "GMT", "UTC", "-500", "-5:00", "EDT", |
| 939 | "US/Eastern", "America/New York" are all valid replies. Return |
| 940 | \code{None} if a string name isn't known. Note that this is a method |
| 941 | rather than a fixed string primarily because some \class{tzinfo} objects |
| 942 | will wish to return different names depending on the specific value |
| 943 | of \var{dt} passed, especially if the \class{tzinfo} class is |
| 944 | accounting for daylight time. |
| 945 | \end{methoddesc} |
| 946 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 947 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 948 | |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 949 | These methods are called by a \class{datetimetz} or \class{timetz} object, |
| 950 | in response to their methods of the same names. A \class{datetimetz} |
| 951 | object passes itself as the argument, and a \class{timetz} object passes |
| 952 | \code{None} as the argument. A \class{tzinfo} subclass's methods should |
| 953 | therefore be prepared to accept a \var{dt} argument of \code{None}, or of |
| 954 | class \class{datetimetz}. |
| 955 | |
| 956 | When \code{None} is passed, it's up to the class designer to decide the |
| 957 | best response. For example, returning \code{None} is appropriate if the |
| 958 | class wishes to say that timetz objects don't participate in the |
| 959 | \class{tzinfo} protocol. In other applications, it may be more useful |
Raymond Hettinger | c5f5f87 | 2002-12-31 14:26:54 +0000 | [diff] [blame] | 960 | for \code{utcoffset(None)} to return the standard UTC offset. |
Tim Peters | bad8ff0 | 2002-12-30 20:52:32 +0000 | [diff] [blame] | 961 | |
| 962 | When a \class{datetimetz} object is passed in response to a |
| 963 | \class{datetimetz} method, \code{dt.tzinfo} is the same object as |
| 964 | \var{self}. \class{tzinfo} methods can rely on this, unless |
| 965 | user code calls \class{tzinfo} methods directly. The intent is that |
| 966 | the \class{tzinfo} methods interpret \var{dt} as being in local time, |
| 967 | and not need to worry about objects in other timezones. |
| 968 | |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 969 | Example \class{tzinfo} classes: |
| 970 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 971 | \verbatiminput{tzinfo-examples.py} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 972 | |
| 973 | |
Raymond Hettinger | 6005a34 | 2002-12-30 20:01:24 +0000 | [diff] [blame] | 974 | \subsection{\class{timetz} Objects \label{datetime-timetz}} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 975 | |
| 976 | A time object represents a (local) time of day, independent of any |
| 977 | particular day, and subject to adjustment via a \class{tzinfo} object. |
| 978 | |
| 979 | Constructor: |
| 980 | |
Fred Drake | 0f8e543 | 2002-12-31 18:31:48 +0000 | [diff] [blame] | 981 | \begin{classdesc}{time}{hour=0, minute=0, second=0, microsecond=0, |
| 982 | tzinfo=None} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 983 | All arguments are optional. \var{tzinfo} may be \code{None}, or |
| 984 | an instance of a \class{tzinfo} subclass. The remaining arguments |
| 985 | may be ints or longs, in the following ranges: |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 986 | |
Fred Drake | 0f8e543 | 2002-12-31 18:31:48 +0000 | [diff] [blame] | 987 | \begin{itemize} |
| 988 | \item \code{0 <= \var{hour} < 24} |
| 989 | \item \code{0 <= \var{minute} < 60} |
| 990 | \item \code{0 <= \var{second} < 60} |
| 991 | \item \code{0 <= \var{microsecond} < 1000000}. |
| 992 | \end{itemize} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 993 | |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 994 | If an argument outside those ranges is given, |
| 995 | \exception{ValueError} is raised. |
Fred Drake | 0f8e543 | 2002-12-31 18:31:48 +0000 | [diff] [blame] | 996 | \end{classdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 997 | |
| 998 | Class attributes: |
| 999 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1000 | \begin{memberdesc}{min} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1001 | The earliest representable time, \code{timetz(0, 0, 0, 0)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1002 | \end{memberdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1003 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1004 | \begin{memberdesc}{max} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1005 | The latest representable time, \code{timetz(23, 59, 59, 999999)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1006 | \end{memberdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1007 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1008 | \begin{memberdesc}{resolution} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1009 | The smallest possible difference between non-equal \class{timetz} |
| 1010 | objects, \code{timedelta(microseconds=1)}, although note that |
| 1011 | arithmetic on \class{timetz} objects is not supported. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1012 | \end{memberdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1013 | |
| 1014 | Instance attributes (read-only): |
| 1015 | |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1016 | \begin{memberdesc}{hour} |
| 1017 | In \code{range(24)}. |
| 1018 | \end{memberdesc} |
| 1019 | |
| 1020 | \begin{memberdesc}{minute} |
| 1021 | In \code{range(60)}. |
| 1022 | \end{memberdesc} |
| 1023 | |
| 1024 | \begin{memberdesc}{second} |
| 1025 | In \code{range(60)}. |
| 1026 | \end{memberdesc} |
| 1027 | |
| 1028 | \begin{memberdesc}{microsecond} |
| 1029 | In \code{range(1000000)}. |
| 1030 | \end{memberdesc} |
| 1031 | |
| 1032 | \begin{memberdesc}{tzinfo} |
| 1033 | The object passed as the tzinfo argument to the \class{timetz} |
| 1034 | constructor, or \code{None} if none was passed. |
| 1035 | \end{memberdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1036 | |
| 1037 | Supported operations: |
| 1038 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 1039 | \begin{itemize} |
| 1040 | \item |
Tim Peters | 60c76e4 | 2002-12-27 00:41:11 +0000 | [diff] [blame] | 1041 | comparison of \class{timetz} to \class{time} or \class{timetz}, |
| 1042 | 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] | 1043 | \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] | 1044 | \exception{TypeError} is raised. If both comparands are aware, and |
| 1045 | have the same \member{tzinfo} member, the common \member{tzinfo} |
| 1046 | member is ignored and the base times are compared. If both |
| 1047 | comparands are aware and have different \member{tzinfo} members, |
| 1048 | the comparands are first adjusted by subtracting their UTC offsets |
| 1049 | (obtained from \code{self.utcoffset()}). |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1050 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 1051 | \item |
| 1052 | hash, use as dict key |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1053 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 1054 | \item |
| 1055 | pickling |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1056 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 1057 | \item |
| 1058 | in Boolean contexts, a \class{timetz} object is considered to be |
| 1059 | true if and only if, after converting it to minutes and |
| 1060 | subtracting \method{utcoffset()} (or \code{0} if that's |
| 1061 | \code{None}), the result is non-zero. |
| 1062 | \end{itemize} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1063 | |
| 1064 | Instance methods: |
| 1065 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1066 | \begin{methoddesc}{replace}(hour=, minute=, second=, microsecond=, tzinfo=) |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1067 | Return a \class{timetz} with the same value, except for those fields given |
| 1068 | new values by whichever keyword arguments are specified. Note that |
| 1069 | \code{tzinfo=None} can be specified to create a naive \class{timetz} from an |
| 1070 | aware \class{timetz}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1071 | \end{methoddesc} |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 1072 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1073 | \begin{methoddesc}{isoformat}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1074 | Return a string representing the time in ISO 8601 format, |
| 1075 | HH:MM:SS.mmmmmm |
| 1076 | or, if self.microsecond is 0, |
| 1077 | HH:MM:SS |
| 1078 | If \method{utcoffset()} does not return \code{None}, a 6-character |
| 1079 | string is appended, giving the UTC offset in (signed) hours and |
| 1080 | minutes: |
| 1081 | HH:MM:SS.mmmmmm+HH:MM |
| 1082 | or, if self.microsecond is 0, |
| 1083 | HH:MM:SS+HH:MM |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1084 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1085 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1086 | \begin{methoddesc}{__str__}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1087 | For a \class{timetz} \var{t}, \code{str(\var{t})} is equivalent to |
| 1088 | \code{\var{t}.isoformat()}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1089 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1090 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1091 | \begin{methoddesc}{strftime}{format} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1092 | Return a string representing the time, controlled by an explicit |
| 1093 | format string. See the section on \method{strftime()} behavior. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1094 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1095 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1096 | \begin{methoddesc}{utcoffset}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1097 | If \member{tzinfo} is \code{None}, returns \code{None}, else |
| 1098 | \code{tzinfo.utcoffset(self)} converted to a \class{timedelta} |
| 1099 | object. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1100 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1101 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1102 | \begin{methoddesc}{tzname}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1103 | If \member{tzinfo} is \code{None}, returns \code{None}, else |
| 1104 | \code{tzinfo.tzname(self)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1105 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1106 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1107 | \begin{methoddesc}{dst}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1108 | If \member{tzinfo} is \code{None}, returns \code{None}, else |
| 1109 | \code{tzinfo.dst(self)} converted to a \class{timedelta} object. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1110 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1111 | |
| 1112 | |
| 1113 | |
Raymond Hettinger | 6005a34 | 2002-12-30 20:01:24 +0000 | [diff] [blame] | 1114 | \subsection{ \class{datetimetz} Objects \label{datetime-datetimetz}} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1115 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 1116 | \begin{notice}[warning] |
| 1117 | I think this is \emph{still} missing some methods from the |
| 1118 | Python implementation. |
| 1119 | \end{notice} |
| 1120 | |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1121 | 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] | 1122 | from a \class{date} object and a \class{timetz} object. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1123 | |
| 1124 | Constructor: |
| 1125 | |
Fred Drake | 0f8e543 | 2002-12-31 18:31:48 +0000 | [diff] [blame] | 1126 | \begin{classdesc}{datetimetz}{year, month, day, |
| 1127 | hour=0, minute=0, second=0, microsecond=0, |
| 1128 | tzinfo=None} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1129 | The year, month and day arguments are required. \var{tzinfo} may |
| 1130 | be \code{None}, or an instance of a \class{tzinfo} subclass. The |
| 1131 | remaining arguments may be ints or longs, in the following ranges: |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1132 | |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1133 | \begin{itemize} |
| 1134 | \item \code{MINYEAR <= \var{year} <= MAXYEAR} |
| 1135 | \item \code{1 <= \var{month} <= 12} |
| 1136 | \item \code{1 <= \var{day} <= number of days in the given month and year} |
| 1137 | \item \code{0 <= \var{hour} < 24} |
| 1138 | \item \code{0 <= \var{minute} < 60} |
| 1139 | \item \code{0 <= \var{second} < 60} |
| 1140 | \item \code{0 <= \var{microsecond} < 1000000} |
| 1141 | \end{itemize} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1142 | |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1143 | If an argument outside those ranges is given, |
| 1144 | \exception{ValueError} is raised. |
Fred Drake | 0f8e543 | 2002-12-31 18:31:48 +0000 | [diff] [blame] | 1145 | \end{classdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1146 | |
| 1147 | Other constructors (class methods): |
| 1148 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1149 | \begin{funcdesc}{today}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1150 | \methodline{utcnow}{} |
| 1151 | \methodline{utcfromtimestamp}{timestamp} |
| 1152 | \methodline{fromordinal}{ordinal} |
| 1153 | These are the same as the \class{datetime} class methods of the |
| 1154 | same names, except that they construct a \class{datetimetz} |
| 1155 | object, with tzinfo \code{None}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1156 | \end{funcdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1157 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1158 | \begin{funcdesc}{now}{\optional{tzinfo=None}} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1159 | \methodline{fromtimestamp}{timestamp\optional{, tzinfo=None}} |
| 1160 | These are the same as the \class{datetime} class methods of the |
| 1161 | same names, except that they accept an additional, optional tzinfo |
| 1162 | argument, and construct a \class{datetimetz} object with that |
| 1163 | \class{tzinfo} object attached. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1164 | \end{funcdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1165 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1166 | \begin{funcdesc}{combine}{date, time} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1167 | This is the same as \method{datetime.combine()}, except that it |
| 1168 | constructs a \class{datetimetz} object, and, if the time object is |
| 1169 | of type timetz, the \class{datetimetz} object has the same |
| 1170 | \class{tzinfo} object as the time object. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1171 | \end{funcdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1172 | |
| 1173 | Class attributes: |
| 1174 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1175 | \begin{memberdesc}{min} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1176 | The earliest representable \class{datetimetz}, |
| 1177 | \code{datetimetz(MINYEAR, 1, 1)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1178 | \end{memberdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1179 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1180 | \begin{memberdesc}{max} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1181 | The latest representable \class{datetime}, |
| 1182 | \code{datetimetz(MAXYEAR, 12, 31, 23, 59, 59, 999999)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1183 | \end{memberdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1184 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1185 | \begin{memberdesc}{resolution} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1186 | The smallest possible difference between non-equal \class{datetimetz} |
| 1187 | objects, \code{timedelta(microseconds=1)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1188 | \end{memberdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1189 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1190 | Instance attributes, all read-only: |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1191 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1192 | \begin{memberdesc}{year} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1193 | Between \constant{MINYEAR} and \constant{MAXYEAR}, inclusive. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1194 | \end{memberdesc} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1195 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1196 | \begin{memberdesc}{month} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1197 | Between 1 and 12 inclusive |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1198 | \end{memberdesc} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1199 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1200 | \begin{memberdesc}{day} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1201 | Between 1 and the number of days in the given month of the given |
| 1202 | year. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1203 | \end{memberdesc} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1204 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1205 | \begin{memberdesc}{hour} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1206 | In \code{range(24)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1207 | \end{memberdesc} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1208 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1209 | \begin{memberdesc}{minute} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1210 | In \code{range(60)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1211 | \end{memberdesc} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1212 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1213 | \begin{memberdesc}{second} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1214 | In \code{range(60)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1215 | \end{memberdesc} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1216 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1217 | \begin{memberdesc}{microsecond} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1218 | In \code{range(1000000)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1219 | \end{memberdesc} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1220 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1221 | \begin{memberdesc}{tzinfo} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1222 | The object passed as the \var{tzinfo} argument to the |
| 1223 | \class{datetimetz} constructor, or \code{None} if none was passed. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1224 | \end{memberdesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1225 | |
| 1226 | Supported operations: |
| 1227 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 1228 | \begin{itemize} |
| 1229 | \item |
| 1230 | datetimetz1 + timedelta -> datetimetz2 |
| 1231 | timedelta + datetimetz1 -> datetimetz2 |
Tim Peters | 60c76e4 | 2002-12-27 00:41:11 +0000 | [diff] [blame] | 1232 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 1233 | The same as addition of \class{datetime} objects, except that |
| 1234 | datetimetz2.tzinfo is set to datetimetz1.tzinfo. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1235 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 1236 | \item |
| 1237 | datetimetz1 - timedelta -> datetimetz2 |
Tim Peters | 60c76e4 | 2002-12-27 00:41:11 +0000 | [diff] [blame] | 1238 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 1239 | The same as addition of \class{datetime} objects, except that |
| 1240 | datetimetz2.tzinfo is set to datetimetz1.tzinfo. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1241 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 1242 | \item |
| 1243 | aware_datetimetz1 - aware_datetimetz2 -> timedelta |
Fred Drake | a37e5cc | 2002-12-30 21:26:42 +0000 | [diff] [blame] | 1244 | naive_datetimetz1 - naive_datetimetz2 -> timedelta |
| 1245 | naive_datetimetz1 - datetime2 -> timedelta |
| 1246 | datetime1 - naive_datetimetz2 -> timedelta |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1247 | |
Tim Peters | 60c76e4 | 2002-12-27 00:41:11 +0000 | [diff] [blame] | 1248 | Subtraction of a \class{datetime} or \class{datetimetz}, from a |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 1249 | \class{datetime} or \class{datetimetz}, is defined only if both |
Fred Drake | a37e5cc | 2002-12-30 21:26:42 +0000 | [diff] [blame] | 1250 | operands are naive, or if both are aware. If one is aware and the |
| 1251 | other is naive, \exception{TypeError} is raised. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1252 | |
Fred Drake | a37e5cc | 2002-12-30 21:26:42 +0000 | [diff] [blame] | 1253 | 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] | 1254 | member, subtraction acts as for \class{datetime} subtraction. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1255 | |
Tim Peters | 60c76e4 | 2002-12-27 00:41:11 +0000 | [diff] [blame] | 1256 | If both are aware and have different \member{tzinfo} members, |
| 1257 | \code{a-b} acts as if \var{a} and \var{b} were first converted to UTC |
| 1258 | datetimes (by subtracting \code{a.utcoffset()} minutes from \var{a}, |
| 1259 | and \code{b.utcoffset()} minutes from \var{b}), and then doing |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 1260 | \class{datetime} subtraction, except that the implementation never |
| 1261 | overflows. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1262 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 1263 | \item |
Tim Peters | 60c76e4 | 2002-12-27 00:41:11 +0000 | [diff] [blame] | 1264 | comparison of \class{datetimetz} to \class{datetime} or |
| 1265 | \class{datetimetz}, where \var{a} is considered less than \var{b} |
| 1266 | when \var{a} precedes \var{b} in time. If one comparand is naive and |
| 1267 | the other is aware, \exception{TypeError} is raised. If both |
| 1268 | comparands are aware, and have the same \member{tzinfo} member, |
| 1269 | the common \member{tzinfo} member is ignored and the base datetimes |
| 1270 | are compared. If both comparands are aware and have different |
| 1271 | \member{tzinfo} members, the comparands are first adjusted by |
| 1272 | subtracting their UTC offsets (obtained from \code{self.utcoffset()}). |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1273 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 1274 | \item |
| 1275 | hash, use as dict key |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1276 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 1277 | \item |
| 1278 | efficient pickling |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1279 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 1280 | \item |
| 1281 | in Boolean contexts, all \class{datetimetz} objects are considered to be |
| 1282 | true |
| 1283 | \end{itemize} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1284 | |
| 1285 | Instance methods: |
| 1286 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1287 | \begin{methoddesc}{date}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1288 | \methodline{time}{} |
| 1289 | \methodline{toordinal}{} |
| 1290 | \methodline{weekday}{} |
| 1291 | \methodline{isoweekday}{} |
| 1292 | \methodline{isocalendar}{} |
| 1293 | \methodline{ctime}{} |
| 1294 | \methodline{__str__}{} |
| 1295 | \methodline{strftime}{format} |
| 1296 | 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] | 1297 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1298 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1299 | \begin{methoddesc}{timetz}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1300 | Return \class{timetz} object with same hour, minute, second, microsecond, |
| 1301 | and tzinfo. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1302 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1303 | |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1304 | \begin{methoddesc}{replace}{year=, month=, day=, hour=, minute=, second=, |
| 1305 | microsecond=, tzinfo=} |
| 1306 | Return a datetimetz with the same value, except for those fields given |
| 1307 | new values by whichever keyword arguments are specified. Note that |
| 1308 | \code{tzinfo=None} can be specified to create a naive datetimetz from |
| 1309 | an aware datetimetz. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1310 | \end{methoddesc} |
Tim Peters | 12bf339 | 2002-12-24 05:41:27 +0000 | [diff] [blame] | 1311 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1312 | \begin{methoddesc}{astimezone}{tz} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1313 | Return a \class{datetimetz} with new tzinfo member \var{tz}. \var{tz} |
| 1314 | must be \code{None}, or an instance of a \class{tzinfo} subclass. If |
| 1315 | \var{tz} is \code{None}, self is naive, or |
| 1316 | \code{tz.utcoffset(self)} returns \code{None}, |
| 1317 | \code{self.astimezone(tz)} is equivalent to |
| 1318 | \code{self.replace(tzinfo=tz)}: a new timezone object is attached |
| 1319 | without any conversion of date or time fields. If self is aware and |
| 1320 | \code{tz.utcoffset(self)} does not return \code{None}, the date and |
| 1321 | time fields are adjusted so that the result is local time in timezone |
| 1322 | tz, representing the same UTC time as self. |
| 1323 | XXX [The treatment of endcases remains unclear: for DST-aware |
| 1324 | classes, one hour per year has two spellings in local time, and |
| 1325 | another hour has no spelling in local time.] XXX |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1326 | \end{methoddesc} |
Tim Peters | 80475bb | 2002-12-25 07:40:55 +0000 | [diff] [blame] | 1327 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1328 | \begin{methoddesc}{utcoffset}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1329 | If \member{tzinfo} is \code{None}, returns \code{None}, else |
| 1330 | \code{tzinfo.utcoffset(self)} converted to a \class{timedelta} |
| 1331 | object. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1332 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1333 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1334 | \begin{methoddesc}{tzname}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1335 | If \member{tzinfo} is \code{None}, returns \code{None}, else |
| 1336 | returns \code{tzinfo.tzname(self)}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1337 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1338 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1339 | \begin{methoddesc}{dst}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1340 | If \member{tzinfo} is \code{None}, returns \code{None}, else |
| 1341 | \code{tzinfo.dst(self)} converted to a \class{timedelta} |
| 1342 | object. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1343 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1344 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1345 | \begin{methoddesc}{timetuple}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1346 | Like \function{datetime.timetuple()}, but sets the |
| 1347 | \member{tm_isdst} flag according to the \method{dst()} method: if |
| 1348 | \method{dst()} returns \code{None}, \member{tm_isdst} is set to |
| 1349 | \code{-1}; else if \method{dst()} returns a non-zero value, |
| 1350 | \member{tm_isdst} is set to \code{1}; else \code{tm_isdst} is set |
| 1351 | to \code{0}. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1352 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1353 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1354 | \begin{methoddesc}{utctimetuple}{} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1355 | If \class{datetimetz} instance \var{d} is naive, this is the same as |
| 1356 | \code{\var{d}.timetuple()} except that \member{tm_isdst} is forced to 0 |
| 1357 | regardless of what \code{d.dst()} returns. DST is never in effect |
| 1358 | for a UTC time. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1359 | |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1360 | If \var{d} is aware, \var{d} is normalized to UTC time, by subtracting |
| 1361 | \code{\var{d}.utcoffset()} minutes, and a timetuple for the |
| 1362 | normalized time is returned. \member{tm_isdst} is forced to 0. |
| 1363 | Note that the result's \member{tm_year} field may be |
| 1364 | \constant{MINYEAR}-1 or \constant{MAXYEAR}+1, if \var{d}.year was |
| 1365 | \code{MINYEAR} or \code{MAXYEAR} and UTC adjustment spills over a |
| 1366 | year boundary. |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1367 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1368 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1369 | \begin{methoddesc}{isoformat}{sep='T'} |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1370 | Return a string representing the date and time in ISO 8601 format, |
| 1371 | YYYY-MM-DDTHH:MM:SS.mmmmmm |
| 1372 | or, if \member{microsecond} is 0, |
| 1373 | YYYY-MM-DDTHH:MM:SS |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1374 | |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1375 | If \method{utcoffset()} does not return \code{None}, a 6-character |
| 1376 | string is appended, giving the UTC offset in (signed) hours and |
| 1377 | minutes: |
| 1378 | YYYY-MM-DDTHH:MM:SS.mmmmmm+HH:MM |
| 1379 | or, if \member{microsecond} is 0 |
| 1380 | YYYY-MM-DDTHH:MM:SS+HH:MM |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1381 | |
Fred Drake | 436eadd | 2002-12-31 18:13:11 +0000 | [diff] [blame] | 1382 | The optional argument \var{sep} (default \code{'T'}) is a |
| 1383 | one-character separator, placed between the date and time portions |
| 1384 | of the result. For example, |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1385 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 1386 | \begin{verbatim} |
| 1387 | >>> from datetime import * |
| 1388 | >>> class TZ(tzinfo): |
Tim Peters | 710fb15 | 2003-01-02 19:35:54 +0000 | [diff] [blame] | 1389 | ... def utcoffset(self, dt): return timedelta(minutes=-399) |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 1390 | ... |
| 1391 | >>> datetimetz(2002, 12, 25, tzinfo=TZ()).isoformat(' ') |
| 1392 | '2002-12-25 00:00:00-06:39' |
| 1393 | \end{verbatim} |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1394 | \end{methoddesc} |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1395 | |
Fred Drake | bbdb250 | 2002-12-23 18:58:06 +0000 | [diff] [blame] | 1396 | \code{str(\var{d})} is equivalent to \code{\var{d}.isoformat(' ')}. |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1397 | |
| 1398 | |
Tim Peters | 29fb9c7 | 2002-12-23 22:21:52 +0000 | [diff] [blame] | 1399 | \subsection{\method{strftime()} Behavior} |
| 1400 | |
| 1401 | \class{date}, \class{datetime}, \class{datetimetz}, \class{time}, |
| 1402 | and \class{timetz} objects all support a \code{strftime(\var{format})} |
| 1403 | method, to create a string representing the time under the control of |
| 1404 | an explicit format string. Broadly speaking, |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 1405 | \code{d.strftime(fmt)} |
Tim Peters | 29fb9c7 | 2002-12-23 22:21:52 +0000 | [diff] [blame] | 1406 | acts like the \refmodule{time} module's |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 1407 | \code{time.strftime(fmt, d.timetuple())} |
Tim Peters | 29fb9c7 | 2002-12-23 22:21:52 +0000 | [diff] [blame] | 1408 | although not all objects support a \method{timetuple()} method. |
| 1409 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 1410 | For \class{time} and \class{timetz} objects, the format codes for |
| 1411 | year, month, and day should not be used, as time objects have no such |
| 1412 | values. If they're used anyway, \code{1900} is substituted for the |
| 1413 | year, and \code{0} for the month and day. |
Tim Peters | 29fb9c7 | 2002-12-23 22:21:52 +0000 | [diff] [blame] | 1414 | |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 1415 | For \class{date} objects, the format codes for hours, minutes, and |
| 1416 | seconds should not be used, as \class{date} objects have no such |
| 1417 | values. If they're used anyway, \code{0} is substituted for them. |
Tim Peters | 29fb9c7 | 2002-12-23 22:21:52 +0000 | [diff] [blame] | 1418 | |
Fred Drake | a37e5cc | 2002-12-30 21:26:42 +0000 | [diff] [blame] | 1419 | 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] | 1420 | replaced by empty strings. |
| 1421 | |
| 1422 | For an aware object: |
| 1423 | |
| 1424 | \begin{itemize} |
| 1425 | \item[\code{\%z}] |
| 1426 | \method{utcoffset()} is transformed into a 5-character string of |
| 1427 | the form +HHMM or -HHMM, where HH is a 2-digit string giving the |
| 1428 | number of UTC offset hours, and MM is a 2-digit string giving the |
| 1429 | number of UTC offset minutes. For example, if |
Andrew M. Kuchling | c97868e | 2002-12-30 03:06:45 +0000 | [diff] [blame] | 1430 | \method{utcoffset()} returns \code{timedelta(hours=-3, minutes=-30)}, |
Tim Peters | 1cff9fc | 2002-12-24 16:25:29 +0000 | [diff] [blame] | 1431 | \code{\%z} is replaced with the string \code{'-0330'}. |
Tim Peters | 29fb9c7 | 2002-12-23 22:21:52 +0000 | [diff] [blame] | 1432 | |
| 1433 | \item[\code{\%Z}] |
| 1434 | If \method{tzname()} returns \code{None}, \code{\%Z} is replaced |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1435 | by an empty string. Otherwise \code{\%Z} is replaced by the returned |
Tim Peters | 29fb9c7 | 2002-12-23 22:21:52 +0000 | [diff] [blame] | 1436 | value, which must be a string. |
| 1437 | \end{itemize} |
| 1438 | |
| 1439 | The full set of format codes supported varies across platforms, |
| 1440 | because Python calls the platform C library's \function{strftime()} |
| 1441 | function, and platform variations are common. The documentation for |
| 1442 | Python's \refmodule{time} module lists the format codes that the C |
| 1443 | standard (1989 version) requires, and those work on all platforms |
| 1444 | with a standard C implementation. Note that the 1999 version of the |
| 1445 | C standard added additional format codes. |
| 1446 | |
| 1447 | The exact range of years for which \method{strftime()} works also |
| 1448 | varies across platforms. Regardless of platform, years before 1900 |
| 1449 | cannot be used. |
| 1450 | |
| 1451 | |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1452 | \begin{comment} |
| 1453 | |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1454 | \subsection{C API} |
| 1455 | |
| 1456 | Struct typedefs: |
| 1457 | |
| 1458 | PyDateTime_Date |
| 1459 | PyDateTime_DateTime |
| 1460 | PyDateTime_DateTimeTZ |
| 1461 | PyDateTime_Time |
| 1462 | PyDateTime_TimeTZ |
| 1463 | PyDateTime_Delta |
| 1464 | PyDateTime_TZInfo |
| 1465 | |
| 1466 | Type-check macros: |
| 1467 | |
| 1468 | PyDate_Check(op) |
| 1469 | PyDate_CheckExact(op) |
| 1470 | |
| 1471 | PyDateTime_Check(op) |
| 1472 | PyDateTime_CheckExact(op) |
| 1473 | |
| 1474 | PyDateTimeTZ_Check(op) |
| 1475 | PyDateTimeTZ_CheckExact(op) |
| 1476 | |
| 1477 | PyTime_Check(op) |
| 1478 | PyTime_CheckExact(op) |
| 1479 | |
| 1480 | PyTimeTZ_Check(op) |
| 1481 | PyTimeTZ_CheckExact(op) |
| 1482 | |
| 1483 | PyDelta_Check(op) |
| 1484 | PyDelta_CheckExact(op) |
| 1485 | |
| 1486 | PyTZInfo_Check(op) |
Raymond Hettinger | cbd6cd2 | 2002-12-31 16:30:49 +0000 | [diff] [blame] | 1487 | PyTZInfo_CheckExact(op) |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1488 | |
| 1489 | Accessor macros: |
| 1490 | |
| 1491 | All objects are immutable, so accessors are read-only. All macros |
| 1492 | return ints: |
| 1493 | |
Tim Peters | 1cff9fc | 2002-12-24 16:25:29 +0000 | [diff] [blame] | 1494 | For \class{date}, \class{datetime}, and \class{datetimetz} instances: |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1495 | PyDateTime_GET_YEAR(o) |
| 1496 | PyDateTime_GET_MONTH(o) |
| 1497 | PyDateTime_GET_DAY(o) |
| 1498 | |
| 1499 | For \class{datetime} and \class{datetimetz} instances: |
| 1500 | PyDateTime_DATE_GET_HOUR(o) |
| 1501 | PyDateTime_DATE_GET_MINUTE(o) |
| 1502 | PyDateTime_DATE_GET_SECOND(o) |
| 1503 | PyDateTime_DATE_GET_MICROSECOND(o) |
| 1504 | |
Tim Peters | 1cff9fc | 2002-12-24 16:25:29 +0000 | [diff] [blame] | 1505 | For \class{time} and \class{timetz} instances: |
Andrew M. Kuchling | ca2623a | 2002-12-18 14:59:11 +0000 | [diff] [blame] | 1506 | PyDateTime_TIME_GET_HOUR(o) |
| 1507 | PyDateTime_TIME_GET_MINUTE(o) |
| 1508 | PyDateTime_TIME_GET_SECOND(o) |
| 1509 | PyDateTime_TIME_GET_MICROSECOND(o) |
Andrew M. Kuchling | fa91858 | 2002-12-30 14:20:16 +0000 | [diff] [blame] | 1510 | |
| 1511 | \end{comment} |