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.

With python3.8 date_time_between_dates returns faker.utils.datetime_safe.datetime instead of datetime.datetime

See original GitHub issue
  • Faker version: 4.1.0
  • OS: Linux
  • Python3.8

Brief summary of the issue goes here.

date_time_between_dates return faker.utils.datetime_safe.datetime instead of datetime.datetime Problem stem from solution of python bug: https://bugs.python.org/issue32417 Faker works fine with earlier version of python (eg 3.7)

Steps to reproduce

from datetime import datetime, timedelta

from faker import Faker

fake = Faker()
Faker.seed(0)

fake_date = fake.date_time_between_dates(
    datetime_start=datetime.utcnow() - timedelta(days=360),
    datetime_end=datetime.utcnow(),
    tzinfo=None
)
    
print(type(fake_date))

Expected behavior

output: <class ‘datetime.datetime’>

Actual behavior

output: <class ‘faker.utils.datetime_safe.datetime’>

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
samcrangcommented, Sep 10, 2020

My understanding is that Faker only supports Python >= 3.5 (from the .travis.yml).

If that’s the case then I think it should be acceptable to remove the workaround which subtypes datetime. According to the comment it’s only needed to support strftime on dates before 1900. From my testing it appears that’s only a problem for older versions of Python:

Python 2.7:

Python 2.7.17 (default, Dec 23 2019, 21:25:34)
[GCC 4.2.1 Compatible Apple LLVM 11.0.0 (clang-1100.0.33.16)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> datetime.datetime(1800, 12, 11)
datetime.datetime(1800, 12, 11, 0, 0)
>>> datetime.datetime(1800, 12, 11).strftime('%Y-%m-%d')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: year=1800 is before 1900; the datetime strftime() methods require year >= 1900

Python 3.5

Python 3.5.9 (default, Sep 10 2020, 22:25:21)
[GCC 4.2.1 Compatible Apple LLVM 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> datetime.datetime(1800, 12, 11)
datetime.datetime(1800, 12, 11, 0, 0)
>>> datetime.datetime(1800, 12, 11).strftime('%Y-%m-%d')
'1800-12-11'

I think PR #1258 should fix this.

0reactions
fcurellacommented, Sep 11, 2020

Closed in #1258

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python Faker Datetime Generation returns tuple with datetime ...
The comparision fails as i try to compare a datetime object with a tuple which has a datetime object in it. If I...
Read more >
datetime — Basic date and time types — Python 3.11.1 ...
Return a string representing the date, controlled by an explicit format string. Format codes referring to hours, minutes or seconds will see 0...
Read more >
faker.providers.date_time — Faker 15.3.4 documentation
Takes two datetime objects and returns a random datetime between the two given datetimes. Accepts datetime objects. Parameters: datetime_start – datetime ...
Read more >
Using Python datetime to Work With Dates and Times
Have you ever wondered about working with dates and times in Python? In this tutorial, you'll learn all about the built-in Python datetime...
Read more >
How to use the datetime.tzinfo function in DateTime - Snyk
To help you get started, we've selected a few datetime.tzinfo examples, ... import six from faker import Faker from faker.providers.date_time import ...
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