Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_localize_raises_error_if_date_has_tzinfo(self, given_date):
self.timezone_info = StaticTzInfo('UTC\\+00:00', timedelta(0))
self.when_date_is_localized(given_date)
self.then_error_was_raised(ValueError, ['Not naive datetime (tzinfo is already set)'])
tzinfo=StaticTzInfo('Z', timedelta(0))))
])
def test_parse_timestamp(self, date_string, expected):
self.given_local_tz_offset(0)
self.given_parser(settings={'TO_TIMEZONE': 'UTC'})
self.when_date_is_parsed(date_string)
self.then_date_obj_exactly_is(expected)
def apply_dateparser_timezone(utc_datetime, offset_or_timezone_abb):
for name, info in _tz_offsets:
if info['regex'].search(' %s' % offset_or_timezone_abb):
tz = StaticTzInfo(name, info['offset'])
return utc_datetime.astimezone(tz)
def localize_timezone(date_time, tz_string):
if date_time.tzinfo:
return date_time
tz = None
try:
tz = timezone(tz_string)
except UnknownTimeZoneError as e:
for name, info in _tz_offsets:
if info['regex'].search(' %s' % tz_string):
tz = StaticTzInfo(name, info['offset'])
break
else:
raise e
return tz.localize(date_time)
def apply_dateparser_timezone(utc_datetime, offset_or_timezone_abb):
for name, info in _tz_offsets:
if info['regex'].search(' %s' % offset_or_timezone_abb):
tz = StaticTzInfo(name, info['offset'])
return utc_datetime.astimezone(tz)
def pop_tz_offset_from_string(date_string, as_offset=True):
if _search_regex_ignorecase.search(date_string):
for name, info in _tz_offsets:
timezone_re = info['regex']
timezone_match = timezone_re.search(date_string)
if timezone_match:
start, stop = timezone_match.span()
date_string = date_string[:start + 1] + date_string[stop:]
return (
date_string,
StaticTzInfo(name, info['offset']) if as_offset else name)
return date_string, None