Skip to content

Commit

Permalink
OARec: safeguard RFC3339 date output
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkralidis committed Aug 27, 2024
1 parent 305e497 commit d148dae
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
52 changes: 39 additions & 13 deletions pygeometa/schemas/ogcapi_records/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#
# =================================================================

from datetime import datetime
from datetime import date, datetime
import json
import logging
import os
Expand Down Expand Up @@ -153,21 +153,13 @@ def write(self, mcf: dict, stringify: str = True) -> Union[dict, str]:
record['time'] = None

LOGGER.debug('Checking for dates')

if 'dates' in mcf['identification']:
if 'creation' in mcf['identification']['dates']:
record['properties']['created'] = str(mcf['identification']['dates']['creation']) # noqa
if 'revision' in mcf['identification']['dates']:
record['properties']['updated'] = str(mcf['identification']['dates']['revision']) # noqa
record['properties']['created'] = self.generate_date(mcf['identification']['dates']['creation']) # noqa

if record['properties'].get('created') is None:
record['properties']['created'] = datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ') # noqa

for date_type in ['created', 'updated']:
ds = record['properties'].get(date_type)
if ds is not None and len(ds) == 10:
LOGGER.debug('Date type found; expanding to date-time')
dt = datetime.strptime(ds, '%Y-%m-%d').strftime('%Y-%m-%dT%H:%M:%SZ') # noqa
record['properties'][date_type] = dt
if 'revision' in mcf['identification']['dates']:
record['properties']['updated'] = self.generate_date(mcf['identification']['dates']['revision']) # noqa

rights = get_charstring(mcf['identification'].get('rights'),
self.lang1, self.lang2)
Expand Down Expand Up @@ -416,3 +408,37 @@ def generate_link(self, distribution: dict) -> dict:
link['channel'] = distribution['channel']

return link

def generate_date(self, date_value: str) -> str:
"""
Helper function to derive RFC3339 date from MCF date type
:param date_value: `str` of date value
:returns: TODO
"""

print("VALUE", date_value)
print("VALUE", type(date_value))
value = None

if isinstance(date_value, (date, datetime)):
value = date_value.strftime('%Y-%m-%dT%H:%M:%SZ')

elif date_value in [None, 'None']:
value = datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ')
else:
value = str(date_value)

if isinstance(value, str):
if len(value) == 10: # YYYY-MM-DD
format_ = '%Y-%m-%d'
elif len(value) == 7: # YYYY-MM
format_ = '%Y-%m'
elif len(value) == 4: # YYYY
format_ = '%Y'

LOGGER.debug('date type found; expanding to date-time')
value = datetime.strptime(value, format_).strftime('%Y-%m-%dT%H:%M:%SZ') # noqa

return value
2 changes: 1 addition & 1 deletion pygeometa/schemas/wmo_wcmp2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def write(self, mcf: dict, stringify: str = True) -> Union[dict, str]:
except KeyError:
LOGGER.warning('Missing wmo:dataPolicy')

if 'created' not in record['properties']:
if record['properties'].get('created') is None:
record['properties']['created'] = datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ') # noqa

if stringify:
Expand Down

0 comments on commit d148dae

Please sign in to comment.