|
43 | 43 | # |
44 | 44 | # ================================================================= |
45 | 45 |
|
46 | | -from datetime import date, datetime |
47 | 46 | import logging |
48 | 47 | import os |
49 | 48 | from typing import Union |
50 | 49 |
|
51 | 50 | from pygeometa import __version__ |
52 | 51 | from pygeometa.core import get_charstring |
53 | | -from pygeometa.helpers import json_dumps |
| 52 | +from pygeometa.helpers import generate_datetime, json_dumps |
54 | 53 | from pygeometa.schemas.base import BaseOutputSchema |
55 | 54 |
|
56 | 55 | THISDIR = os.path.dirname(os.path.realpath(__file__)) |
@@ -165,12 +164,11 @@ def write(self, mcf: dict, stringify: str = True) -> Union[dict, str]: |
165 | 164 |
|
166 | 165 | LOGGER.debug('Checking for dates') |
167 | 166 |
|
168 | | - if 'dates' in mcf['identification']: |
169 | | - if 'creation' in mcf['identification']['dates']: |
170 | | - record['properties']['created'] = self.generate_date(mcf['identification']['dates']['creation']) # noqa |
171 | | - |
172 | | - if 'revision' in mcf['identification']['dates']: |
173 | | - record['properties']['updated'] = self.generate_date(mcf['identification']['dates']['revision']) # noqa |
| 167 | + for key, value in mcf['identification']['dates'].items(): |
| 168 | + if key == 'creation': |
| 169 | + record['properties']['created'] = generate_datetime(value) |
| 170 | + elif key == 'revision': |
| 171 | + record['properties']['updated'] = generate_datetime(value) |
174 | 172 |
|
175 | 173 | rights = get_charstring(mcf['identification'].get('rights'), |
176 | 174 | self.lang1, self.lang2) |
@@ -426,47 +424,3 @@ def generate_link(self, distribution: dict) -> dict: |
426 | 424 | link['channel'] = distribution['channel'] |
427 | 425 |
|
428 | 426 | return link |
429 | | - |
430 | | - def generate_date(self, date_value: str) -> str: |
431 | | - """ |
432 | | - Helper function to derive RFC3339 date from MCF date type |
433 | | -
|
434 | | - :param date_value: `str` of date value |
435 | | -
|
436 | | - :returns: `str` of date-time value |
437 | | - """ |
438 | | - |
439 | | - value = None |
440 | | - |
441 | | - if isinstance(date_value, str) and date_value != 'None': |
442 | | - if len(date_value) == 10: # YYYY-MM-DD |
443 | | - format_ = '%Y-%m-%d' |
444 | | - elif len(date_value) == 7: # YYYY-MM |
445 | | - format_ = '%Y-%m' |
446 | | - elif len(date_value) == 4: # YYYY |
447 | | - format_ = '%Y' |
448 | | - elif len(date_value) == 19: # YYYY-MM-DDTHH:MM:SS |
449 | | - msg = 'YYYY-MM-DDTHH:MM:SS with no timezone; converting to UTC' |
450 | | - LOGGER.debug(msg) |
451 | | - format_ = '%Y-%m-%dT%H:%M:%S' |
452 | | - |
453 | | - LOGGER.debug('date type found; expanding to date-time') |
454 | | - value = datetime.strptime(date_value, format_).strftime('%Y-%m-%dT%H:%M:%SZ') # noqa |
455 | | - |
456 | | - elif isinstance(date_value, int) and len(str(date_value)) == 4: |
457 | | - date_value2 = str(date_value) |
458 | | - LOGGER.debug('date type found; expanding to date-time') |
459 | | - format_ = '%Y' |
460 | | - value = datetime.strptime(date_value2, format_).strftime('%Y-%m-%dT%H:%M:%SZ') # noqa |
461 | | - |
462 | | - elif isinstance(date_value, (date, datetime)): |
463 | | - value = date_value.strftime('%Y-%m-%dT%H:%M:%SZ') |
464 | | - |
465 | | - elif date_value in [None, 'None']: |
466 | | - value = datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ') |
467 | | - |
468 | | - else: |
469 | | - msg = f'Unknown date string: {date_value}' |
470 | | - raise RuntimeError(msg) |
471 | | - |
472 | | - return value |
0 commit comments