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