Wiki

Clone wiki

python-chrono / Home

python-chrono

A Python module for simple and convenient date/time handling, including parsing, arithmetic, comparison, formatting, and calendar functionality. It supports both the ISO and US calendars, and dates/times in ISO, US, and european formats.

python-chrono is developed by Erik Grinaker <erikg@codepoet.no>, and licensed under the terms of the GNU General Public License version 3.

News

2010-03-08: Version 0.3.0 released

This release adds support for date differences (intervals), julian day numbers, and constants for setting the default calendar and parser. It also fixes some errors in the documentation code examples.

2010-02-06: Version 0.2.0 released

This release adds support for US and european date formats, as well as the US calendar and 12-hour clocks. It also fixes formatting of 12-hour times.

Download

python-chrono only requires a recent version of Python installed. It has been tested on Python versions 2.6 and 3.1, but should work on version 2.5 as well.

To install from a source distribution, simply run the usual setup.py script as root:

./setup.py install

Documentation

The documentation is available online, and is also distributed along with the module itself, in the archives available for download above.

Usage

Below are some simple examples of python-chrono usage. For more information see the python-chrono documentation, which also includes a more in-depth usage introduction.

>>> import chrono

>>> # parse a date from an ISO date string
>>> date = chrono.Date("2010-07-23")
>>>
>>> # get date parts
>>> date.year
2010
>>> date.month
7
>>> date.day
23
>>> date.get()
(2010, 7, 23)
>>>
>>> # do some simple arithetic
>>> date.month += 6
>>> date.day += 14
>>> date.get()
(2011, 2, 6)
>>>
>>> # format the date
>>> date.format("$weekdayname $day. $monthname $year")
'Sunday 6. February 2011'
>>>
>>> # get some calendar information about the date
>>> date.week()
(2011, 5)
>>> date.leapyear()
False
>>> date.monthdays()
28
>>>
>>> # compare date with other dates, even as other datatypes like strings
>>> date > chrono.Date("2011-01-01")
True
>>> date <= "2011-07-01"
True
>>> date == time.localtime()
False

Updated

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.