Skip to content

Commit

Permalink
fix parse
Browse files Browse the repository at this point in the history
  • Loading branch information
girarda committed Aug 9, 2022
1 parent 558c50c commit 3c76c5a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,15 @@ def _get_date(self, cursor_value, default_date: datetime.datetime, comparator) -
return comparator(cursor_date, default_date)

def parse_date(self, date: Union[str, datetime.datetime]) -> datetime.datetime:
if isinstance(date, str):
return datetime.datetime.strptime(str(date), self.datetime_format).replace(tzinfo=self._timezone)
else:
if isinstance(date, datetime.datetime):
return date
elif self.datetime_format == "%s":
# strftime("%s") is unreliable because it ignores the time zone information and assumes the time zone of the system it's running on
# It's safer to use the timestamp() method than the %s directive
# See https://stackoverflow.com/a/4974930
return datetime.datetime.fromtimestamp(int(date)).replace(tzinfo=self._timezone)
else:
return datetime.datetime.strptime(str(date), self.datetime_format).replace(tzinfo=self._timezone)

@classmethod
def _parse_timedelta(cls, time_str):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,12 @@ def test_request_option(test_name, inject_into, field_name, expected_req_params,
"%Y%m%d",
datetime.datetime(2021, 1, 1, 0, 0, tzinfo=datetime.timezone.utc),
),
(
"test_parse_timestamp",
"1609459200",
"%s",
datetime.datetime(2021, 1, 1, 0, 0, tzinfo=datetime.timezone.utc),
),
],
)
def test_parse_date(test_name, input_date, date_format, expected_output_date):
Expand Down

0 comments on commit 3c76c5a

Please sign in to comment.