times
Advanced tools
+119
| Times | ||
| ===== | ||
| Build status: | ||
| [](http://travis-ci.org/nvie/times) | ||
| Times is a small, minimalistic, Python library for dealing with time | ||
| conversions to and from timezones, for once and for all. | ||
| It is designed to be simple and clear, but also opinionated about good and bad | ||
| practices. | ||
| [Armin Ronacher][1] wrote about timezone best practices in his blog post | ||
| [Dealing with Timezones in Python][2]. The **tl;dr** summary is that | ||
| everything sucks about our mechanisms to represent absolute moments in time, | ||
| but the least worst one of all is UTC. | ||
| [1]: http://twitter.com/mitsuhiko | ||
| [2]: http://lucumr.pocoo.org/2011/7/15/eppur-si-muove/ | ||
| Rationale | ||
| --------- | ||
| Python's `datetime` library and the `pytz` library are powerful, but because | ||
| they don't prescribe a standard practice of working with dates, everybody is | ||
| free to pick his or her own way. | ||
| `times` tries to make working with times and timezones a little less of | ||
| a clusterfuck and hopefully set a standard of some sort. | ||
| It still uses `datetime` and `pytz` under the covers, but as long as you never | ||
| use any timezone related stuff outside `times`, you should be safe. | ||
| 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][3]. 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') | ||
| This is the inverse of `times.format()`. | ||
| POSIX timestamps | ||
| ---------------- | ||
| If you prefer working with UNIX (POSIX) timestamps, you can convert them to | ||
| safe datetime representations easily: | ||
| >>> import time, times | ||
| >>> print times.to_universal(time.time()) | ||
| 2012-02-03 11:59:03.588419 | ||
| Note that `to_universal` auto-detects that you give it a UNIX timestamp. | ||
| To get the UNIX timestamp representation of a universal datetime, use: | ||
| >>> print times.to_unix(universal_time) | ||
| Current time | ||
| ------------ | ||
| When you want to record the current time, you can use this convenience method: | ||
| >>> import times | ||
| >>> print times.now() | ||
| datetime.datetime(2012, 2, 1, 11, 51, 27, 621491) | ||
| Presenting times | ||
| ---------------- | ||
| To _present_ times to the end user of your software, you should explicitly | ||
| format your universal time to your user's local timezone. | ||
| >>> import times | ||
| >>> now = times.now() | ||
| >>> print times.format(now, 'CET') | ||
| 2012-02-01 21:32:10+0100 | ||
| As with the `to_universal` function, the second argument may be either | ||
| a timezone instance or a timezone string. | ||
| **Note**: It _is_ possible to convert universal times to local times, using | ||
| `to_local`). However, you probably shouldn't do it, unless you want to | ||
| `strftime()` the resulting local date multiple times. In any other case, you | ||
| are advised to use `times.format()` directly instead. | ||
| [3]: http://labix.org/python-dateutil#head-c0e81a473b647dfa787dc11e8c69557ec2c3ecd2 |
+6
-1
| Metadata-Version: 1.0 | ||
| Name: times | ||
| Version: 0.5 | ||
| Version: 0.6 | ||
| Summary: Times is a small, minimalistic, Python library for dealing with time conversions between universal time and arbitrary timezones. | ||
@@ -36,2 +36,7 @@ Home-page: https://github.com/nvie/times/ | ||
| Classifier: Programming Language :: Python | ||
| Classifier: Programming Language :: Python :: 2 | ||
| Classifier: Programming Language :: Python :: 2.7 | ||
| Classifier: Programming Language :: Python :: 3.2 | ||
| Classifier: Programming Language :: Python :: 3.3 | ||
| Classifier: Programming Language :: Python :: Implementation :: PyPy | ||
| Classifier: Topic :: Internet | ||
@@ -38,0 +43,0 @@ Classifier: Topic :: Internet :: WWW/HTTP |
+8
-3
@@ -9,5 +9,5 @@ import sys | ||
| with open(os.path.join(basedir, 'times/version.py')) as f: | ||
| VERSION = None | ||
| exec(f.read()) | ||
| return VERSION | ||
| variables = {} | ||
| exec(f.read(), variables) | ||
| return variables.get('VERSION') | ||
| raise RuntimeError('No version info found.') | ||
@@ -65,2 +65,7 @@ | ||
| 'Programming Language :: Python', | ||
| 'Programming Language :: Python :: 2', | ||
| 'Programming Language :: Python :: 2.7', | ||
| 'Programming Language :: Python :: 3.2', | ||
| 'Programming Language :: Python :: 3.3', | ||
| 'Programming Language :: Python :: Implementation :: PyPy', | ||
| 'Topic :: Internet', | ||
@@ -67,0 +72,0 @@ 'Topic :: Internet :: WWW/HTTP', |
| Metadata-Version: 1.0 | ||
| Name: times | ||
| Version: 0.5 | ||
| Version: 0.6 | ||
| Summary: Times is a small, minimalistic, Python library for dealing with time conversions between universal time and arbitrary timezones. | ||
@@ -36,2 +36,7 @@ Home-page: https://github.com/nvie/times/ | ||
| Classifier: Programming Language :: Python | ||
| Classifier: Programming Language :: Python :: 2 | ||
| Classifier: Programming Language :: Python :: 2.7 | ||
| Classifier: Programming Language :: Python :: 3.2 | ||
| Classifier: Programming Language :: Python :: 3.3 | ||
| Classifier: Programming Language :: Python :: Implementation :: PyPy | ||
| Classifier: Topic :: Internet | ||
@@ -38,0 +43,0 @@ Classifier: Topic :: Internet :: WWW/HTTP |
@@ -0,1 +1,2 @@ | ||
| README.md | ||
| setup.cfg | ||
@@ -2,0 +3,0 @@ setup.py |
+12
-3
@@ -0,4 +1,13 @@ | ||
| import sys | ||
| import datetime | ||
| import calendar | ||
| import pytz | ||
| PY3 = sys.version_info[0] == 3 | ||
| if PY3: | ||
| string_types = str | ||
| else: | ||
| string_types = basestring | ||
| from dateutil.parser import parse | ||
@@ -21,3 +30,3 @@ from .version import VERSION | ||
| return from_unix(local_dt) | ||
| elif isinstance(local_dt, basestring): | ||
| elif isinstance(local_dt, string_types): | ||
| local_dt = parse(local_dt) | ||
@@ -40,3 +49,3 @@ | ||
| if isinstance(timezone, basestring): | ||
| if isinstance(timezone, string_types): | ||
| timezone = pytz.timezone(timezone) | ||
@@ -73,3 +82,3 @@ dt_with_tzinfo = timezone.localize(local_dt) | ||
| ) | ||
| if isinstance(timezone, basestring): | ||
| if isinstance(timezone, string_types): | ||
| timezone = pytz.timezone(timezone) | ||
@@ -76,0 +85,0 @@ return pytz.utc.localize(dt).astimezone(timezone) |
+1
-1
@@ -1,1 +0,1 @@ | ||
| VERSION = '0.5' | ||
| VERSION = '0.6' |
Alert delta unavailable
Currently unable to show alert delta for PyPI packages.
14220
52.67%12
9.09%158
7.48%