faker icon indicating copy to clipboard operation
faker copied to clipboard

fake.time_delta is always datetime.timedelta(0)

Open amingilani opened this issue 1 year ago • 4 comments

  • 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.

amingilani avatar Jul 29 '24 16:07 amingilani

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

stefan6419846 avatar Jul 29 '24 16:07 stefan6419846

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:

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

amingilani avatar Jul 29 '24 16:07 amingilani

I think we need to change the signature of timedelta to make end_datetime mandatory.

fcurella avatar Jul 29 '24 17:07 fcurella

I can submit a patch for that.

amingilani avatar Aug 22 '24 16:08 amingilani

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Nov 21 '24 02:11 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Dec 05 '24 02:12 github-actions[bot]