Inconsistency in unix_time handling start and end time parameters
See original GitHub issueThere is an inconsistency the way Faker handles unix_time()
when parameter start_datetime
is provided as ‘now’. start_datetime
is parsed using class method _parse_start_datetime
https://github.com/joke2k/faker/blob/6db5503cf070c0b09da8e98cd662c10230cea22f/faker/providers/date_time/__init__.py#L1374
which is eventually parsed by _parse_date_time
, which can take argument ‘now’. When ‘now’ is the provided, datetime.now
, which will use local time,
https://github.com/joke2k/faker/blob/6db5503cf070c0b09da8e98cd662c10230cea22f/faker/providers/date_time/__init__.py#L1525
However, end time parameter is parsed by _parse_end_datetime
, which uses time.time()
. time.time()
returns time since epoc ie it is utc timestamp.
https://github.com/joke2k/faker/blob/6db5503cf070c0b09da8e98cd662c10230cea22f/faker/providers/date_time/__init__.py#L1477
on machines with negetive time offset, faker will run into problems because of this inconsistancy. Example
>>> import faker
>>> f = faker.Faker()
>>> f.unix_time(start_datetime='-6h')
1555352053
>>> f.unix_time(start_datetime='now')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.7/dist-packages/faker/providers/date_time/__init__.py", line 1376, in unix_time
return self.generator.random.randint(start_datetime, end_datetime)
File "/usr/lib/python3.7/random.py", line 222, in randint
return self.randrange(a, b+1)
File "/usr/lib/python3.7/random.py", line 200, in randrange
raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))
*ValueError: empty range for randrange() (1555373558,1555353759, -19799)*
>>>
Perhaps ‘now’ should be utcnow
here
https://github.com/joke2k/faker/blob/6db5503cf070c0b09da8e98cd662c10230cea22f/faker/providers/date_time/__init__.py#L1525
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
Yes, closed with #965
No worries, I have done it, was just a notification to remember to close this issue 😃.
Thanks for noticing the 🐛