Erik Grinaker is sharing code with you

Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.

Don't show this again

erikg / python-chrono

A Python module for simple and convenient date/time handling

Clone this repository (size: 1.2 MB): HTTPS / SSH
hg clone https://bitbucket.org/erikg/python-chrono
hg clone ssh://hg@bitbucket.org/erikg/python-chrono

python-chrono / doc / source / usage.rst

commit
20944f811650
parent
31a02f854246
branch
default

Fixed incorrect output for doctest blocks

1
9827f80818dc
.. _usage:
2
9827f80818dc
3
9827f80818dc
Usage
4
9827f80818dc
=====
5
9827f80818dc
6
9827f80818dc
The main classes are :class:`chrono.Date`, :class:`chrono.Time`, and
7
9827f80818dc
:class:`chrono.DateTime`, which handles dates, times, and date/times
8
9827f80818dc
respectively. A range of other classes are also available, which provide
9
e648d55fdf4c
the functionality that these classes build upon.
10
9827f80818dc
11
e648d55fdf4c
python-chrono does not include any handling of time zones or daylight
12
9827f80818dc
savings time, but this is planned for a future release.
13
9827f80818dc
14
9827f80818dc
The following sections describe some typical usage of the
15
9827f80818dc
:class:`chrono.Date` class. The :class:`chrono.Time` and :class:`chrono.DateTime`
16
ad7845283d50
classes behave in much the same way, but for simplicity only :class:`chrono.Date` is
17
ad7845283d50
covered here.
18
9827f80818dc
19
2c5482bf4e5c
Parsing
20
2c5482bf4e5c
-------
21
2c5482bf4e5c
22
2c5482bf4e5c
Dates can be specified using either ISO, US, or european formats. Lists
23
2c5482bf4e5c
of valid formats are available in the :mod:`chrono.parser` documentation.
24
2c5482bf4e5c
25
333b28ba2b2d
By default, python-chrono uses the parser set in
26
333b28ba2b2d
:attr:`chrono.DEFAULT_PARSER` - normally :class:`chrono.parser.CommonParser`,
27
2c5482bf4e5c
which accepts the most commonly used date formats. The notable exceptions
28
2c5482bf4e5c
are formats without separators (which for example can be interpreted as
29
2c5482bf4e5c
either US or european dates), and unusual separators such as . in US dates
30
2c5482bf4e5c
(which is the standard separator in Europe). In order to parse such formats,
31
333b28ba2b2d
you need to either set another default in :attr:`chrono.DEFAULT_PARSER`, or
32
333b28ba2b2d
pass the proper parser to :class:`chrono.Date`.
33
9827f80818dc
34
9827f80818dc
Date parsing is done simply by instantiating a :class:`chrono.Date` object,
35
9827f80818dc
passing the date string to be parsed as input. Once instantiated, the
36
9827f80818dc
attributes :attr:`chrono.Date.year`, :attr:`chrono.Date.month`, and
37
20944f811650
:attr:`chrono.Date.day` will contain the respective date parts:
38
9827f80818dc
39
e0a34d0b4e61
.. doctest::
40
e0a34d0b4e61
41
9827f80818dc
   >>> date = chrono.Date("2009-07-23")
42
9827f80818dc
   >>> date.year
43
9827f80818dc
   2009
44
9827f80818dc
   >>> date.month
45
9827f80818dc
   7
46
9827f80818dc
   >>> date.day
47
9827f80818dc
   23
48
9827f80818dc
49
20944f811650
To retrieve all the attributes at once, use :meth:`chrono.Date.get`:
50
9827f80818dc
51
e0a34d0b4e61
.. doctest::
52
e0a34d0b4e61
53
9827f80818dc
   >>> date = chrono.Date("2009-07-23")
54
9827f80818dc
   >>> date.get()
55
9827f80818dc
   (2009, 7, 23)
56
9827f80818dc
57
2c5482bf4e5c
The default :class:`chrono.parser.CommonParser` parser handles most normal
58
20944f811650
date formats, such as:
59
2c5482bf4e5c
60
e0a34d0b4e61
.. doctest::
61
e0a34d0b4e61
62
2c5482bf4e5c
   >>> # ISO dates
