def find_earliest_oai_date(self, metadata): """ Find the latest publication date (if any) in a record """ earliest = None for date in metadata['date']: try: parsed = tolerant_datestamp_to_datetime(date) if not valid_publication_date(parsed): continue if earliest is None or parsed < earliest: earliest = parsed except (DatestampError, ValueError): continue return earliest
def parse_crossref_date(date): """ Parse the date representation from CrossRef to a python object """ if not date: return None ret = None if 'date-parts' in date: try: for date in date['date-parts']: ret = date_from_dateparts(date) if ret == datetime.date(year=1970, month=1, day=1): ret = None if ret is not None: return ret except ValueError: pass if 'raw' in date: ret = tolerant_datestamp_to_datetime(date['raw']).date() if valid_publication_date(ret): return ret
def _parse_date(cls, data): """ Parse the date representation from citeproc to a date object :param data: date extracted from citeproc :returns: date object or None """ if not isinstance(data, dict): return None d = None # First we try with date parts try: d = cls._parse_date_parts(data.get('date-parts')[0]) except Exception: pass # If this has no success, we try with raw date if d is None and data.get('raw') is not None: d = tolerant_datestamp_to_datetime(data['raw']).date() # We validate, if bad, then set to None if not valid_publication_date(d): d = None return d
def test_valid_publication_date(self): self.assertEqual(valid_publication_date(datetime.date(6789, 1, 1)), False) self.assertEqual(valid_publication_date(datetime.date(2018, 3, 4)), True) self.assertEqual(valid_publication_date(None), False)