Skip to content

Commit

Permalink
Clarify logic for timezones when parsing unix timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
onlynone committed Sep 3, 2022
1 parent 3e8b347 commit 6934648
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions dateparser/date.py
Expand Up @@ -119,19 +119,24 @@ def get_date_from_timestamp(date_string, settings, negative=False):
if negative:
match = RE_SEARCH_NEGATIVE_TIMESTAMP.search(date_string)
else:
match = RE_SEARCH_TIMESTAMP.search(date_string)
match = RE_SEARCH_TIMESTAMP.search(date_string)

if match:
if settings is not None and settings.TIMEZONE is not None and 'local' not in settings.TIMEZONE.lower():
local_timezone = get_timezone_from_tz_string(settings.TIMEZONE)
if (settings is None or
settings.TIMEZONE is None or
'local' in settings.TIMEZONE.lower()):
# If the timezone in settings is unset, or it's 'local', use the
# local timezone
timezone = get_localzone()
else:
local_timezone = get_localzone()
# Otherwise, use the timezone given in settings
timezone = get_timezone_from_tz_string(settings.TIMEZONE)

seconds = int(match.group(1))
millis = int(match.group(2) or 0)
micros = int(match.group(3) or 0)
date_obj = (datetime
.fromtimestamp(seconds, local_timezone)
.fromtimestamp(seconds, timezone)
.replace(microsecond=millis * 1000 + micros, tzinfo=None)
)
date_obj = apply_timezone_from_settings(date_obj, settings)
Expand Down

0 comments on commit 6934648

Please sign in to comment.