Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Workaround for dates < 1970 on Windows #1392

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions tests/unit/ibis_addon/test_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
CLIENT = ibis.pandas.connect({"table": TABLE_DF})
WHERE_FILTER = "id > 100"

SECONDS_IN_A_DAY = 60 * 60 * 24


@pytest.fixture
def module_under_test():
Expand All @@ -45,3 +47,37 @@ def test_format_raw_sql_expr(module_under_test):
raw_sql = operations.format_raw_sql(ibis_table.column, raw_sql_column_expr)

assert raw_sql == WHERE_FILTER


@pytest.mark.parametrize(
"test_input,expected",
[
(
"1970-01-01",
0,
),
(
"1970-01-01 00:00:01",
1,
),
(
"1970-01-02",
SECONDS_IN_A_DAY,
),
(
"1970-02-01 00:00:01",
(SECONDS_IN_A_DAY * 31) + 1,
),
(
"1969-12-31",
-SECONDS_IN_A_DAY,
),
(
"1969-12-31 23:59:00",
-60,
),
],
)
def test_string_to_epoch(module_under_test, test_input: str, expected: int):
result = module_under_test.string_to_epoch(test_input)
assert result == expected
22 changes: 11 additions & 11 deletions third_party/ibis/ibis_addon/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
non-textual languages.
"""
import datetime
import dateutil

import google.cloud.bigquery as bq
import ibis
Expand Down Expand Up @@ -588,19 +589,18 @@ def _bigquery_field_to_ibis_dtype(field):
return ibis_type


def string_to_epoch(ts: str):
"Function to convert string timestamp to epoch seconds"
from dateutil.parser import isoparse
from dateutil.tz import UTC
from datetime import datetime, timezone

def string_to_epoch(ts: str) -> int:
"""Function to convert string timestamp to epoch seconds"""
try:
parsed_ts = isoparse(ts).astimezone(UTC)
return (parsed_ts - datetime(1970, 1, 1, tzinfo=timezone.utc)).total_seconds()
except ValueError:
parsed_ts = dateutil.parser.isoparse(ts).astimezone(dateutil.tz.UTC)
return (
parsed_ts - datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc)
).total_seconds()
except (ValueError, OSError):
# Support DATE '0001-01-01' which throws error when converted to UTC
parsed_ts = isoparse(ts)
return (parsed_ts - datetime(1970, 1, 1)).total_seconds()
# Catching OSError above because all dates prior to 1970 fail in astimezone on Windows.
parsed_ts = dateutil.parser.isoparse(ts)
return (parsed_ts - datetime.datetime(1970, 1, 1)).total_seconds()


@execute_node.register(ops.ExtractEpochSeconds, (datetime.datetime, pd.Series))
Expand Down
Loading