Jelajahi Sumber

DatePeriod: added readonly property 'isoformat' compatible to iso8601

Fabian Peter Hammerle 9 tahun lalu
induk
melakukan
bc83e7599d
3 mengubah file dengan 47 tambahan dan 4 penghapusan
  1. 9 0
      ioex/__init__.py
  2. 1 1
      setup.py
  3. 37 3
      tests/test_.py

+ 9 - 0
ioex/__init__.py

@@ -72,3 +72,12 @@ class DatePeriod(object):
         if not (end is None or type(end) is datetime.datetime):
             raise TypeError()
         self._end = end
+
+    @property
+    def isoformat(self):
+        if self.start is None or self.end is None:
+            raise ValueError('both start and end must be set')
+        return '%s/%s' % (
+                self.start.isoformat().replace('+00:00', 'Z'), 
+                self.end.isoformat().replace('+00:00', 'Z'), 
+                )

+ 1 - 1
setup.py

@@ -14,5 +14,5 @@ setup(
     keywords = [],
     classifiers = [],
     scripts = glob.glob('scripts/*'),
-    tests_require = ['pytest']
+    tests_require = ['pytest', 'pytz']
     )

+ 37 - 3
tests/test_.py

@@ -1,6 +1,7 @@
 # -*- coding: utf-8 -*-
 import pytest
 
+import pytz
 import ioex
 import locale
 import datetime
@@ -16,7 +17,6 @@ def test_setlocale_unsupported(locale_code):
 def test_setlocale_unsupported_inheritance():
     assert issubclass(ioex.UnsupportedLocaleSettingError, locale.Error)
 
-@pytest.mark.xfail(raises = ioex.UnsupportedLocaleSettingError)
 @pytest.mark.parametrize(('dt', 'dt_format', 'locale_code', 'expected_string'), [
     [datetime.datetime(2016, 07, 23, 1, 7, 12), '%x', 'de_DE.utf8', u'23.07.2016'],
     [datetime.datetime(2016, 07, 23, 1, 7, 12), '%X', 'de_DE.utf8', u'01:07:12'],
@@ -28,8 +28,11 @@ def test_setlocale_unsupported_inheritance():
     [datetime.datetime(2016, 07, 23, 1, 7, 12), '%X', 'zh_CN.utf8', u'01时07分12秒'],
     ])
 def test_setlocale_strtime(dt, dt_format, locale_code, expected_string):
-    with ioex.setlocale(locale_code):
-        assert dt.strftime(dt_format).decode('utf-8') == expected_string
+    try:
+        with ioex.setlocale(locale_code):
+            assert dt.strftime(dt_format).decode('utf-8') == expected_string
+    except ioex.UnsupportedLocaleSettingError, ex:
+        pytest.skip('locale %s unsupported' % locale_code)
 
 @pytest.mark.parametrize(('start', 'end'), [
     [datetime.datetime(2016, 7, 24, 12, 21), datetime.datetime(2016, 7, 24, 12, 22)],
@@ -85,3 +88,34 @@ def test_dateperiod_set_end_fail(end):
     p = ioex.DatePeriod()
     with pytest.raises(TypeError):
         p.end = end
+
+@pytest.mark.parametrize(('start', 'end', 'iso'), [
+    [
+        datetime.datetime(2016, 7, 24, 12, 21, 0),
+        datetime.datetime(2016, 7, 24, 12, 22, 13),
+        '2016-07-24T12:21:00/2016-07-24T12:22:13',
+        ],
+    [
+        datetime.datetime(2016, 7, 24, 12, 21, 0, tzinfo = pytz.utc),
+        datetime.datetime(2016, 7, 24, 12, 22, 13, tzinfo = pytz.utc),
+        '2016-07-24T12:21:00Z/2016-07-24T12:22:13Z',
+        ],
+    [
+        datetime.datetime(2016, 7, 24, 12, 21, 0, tzinfo = pytz.utc),
+        pytz.timezone('Europe/Vienna').localize(datetime.datetime(2016, 7, 24, 12, 22, 13)),
+        '2016-07-24T12:21:00Z/2016-07-24T12:22:13+02:00',
+        ],
+    [
+        pytz.timezone('US/Pacific').localize(datetime.datetime(2016, 1, 12, 12, 22, 13)),
+        pytz.timezone('Europe/London').localize(datetime.datetime(2016, 1, 24, 12, 22, 13)),
+        '2016-01-12T12:22:13-08:00/2016-01-24T12:22:13Z',
+        ],
+    [
+        datetime.datetime(2016, 7, 24, 12, 20, 0, microsecond = 25500),
+        datetime.datetime(2016, 7, 24, 12, 21, 0, microsecond = 13, tzinfo = pytz.utc),
+        '2016-07-24T12:20:00.025500/2016-07-24T12:21:00.000013Z',
+        ],
+    ])
+def test_dateperiod_get_isoformat(start, end, iso):
+    p = ioex.DatePeriod(start = start, end = end)
+    assert p.isoformat == iso