Skip to content

Commit

Permalink
Fix datetime calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
fcurella committed Jan 21, 2025
1 parent 50c2baf commit 388fe06
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
30 changes: 23 additions & 7 deletions faker/providers/date_time/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
from datetime import tzinfo as TzInfo
from typing import Any, Callable, Dict, Iterator, Optional, Tuple, Union

import pytz

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

from faker.typing import Country, DateParseType

Expand All @@ -21,8 +23,10 @@


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())
if dt == datetime.min:
return timegm(dt.timetuple())
if isinstance(dt, datetime):
dt = dt.astimezone(pytz.utc)
return timegm(dt.timetuple())


Expand All @@ -38,7 +42,7 @@ def convert_timestamp_to_datetime(timestamp: Union[int, float], tzinfo: TzInfo)
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)
return pick.astimezone(pytz.utc).replace(tzinfo=None)
return convert_timestamp_to_datetime(timestamp, tzinfo)


Expand Down Expand Up @@ -1849,10 +1853,15 @@ def _get_reference_date_time(
If both are absolute, return the most recent one.
If both are None, return now.
"""

min_ = datetime_to_timestamp(datetime.min)
now = datetime.now(tzinfo)
if start_date is None and end_date is None:
return now
if start_date is None and end_date is not None:
start_date = now
elif start_date is not None and end_date is None:
end_date = now

start_int = cls._parse_date_time(start_date, now) if start_date is not None else min_
end_int = cls._parse_date_time(end_date, now) if end_date is not None else min_
Expand Down Expand Up @@ -1965,7 +1974,14 @@ def unix_time(
:example: 1061306726.6
"""
now = self._get_reference_date_time(start_datetime, end_datetime, tzinfo=None)
if start_datetime is not None and end_datetime is None:
if start_datetime == "now":
end_datetime = "+30d"
else:
end_datetime = datetime.now(tz=pytz.utc)
elif start_datetime is None and end_datetime is not None:
start_datetime = datetime(1970, 1, 1, tzinfo=pytz.utc)
now = self._get_reference_date_time(start_datetime, end_datetime, tzinfo=pytz.utc)
start_datetime = self._parse_start_datetime(now, start_datetime)
end_datetime = self._parse_end_datetime(now, end_datetime)
return float(self._rand_seconds(start_datetime, end_datetime))
Expand Down Expand Up @@ -2113,7 +2129,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=pytz.utc) + timedelta(seconds=ts)).astimezone(tzinfo)

def date_between(self, start_date: DateParseType = "-30y", end_date: DateParseType = "today") -> dtdate:
"""
Expand Down Expand Up @@ -2220,7 +2236,7 @@ def date_time_between_dates(
if tzinfo is None:
pick = convert_timestamp_to_datetime(timestamp, tzlocal())
try:
pick = pick.astimezone(tzutc()).replace(tzinfo=None)
pick = pick.replace(tzinfo=None)
except OSError:
pass
else:
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
python_requires=">=3.8",
install_requires=[
"python-dateutil>=2.4",
"pytz",
"typing_extensions",
],
)

0 comments on commit 388fe06

Please sign in to comment.