Skip to content

Commit

Permalink
Close joke2k#667. Allow past dates within a second
Browse files Browse the repository at this point in the history
  • Loading branch information
fcurella committed Jan 5, 2018
1 parent fc7bbc4 commit 18fd7c0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
5 changes: 4 additions & 1 deletion faker/providers/date_time/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1342,7 +1342,10 @@ def date_time_between(self, start_date='-30y', end_date='now', tzinfo=None):
"""
start_date = self._parse_date_time(start_date, tzinfo=tzinfo)
end_date = self._parse_date_time(end_date, tzinfo=tzinfo)
ts = self.generator.random.randint(start_date, end_date)
if end_date - start_date <= 1:
ts = start_date + self.generator.random.random()
else:
ts = self.generator.random.randint(start_date, end_date)
return datetime(1970, 1, 1, tzinfo=tzinfo) + timedelta(seconds=ts)

def date_between(self, start_date='-30y', end_date='today'):
Expand Down
4 changes: 4 additions & 0 deletions tests/providers/test_date_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ def test_date_time_between_dates_with_tzinfo(self):
self.assertTrue(datetime_start <= random_date)
self.assertTrue(datetime_end >= random_date)

def test_past_datetime_within_second(self):
# Should not raise a ``ValueError``
self.factory.past_datetime(start_date='+1s')

def test_date_between_dates(self):
date_end = date.today()
date_start = date_end - timedelta(days=10)
Expand Down

0 comments on commit 18fd7c0

Please sign in to comment.