Skip to content

Commit

Permalink
Fix duckdb's from_iso8601_timestamp() to handle just-year timestamps
Browse files Browse the repository at this point in the history
i.e. "2024" -> "2024-01-01"
  • Loading branch information
mikix committed Jan 10, 2024
1 parent 4c66973 commit 701b00a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
8 changes: 8 additions & 0 deletions cumulus_library/databases.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ def _compat_from_iso8601_timestamp(
) -> Optional[datetime.datetime]:
if value is None:
return None
if (
len(value) < 10
): # handle partial dates like 1970 or 1980-12 (which spec allows)
pieces = value.split("-")
if len(pieces) == 1:
return datetime.datetime(int(pieces[0]), 1, 1)
else:
return datetime.datetime(int(pieces[0]), int(pieces[1]), 1)
return datetime.datetime.fromisoformat(value)

def cursor(self) -> duckdb.DuckDBPyConnection:
Expand Down
27 changes: 26 additions & 1 deletion tests/test_duckdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
import os
import tempfile

from datetime import datetime, timedelta, timezone
from unittest import mock
from pathlib import Path

from cumulus_library import cli
import pytest

from cumulus_library import cli, databases


@mock.patch.dict(
Expand Down Expand Up @@ -48,3 +51,25 @@ def test_duckdb_core_build_and_export():
) as f:
expected = f.read().strip()
assert generated == expected, basename


@pytest.mark.parametrize(
"timestamp,expected",
[
("2021", datetime(2021, 1, 1, tzinfo=timezone.utc)),
("2019-10", datetime(2019, 10, 1, tzinfo=timezone.utc)),
("1923-01-23", datetime(1923, 1, 23, tzinfo=timezone.utc)),
(
"2023-01-16T07:55:25-05:00",
datetime(2023, 1, 16, 7, 55, 25, tzinfo=timezone(timedelta(hours=-5))),
),
],
)
def test_duckdb_from_iso8601_timestamp(timestamp, expected):
db = databases.DuckDatabaseBackend(":memory:")
parsed = (
db.cursor()
.execute(f"select from_iso8601_timestamp('{timestamp}')")
.fetchone()[0]
)
assert parsed, expected

0 comments on commit 701b00a

Please sign in to comment.