63
2c5482bf4e5c
   >>> chrono.Date("2009-07-23").get()
64
2c5482bf4e5c
   (2009, 7, 23)
65
2c5482bf4e5c
66
2c5482bf4e5c
   >>> # US dates
67
2c5482bf4e5c
   >>> chrono.Date("07/23/2009").get()
68
2c5482bf4e5c
   (2009, 7, 23)
69
2c5482bf4e5c
70
2c5482bf4e5c
   >>> # european dates
71
2c5482bf4e5c
   >>> chrono.Date("23.07.2009").get()
72
e0a34d0b4e61
   (2009, 7, 23)
73
2c5482bf4e5c
74
2c5482bf4e5c
   >>> # ISO week dates
75
2c5482bf4e5c
   >>> chrono.Date("2009-W32").get()
76
2c5482bf4e5c
   (2009, 8, 3)
77
2c5482bf4e5c
78
2c5482bf4e5c
   >>> # ISO ordinal dates
79
2c5482bf4e5c
   >>> chrono.Date("2009-314").get()
80
2c5482bf4e5c
   (2009, 11, 10)
81
2c5482bf4e5c
82
2c5482bf4e5c
   >>> # ISO month dates
83
2c5482bf4e5c
   >>> chrono.Date("2009-07").get()
84
2c5482bf4e5c
   (2009, 7, 1)
85
2c5482bf4e5c
86
2c5482bf4e5c
In order to parse all valid date formats for a region, you can pass the
87
20944f811650
proper parser class to :class:`chrono.Date`:
88
2c5482bf4e5c
89
e0a34d0b4e61
.. doctest::
90
e0a34d0b4e61
91
2c5482bf4e5c
   >>> # US dates with two-digit year and no separator
92
2c5482bf4e5c
   >>> chrono.Date("072309", chrono.parser.USParser).get()
93
2c5482bf4e5c
   (2009, 7, 23)
94
2c5482bf4e5c
95
2c5482bf4e5c
   >>> # slash-separated european dates
96
2c5482bf4e5c
   >>> chrono.Date("23/07/2009", chrono.parser.EuroParser).get()
97
2c5482bf4e5c
   (2009, 7, 23)
98
2c5482bf4e5c
99
9827f80818dc
If :class:`chrono.Date` is passed an invalid date it will raise either
100
9827f80818dc
:exc:`chrono.error.ParseError` for invalid/unknown format, or a subclass
101
9827f80818dc
of :exc:`chrono.error.DateError` (such as :exc:`chrono.error.MonthError`)
102
20944f811650
if the date was parsed properly but contained an invalid date value:
103
9827f80818dc
104
e0a34d0b4e61
.. doctest::
105
e0a34d0b4e61
106
9827f80818dc
   >>> date = chrono.Date("xyz")
107
e0a34d0b4e61
   Traceback (most recent call last):
108
e0a34d0b4e61
   ParseError: Invalid ISO date value 'xyz'
109
9827f80818dc
110
9827f80818dc
   >>> date = chrono.Date("2009-13-27")
111
e0a34d0b4e61
   Traceback (most recent call last):
112
e0a34d0b4e61
   MonthError: Month '13' not in range 1-12
113
9827f80818dc
114
9827f80818dc
You can also pass a range of non-string inputs to the class, which will
115
20944f811650
be handled according to the object type:
116
9827f80818dc
117
e0a34d0b4e61
.. doctest::
118
e0a34d0b4e61
119
2c5482bf4e5c
   >>> # boolean True indicates the current date
120
e0a34d0b4e61
   >>> chrono.Date(True).get() # doctest: +SKIP
121
9827f80818dc
   (2010, 1, 23)
122
9827f80818dc
123
9827f80818dc
   >>> # integers are interpreted as UNIX timestamps
124
9827f80818dc
   >>> chrono.Date(1263745408).get()
125
9827f80818dc
   (2010, 1, 17)
126
9827f80818dc
127
9827f80818dc
   >>> # fetch data from time.struct_time objects
128
e0a34d0b4e61
   >>> chrono.Date(time.localtime()).get() # doctest: +SKIP
129
9827f80818dc
   (2010, 1, 23)
130
9827f80818dc
131
9827f80818dc
   >>> # fetch data from datetime.date objects
