faker
faker copied to clipboard
unix_time() with start_datetime crashes with OverflowError
- Faker version: 13.12.0
- OS: Linux 5.18.1-arch1-1
I want to create two unix timestamps: One with any value and the other one with a value higher than the first one (minimal example below). Does not work but crashes with an OverflowError (Traceback below).
Steps to reproduce
Create a minimal example:
from faker import Faker
fake = Faker()
Faker.seed(0)
unix_time_1 = fake.unix_time()
unix_time_2 = fake.unix_time(start_datetime=unix_time_1)
Execute the example and get the following traceback:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/…/venv/lib/python3.10/site-packages/faker/providers/date_time/__init__.py", line 1813, in unix_time
start_datetime = self._parse_start_datetime(start_datetime)
File "/home/…/venv/lib/python3.10/site-packages/faker/providers/date_time/__init__.py", line 1924, in _parse_start_datetime
return cls._parse_date_time(value)
File "/home/…/venv/lib/python3.10/site-packages/faker/providers/date_time/__init__.py", line 1981, in _parse_date_time
return datetime_to_timestamp(now + timedelta(value))
OverflowError: date value out of range
Expected behavior
unix_time_2
should be a unix timestamp with a value higher than unix_time_1
.
Actual behavior
Crashes with OverflowError.
Howdy! I was just looking into this bug and I noticed the OverflowError is caused because Provider._parse_date_time when taking an integer as a parameter treats it as a timedelta in days where as unix_time() returns the POSIX timestamp in seconds. Provider._parse_date_time's behavior is set to treat integer arguments as days in the tests, so would the preferred behavior be to accept integer date times as timestamps or keep it as days?
Thank you for looking into this @mochi-moshi . I think integers as timestamps is what most people would expect, so I'd prefer that.