def parse_datetime(v: str, tz: Any = None) -> datetime: """ Parses ISO date/datetime string to timezone-aware datetime. Supports YYYY-MM-DD date strings where time part is missing. Returns always timezone-aware datetime (assumes UTC if timezone missing). :param v: Input string to parse :param tz: Default pytz timezone or if None then use UTC as default :return: datetime with timezone """ try: t = django_parse_datetime(v) if t is None: t_date: Optional[date] = django_parse_date(v) if t_date is None: raise ValidationError( _("“%(value)s” value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format." ) % {"value": v}) t = datetime.combine(t_date, time()) if tz is None: tz = pytz.utc return t if t.tzinfo else tz.localize(t) except Exception as err: raise ValidationError( _("“%(value)s” value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format." ) % {"value": v}) from err
def parse_datetime_or_none(v: str, tz: Any = None) -> Optional[datetime]: """ Parses ISO date/datetime string to timezone-aware datetime. Supports YYYY-MM-DD date strings where time part is missing. Returns timezone-aware datetime (assumes UTC if timezone missing) or None if parsing fails. :param v: Input string to parse :param tz: Default pytz timezone or if None then use UTC as default :return: datetime with timezone or None """ try: t = django_parse_datetime(v) if t is None: t_date: Optional[date] = django_parse_date(v) if t_date is None: return None t = datetime.combine(t_date, time()) if tz is None: tz = pytz.utc return t if t.tzinfo else tz.localize(t) except Exception: return None
def parse_datetime(value): return django_parse_datetime(value)
def parse_datetime(dt_str): return django_parse_datetime(dt_str)
def test_should_return_django_datetime(self): self.assertEqual(parse_datetime('2016-05-05 14:10:00'), django_parse_datetime('2016-05-05 14:10:00'))
def parse_datetime(text): """ Parse and return a datetime value from the given text that represents a date and time. """ return django_parse_datetime(text)