132
9827f80818dc
   >>> chrono.Date(datetime.date(2010, 7, 23)).get()
133
9827f80818dc
   (2010, 7, 23)
134
9827f80818dc
135
2c5482bf4e5c
For a complete list of all accepted input types, see the :class:`chrono.Date`
136
2c5482bf4e5c
documentation.
137
9827f80818dc
138
ad7845283d50
To parse date strings without instantiating a :class:`chrono.Date` object, you
139
20944f811650
can use the parser classes directly:
140
9827f80818dc
141
e0a34d0b4e61
.. doctest::
142
e0a34d0b4e61
143
2c5482bf4e5c
   >>> # parses all supported ISO date formats
144
9827f80818dc
   >>> chrono.parser.ISOParser.parse_date("2009-07-23")
145
9827f80818dc
   (2009, 7, 23)
146
9827f80818dc
147
9827f80818dc
   >>> # only parses week dates
148
9827f80818dc
   >>> chrono.parser.ISOParser.week("2009-W32")
149
2c5482bf4e5c
   (2009, 8, 3)
150
9827f80818dc
151
9827f80818dc
   >>> # only parses ordinal dates
152
30f63cb889ab
   >>> chrono.parser.ISOParser.ordinal("2009-314")
153
2c5482bf4e5c
   (2009, 11, 10)
154
9827f80818dc
155
2c5482bf4e5c
See the :mod:`chrono.parser` documentation for more information on parser
156
2c5482bf4e5c
classes.
157
9827f80818dc
158
2c5482bf4e5c
Calendar info
159
2c5482bf4e5c
-------------
160
9827f80818dc
161
2c5482bf4e5c
python-chrono supports both the ISO and US calendars, which have the
162
2c5482bf4e5c
following characteristics:
163
9827f80818dc
164
2c5482bf4e5c
**ISO Calendar:**
165
9827f80818dc
166
9827f80818dc
 * Weeks start on Monday
167
9827f80818dc
 * The first week of a year is the week which contains the first Thursday
168
9827f80818dc
169
2c5482bf4e5c
**US Calendar:**
170
2c5482bf4e5c
171
2c5482bf4e5c
 * Weeks start on Sunday
172
2c5482bf4e5c
 * The first week of a year is the week which contains January 1st
173
2c5482bf4e5c
174
333b28ba2b2d
By default the calendar set in :attr:`chrono.DEFAULT_CALENDAR` is used,
175
333b28ba2b2d
normally :class:`chrono.calendar.ISOCalendar`. To use another calendar,
176
333b28ba2b2d
either set it as the default in :attr:`chrono.DEFAULT_CALENDAR`, or pass
177
333b28ba2b2d
the proper calendar to :class:`chrono.Date`. As can be seen above, this only
178
2c5482bf4e5c
affects functionality related to week numbers or week days.
179
2c5482bf4e5c
180
2c5482bf4e5c
:class:`chrono.Date` has a number of methods for retreiving calendar-related
181
20944f811650
information about about a date, such as:
182
9827f80818dc
183
e0a34d0b4e61
.. doctest::
184
e0a34d0b4e61
185
9827f80818dc
   >>> # week that contains the date
186
9827f80818dc
   >>> chrono.Date("2009-07-23").week()
187
9827f80818dc
   (2009, 30)
188
9827f80818dc
189
9827f80818dc
   >>> # whether the date is in a leap year
190
9827f80818dc
   >>> chrono.Date("2008-07-23").leapyear()
191
9827f80818dc
   True
192
9827f80818dc
193
9827f80818dc
   >>> # number of days in the month
194
9827f80818dc
   >>> chrono.Date("2009-07-23").monthdays()
195
9827f80818dc
   31
196
9827f80818dc
197
9827f80818dc
   >>> # weekday of the date
198
9827f80818dc
   >>> chrono.Date("2009-07-23").weekday()
199
9827f80818dc
   4
200
9827f80818dc
201
2c5482bf4e5c
To use the US calendar instead, pass the :class:`chrono.calendar.USCalendar`
202
20944f811650
class to :class:`chrono.Date`:
203
2c5482bf4e5c
204
e0a34d0b4e61
.. doctest::
205
e0a34d0b4e61
206
2c5482bf4e5c
   >>> # US week containing date
