-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'alex/datetimeFormatTimestamp' into alex/configbasedsend…
…grid
- Loading branch information
Showing
5 changed files
with
107 additions
and
26 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
airbyte-cdk/python/airbyte_cdk/sources/declarative/datetime/datetime_parser.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# | ||
# Copyright (c) 2022 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
import datetime | ||
from typing import Union | ||
|
||
|
||
class DatetimeParser: | ||
""" | ||
Parses and formats datetime objects according to a specified format. | ||
This class mainly acts as a wrapper to properly handling timestamp formatting through the "%s" directive. | ||
%s is part of the list of format codes required by the 1989 C standard, but it is unreliable because it ignores the time zone information | ||
Instead of using the directive directly, we can use datetime.fromtimestamp and dt.timestamp() | ||
""" | ||
|
||
def parse(self, date: Union[str, int], format: str, timezone): | ||
# "%s" is a valid (but unreliable) directive for formatting, but not for parsing | ||
# It is defined as | ||
# The number of seconds since the Epoch, 1970-01-01 00:00:00+0000 (UTC). https://man7.org/linux/man-pages/man3/strptime.3.html | ||
# | ||
# The recommended way to parse a date from its timestamp representation is to use datetime.fromtimestamp | ||
# See https://stackoverflow.com/a/4974930 | ||
if format == "%s": | ||
return datetime.datetime.fromtimestamp(int(date), tz=timezone) | ||
else: | ||
return datetime.datetime.strptime(str(date), format).replace(tzinfo=timezone) | ||
|
||
def format(self, dt: datetime.datetime, format: str) -> str: | ||
# 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 | ||
if format == "%s": | ||
return str(int(dt.timestamp())) | ||
else: | ||
return dt.strftime(format) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
airbyte-cdk/python/unit_tests/sources/declarative/datetime/test_datetime_parser.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# | ||
# Copyright (c) 2022 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
import datetime | ||
|
||
import pytest | ||
from airbyte_cdk.sources.declarative.datetime.datetime_parser import DatetimeParser | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"test_name, input_date, date_format, expected_output_date", | ||
[ | ||
( | ||
"test_parse_date_iso", | ||
"2021-01-01T00:00:00.000000+0000", | ||
"%Y-%m-%dT%H:%M:%S.%f%z", | ||
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), | ||
), | ||
("test_parse_date_number", "20210101", "%Y%m%d", datetime.datetime(2021, 1, 1, 0, 0, tzinfo=datetime.timezone.utc)), | ||
], | ||
) | ||
def test_parse_date(test_name, input_date, date_format, expected_output_date): | ||
parser = DatetimeParser() | ||
output_date = parser.parse(input_date, date_format, datetime.timezone.utc) | ||
assert expected_output_date == output_date | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"test_name, input_dt, datetimeformat, expected_output", | ||
[ | ||
("test_format_timestamp", datetime.datetime(2021, 1, 1, 0, 0, tzinfo=datetime.timezone.utc), "%s", "1609459200"), | ||
("test_format_string", datetime.datetime(2021, 1, 1, 0, 0, tzinfo=datetime.timezone.utc), "%Y-%m-%d", "2021-01-01"), | ||
("test_format_to_number", datetime.datetime(2021, 1, 1, 0, 0, tzinfo=datetime.timezone.utc), "%Y%m%d", "20210101"), | ||
], | ||
) | ||
def test_format_datetime(test_name, input_dt, datetimeformat, expected_output): | ||
parser = DatetimeParser() | ||
output_date = parser.format(input_dt, datetimeformat) | ||
assert expected_output == output_date |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters