Skip to content

Commit

Permalink
Make “today” and “now” localizable
Browse files Browse the repository at this point in the history
  • Loading branch information
fcurella committed Jan 17, 2025
1 parent a0e656c commit 6609891
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
31 changes: 26 additions & 5 deletions faker/providers/date_time/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1837,6 +1837,8 @@ def _rand_seconds(self, start_datetime: int, end_datetime: int) -> float:
),
]

today_word = "today"
now_word = "now"
regex = re.compile(timedelta_pattern)

def unix_time(
Expand Down Expand Up @@ -1971,6 +1973,18 @@ def time_object(self, end_datetime: Optional[DateParseType] = None) -> dttime:
"""
return self.date_time(end_datetime=end_datetime).time()

@classmethod
def _is_absolute(cls, obj: DateParseType) -> bool:
if isinstance(obj, (datetime, dtdate, int)):
return True
elif isinstance(obj, timedelta):
return False
elif isinstance(obj, str):
if obj in (cls.today_word, cls.now_word):
return False
return cls.regex.fullmatch(obj) is None
return False

@classmethod
def _parse_start_datetime(cls, now: datetime, value: Optional[DateParseType]) -> int:
if value is None:
Expand Down Expand Up @@ -2027,7 +2041,7 @@ def _parse_date_time(cls, value: DateParseType, now: datetime, tzinfo: Optional[
if isinstance(value, timedelta):
return datetime_to_timestamp(now + value)
if isinstance(value, str):
if value == "now":
if value == cls.now_word:
return datetime_to_timestamp(datetime.now(tzinfo))
time_params = cls._parse_date_string(value)
return datetime_to_timestamp(now + timedelta(**time_params)) # type: ignore
Expand All @@ -2044,7 +2058,7 @@ def _parse_date(cls, value: DateParseType, today: dtdate) -> dtdate:
if isinstance(value, timedelta):
return today + value
if isinstance(value, str):
if value in ("today", "now"):
if value in (cls.today_word, cls.now_word):
return today
time_params = cls._parse_date_string(value)
return today + timedelta(**time_params) # type: ignore
Expand All @@ -2055,7 +2069,7 @@ def _parse_date(cls, value: DateParseType, today: dtdate) -> dtdate:
def date_time_between(
self,
start_date: DateParseType = "-30y",
end_date: DateParseType = "now",
end_date: Optional[DateParseType] = None,
tzinfo: Optional[TzInfo] = None,
) -> datetime:
"""
Expand All @@ -2068,6 +2082,9 @@ def date_time_between(
:example: datetime('1999-02-02 11:42:52')
:return: datetime
"""
if end_date is None:
end_date = self.now_word

now = get_now_date_time(start_date, end_date, tzinfo)
start_date = self._parse_date_time(start_date, now, tzinfo=tzinfo)
end_date = self._parse_date_time(end_date, now, tzinfo=tzinfo)
Expand All @@ -2080,7 +2097,7 @@ def date_time_between(
else:
return (datetime(1970, 1, 1, tzinfo=tzutc()) + timedelta(seconds=ts)).astimezone(tzinfo)

def date_between(self, start_date: DateParseType = "-30y", end_date: DateParseType = "today") -> dtdate:
def date_between(self, start_date: DateParseType = "-30y", end_date: Optional[DateParseType] = None) -> dtdate:
"""
Get a Date object based on a random date between two given dates.
Accepts date strings that can be recognized by strtotime().
Expand All @@ -2090,6 +2107,8 @@ def date_between(self, start_date: DateParseType = "-30y", end_date: DateParseTy
:example: Date('1999-02-02')
:return: Date
"""
if end_date is None:
end_date = self.today_word
today = get_now_date(start_date, end_date)
start_date = self._parse_date(start_date, today)
end_date = self._parse_date(end_date, today)
Expand Down Expand Up @@ -2412,7 +2431,7 @@ def date_this_month(self, before_today: bool = True, after_today: bool = False)
def time_series(
self,
start_date: DateParseType = "-30d",
end_date: DateParseType = "now",
end_date: Optional[DateParseType] = None,
precision: Optional[float] = None,
distrib: Optional[Callable[[datetime], float]] = None,
tzinfo: Optional[TzInfo] = None,
Expand All @@ -2425,6 +2444,8 @@ def time_series(
``distrib`` is a callable that accepts ``<datetime>`` and returns ``<value>``
"""
if end_date is None:
end_date = self.now_word
now = get_now_date_time(start_date, end_date, tzinfo)
start_date_ = self._parse_date_time(start_date, now, tzinfo=tzinfo)
end_date_ = self._parse_date_time(end_date, now, tzinfo=tzinfo)
Expand Down
6 changes: 3 additions & 3 deletions faker/proxy.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ class Faker:
def date_between(
self,
start_date: Union[datetime.date, datetime.datetime, datetime.timedelta, str, int] = ...,
end_date: Union[datetime.date, datetime.datetime, datetime.timedelta, str, int] = ...,
end_date: Union[datetime.date, datetime.datetime, datetime.timedelta, str, int, None] = ...,
) -> datetime.date:
"""
Get a Date object based on a random date between two given dates.
Expand Down Expand Up @@ -1098,7 +1098,7 @@ class Faker:
def date_time_between(
self,
start_date: Union[datetime.date, datetime.datetime, datetime.timedelta, str, int] = ...,
end_date: Union[datetime.date, datetime.datetime, datetime.timedelta, str, int] = ...,
end_date: Union[datetime.date, datetime.datetime, datetime.timedelta, str, int, None] = ...,
tzinfo: Optional[datetime.tzinfo] = ...,
) -> datetime.datetime:
"""
Expand Down Expand Up @@ -1322,7 +1322,7 @@ class Faker:
def time_series(
self,
start_date: Union[datetime.date, datetime.datetime, datetime.timedelta, str, int] = ...,
end_date: Union[datetime.date, datetime.datetime, datetime.timedelta, str, int] = ...,
end_date: Union[datetime.date, datetime.datetime, datetime.timedelta, str, int, None] = ...,
precision: Optional[float] = ...,
distrib: Optional[Callable[[datetime.datetime], float]] = ...,
tzinfo: Optional[datetime.tzinfo] = ...,
Expand Down

0 comments on commit 6609891

Please sign in to comment.