207
2c5482bf4e5c
   >>> chrono.Date("2009-07-23", calendar=chrono.calendar.USCalendar).week()
208
2c5482bf4e5c
   (2009, 30)
209
2c5482bf4e5c
210
e0a34d0b4e61
   >>> # US weekday of the date
211
2c5482bf4e5c
   >>> chrono.Date("2009-07-23", calendar=chrono.calendar.USCalendar).weekday()
212
2c5482bf4e5c
   5
213
2c5482bf4e5c
214
2c5482bf4e5c
For a full list of calendar-related methods, see the :class:`chrono.Date`
215
2c5482bf4e5c
documentation.
216
9827f80818dc
217
9827f80818dc
If you would like to retreive calendar information without having to
218
9827f80818dc
instantiate a :class:`chrono.Date` object, you can use the underlying
219
20944f811650
calendar class directly:
220
9827f80818dc
221
e0a34d0b4e61
.. doctest::
222
e0a34d0b4e61
223
9827f80818dc
   >>> chrono.calendar.ISOCalendar.yeardays(2008)
224
9827f80818dc
   366
225
9827f80818dc
226
9827f80818dc
   >>> chrono.calendar.ISOCalendar.ordinal(2009, 7, 23)
227
9827f80818dc
   204
228
9827f80818dc
229
9827f80818dc
   >>> chrono.calendar.ISOCalendar.weekdate(2009, 7, 23)
230
9827f80818dc
   (2009, 30, 4)
231
9827f80818dc
232
2c5482bf4e5c
See the :mod:`chrono.calendar` documentation for more
233
9827f80818dc
information.
234
9827f80818dc
235
2c5482bf4e5c
Arithmetic
236
2c5482bf4e5c
----------
237
9827f80818dc
238
9827f80818dc
Date arithmetic (addition, subtraction, etc) is done by special handling of
239
9827f80818dc
the :attr:`chrono.Date.year`, :attr:`chrono.Date.month`, and :attr:`chrono.Date.day`
240
9827f80818dc
attributes. If any of these are set to a value that is outside their valid range,
241
9827f80818dc
the object will automatically update the attributes to a proper date, by
242
9827f80818dc
incrementing or decrementing values as necessary.
243
9827f80818dc
244
20944f811650
Here are some examples:
245
9827f80818dc
246
e0a34d0b4e61
.. doctest::
247
e0a34d0b4e61
248
9827f80818dc
   >>> # adding days to a date
249
9827f80818dc
   >>> date = chrono.Date("2009-07-26")
250
9827f80818dc
   >>> date.day += 10
251
9827f80818dc
   >>> date.get()
252
9827f80818dc
   (2009, 8, 5)
253
9827f80818dc
254
9827f80818dc
   >>> # subtracting months from a date
255
9827f80818dc
   >>> date.month -= 2
256
9827f80818dc
   >>> date.get()
257
9827f80818dc
   (2009, 6, 5)
258
9827f80818dc
259
9827f80818dc
   >>> # adding years to a date
260
9827f80818dc
   >>> date.year += 3
261
9827f80818dc
   >>> date.get()
262
9827f80818dc
   (2012, 6, 5)
263
9827f80818dc
264
9827f80818dc
.. warning::
265
9827f80818dc
266
9827f80818dc
   When the date is on one of the last days of a month, and the :attr:`chrono.Date.month` or
267
9827f80818dc
   :attr:`chrono.Date.year` attribute is changed, you may get a result which is in a different
268
9827f80818dc
   month than the one you expect. This happens when the day number is out of range
269
20944f811650
   for the new month, due to differences in month lengths:
270
20944f811650
271
20944f811650
   .. doctest::
272
9827f80818dc
273
9827f80818dc
      >>> date = chrono.Date("2009-07-31")
274
9827f80818dc
      >>> date.month -= 1
275
9827f80818dc
      >>> date.get()
276
9827f80818dc
      (2009, 7, 1)
277
9827f80818dc
278
9827f80818dc
   When :attr:`chrono.Date.month` is set to 6, the date will become 2009-06-31. Since June
