Skip to content

Commit

Permalink
atom timestamps: fully convert to RFC 3339
Browse files Browse the repository at this point in the history
for #126, #127
  • Loading branch information
snarfed committed Jan 17, 2018
1 parent 6cbc8f5 commit bf134f3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
10 changes: 7 additions & 3 deletions granary/atom.py
Expand Up @@ -401,9 +401,13 @@ def _prepare_activity(a, reader=True):
# make sure published and updated are strict RFC 3339 timestamps
for prop in 'published', 'updated':
val = obj.get(prop)
if val and '+' not in val and not val.endswith('Z'):
obj[prop] += 'Z'

if val:
obj[prop] = util.maybe_iso8601_to_rfc3339(val)
# Atom timestamps are even stricter than RFC 3339: they can't be naive ie
# time zone unaware. They must have either an offset or the Z suffix.
# https://www.feedvalidator.org/docs/error/InvalidRFC3339Date.html
if not util.TIMEZONE_OFFSET_RE.search(obj[prop]):
obj[prop] += 'Z'

def _remove_query_params(url):
parsed = list(urlparse.urlparse(url))
Expand Down
9 changes: 6 additions & 3 deletions granary/test/test_atom.py
Expand Up @@ -366,12 +366,15 @@ def test_render_missing_object_type_and_verb(self):

def test_updated_defaults_to_published(self):
activities = [
{'object': {'published': '2013-12-27T17:25:55+00:00'}},
{'object': {'published': '2014-12-27T17:25:55+00:00'}},
{'object': {'published': '2013-12-27T17:25:55+02:00'}},
{'object': {'published': '2014-12-27 17:25:55-0800'}},
{'object': {'published': '2015-12-27 17:25:55'}},
]

out = atom.activities_to_atom(activities, test_twitter.ACTOR, title='my title')
self.assert_multiline_in('<updated>2014-12-27T17:25:55+00:00</updated>', out)
self.assert_multiline_in('<updated>2013-12-27T17:25:55+02:00</updated>', out)
self.assert_multiline_in('<updated>2014-12-27T17:25:55-08:00</updated>', out)
self.assert_multiline_in('<updated>2015-12-27T17:25:55Z</updated>', out)

def test_escape_urls(self):
url = 'http://foo/bar?baz&baj'
Expand Down

0 comments on commit bf134f3

Please sign in to comment.