summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVincent Driessen <vincent@3rdcloud.com>2012-02-08 00:06:05 +0100
committerVincent Driessen <vincent@3rdcloud.com>2012-02-08 00:16:11 +0100
commitebcc4449325fd64f3441343c3f6274bfc2ac3e32 (patch)
tree41962458e0b989b1e8c1d986eb7d377f2ae9bea6
parent3a73223204576868dcd3e40481a6ad916ab128a9 (diff)
downloadtimes-ebcc4449325fd64f3441343c3f6274bfc2ac3e32.tar.gz
Add support for _parsing_ dates.
It utilizes the python-dateutil parser for this purpose.
-rw-r--r--setup.py11
-rw-r--r--tests/test_times.py20
-rw-r--r--times/__init__.py10
3 files changed, 36 insertions, 5 deletions
diff --git a/setup.py b/setup.py
index 7ad8c84..6b44b70 100644
--- a/setup.py
+++ b/setup.py
@@ -1,3 +1,4 @@
+import sys
import os
from setuptools import setup
@@ -9,6 +10,14 @@ def get_version():
return VERSION
raise RuntimeError('No version info found.')
+def get_dependencies():
+ deps = ['pytz']
+ if sys.version_info[0] == 3: # Python >= 3
+ deps.append('python-dateutil >= 2')
+ else:
+ deps.append('python-dateutil < 2')
+ return deps
+
setup(
name='times',
version=get_version(),
@@ -24,7 +33,7 @@ setup(
include_package_data=True,
zip_safe=False,
platforms='any',
- install_requires=['pytz'],
+ install_requires=get_dependencies(),
classifiers=[
# As from http://pypi.python.org/pypi?%3Aaction=list_classifiers
#'Development Status :: 1 - Planning',
diff --git a/tests/test_times.py b/tests/test_times.py
index e24aa56..327c2f0 100644
--- a/tests/test_times.py
+++ b/tests/test_times.py
@@ -73,6 +73,24 @@ class TestTimes(TestCase):
datetime(2012, 2, 3, 8, 16, 44, 456000)
)
+ def test_to_universal_with_string(self):
+ 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'))
+
+ # Timezone-less strings require explicit source timezone
+ self.assertEquals(dt, times.to_universal('2012-02-02 00:56:31', 'Pacific/Auckland'))
+ self.assertEquals(dt, times.to_universal('2012-02-01 12:56:31', 'CET'))
+ self.assertEquals(dt, times.to_universal('2012-02-01 06:56:31', 'EST'))
+
+ # Timezone-less strings are rejected if source timezone is not specified
+ with self.assertRaises(ValueError):
+ self.assertEquals(dt, times.to_universal('2012-02-01 12:56:31'))
+
+
def test_to_universal_rejects_no_tzinfo(self):
"""Converting to universal times requires source timezone"""
now = datetime.now()
@@ -82,7 +100,7 @@ class TestTimes(TestCase):
def test_to_universal_rejects_non_date_arguments(self):
"""to_universal rejects non-date arguments"""
with self.assertRaises(ValueError):
- times.to_universal('lol')
+ times.to_universal([1, 2, 3])
# from_unix()
diff --git a/times/__init__.py b/times/__init__.py
index a01fe78..b6f39bc 100644
--- a/times/__init__.py
+++ b/times/__init__.py
@@ -1,11 +1,13 @@
import datetime
import calendar
import pytz
+from dateutil.parser import parse
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
@@ -17,8 +19,10 @@ def to_universal(local_dt, timezone=None):
'Timezone argument illegal when using UNIX timestamps.'
)
return from_unix(local_dt)
- else:
- return from_local(local_dt, timezone)
+ elif isinstance(local_dt, basestring):
+ local_dt = parse(local_dt)
+
+ return from_local(local_dt, timezone)
def from_local(local_dt, timezone=None):
@@ -84,7 +88,7 @@ def format(dt, timezone, fmt=None):
"""Formats the given universal time for display in the given time zone."""
if fmt is None:
- fmt = '%Y-%m-%d %H:%M:%S%z'
+ fmt = _default_fmt
if timezone is None:
raise ValueError('Please give an explicit timezone.')
return to_local(dt, timezone).strftime(fmt)