Skip to content

Commit

Permalink
add more robust created date-time handling (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkralidis authored Aug 12, 2024
1 parent 7f69057 commit 2966103
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
28 changes: 27 additions & 1 deletion pywcmp/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Authors: Tom Kralidis <tomkralidis@gmail.com>
# Ján Osuský <jan.osusky@iblsoft.com>
#
# Copyright (c) 2023 Tom Kralidis
# Copyright (c) 2024 Tom Kralidis
# Copyright (c) 2022 Government of Canada
# Copyright (c) 2020 IBL Software Engineering spol. s r. o.
#
Expand Down Expand Up @@ -227,3 +227,29 @@ def get_current_datetime_rfc3339() -> str:
"""

return datetime.now(timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')


def is_valid_created_datetime(value: str) -> bool:
"""
Helper function to test for accepted RFC3339 strings
:param value: `str` of datetime
:returns: `bool` of whether datetime is valid/acceptable
"""

datetime_formats = [
'%Y-%m-%dT%H:%M:%SZ', # 2024-08-09T14:29Z
'%Y-%m-%dT%H:%M:%S.%fZ', # 2024-08-09T14:29.12Z
'%Y-%m-%dT%H:%M:%S%z' # 2024-08-09T14:29+0400
]

for datetime_format in datetime_formats:
try:
datetime.strptime(value, datetime_format)
return True
except ValueError as err:
msg = f'datetime {value} invalid against {datetime_format}: {err}'
LOGGER.debug(msg)

return False
14 changes: 6 additions & 8 deletions pywcmp/wcmp2/ets.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
# executable test suite as per WMO Core Metadata Profile 2, Annex A

import csv
from datetime import datetime
import json
import logging
from pathlib import Path
Expand All @@ -36,7 +35,8 @@
import pywcmp
from pywcmp.bundle import WCMP2_FILES
from pywis_topics.topics import TopicHierarchy
from pywcmp.util import get_current_datetime_rfc3339, get_userdir
from pywcmp.util import (get_current_datetime_rfc3339, get_userdir,
is_valid_created_datetime)

LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -360,21 +360,19 @@ def test_requirement_contacts(self):

return status

def test_requirement_creation_date(self):
def test_requirement_created_datetime(self):
"""
Validate that a WCMP record provides a record creation date.
Validate that a WCMP record provides a valid record creation date.
"""

status = {
'id': gen_test_id('record_creation_date'),
'id': gen_test_id('record_created_datetime'),
'code': 'PASSED'
}

created = self.record['properties']['created']

try:
datetime.strptime(created, '%Y-%m-%dT%H:%M:%S%z')
except ValueError:
if not is_valid_created_datetime(created):
status['code'] = 'FAILED'
status['message'] = 'Invalid date-time format'

Expand Down
10 changes: 9 additions & 1 deletion tests/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from pywcmp.ets import WMOCoreMetadataProfileTestSuite2
from pywcmp.wcmp2.kpi import (
calculate_grade, WMOCoreMetadataProfileKeyPerformanceIndicators)
from pywcmp.util import parse_wcmp
from pywcmp.util import is_valid_created_datetime, parse_wcmp


def get_test_file_path(filename):
Expand Down Expand Up @@ -187,6 +187,14 @@ def test_parse_wcmp(self):
with open(get_test_file_path(file_)) as fh:
_ = parse_wcmp(fh.read())

def test_is_valid_created_datetime(self):
"""test for valid/accepted RFC3339 datetimes"""

self.assertTrue(is_valid_created_datetime('2024-08-09T14:29:22Z'))
self.assertTrue(is_valid_created_datetime('2024-08-09T14:29:22.12Z'))
self.assertTrue(is_valid_created_datetime('2024-08-09T14:29:22+0400'))
self.assertTrue(is_valid_created_datetime('2024-08-09T14:29:22+04:00'))


if __name__ == '__main__':
unittest.main()

0 comments on commit 2966103

Please sign in to comment.