question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inconsistency in unix_time handling start and end time parameters

See original GitHub issue

There 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:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
fcurellacommented, Jul 15, 2019

Yes, closed with #965

0reactions
bluesheeptokencommented, Jul 15, 2019

No worries, I have done it, was just a notification to remember to close this issue 😃.

Thanks for noticing the 🐛

Read more comments on GitHub >

github_iconTop Results From Across the Web

Perl time vs. Unix date output inconsistency - Stack Overflow
I am a total beginner in Perl and trying my first Perl script to convert time information in epoch-seconds to a specific format....
Read more >
Falsehoods programmers believe about Unix time
I read articles like this and come to the conclusion that UTC is flawed, not Unix time. Leap seconds seem mostly useless.
Read more >
Demystifying DateTime Manipulation in JavaScript - Toptal
In this article, I'm going to help you think clearly about date and time fields and suggest some best practices that can help...
Read more >
Unix time - Wikipedia
Unix time is a date and time representation widely used in computing. It measures time by the number of seconds that have elapsed...
Read more >
Date Functions - Trifacta Documentation
NOTE: Other than the UNIXTIME function, date functions ignore time zone in Datetime values. Date ranges: Valid dates fit in the following range: ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found