279
9827f80818dc
   only has 30 days this will trigger the overflow-handling that the date arithmetic relies
280
20944f811650
   on, and update the date to a valid date. The same happens with leap years:
281
20944f811650
282
20944f811650
   .. doctest::
283
9827f80818dc
284
9827f80818dc
      >>> date = chrono.Date("2008-02-29")
285
9827f80818dc
      >>> date.year += 1
286
9827f80818dc
      >>> date.get()
287
9827f80818dc
      (2009, 3, 1)
288
9827f80818dc
289
2c5482bf4e5c
Formatting
290
2c5482bf4e5c
----------
291
2c5482bf4e5c
292
2c5482bf4e5c
Date formatting is done via the :meth:`chrono.Date.format` method, which
293
2c5482bf4e5c
takes a string containing substitution variables of the form ``$name`` or
294
20944f811650
``${name}``, and replaces them with actual values:
295
2c5482bf4e5c
296
e0a34d0b4e61
.. doctest::
297
e0a34d0b4e61
298
2c5482bf4e5c
   >>> # full human-readable date
299
2c5482bf4e5c
   >>> chrono.Date("2009-07-23").format("$weekdayname $day. $monthname $year")
300
2c5482bf4e5c
   'Thursday 23. July 2009'
301
2c5482bf4e5c
302
2c5482bf4e5c
   >>> # ISO-date, using 0-padded values
303
2c5482bf4e5c
   >>> chrono.Date("2009-07-23").format("$0year-$0month-$0day")
304
2c5482bf4e5c
   '2009-07-23'
305
2c5482bf4e5c
306
2c5482bf4e5c
For a full list of substitution variables, see the
307
2c5482bf4e5c
:class:`chrono.formatter.Formatter` documentation.
308
2c5482bf4e5c
309
2c5482bf4e5c
Comparison
310
2c5482bf4e5c
----------
311
9827f80818dc
312
9827f80818dc
Date comparisons can be done using the normal Python comparison operators: ``==``,
313
20944f811650
``!=``, ``>``, and ``<``:
314
9827f80818dc
315
e0a34d0b4e61
.. doctest::
316
e0a34d0b4e61
317
9827f80818dc
   >>> chrono.Date("2009-07-31") == chrono.Date(year = 2009, month = 7, day = 31)
318
9827f80818dc
   True
319
9827f80818dc
320
9827f80818dc
   >>> chrono.Date("2009-07-31") > chrono.Date("2009-07-01")
321
9827f80818dc
   True
322
9827f80818dc
323
9827f80818dc
   >>> chrono.Date("2009-07-31") <= chrono.Date("2009-07-01")
324
9827f80818dc
   False
325
9827f80818dc
326
9827f80818dc
If the value that is being compared with is not a :class:`chrono.Date` object, it will
327
9827f80818dc
be converted to one if possible. This allows for comparisons with strings, UNIX timestamps,
328
9827f80818dc
:class:`time.struct_time` or :class:`datetime.date` objects, and any other value that
329
20944f811650
:class:`chrono.Date` is able to process:
330
9827f80818dc
331
e0a34d0b4e61
.. doctest::
332
e0a34d0b4e61
333
9827f80818dc
   >>> # string with ISO date
334
9827f80818dc
   >>> chrono.Date("2009-07-31") == "2009-07-31"
335
9827f80818dc
   True
336
9827f80818dc
337
9827f80818dc
   >>> # string with ISO weekdate
338
9827f80818dc
   >>> chrono.Date("2009-07-31") != "2009-W31-5"
339
9827f80818dc
   False
340
9827f80818dc
341
9827f80818dc
   >>> # integer UNIX timestamp
342
9827f80818dc
   >>> chrono.Date("2009-07-31") > 1241683613
343
9827f80818dc
   True
344
9827f80818dc
345
9827f80818dc
   >>> # time.struct_time, as returned by time.localtime() etc
346
9827f80818dc
   >>> chrono.Date("2009-07-31") > time.localtime()
347
9827f80818dc
   False
348
9827f80818dc
349
9827f80818dc
   >>> # datetime.date objects
350
e0a34d0b4e61
   >>> chrono.Date("2009-07-31") >= datetime.date(2009, 2, 17)
351
9827f80818dc
   True