Skip to content

Commit

Permalink
feat: Duration field
Browse files Browse the repository at this point in the history
  • Loading branch information
betodealmeida committed Sep 26, 2024
1 parent b9e09b5 commit 8be9805
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/shillelagh/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,15 @@ def parse(self, value: Optional[str]) -> Optional[datetime.datetime]:
return timestamp


class Duration(Field[datetime.timedelta, datetime.timedelta]):
"""
Shillelagh field used for representing durations as `timedelta` objects.
"""

type = "DURATION"
db_api_type = "DATETIME"


class StringDuration(Field[str, datetime.timedelta]):
"""
A duration.
Expand Down
18 changes: 18 additions & 0 deletions tests/fields_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,24 @@ def test_type_code() -> None:
assert NUMBER != 1


def test_duration() -> None:
"""
Test ``Duration``.
"""
connection = connect(":memory:")
cursor = connection.cursor()

sql = "CREATE TABLE test_table (a DURATION)"
cursor.execute(sql)

sql = "INSERT INTO test_table (a) VALUES (?)"
cursor.execute(sql, (datetime.timedelta(hours=1),))

sql = "SELECT * FROM test_table"
cursor.execute(sql)
assert cursor.fetchall() == [(datetime.timedelta(hours=1),)]


def test_string_duration() -> None:
"""
Test ``StringDuration``.
Expand Down

0 comments on commit 8be9805

Please sign in to comment.