diff options
author | Vincent Driessen <vincent@3rdcloud.com> | 2012-02-17 16:51:01 +0100 |
---|---|---|
committer | Vincent Driessen <vincent@3rdcloud.com> | 2012-02-17 16:51:01 +0100 |
commit | 220df60a867592ecd47eb75c5b56cafda95c947a (patch) | |
tree | 2a5307a977c614cca86e46d2054b145725da12d0 | |
parent | 7b870e91bdde62d2d9700342a714a960827c10a0 (diff) | |
download | times-220df60a867592ecd47eb75c5b56cafda95c947a.tar.gz |
By default, use isoformat() to format datetimes.
This makes it more portable to JavaScript or MongoDB, for example, when
using these times in APIs.
This fixes #5.
-rw-r--r-- | tests/test_times.py | 18 | ||||
-rw-r--r-- | times/__init__.py | 10 |
2 files changed, 14 insertions, 14 deletions
diff --git a/tests/test_times.py b/tests/test_times.py index 327c2f0..10d7f1c 100644 --- a/tests/test_times.py +++ b/tests/test_times.py @@ -77,9 +77,9 @@ class TestTimes(TestCase): dt = self.sometime_univ # Timezone-aware strings - self.assertEquals(dt, times.to_universal('2012-02-02 00:56:31+1300')) - self.assertEquals(dt, times.to_universal('2012-02-01 12:56:31+0100')) - self.assertEquals(dt, times.to_universal('2012-02-01 06:56:31-0500')) + self.assertEquals(dt, times.to_universal('2012-02-02 00:56:31+13:00')) + self.assertEquals(dt, times.to_universal('2012-02-01 12:56:31+01:00')) + self.assertEquals(dt, times.to_universal('2012-02-01 06:56:31-05:00')) # Timezone-less strings require explicit source timezone self.assertEquals(dt, times.to_universal('2012-02-02 00:56:31', 'Pacific/Auckland')) @@ -114,13 +114,13 @@ class TestTimes(TestCase): self.assertEquals( times.format(times.from_unix(unix_time), 'UTC'), - '2012-02-03 08:16:44+0000') + '2012-02-03T08:16:44.456000+00:00') self.assertEquals( times.format(times.from_unix(unix_time), 'Europe/Amsterdam'), - '2012-02-03 09:16:44+0100') + '2012-02-03T09:16:44.456000+01:00') self.assertEquals( times.format(times.from_unix(unix_time), 'Pacific/Auckland'), - '2012-02-03 21:16:44+1300') + '2012-02-03T21:16:44.456000+13:00') def test_convert_non_numeric_from_unix(self): """from_unix refuses to accept non-numeric input""" @@ -167,9 +167,9 @@ class TestTimes(TestCase): auckland = pytz.timezone('Pacific/Auckland') est = pytz.timezone('EST') ams = pytz.timezone('Europe/Amsterdam') - self.assertEquals(times.format(dt, auckland), '2012-02-02 00:56:31+1300') - self.assertEquals(times.format(dt, ams), '2012-02-01 12:56:31+0100') - self.assertEquals(times.format(dt, est), '2012-02-01 06:56:31-0500') + self.assertEquals(times.format(dt, auckland), '2012-02-02T00:56:31+13:00') + self.assertEquals(times.format(dt, ams), '2012-02-01T12:56:31+01:00') + self.assertEquals(times.format(dt, est), '2012-02-01T06:56:31-05:00') def test_custom_format(self): dt = self.sometime_univ diff --git a/times/__init__.py b/times/__init__.py index b6f39bc..7f5865c 100644 --- a/times/__init__.py +++ b/times/__init__.py @@ -7,7 +7,6 @@ from .version import VERSION __author__ = 'Vincent Driessen <vincent@3rdcloud.com>' __version__ = VERSION -_default_fmt = '%Y-%m-%d %H:%M:%S%z' def to_universal(local_dt, timezone=None): """Converts the given local datetime or UNIX timestamp to a universal @@ -86,12 +85,13 @@ def to_unix(dt): def format(dt, timezone, fmt=None): """Formats the given universal time for display in the given time zone.""" - - if fmt is None: - fmt = _default_fmt if timezone is None: raise ValueError('Please give an explicit timezone.') - return to_local(dt, timezone).strftime(fmt) + local = to_local(dt, timezone) + if fmt is None: + return local.isoformat() + else: + return local.strftime(fmt) now = datetime.datetime.utcnow |