Skip to content

Commit fe8f791

Browse files
authored
✨ use dateutil.parse to parse SQLite dates (#121)
1 parent f66232b commit fe8f791

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ dependencies = [
4141
"Click>=8.1.3",
4242
"mysql-connector-python>=8.2.0",
4343
"pytimeparse2",
44+
"python-dateutil>=2.9.0.post0",
45+
"types_python_dateutil",
4446
"simplejson>=3.19.1",
4547
"tqdm>=4.65.0",
4648
"tabulate",

requirements_dev.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ pytest-cov
99
pytest-mock
1010
pytest-timeout
1111
pytimeparse2
12+
python-dateutil>=2.9.0.post0
13+
types_python_dateutil
1214
simplejson>=3.19.1
1315
types-simplejson
1416
sqlalchemy>=2.0.0

src/sqlite3_to_mysql/sqlite_utils.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
"""SQLite adapters and converters for unsupported data types."""
22

3+
import typing as t
34
from datetime import date, timedelta
45
from decimal import Decimal
56

7+
from dateutil.parser import ParserError
8+
from dateutil.parser import parse as dateutil_parse
69
from packaging import version
710
from packaging.version import Version
811
from pytimeparse2 import parse
@@ -38,11 +41,11 @@ def unicase_compare(string_1: str, string_2: str) -> int:
3841
return 1 if _string_1 > _string_2 else -1 if _string_1 < _string_2 else 0
3942

4043

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

4851

0 commit comments

Comments
 (0)