def to_string(self, value): if isinstance(value, basestring): return dateparser_parse(value).strftime( self._output_format) if self._output_format else value elif isinstance(value, self._stdlib_class): return value.strftime( self._output_format ) if self._output_format else value.isoformat() else: raise ValueError('Value `{}` is neither string nor {}'.format( value, self._stdlib_class))
def get_seconds_from_epoch(timestamp: str) -> float: """If dateutil.parser.parse cannot parse DST timezones (e.g. PDT, EDT) correctly, then use dateparser.parse instead. """ utc_epoch = datetime(1970, 1, 1, tzinfo=timezone.utc) utc_t = None try: with warnings.catch_warnings(): warnings.simplefilter("ignore") utc_t = dateutil_parse(timestamp) except Exception: pass if utc_t is None or utc_t.tzname() not in ("UTC", "Z"): utc_t = dateparser_parse(timestamp) utc_t = utc_t.astimezone(timezone.utc) return (utc_t - utc_epoch).total_seconds()
def from_string(self, value): return dateparser_parse(value.strip(self.fill_char), **self._parse_kwargs)