jpayne@69
|
1 The DateTime package
|
jpayne@69
|
2 ====================
|
jpayne@69
|
3
|
jpayne@69
|
4 Encapsulation of date/time values.
|
jpayne@69
|
5
|
jpayne@69
|
6
|
jpayne@69
|
7 Function Timezones()
|
jpayne@69
|
8 --------------------
|
jpayne@69
|
9
|
jpayne@69
|
10 Returns the list of recognized timezone names:
|
jpayne@69
|
11
|
jpayne@69
|
12 >>> from DateTime import Timezones
|
jpayne@69
|
13 >>> zones = set(Timezones())
|
jpayne@69
|
14
|
jpayne@69
|
15 Almost all of the standard pytz timezones are included, with the exception
|
jpayne@69
|
16 of some commonly-used but ambiguous abbreviations, where historical Zope
|
jpayne@69
|
17 usage conflicts with the name used by pytz:
|
jpayne@69
|
18
|
jpayne@69
|
19 >>> import pytz
|
jpayne@69
|
20 >>> [x for x in pytz.all_timezones if x not in zones]
|
jpayne@69
|
21 ['CET', 'EET', 'EST', 'MET', 'MST', 'WET']
|
jpayne@69
|
22
|
jpayne@69
|
23 Class DateTime
|
jpayne@69
|
24 --------------
|
jpayne@69
|
25
|
jpayne@69
|
26 DateTime objects represent instants in time and provide interfaces for
|
jpayne@69
|
27 controlling its representation without affecting the absolute value of
|
jpayne@69
|
28 the object.
|
jpayne@69
|
29
|
jpayne@69
|
30 DateTime objects may be created from a wide variety of string or
|
jpayne@69
|
31 numeric data, or may be computed from other DateTime objects.
|
jpayne@69
|
32 DateTimes support the ability to convert their representations to many
|
jpayne@69
|
33 major timezones, as well as the ability to create a DateTime object
|
jpayne@69
|
34 in the context of a given timezone.
|
jpayne@69
|
35
|
jpayne@69
|
36 DateTime objects provide partial numerical behavior:
|
jpayne@69
|
37
|
jpayne@69
|
38 * Two date-time objects can be subtracted to obtain a time, in days
|
jpayne@69
|
39 between the two.
|
jpayne@69
|
40
|
jpayne@69
|
41 * A date-time object and a positive or negative number may be added to
|
jpayne@69
|
42 obtain a new date-time object that is the given number of days later
|
jpayne@69
|
43 than the input date-time object.
|
jpayne@69
|
44
|
jpayne@69
|
45 * A positive or negative number and a date-time object may be added to
|
jpayne@69
|
46 obtain a new date-time object that is the given number of days later
|
jpayne@69
|
47 than the input date-time object.
|
jpayne@69
|
48
|
jpayne@69
|
49 * A positive or negative number may be subtracted from a date-time
|
jpayne@69
|
50 object to obtain a new date-time object that is the given number of
|
jpayne@69
|
51 days earlier than the input date-time object.
|
jpayne@69
|
52
|
jpayne@69
|
53 DateTime objects may be converted to integer, long, or float numbers
|
jpayne@69
|
54 of days since January 1, 1901, using the standard int, long, and float
|
jpayne@69
|
55 functions (Compatibility Note: int, long and float return the number
|
jpayne@69
|
56 of days since 1901 in GMT rather than local machine timezone).
|
jpayne@69
|
57 DateTime objects also provide access to their value in a float format
|
jpayne@69
|
58 usable with the Python time module, provided that the value of the
|
jpayne@69
|
59 object falls in the range of the epoch-based time module.
|
jpayne@69
|
60
|
jpayne@69
|
61 A DateTime object should be considered immutable; all conversion and numeric
|
jpayne@69
|
62 operations return a new DateTime object rather than modify the current object.
|
jpayne@69
|
63
|
jpayne@69
|
64 A DateTime object always maintains its value as an absolute UTC time,
|
jpayne@69
|
65 and is represented in the context of some timezone based on the
|
jpayne@69
|
66 arguments used to create the object. A DateTime object's methods
|
jpayne@69
|
67 return values based on the timezone context.
|
jpayne@69
|
68
|
jpayne@69
|
69 Note that in all cases the local machine timezone is used for
|
jpayne@69
|
70 representation if no timezone is specified.
|
jpayne@69
|
71
|
jpayne@69
|
72 Constructor for DateTime
|
jpayne@69
|
73 ------------------------
|
jpayne@69
|
74
|
jpayne@69
|
75 DateTime() returns a new date-time object. DateTimes may be created
|
jpayne@69
|
76 with from zero to seven arguments:
|
jpayne@69
|
77
|
jpayne@69
|
78 * If the function is called with no arguments, then the current date/
|
jpayne@69
|
79 time is returned, represented in the timezone of the local machine.
|
jpayne@69
|
80
|
jpayne@69
|
81 * If the function is invoked with a single string argument which is a
|
jpayne@69
|
82 recognized timezone name, an object representing the current time is
|
jpayne@69
|
83 returned, represented in the specified timezone.
|
jpayne@69
|
84
|
jpayne@69
|
85 * If the function is invoked with a single string argument
|
jpayne@69
|
86 representing a valid date/time, an object representing that date/
|
jpayne@69
|
87 time will be returned.
|
jpayne@69
|
88
|
jpayne@69
|
89 As a general rule, any date-time representation that is recognized
|
jpayne@69
|
90 and unambiguous to a resident of North America is acceptable. (The
|
jpayne@69
|
91 reason for this qualification is that in North America, a date like:
|
jpayne@69
|
92 2/1/1994 is interpreted as February 1, 1994, while in some parts of
|
jpayne@69
|
93 the world, it is interpreted as January 2, 1994.) A date/ time
|
jpayne@69
|
94 string consists of two components, a date component and an optional
|
jpayne@69
|
95 time component, separated by one or more spaces. If the time
|
jpayne@69
|
96 component is omitted, 12:00am is assumed.
|
jpayne@69
|
97
|
jpayne@69
|
98 Any recognized timezone name specified as the final element of the
|
jpayne@69
|
99 date/time string will be used for computing the date/time value.
|
jpayne@69
|
100 (If you create a DateTime with the string,
|
jpayne@69
|
101 "Mar 9, 1997 1:45pm US/Pacific", the value will essentially be the
|
jpayne@69
|
102 same as if you had captured time.time() at the specified date and
|
jpayne@69
|
103 time on a machine in that timezone). If no timezone is passed, then
|
jpayne@69
|
104 the timezone configured on the local machine will be used, **except**
|
jpayne@69
|
105 that if the date format matches ISO 8601 ('YYYY-MM-DD'), the instance
|
jpayne@69
|
106 will use UTC / GMT+0 as the timezone.
|
jpayne@69
|
107
|
jpayne@69
|
108 o Returns current date/time, represented in US/Eastern:
|
jpayne@69
|
109
|
jpayne@69
|
110 >>> from DateTime import DateTime
|
jpayne@69
|
111 >>> e = DateTime('US/Eastern')
|
jpayne@69
|
112 >>> e.timezone()
|
jpayne@69
|
113 'US/Eastern'
|
jpayne@69
|
114
|
jpayne@69
|
115 o Returns specified time, represented in local machine zone:
|
jpayne@69
|
116
|
jpayne@69
|
117 >>> x = DateTime('1997/3/9 1:45pm')
|
jpayne@69
|
118 >>> x.parts() # doctest: +ELLIPSIS
|
jpayne@69
|
119 (1997, 3, 9, 13, 45, ...)
|
jpayne@69
|
120
|
jpayne@69
|
121 o Specified time in local machine zone, verbose format:
|
jpayne@69
|
122
|
jpayne@69
|
123 >>> y = DateTime('Mar 9, 1997 13:45:00')
|
jpayne@69
|
124 >>> y.parts() # doctest: +ELLIPSIS
|
jpayne@69
|
125 (1997, 3, 9, 13, 45, ...)
|
jpayne@69
|
126 >>> y == x
|
jpayne@69
|
127 True
|
jpayne@69
|
128
|
jpayne@69
|
129 o Specified time in UTC via ISO 8601 rule:
|
jpayne@69
|
130
|
jpayne@69
|
131 >>> z = DateTime('2014-03-24')
|
jpayne@69
|
132 >>> z.parts() # doctest: +ELLIPSIS
|
jpayne@69
|
133 (2014, 3, 24, 0, 0, ...)
|
jpayne@69
|
134 >>> z.timezone()
|
jpayne@69
|
135 'GMT+0'
|
jpayne@69
|
136
|
jpayne@69
|
137 The date component consists of year, month, and day values. The
|
jpayne@69
|
138 year value must be a one-, two-, or four-digit integer. If a one-
|
jpayne@69
|
139 or two-digit year is used, the year is assumed to be in the
|
jpayne@69
|
140 twentieth century. The month may an integer, from 1 to 12, a month
|
jpayne@69
|
141 name, or a month abbreviation, where a period may optionally follow
|
jpayne@69
|
142 the abbreviation. The day must be an integer from 1 to the number of
|
jpayne@69
|
143 days in the month. The year, month, and day values may be separated
|
jpayne@69
|
144 by periods, hyphens, forward slashes, or spaces. Extra spaces are
|
jpayne@69
|
145 permitted around the delimiters. Year, month, and day values may be
|
jpayne@69
|
146 given in any order as long as it is possible to distinguish the
|
jpayne@69
|
147 components. If all three components are numbers that are less than
|
jpayne@69
|
148 13, then a month-day-year ordering is assumed.
|
jpayne@69
|
149
|
jpayne@69
|
150 The time component consists of hour, minute, and second values
|
jpayne@69
|
151 separated by colons. The hour value must be an integer between 0
|
jpayne@69
|
152 and 23 inclusively. The minute value must be an integer between 0
|
jpayne@69
|
153 and 59 inclusively. The second value may be an integer value
|
jpayne@69
|
154 between 0 and 59.999 inclusively. The second value or both the
|
jpayne@69
|
155 minute and second values may be omitted. The time may be followed
|
jpayne@69
|
156 by am or pm in upper or lower case, in which case a 12-hour clock is
|
jpayne@69
|
157 assumed.
|
jpayne@69
|
158
|
jpayne@69
|
159 * If the DateTime function is invoked with a single numeric argument,
|
jpayne@69
|
160 the number is assumed to be either a floating point value such as
|
jpayne@69
|
161 that returned by time.time(), or a number of days after January 1,
|
jpayne@69
|
162 1901 00:00:00 UTC.
|
jpayne@69
|
163
|
jpayne@69
|
164 A DateTime object is returned that represents either the GMT value
|
jpayne@69
|
165 of the time.time() float represented in the local machine's
|
jpayne@69
|
166 timezone, or that number of days after January 1, 1901. Note that
|
jpayne@69
|
167 the number of days after 1901 need to be expressed from the
|
jpayne@69
|
168 viewpoint of the local machine's timezone. A negative argument will
|
jpayne@69
|
169 yield a date-time value before 1901.
|
jpayne@69
|
170
|
jpayne@69
|
171 * If the function is invoked with two numeric arguments, then the
|
jpayne@69
|
172 first is taken to be an integer year and the second argument is
|
jpayne@69
|
173 taken to be an offset in days from the beginning of the year, in the
|
jpayne@69
|
174 context of the local machine timezone. The date-time value returned
|
jpayne@69
|
175 is the given offset number of days from the beginning of the given
|
jpayne@69
|
176 year, represented in the timezone of the local machine. The offset
|
jpayne@69
|
177 may be positive or negative. Two-digit years are assumed to be in
|
jpayne@69
|
178 the twentieth century.
|
jpayne@69
|
179
|
jpayne@69
|
180 * If the function is invoked with two arguments, the first a float
|
jpayne@69
|
181 representing a number of seconds past the epoch in GMT (such as
|
jpayne@69
|
182 those returned by time.time()) and the second a string naming a
|
jpayne@69
|
183 recognized timezone, a DateTime with a value of that GMT time will
|
jpayne@69
|
184 be returned, represented in the given timezone.
|
jpayne@69
|
185
|
jpayne@69
|
186 >>> import time
|
jpayne@69
|
187 >>> t = time.time()
|
jpayne@69
|
188
|
jpayne@69
|
189 Time t represented as US/Eastern:
|
jpayne@69
|
190
|
jpayne@69
|
191 >>> now_east = DateTime(t, 'US/Eastern')
|
jpayne@69
|
192
|
jpayne@69
|
193 Time t represented as US/Pacific:
|
jpayne@69
|
194
|
jpayne@69
|
195 >>> now_west = DateTime(t, 'US/Pacific')
|
jpayne@69
|
196
|
jpayne@69
|
197 Only their representations are different:
|
jpayne@69
|
198
|
jpayne@69
|
199 >>> now_east.equalTo(now_west)
|
jpayne@69
|
200 True
|
jpayne@69
|
201
|
jpayne@69
|
202 * If the function is invoked with three or more numeric arguments,
|
jpayne@69
|
203 then the first is taken to be an integer year, the second is taken
|
jpayne@69
|
204 to be an integer month, and the third is taken to be an integer day.
|
jpayne@69
|
205 If the combination of values is not valid, then a DateTimeError is
|
jpayne@69
|
206 raised. One- or two-digit years up to 69 are assumed to be in the
|
jpayne@69
|
207 21st century, whereas values 70-99 are assumed to be 20th century.
|
jpayne@69
|
208 The fourth, fifth, and sixth arguments are floating point, positive
|
jpayne@69
|
209 or negative offsets in units of hours, minutes, and days, and
|
jpayne@69
|
210 default to zero if not given. An optional string may be given as
|
jpayne@69
|
211 the final argument to indicate timezone (the effect of this is as if
|
jpayne@69
|
212 you had taken the value of time.time() at that time on a machine in
|
jpayne@69
|
213 the specified timezone).
|
jpayne@69
|
214
|
jpayne@69
|
215 If a string argument passed to the DateTime constructor cannot be
|
jpayne@69
|
216 parsed, it will raise SyntaxError. Invalid date, time, or
|
jpayne@69
|
217 timezone components will raise a DateTimeError.
|
jpayne@69
|
218
|
jpayne@69
|
219 The module function Timezones() will return a list of the timezones
|
jpayne@69
|
220 recognized by the DateTime module. Recognition of timezone names is
|
jpayne@69
|
221 case-insensitive.
|
jpayne@69
|
222
|
jpayne@69
|
223 Instance Methods for DateTime (IDateTime interface)
|
jpayne@69
|
224 ---------------------------------------------------
|
jpayne@69
|
225
|
jpayne@69
|
226 Conversion and comparison methods
|
jpayne@69
|
227 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
jpayne@69
|
228
|
jpayne@69
|
229 * ``timeTime()`` returns the date/time as a floating-point number in
|
jpayne@69
|
230 UTC, in the format used by the Python time module. Note that it is
|
jpayne@69
|
231 possible to create date /time values with DateTime that have no
|
jpayne@69
|
232 meaningful value to the time module, and in such cases a
|
jpayne@69
|
233 DateTimeError is raised. A DateTime object's value must generally
|
jpayne@69
|
234 be between Jan 1, 1970 (or your local machine epoch) and Jan 2038 to
|
jpayne@69
|
235 produce a valid time.time() style value.
|
jpayne@69
|
236
|
jpayne@69
|
237 >>> dt = DateTime('Mar 9, 1997 13:45:00 US/Eastern')
|
jpayne@69
|
238 >>> dt.timeTime()
|
jpayne@69
|
239 857933100.0
|
jpayne@69
|
240
|
jpayne@69
|
241 >>> DateTime('2040/01/01 UTC').timeTime()
|
jpayne@69
|
242 2208988800.0
|
jpayne@69
|
243
|
jpayne@69
|
244 >>> DateTime('1900/01/01 UTC').timeTime()
|
jpayne@69
|
245 -2208988800.0
|
jpayne@69
|
246
|
jpayne@69
|
247 * ``toZone(z)`` returns a DateTime with the value as the current
|
jpayne@69
|
248 object, represented in the indicated timezone:
|
jpayne@69
|
249
|
jpayne@69
|
250 >>> dt.toZone('UTC')
|
jpayne@69
|
251 DateTime('1997/03/09 18:45:00 UTC')
|
jpayne@69
|
252
|
jpayne@69
|
253 >>> dt.toZone('UTC').equalTo(dt)
|
jpayne@69
|
254 True
|
jpayne@69
|
255
|
jpayne@69
|
256 * ``isFuture()`` returns true if this object represents a date/time
|
jpayne@69
|
257 later than the time of the call:
|
jpayne@69
|
258
|
jpayne@69
|
259 >>> dt.isFuture()
|
jpayne@69
|
260 False
|
jpayne@69
|
261 >>> DateTime('Jan 1 3000').isFuture() # not time-machine safe!
|
jpayne@69
|
262 True
|
jpayne@69
|
263
|
jpayne@69
|
264 * ``isPast()`` returns true if this object represents a date/time
|
jpayne@69
|
265 earlier than the time of the call:
|
jpayne@69
|
266
|
jpayne@69
|
267 >>> dt.isPast()
|
jpayne@69
|
268 True
|
jpayne@69
|
269 >>> DateTime('Jan 1 3000').isPast() # not time-machine safe!
|
jpayne@69
|
270 False
|
jpayne@69
|
271
|
jpayne@69
|
272 * ``isCurrentYear()`` returns true if this object represents a
|
jpayne@69
|
273 date/time that falls within the current year, in the context of this
|
jpayne@69
|
274 object's timezone representation:
|
jpayne@69
|
275
|
jpayne@69
|
276 >>> dt.isCurrentYear()
|
jpayne@69
|
277 False
|
jpayne@69
|
278 >>> DateTime().isCurrentYear()
|
jpayne@69
|
279 True
|
jpayne@69
|
280
|
jpayne@69
|
281 * ``isCurrentMonth()`` returns true if this object represents a
|
jpayne@69
|
282 date/time that falls within the current month, in the context of
|
jpayne@69
|
283 this object's timezone representation:
|
jpayne@69
|
284
|
jpayne@69
|
285 >>> dt.isCurrentMonth()
|
jpayne@69
|
286 False
|
jpayne@69
|
287 >>> DateTime().isCurrentMonth()
|
jpayne@69
|
288 True
|
jpayne@69
|
289
|
jpayne@69
|
290 * ``isCurrentDay()`` returns true if this object represents a
|
jpayne@69
|
291 date/time that falls within the current day, in the context of this
|
jpayne@69
|
292 object's timezone representation:
|
jpayne@69
|
293
|
jpayne@69
|
294 >>> dt.isCurrentDay()
|
jpayne@69
|
295 False
|
jpayne@69
|
296 >>> DateTime().isCurrentDay()
|
jpayne@69
|
297 True
|
jpayne@69
|
298
|
jpayne@69
|
299 * ``isCurrentHour()`` returns true if this object represents a
|
jpayne@69
|
300 date/time that falls within the current hour, in the context of this
|
jpayne@69
|
301 object's timezone representation:
|
jpayne@69
|
302
|
jpayne@69
|
303 >>> dt.isCurrentHour()
|
jpayne@69
|
304 False
|
jpayne@69
|
305
|
jpayne@69
|
306 >>> DateTime().isCurrentHour()
|
jpayne@69
|
307 True
|
jpayne@69
|
308
|
jpayne@69
|
309 * ``isCurrentMinute()`` returns true if this object represents a
|
jpayne@69
|
310 date/time that falls within the current minute, in the context of
|
jpayne@69
|
311 this object's timezone representation:
|
jpayne@69
|
312
|
jpayne@69
|
313 >>> dt.isCurrentMinute()
|
jpayne@69
|
314 False
|
jpayne@69
|
315 >>> DateTime().isCurrentMinute()
|
jpayne@69
|
316 True
|
jpayne@69
|
317
|
jpayne@69
|
318 * ``isLeapYear()`` returns true if the current year (in the context of
|
jpayne@69
|
319 the object's timezone) is a leap year:
|
jpayne@69
|
320
|
jpayne@69
|
321 >>> dt.isLeapYear()
|
jpayne@69
|
322 False
|
jpayne@69
|
323 >>> DateTime('Mar 8 2004').isLeapYear()
|
jpayne@69
|
324 True
|
jpayne@69
|
325
|
jpayne@69
|
326 * ``earliestTime()`` returns a new DateTime object that represents the
|
jpayne@69
|
327 earliest possible time (in whole seconds) that still falls within
|
jpayne@69
|
328 the current object's day, in the object's timezone context:
|
jpayne@69
|
329
|
jpayne@69
|
330 >>> dt.earliestTime()
|
jpayne@69
|
331 DateTime('1997/03/09 00:00:00 US/Eastern')
|
jpayne@69
|
332
|
jpayne@69
|
333 * ``latestTime()`` return a new DateTime object that represents the
|
jpayne@69
|
334 latest possible time (in whole seconds) that still falls within the
|
jpayne@69
|
335 current object's day, in the object's timezone context
|
jpayne@69
|
336
|
jpayne@69
|
337 >>> dt.latestTime()
|
jpayne@69
|
338 DateTime('1997/03/09 23:59:59 US/Eastern')
|
jpayne@69
|
339
|
jpayne@69
|
340 Component access
|
jpayne@69
|
341 ~~~~~~~~~~~~~~~~
|
jpayne@69
|
342
|
jpayne@69
|
343 * ``parts()`` returns a tuple containing the calendar year, month,
|
jpayne@69
|
344 day, hour, minute second and timezone of the object
|
jpayne@69
|
345
|
jpayne@69
|
346 >>> dt.parts() # doctest: +ELLIPSIS
|
jpayne@69
|
347 (1997, 3, 9, 13, 45, ... 'US/Eastern')
|
jpayne@69
|
348
|
jpayne@69
|
349 * ``timezone()`` returns the timezone in which the object is represented:
|
jpayne@69
|
350
|
jpayne@69
|
351 >>> dt.timezone() in Timezones()
|
jpayne@69
|
352 True
|
jpayne@69
|
353
|
jpayne@69
|
354 * ``tzoffset()`` returns the timezone offset for the objects timezone:
|
jpayne@69
|
355
|
jpayne@69
|
356 >>> dt.tzoffset()
|
jpayne@69
|
357 -18000
|
jpayne@69
|
358
|
jpayne@69
|
359 * ``year()`` returns the calendar year of the object:
|
jpayne@69
|
360
|
jpayne@69
|
361 >>> dt.year()
|
jpayne@69
|
362 1997
|
jpayne@69
|
363
|
jpayne@69
|
364 * ``month()`` returns the month of the object as an integer:
|
jpayne@69
|
365
|
jpayne@69
|
366 >>> dt.month()
|
jpayne@69
|
367 3
|
jpayne@69
|
368
|
jpayne@69
|
369 * ``Month()`` returns the full month name:
|
jpayne@69
|
370
|
jpayne@69
|
371 >>> dt.Month()
|
jpayne@69
|
372 'March'
|
jpayne@69
|
373
|
jpayne@69
|
374 * ``aMonth()`` returns the abbreviated month name:
|
jpayne@69
|
375
|
jpayne@69
|
376 >>> dt.aMonth()
|
jpayne@69
|
377 'Mar'
|
jpayne@69
|
378
|
jpayne@69
|
379 * ``pMonth()`` returns the abbreviated (with period) month name:
|
jpayne@69
|
380
|
jpayne@69
|
381 >>> dt.pMonth()
|
jpayne@69
|
382 'Mar.'
|
jpayne@69
|
383
|
jpayne@69
|
384 * ``day()`` returns the integer day:
|
jpayne@69
|
385
|
jpayne@69
|
386 >>> dt.day()
|
jpayne@69
|
387 9
|
jpayne@69
|
388
|
jpayne@69
|
389 * ``Day()`` returns the full name of the day of the week:
|
jpayne@69
|
390
|
jpayne@69
|
391 >>> dt.Day()
|
jpayne@69
|
392 'Sunday'
|
jpayne@69
|
393
|
jpayne@69
|
394 * ``dayOfYear()`` returns the day of the year, in context of the
|
jpayne@69
|
395 timezone representation of the object:
|
jpayne@69
|
396
|
jpayne@69
|
397 >>> dt.dayOfYear()
|
jpayne@69
|
398 68
|
jpayne@69
|
399
|
jpayne@69
|
400 * ``aDay()`` returns the abbreviated name of the day of the week:
|
jpayne@69
|
401
|
jpayne@69
|
402 >>> dt.aDay()
|
jpayne@69
|
403 'Sun'
|
jpayne@69
|
404
|
jpayne@69
|
405 * ``pDay()`` returns the abbreviated (with period) name of the day of
|
jpayne@69
|
406 the week:
|
jpayne@69
|
407
|
jpayne@69
|
408 >>> dt.pDay()
|
jpayne@69
|
409 'Sun.'
|
jpayne@69
|
410
|
jpayne@69
|
411 * ``dow()`` returns the integer day of the week, where Sunday is 0:
|
jpayne@69
|
412
|
jpayne@69
|
413 >>> dt.dow()
|
jpayne@69
|
414 0
|
jpayne@69
|
415
|
jpayne@69
|
416 * ``dow_1()`` returns the integer day of the week, where sunday is 1:
|
jpayne@69
|
417
|
jpayne@69
|
418 >>> dt.dow_1()
|
jpayne@69
|
419 1
|
jpayne@69
|
420
|
jpayne@69
|
421 * ``h_12()`` returns the 12-hour clock representation of the hour:
|
jpayne@69
|
422
|
jpayne@69
|
423 >>> dt.h_12()
|
jpayne@69
|
424 1
|
jpayne@69
|
425
|
jpayne@69
|
426 * ``h_24()`` returns the 24-hour clock representation of the hour:
|
jpayne@69
|
427
|
jpayne@69
|
428 >>> dt.h_24()
|
jpayne@69
|
429 13
|
jpayne@69
|
430
|
jpayne@69
|
431 * ``ampm()`` returns the appropriate time modifier (am or pm):
|
jpayne@69
|
432
|
jpayne@69
|
433 >>> dt.ampm()
|
jpayne@69
|
434 'pm'
|
jpayne@69
|
435
|
jpayne@69
|
436 * ``hour()`` returns the 24-hour clock representation of the hour:
|
jpayne@69
|
437
|
jpayne@69
|
438 >>> dt.hour()
|
jpayne@69
|
439 13
|
jpayne@69
|
440
|
jpayne@69
|
441 * ``minute()`` returns the minute:
|
jpayne@69
|
442
|
jpayne@69
|
443 >>> dt.minute()
|
jpayne@69
|
444 45
|
jpayne@69
|
445
|
jpayne@69
|
446 * ``second()`` returns the second:
|
jpayne@69
|
447
|
jpayne@69
|
448 >>> dt.second() == 0
|
jpayne@69
|
449 True
|
jpayne@69
|
450
|
jpayne@69
|
451 * ``millis()`` returns the milliseconds since the epoch in GMT.
|
jpayne@69
|
452
|
jpayne@69
|
453 >>> dt.millis() == 857933100000
|
jpayne@69
|
454 True
|
jpayne@69
|
455
|
jpayne@69
|
456 strftime()
|
jpayne@69
|
457 ~~~~~~~~~~
|
jpayne@69
|
458
|
jpayne@69
|
459 See ``tests/test_datetime.py``.
|
jpayne@69
|
460
|
jpayne@69
|
461 General formats from previous DateTime
|
jpayne@69
|
462 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
jpayne@69
|
463
|
jpayne@69
|
464 * ``Date()`` return the date string for the object:
|
jpayne@69
|
465
|
jpayne@69
|
466 >>> dt.Date()
|
jpayne@69
|
467 '1997/03/09'
|
jpayne@69
|
468
|
jpayne@69
|
469 * ``Time()`` returns the time string for an object to the nearest
|
jpayne@69
|
470 second:
|
jpayne@69
|
471
|
jpayne@69
|
472 >>> dt.Time()
|
jpayne@69
|
473 '13:45:00'
|
jpayne@69
|
474
|
jpayne@69
|
475 * ``TimeMinutes()`` returns the time string for an object not showing
|
jpayne@69
|
476 seconds:
|
jpayne@69
|
477
|
jpayne@69
|
478 >>> dt.TimeMinutes()
|
jpayne@69
|
479 '13:45'
|
jpayne@69
|
480
|
jpayne@69
|
481 * ``AMPM()`` returns the time string for an object to the nearest second:
|
jpayne@69
|
482
|
jpayne@69
|
483 >>> dt.AMPM()
|
jpayne@69
|
484 '01:45:00 pm'
|
jpayne@69
|
485
|
jpayne@69
|
486 * ``AMPMMinutes()`` returns the time string for an object not showing
|
jpayne@69
|
487 seconds:
|
jpayne@69
|
488
|
jpayne@69
|
489 >>> dt.AMPMMinutes()
|
jpayne@69
|
490 '01:45 pm'
|
jpayne@69
|
491
|
jpayne@69
|
492 * ``PreciseTime()`` returns the time string for the object:
|
jpayne@69
|
493
|
jpayne@69
|
494 >>> dt.PreciseTime()
|
jpayne@69
|
495 '13:45:00.000'
|
jpayne@69
|
496
|
jpayne@69
|
497 * ``PreciseAMPM()`` returns the time string for the object:
|
jpayne@69
|
498
|
jpayne@69
|
499 >>> dt.PreciseAMPM()
|
jpayne@69
|
500 '01:45:00.000 pm'
|
jpayne@69
|
501
|
jpayne@69
|
502 * ``yy()`` returns the calendar year as a 2 digit string
|
jpayne@69
|
503
|
jpayne@69
|
504 >>> dt.yy()
|
jpayne@69
|
505 '97'
|
jpayne@69
|
506
|
jpayne@69
|
507 * ``mm()`` returns the month as a 2 digit string
|
jpayne@69
|
508
|
jpayne@69
|
509 >>> dt.mm()
|
jpayne@69
|
510 '03'
|
jpayne@69
|
511
|
jpayne@69
|
512 * ``dd()`` returns the day as a 2 digit string:
|
jpayne@69
|
513
|
jpayne@69
|
514 >>> dt.dd()
|
jpayne@69
|
515 '09'
|
jpayne@69
|
516
|
jpayne@69
|
517 * ``rfc822()`` returns the date in RFC 822 format:
|
jpayne@69
|
518
|
jpayne@69
|
519 >>> dt.rfc822()
|
jpayne@69
|
520 'Sun, 09 Mar 1997 13:45:00 -0500'
|
jpayne@69
|
521
|
jpayne@69
|
522 New formats
|
jpayne@69
|
523 ~~~~~~~~~~~
|
jpayne@69
|
524
|
jpayne@69
|
525 * ``fCommon()`` returns a string representing the object's value in
|
jpayne@69
|
526 the format: March 9, 1997 1:45 pm:
|
jpayne@69
|
527
|
jpayne@69
|
528 >>> dt.fCommon()
|
jpayne@69
|
529 'March 9, 1997 1:45 pm'
|
jpayne@69
|
530
|
jpayne@69
|
531 * ``fCommonZ()`` returns a string representing the object's value in
|
jpayne@69
|
532 the format: March 9, 1997 1:45 pm US/Eastern:
|
jpayne@69
|
533
|
jpayne@69
|
534 >>> dt.fCommonZ()
|
jpayne@69
|
535 'March 9, 1997 1:45 pm US/Eastern'
|
jpayne@69
|
536
|
jpayne@69
|
537 * ``aCommon()`` returns a string representing the object's value in
|
jpayne@69
|
538 the format: Mar 9, 1997 1:45 pm:
|
jpayne@69
|
539
|
jpayne@69
|
540 >>> dt.aCommon()
|
jpayne@69
|
541 'Mar 9, 1997 1:45 pm'
|
jpayne@69
|
542
|
jpayne@69
|
543 * ``aCommonZ()`` return a string representing the object's value in
|
jpayne@69
|
544 the format: Mar 9, 1997 1:45 pm US/Eastern:
|
jpayne@69
|
545
|
jpayne@69
|
546 >>> dt.aCommonZ()
|
jpayne@69
|
547 'Mar 9, 1997 1:45 pm US/Eastern'
|
jpayne@69
|
548
|
jpayne@69
|
549 * ``pCommon()`` returns a string representing the object's value in
|
jpayne@69
|
550 the format Mar. 9, 1997 1:45 pm:
|
jpayne@69
|
551
|
jpayne@69
|
552 >>> dt.pCommon()
|
jpayne@69
|
553 'Mar. 9, 1997 1:45 pm'
|
jpayne@69
|
554
|
jpayne@69
|
555 * ``pCommonZ()`` returns a string representing the object's value in
|
jpayne@69
|
556 the format: Mar. 9, 1997 1:45 pm US/Eastern:
|
jpayne@69
|
557
|
jpayne@69
|
558 >>> dt.pCommonZ()
|
jpayne@69
|
559 'Mar. 9, 1997 1:45 pm US/Eastern'
|
jpayne@69
|
560
|
jpayne@69
|
561 * ``ISO()`` returns a string with the date/time in ISO format. Note:
|
jpayne@69
|
562 this is not ISO 8601-format! See the ISO8601 and HTML4 methods below
|
jpayne@69
|
563 for ISO 8601-compliant output. Dates are output as: YYYY-MM-DD HH:MM:SS
|
jpayne@69
|
564
|
jpayne@69
|
565 >>> dt.ISO()
|
jpayne@69
|
566 '1997-03-09 13:45:00'
|
jpayne@69
|
567
|
jpayne@69
|
568 * ``ISO8601()`` returns the object in ISO 8601-compatible format
|
jpayne@69
|
569 containing the date, time with seconds-precision and the time zone
|
jpayne@69
|
570 identifier - see http://www.w3.org/TR/NOTE-datetime. Dates are
|
jpayne@69
|
571 output as: YYYY-MM-DDTHH:MM:SSTZD (T is a literal character, TZD is
|
jpayne@69
|
572 Time Zone Designator, format +HH:MM or -HH:MM).
|
jpayne@69
|
573
|
jpayne@69
|
574 The ``HTML4()`` method below offers the same formatting, but
|
jpayne@69
|
575 converts to UTC before returning the value and sets the TZD"Z"
|
jpayne@69
|
576
|
jpayne@69
|
577 >>> dt.ISO8601()
|
jpayne@69
|
578 '1997-03-09T13:45:00-05:00'
|
jpayne@69
|
579
|
jpayne@69
|
580
|
jpayne@69
|
581 * ``HTML4()`` returns the object in the format used in the HTML4.0
|
jpayne@69
|
582 specification, one of the standard forms in ISO8601. See
|
jpayne@69
|
583 http://www.w3.org/TR/NOTE-datetime. Dates are output as:
|
jpayne@69
|
584 YYYY-MM-DDTHH:MM:SSZ (T, Z are literal characters, the time is in
|
jpayne@69
|
585 UTC.):
|
jpayne@69
|
586
|
jpayne@69
|
587 >>> dt.HTML4()
|
jpayne@69
|
588 '1997-03-09T18:45:00Z'
|
jpayne@69
|
589
|
jpayne@69
|
590 * ``JulianDay()`` returns the Julian day according to
|
jpayne@69
|
591 http://www.tondering.dk/claus/cal/node3.html#sec-calcjd
|
jpayne@69
|
592
|
jpayne@69
|
593 >>> dt.JulianDay()
|
jpayne@69
|
594 2450517
|
jpayne@69
|
595
|
jpayne@69
|
596 * ``week()`` returns the week number according to ISO
|
jpayne@69
|
597 see http://www.tondering.dk/claus/cal/node6.html#SECTION00670000000000000000
|
jpayne@69
|
598
|
jpayne@69
|
599 >>> dt.week()
|
jpayne@69
|
600 10
|
jpayne@69
|
601
|
jpayne@69
|
602 Deprecated API
|
jpayne@69
|
603 ~~~~~~~~~~~~~~
|
jpayne@69
|
604
|
jpayne@69
|
605 * DayOfWeek(): see Day()
|
jpayne@69
|
606
|
jpayne@69
|
607 * Day_(): see pDay()
|
jpayne@69
|
608
|
jpayne@69
|
609 * Mon(): see aMonth()
|
jpayne@69
|
610
|
jpayne@69
|
611 * Mon_(): see pMonth
|
jpayne@69
|
612
|
jpayne@69
|
613 General Services Provided by DateTime
|
jpayne@69
|
614 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
jpayne@69
|
615
|
jpayne@69
|
616 DateTimes can be repr()'ed; the result will be a string indicating how
|
jpayne@69
|
617 to make a DateTime object like this:
|
jpayne@69
|
618
|
jpayne@69
|
619 >>> repr(dt)
|
jpayne@69
|
620 "DateTime('1997/03/09 13:45:00 US/Eastern')"
|
jpayne@69
|
621
|
jpayne@69
|
622 When we convert them into a string, we get a nicer string that could
|
jpayne@69
|
623 actually be shown to a user:
|
jpayne@69
|
624
|
jpayne@69
|
625 >>> str(dt)
|
jpayne@69
|
626 '1997/03/09 13:45:00 US/Eastern'
|
jpayne@69
|
627
|
jpayne@69
|
628 The hash value of a DateTime is based on the date and time and is
|
jpayne@69
|
629 equal for different representations of the DateTime:
|
jpayne@69
|
630
|
jpayne@69
|
631 >>> hash(dt)
|
jpayne@69
|
632 3618678
|
jpayne@69
|
633 >>> hash(dt.toZone('UTC'))
|
jpayne@69
|
634 3618678
|
jpayne@69
|
635
|
jpayne@69
|
636 DateTime objects can be compared to other DateTime objects OR floating
|
jpayne@69
|
637 point numbers such as the ones which are returned by the Python time
|
jpayne@69
|
638 module by using the equalTo method. Using this API, True is returned if the
|
jpayne@69
|
639 object represents a date/time equal to the specified DateTime or time module
|
jpayne@69
|
640 style time:
|
jpayne@69
|
641
|
jpayne@69
|
642 >>> dt.equalTo(dt)
|
jpayne@69
|
643 True
|
jpayne@69
|
644 >>> dt.equalTo(dt.toZone('UTC'))
|
jpayne@69
|
645 True
|
jpayne@69
|
646 >>> dt.equalTo(dt.timeTime())
|
jpayne@69
|
647 True
|
jpayne@69
|
648 >>> dt.equalTo(DateTime())
|
jpayne@69
|
649 False
|
jpayne@69
|
650
|
jpayne@69
|
651 Same goes for inequalities:
|
jpayne@69
|
652
|
jpayne@69
|
653 >>> dt.notEqualTo(dt)
|
jpayne@69
|
654 False
|
jpayne@69
|
655 >>> dt.notEqualTo(dt.toZone('UTC'))
|
jpayne@69
|
656 False
|
jpayne@69
|
657 >>> dt.notEqualTo(dt.timeTime())
|
jpayne@69
|
658 False
|
jpayne@69
|
659 >>> dt.notEqualTo(DateTime())
|
jpayne@69
|
660 True
|
jpayne@69
|
661
|
jpayne@69
|
662 Normal equality operations only work with DateTime objects and take the
|
jpayne@69
|
663 timezone setting into account:
|
jpayne@69
|
664
|
jpayne@69
|
665 >>> dt == dt
|
jpayne@69
|
666 True
|
jpayne@69
|
667 >>> dt == dt.toZone('UTC')
|
jpayne@69
|
668 False
|
jpayne@69
|
669 >>> dt == DateTime()
|
jpayne@69
|
670 False
|
jpayne@69
|
671
|
jpayne@69
|
672 >>> dt != dt
|
jpayne@69
|
673 False
|
jpayne@69
|
674 >>> dt != dt.toZone('UTC')
|
jpayne@69
|
675 True
|
jpayne@69
|
676 >>> dt != DateTime()
|
jpayne@69
|
677 True
|
jpayne@69
|
678
|
jpayne@69
|
679 But the other comparison operations compare the referenced moment in time and
|
jpayne@69
|
680 not the representation itself:
|
jpayne@69
|
681
|
jpayne@69
|
682 >>> dt > dt
|
jpayne@69
|
683 False
|
jpayne@69
|
684 >>> DateTime() > dt
|
jpayne@69
|
685 True
|
jpayne@69
|
686 >>> dt > DateTime().timeTime()
|
jpayne@69
|
687 False
|
jpayne@69
|
688 >>> DateTime().timeTime() > dt
|
jpayne@69
|
689 True
|
jpayne@69
|
690
|
jpayne@69
|
691 >>> dt.greaterThan(dt)
|
jpayne@69
|
692 False
|
jpayne@69
|
693 >>> DateTime().greaterThan(dt)
|
jpayne@69
|
694 True
|
jpayne@69
|
695 >>> dt.greaterThan(DateTime().timeTime())
|
jpayne@69
|
696 False
|
jpayne@69
|
697
|
jpayne@69
|
698 >>> dt >= dt
|
jpayne@69
|
699 True
|
jpayne@69
|
700 >>> DateTime() >= dt
|
jpayne@69
|
701 True
|
jpayne@69
|
702 >>> dt >= DateTime().timeTime()
|
jpayne@69
|
703 False
|
jpayne@69
|
704 >>> DateTime().timeTime() >= dt
|
jpayne@69
|
705 True
|
jpayne@69
|
706
|
jpayne@69
|
707 >>> dt.greaterThanEqualTo(dt)
|
jpayne@69
|
708 True
|
jpayne@69
|
709 >>> DateTime().greaterThanEqualTo(dt)
|
jpayne@69
|
710 True
|
jpayne@69
|
711 >>> dt.greaterThanEqualTo(DateTime().timeTime())
|
jpayne@69
|
712 False
|
jpayne@69
|
713
|
jpayne@69
|
714 >>> dt < dt
|
jpayne@69
|
715 False
|
jpayne@69
|
716 >>> DateTime() < dt
|
jpayne@69
|
717 False
|
jpayne@69
|
718 >>> dt < DateTime().timeTime()
|
jpayne@69
|
719 True
|
jpayne@69
|
720 >>> DateTime().timeTime() < dt
|
jpayne@69
|
721 False
|
jpayne@69
|
722
|
jpayne@69
|
723 >>> dt.lessThan(dt)
|
jpayne@69
|
724 False
|
jpayne@69
|
725 >>> DateTime().lessThan(dt)
|
jpayne@69
|
726 False
|
jpayne@69
|
727 >>> dt.lessThan(DateTime().timeTime())
|
jpayne@69
|
728 True
|
jpayne@69
|
729
|
jpayne@69
|
730 >>> dt <= dt
|
jpayne@69
|
731 True
|
jpayne@69
|
732 >>> DateTime() <= dt
|
jpayne@69
|
733 False
|
jpayne@69
|
734 >>> dt <= DateTime().timeTime()
|
jpayne@69
|
735 True
|
jpayne@69
|
736 >>> DateTime().timeTime() <= dt
|
jpayne@69
|
737 False
|
jpayne@69
|
738
|
jpayne@69
|
739 >>> dt.lessThanEqualTo(dt)
|
jpayne@69
|
740 True
|
jpayne@69
|
741 >>> DateTime().lessThanEqualTo(dt)
|
jpayne@69
|
742 False
|
jpayne@69
|
743 >>> dt.lessThanEqualTo(DateTime().timeTime())
|
jpayne@69
|
744 True
|
jpayne@69
|
745
|
jpayne@69
|
746 Numeric Services Provided by DateTime
|
jpayne@69
|
747 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
jpayne@69
|
748
|
jpayne@69
|
749 A DateTime may be added to a number and a number may be added to a
|
jpayne@69
|
750 DateTime:
|
jpayne@69
|
751
|
jpayne@69
|
752 >>> dt + 5
|
jpayne@69
|
753 DateTime('1997/03/14 13:45:00 US/Eastern')
|
jpayne@69
|
754 >>> 5 + dt
|
jpayne@69
|
755 DateTime('1997/03/14 13:45:00 US/Eastern')
|
jpayne@69
|
756
|
jpayne@69
|
757 Two DateTimes cannot be added:
|
jpayne@69
|
758
|
jpayne@69
|
759 >>> from DateTime.interfaces import DateTimeError
|
jpayne@69
|
760 >>> try:
|
jpayne@69
|
761 ... dt + dt
|
jpayne@69
|
762 ... print('fail')
|
jpayne@69
|
763 ... except DateTimeError:
|
jpayne@69
|
764 ... print('ok')
|
jpayne@69
|
765 ok
|
jpayne@69
|
766
|
jpayne@69
|
767 Either a DateTime or a number may be subtracted from a DateTime,
|
jpayne@69
|
768 however, a DateTime may not be subtracted from a number:
|
jpayne@69
|
769
|
jpayne@69
|
770 >>> DateTime('1997/03/10 13:45 US/Eastern') - dt
|
jpayne@69
|
771 1.0
|
jpayne@69
|
772 >>> dt - 1
|
jpayne@69
|
773 DateTime('1997/03/08 13:45:00 US/Eastern')
|
jpayne@69
|
774 >>> 1 - dt
|
jpayne@69
|
775 Traceback (most recent call last):
|
jpayne@69
|
776 ...
|
jpayne@69
|
777 TypeError: unsupported operand type(s) for -: 'int' and 'DateTime'
|
jpayne@69
|
778
|
jpayne@69
|
779 DateTimes can also be converted to integers (number of seconds since
|
jpayne@69
|
780 the epoch) and floats:
|
jpayne@69
|
781
|
jpayne@69
|
782 >>> int(dt)
|
jpayne@69
|
783 857933100
|
jpayne@69
|
784 >>> float(dt)
|
jpayne@69
|
785 857933100.0
|