fake.time_delta is always datetime.timedelta(0)
- Faker version: all
- OS: all
I'm not sure I understand why this is the case. But the expected behvaior as described in the documentation is that fake.time_delta() always returns timedelta(0). I would expect this to be randomized like all the other fake values we generate. If this is intentional, please let me know. I couldn't discussions around this behavior.
Actual behavior
from faker import Faker
Faker.seed(0)
fake = Faker()
for _ in range(5):
fake.time_delta()
datetime.timedelta(0)
datetime.timedelta(0)
datetime.timedelta(0)
datetime.timedelta(0)
datetime.timedelta(0)
Expected behavior
The result should be
datetime.timedelta(days=8041, seconds=60445, microseconds=593254)
datetime.timedelta(days=-9448, seconds=15856, microseconds=620945)
datetime.timedelta(days=7332, seconds=68815, microseconds=994962)
datetime.timedelta(days=-1456, seconds=6174, microseconds=787389)
datetime.timedelta(days=-1833, seconds=11987, microseconds=179397)
Workaround
from faker import Faker
Faker.seed(0)
fake = Faker()
for _ in range(5):
fake.date_time() - fake.date_time()
datetime.timedelta(days=8041, seconds=60445, microseconds=593254)
datetime.timedelta(days=-9448, seconds=15856, microseconds=620945)
datetime.timedelta(days=7332, seconds=68815, microseconds=994962)
datetime.timedelta(days=-1456, seconds=6174, microseconds=787389)
datetime.timedelta(days=-1833, seconds=11987, microseconds=179397)
I am more than happy to submit a patch if this needs to be fixed.
This has been changed in #907 and only occurs when running this without end_datetime as it seems to choose a seconds value in the range [0, datetime.now() - datetime.now()] in this case, which will basically always map to [0, 0] and thus always generate 0: https://github.com/joke2k/faker/blob/554d1aa1ce75ff26cf36e35fa9aafb9f03d1e5f4/faker/providers/date_time/init.py#L1837-L1846
This has been changed in #907 and only occurs when running this without
end_datetimeas it seems to choose a seconds value in the range[0, datetime.now() - datetime.now()]in this case, which will basically always map to[0, 0]and thus always generate0:
Thanks for the reply and the additional context @stefan6419846!
While #907 does improve the behavior with end_datetime. I still don't think the behavior when trying to generate a random time_delta should result in non-random values. If an end_datetime is required to output random timedeltas, then it should be a required parameter. But I don't it should be required parameter to get random timedeltas.
As an example, this wouldn't be considered acceptable:
>>> for _ in range(5):
>>> print(fake.pyint())
0
0
0
0
0
I think we need to change the signature of timedelta to make end_datetime mandatory.
I can submit a patch for that.
This issue is stale because it has been open for 30 days with no activity.
This issue was closed because it has been inactive for 14 days since being marked as stale.