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