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