Times is a small, minimalistic, Python library for dealing with time conversions between universal time and arbitrary timezones.
Archived. The repository is read-only: no more fixes or releases will come.
pip install timesNOTE:
This library will not be maintained any further. You probably want to use
the excellent Arrow library.
“There should be one—and preferably only one—obvious way to do it.”
In fact, version 0.7 of times has been rewritten to be implemented on top of Arrow, so it still provides the Times interface, but you'll already be using Arrow. You can probably easily replace your times function calls by Arrow objects.
Times
Times is a small, minimalistic, Python library for dealing with time conversions to and from timezones, for once and for all.
Accepting time
Never work with local times. Whenever you must accept local time input (e.g. from a user), convert it to universal time immediately:
>>> times.to_universal(local_time, 'Europe/Amsterdam')
datetime.datetime(2012, 2, 1, 10, 31, 45, 781262)
The second argument can be a pytz.timezone instance, or a timezone string.
If the local_time variable already holds timezone info, you must leave out
the source timezone from the call.
To enforce best practices, times will never implicitly convert times for you,
even if that would technically be possible.
Date Strings
If you want to accepting datetime representations in string form (for example, from JSON APIs), you can convert them to universal datetimes easily:
>>> import time, times
>>> print times.to_universal('2012-02-03 11:59:03-0500') # auto-detects source timezone
Times utilizes the string parsing routines available in dateutil. Note
that the source timezone is auto-detected from the string. If the string
contains a timezone offset, you are not allowed to explicitly specify one.
If the string does not contain any timezone offset, you must specify the source timezone explicitly:
>>> print times.to_universal('2012-02-03 11:59:03', 'Europe/Amsterdam')