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

✨ use dateutil.parse to parse SQLite dates #83

Merged
merged 3 commits into from
Jul 20, 2024
Merged
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
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 @@
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:

Check warning on line 55 in src/mysql_to_sqlite3/sqlite_utils.py

View check run for this annotation

Codecov / codecov/patch

src/mysql_to_sqlite3/sqlite_utils.py#L54-L55

Added lines #L54 - L55 were not covered by tests
raise ValueError(f"DATE field contains {err}") # pylint: disable=W0707


Expand Down