Skip to content

Commit

Permalink
Support timestamps passed as ISO 8601 strings (Fixes #94)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed May 25, 2024
1 parent a4cf80d commit e9663bb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/flask_moment/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion tests/test_flask_moment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit e9663bb

Please sign in to comment.