Skip to content

Commit

Permalink
Drop dateutil dependency (#2173)
Browse files Browse the repository at this point in the history
* Drop dateutil dependency

* Remove now-empty install_requires

* Reduce duplication

---------

Co-authored-by: Flavio Curella <[email protected]>
  • Loading branch information
knyghty and fcurella authored Feb 10, 2025
1 parent c31a8e6 commit 604e1ed
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
33 changes: 21 additions & 12 deletions faker/providers/date_time/__init__.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,44 @@
import platform

Check failure on line 1 in faker/providers/date_time/__init__.py

View workflow job for this annotation

GitHub Actions / isort

Imports are incorrectly sorted and/or formatted.
import re
import zoneinfo

from calendar import timegm
from datetime import MAXYEAR
from datetime import date as dtdate
from datetime import datetime
from datetime import time as dttime
from datetime import timedelta
from datetime import timezone
from datetime import tzinfo as TzInfo
from typing import Any, Callable, Dict, Iterator, Optional, Tuple, Union

from dateutil import relativedelta
from dateutil.tz import gettz, tzlocal, tzutc

from faker.typing import Country, DateParseType

from .. import BaseProvider, ElementsType

localized = True


def _get_local_timezone():
datetime.now().astimezone().tzinfo


def _get_next_month_start(dt: Union[dtdate, datetime]):

Check failure on line 26 in faker/providers/date_time/__init__.py

View workflow job for this annotation

GitHub Actions / typing (3.9)

Function is missing a return

Check failure on line 26 in faker/providers/date_time/__init__.py

View workflow job for this annotation

GitHub Actions / typing (3.11)

Function is missing a return
if dt.month == 12:
return dt.replace(year=dt.year + 1, month=1)
return dt.replace(month=dt.month + 1)


def datetime_to_timestamp(dt: Union[dtdate, datetime]) -> int:
if isinstance(dt, datetime) and getattr(dt, "tzinfo", None) is not None:
dt = dt.astimezone(tzutc())
dt = dt.astimezone(timezone.utc)
return timegm(dt.timetuple())


def timestamp_to_datetime(timestamp: Union[int, float], tzinfo: Optional[TzInfo]) -> datetime:
if tzinfo is None:
pick = convert_timestamp_to_datetime(timestamp, tzlocal())
return pick.astimezone(tzutc()).replace(tzinfo=None)
pick = convert_timestamp_to_datetime(timestamp, _get_local_timezone())
return pick.astimezone(timezone.utc).replace(tzinfo=None)
return convert_timestamp_to_datetime(timestamp, tzinfo)


Expand Down Expand Up @@ -2100,7 +2109,7 @@ def date_time_between(
if tzinfo is None:
return datetime(1970, 1, 1, tzinfo=tzinfo) + timedelta(seconds=ts)
else:
return (datetime(1970, 1, 1, tzinfo=tzutc()) + timedelta(seconds=ts)).astimezone(tzinfo)
return (datetime(1970, 1, 1, tzinfo=timezone.utc) + timedelta(seconds=ts)).astimezone(tzinfo)

def date_between(self, start_date: DateParseType = "-30y", end_date: DateParseType = "today") -> dtdate:
"""
Expand Down Expand Up @@ -2202,9 +2211,9 @@ def date_time_between_dates(
timestamp = self._rand_seconds(datetime_start_, datetime_end_)
try:
if tzinfo is None:
pick = convert_timestamp_to_datetime(timestamp, tzlocal())
pick = convert_timestamp_to_datetime(timestamp, _get_local_timezone())
try:
pick = pick.astimezone(tzutc()).replace(tzinfo=None)
pick = pick.astimezone(timezone.utc).replace(tzinfo=None)
except OSError:
pass
else:
Expand Down Expand Up @@ -2336,8 +2345,8 @@ def date_time_this_month(
"""
now = datetime.now(tzinfo)
this_month_start = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
next_month_start = _get_next_month_start(this_month_start)

next_month_start = this_month_start + relativedelta.relativedelta(months=1)
if before_now and after_now:
return self.date_time_between_dates(this_month_start, next_month_start, tzinfo)
elif not before_now and after_now:
Expand Down Expand Up @@ -2428,8 +2437,8 @@ def date_this_month(self, before_today: bool = True, after_today: bool = False)
"""
today = dtdate.today()
this_month_start = today.replace(day=1)
next_month_start = _get_next_month_start(this_month_start)

next_month_start = this_month_start + relativedelta.relativedelta(months=1)
if before_today and after_today:
return self.date_between_dates(this_month_start, next_month_start)
elif not before_today and after_today:
Expand Down Expand Up @@ -2540,7 +2549,7 @@ def pytimezone(self, *args: Any, **kwargs: Any) -> Optional[TzInfo]:
:sample:
"""
return gettz(self.timezone(*args, **kwargs)) # type: ignore
return zoneinfo.ZoneInfo(self.timezone(*args, **kwargs)) # type: ignore

def date_of_birth(
self,
Expand Down
3 changes: 0 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,4 @@
platforms=["any"],
zip_safe=zip_safe,
python_requires=">=3.9",
install_requires=[
"python-dateutil>=2.4",
],
)
5 changes: 2 additions & 3 deletions tests/providers/test_date_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sys
import time
import unittest
import zoneinfo

from datetime import date, datetime
from datetime import time as datetime_time
Expand Down Expand Up @@ -167,10 +168,8 @@ def test_timezone_conversion(self):
assert today == today_back

def test_pytimezone(self):
import dateutil

pytz = self.fake.pytimezone()
assert isinstance(pytz, dateutil.tz.tz.tzfile)
assert isinstance(pytz, zoneinfo.ZoneInfo)

def test_pytimezone_usable(self):
pytz = self.fake.pytimezone()
Expand Down

0 comments on commit 604e1ed

Please sign in to comment.