diff --git a/src/flask_moment/__init__.py b/src/flask_moment/__init__.py index 08edaf5..98b52b9 100644 --- a/src/flask_moment/__init__.py +++ b/src/flask_moment/__init__.py @@ -48,7 +48,8 @@ class moment(object): """Create a moment object. - :param timestamp: The ``datetime`` object representing the timestamp. + :param timestamp: The ``datetime`` object or ISO 8601 string representing + the timestamp. :param local: If ``True``, the ``timestamp`` argument is given in the local client time. In most cases this argument will be set to ``False`` and all the timestamps managed by the server @@ -176,6 +177,8 @@ def __init__(self, timestamp=None, local=False): self.local = local def _timestamp_as_iso_8601(self, timestamp): + if isinstance(timestamp, str): + return timestamp # assume it is already in ISO 8601 format tz = '' if not self.local: tz = 'Z' @@ -235,7 +238,7 @@ def fromTime(self, timestamp, no_suffix=False, refresh=False): This function maps to the ``from()`` function from moment.js. - :param timestamp: The reference ``datetime`` object. + :param timestamp: The reference ``datetime`` object or ISO 8601 string. :param no_suffix: if set to ``True``, the time difference does not include the suffix (the "ago" or similar). :param refresh: If set to ``True``, refresh the timestamp at one @@ -268,7 +271,7 @@ def toTime(self, timestamp, no_suffix=False, refresh=False): This function maps to the ``to()`` function from moment.js. - :param timestamp: The reference ``datetime`` object. + :param timestamp: The reference ``datetime`` object or ISO 8601 string. :param no_suffix: if set to ``True``, the time difference does not include the suffix (the "ago" or similar). :param refresh: If set to ``True``, refresh the timestamp at one @@ -321,7 +324,7 @@ def diff(self, timestamp, units, refresh=False): """Render the difference between the moment object and the given timestamp using the provided units. - :param timestamp: The reference ``datetime`` object. + :param timestamp: The reference ``datetime`` object or ISO 8601 string. :param units: A time unit such as `years`, `months`, `weeks`, `days`, `hours`, `minutes` or `seconds`. :param refresh: If set to ``True``, refresh the timestamp at one diff --git a/tests/test_flask_moment.py b/tests/test_flask_moment.py index 02fefd0..4dc0eee 100644 --- a/tests/test_flask_moment.py +++ b/tests/test_flask_moment.py @@ -96,12 +96,18 @@ def test_flask_moment_js(self): assert isinstance(js, str) assert 'function flask_moment_render(elem) {' in js - def test__moment_timestamp_passed(self): + def test__moment_datetime_passed(self): ts = datetime(2017, 1, 15, 22, 47, 6, 479898) m = self.moment(timestamp=ts) assert m.timestamp == ts assert m.local is False + def test__moment_iso8601_passed(self): + ts = '2017-01-15T22:47:06.479898Z' + m = self.moment(timestamp=ts) + assert m.timestamp == ts + assert m.local is False + @mock.patch('flask_moment.datetime') def test__timestamp_as_iso_8601_default(self, dt): dt.utcnow.return_value = datetime(2017, 1, 15, 22, 1, 21, 101361) @@ -116,6 +122,11 @@ def test__timestamp_as_iso_8601_local_true(self, dt): ts = m._timestamp_as_iso_8601(timestamp=m.timestamp) assert ts == '2017-01-15T22:01:21' + def test__timestamp_as_iso_8601_string(self): + ts = '2017-01-15T22:47:06.479898Z' + m = self.moment(local=True) # local is ignored in this case + assert m._timestamp_as_iso_8601(timestamp=ts) == ts + def test__render_default(self): m = self.moment() rts = m._render(func='format') # rts: rendered time stamp