Skip to content

Commit

Permalink
Merge pull request #69 from azavea/rde/iso8601
Browse files Browse the repository at this point in the history
Modified output datetime strings to ISO8601.
  • Loading branch information
lossyrob authored Jan 17, 2020
2 parents 8d3218a + 4843e47 commit 1fc7d8a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
7 changes: 3 additions & 4 deletions pystac/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pystac import STACError
from pystac.catalog import Catalog
from pystac.link import Link
from pystac.utils import datetime_to_str


class Collection(Catalog):
Expand Down Expand Up @@ -335,12 +336,10 @@ def to_dict(self):
end = None

if i[0]:
start = '{}Z'.format(i[0].replace(microsecond=0,
tzinfo=None).isoformat())
start = datetime_to_str(i[0])

if i[1]:
end = '{}Z'.format(i[1].replace(microsecond=0,
tzinfo=None).isoformat())
end = datetime_to_str(i[1])

encoded_intervals.append([start, end])

Expand Down
5 changes: 2 additions & 3 deletions pystac/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pystac.link import Link, LinkType
from pystac.stac_object import STACObject
from pystac.utils import (is_absolute_href, make_absolute_href,
make_relative_href)
make_relative_href, datetime_to_str)
from pystac.collection import Collection


Expand Down Expand Up @@ -173,8 +173,7 @@ def to_dict(self, include_self_link=True):
assets = dict(
map(lambda x: (x[0], x[1].to_dict()), self.assets.items()))

self.properties['datetime'] = '{}Z'.format(
self.datetime.replace(microsecond=0, tzinfo=None))
self.properties['datetime'] = datetime_to_str(self.datetime)

d = {
'type': 'Feature',
Expand Down
12 changes: 12 additions & 0 deletions pystac/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,15 @@ def is_absolute_href(href):
"""
parsed = _urlparse(href)
return parsed.scheme != '' or _pathlib.isabs(parsed.path)


def datetime_to_str(dt):
"""Convert a python datetime to an ISO8601 stirng
Args:
dt (datetime): The datetime to convert.
Returns:
str: The ISO8601 formatted string representing the datetime.
"""
return dt.strftime('%Y-%m-%dT%H:%M:%SZ')
12 changes: 12 additions & 0 deletions tests/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,15 @@ def test_asset_absolute_href(self):
expected_href = 'http://cool-sat.com/catalog/CS3-20160503_132130_04/data.geojson'
actual_href = rel_asset.get_absolute_href()
self.assertEqual(expected_href, actual_href)

def test_datetime_ISO8601_format(self):
m = TestCases.get_path(
'data-files/itemcollections/sample-item-collection.json')
with open(m) as f:
item_dict = json.load(f)['features'][0]

item = Item.from_dict(item_dict)

formatted_time = item.to_dict()['properties']['datetime']

self.assertEqual('2016-05-03T13:22:30Z', formatted_time)

0 comments on commit 1fc7d8a

Please sign in to comment.