Skip to content

Commit

Permalink
Merge branch 'develop' into 1396-validate-column-combiner-exception-f…
Browse files Browse the repository at this point in the history
…or-minmax-of-dates
  • Loading branch information
nj1973 authored Jan 23, 2025
2 parents 1c60e5f + 4ff5d3a commit da722ec
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
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 @@ -541,19 +542,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

0 comments on commit da722ec

Please sign in to comment.