Skip to content

Commit

Permalink
✨ use dateutil.parse to parse SQLite dates (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
techouse authored Jul 20, 2024
1 parent aa5c4b2 commit fb51d3a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ dependencies = [
"Click>=8.1.3",
"mysql-connector-python==8.4.0",
"pytimeparse2",
"python-dateutil>=2.9.0.post0",
"types_python_dateutil",
"python-slugify>=7.0.0",
"simplejson>=3.19.0",
"tqdm>=4.65.0",
Expand Down
2 changes: 2 additions & 0 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pytest-cov
pytest-mock
pytest-timeout
pytimeparse2
python-dateutil>=2.9.0.post0
types_python_dateutil
python-slugify>=7.0.0
types-python-slugify
simplejson>=3.19.1
Expand Down
8 changes: 5 additions & 3 deletions src/mysql_to_sqlite3/sqlite_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from datetime import date, timedelta
from decimal import Decimal

from dateutil.parser import ParserError
from dateutil.parser import parse as dateutil_parse
from pytimeparse2 import parse


Expand Down Expand Up @@ -46,11 +48,11 @@ class CollatingSequences:
RTRIM: str = "RTRIM"


def convert_date(value: t.Any) -> date:
def convert_date(value: t.Union[str, bytes]) -> date:
"""Handle SQLite date conversion."""
try:
return date.fromisoformat(value.decode())
except ValueError as err:
return dateutil_parse(value.decode() if isinstance(value, bytes) else value).date()
except ParserError as err:
raise ValueError(f"DATE field contains {err}") # pylint: disable=W0707


Expand Down

0 comments on commit fb51d3a

Please sign in to comment.