time-machine icon indicating copy to clipboard operation
time-machine copied to clipboard

String times get interpreted in a wrong way

Open aboueleyes opened this issue 2 years ago • 5 comments

Python Version

3.10

pytest Version

No response

Package Version

No response

Description

class TempTest(TestCase):
    @travel(datetime.datetime(2020, 6, 1, 0, 0, 0))
    def test_time(self):
        print(timezone.now())
        self.assertEqual(datetime.date(2020, 6, 1), timezone.now().date())
        self.assertEqual(timezone.now().date(), datetime.date(2020, 6, 1))

    @travel("2020-06-01")
    def test_time2(self):
        print(timezone.now())
        self.assertEqual(datetime.date(2020, 6, 1), timezone.now().date())
        self.assertEqual(timezone.now().date(), datetime.date(2020, 6, 1))
2020-06-01 00:00:00+00:00
.2020-05-31 22:00:00+00:00
F
======================================================================
FAIL: test_time2 (shahry.core.tests.test_utils.TempTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/time_machine/__init__.py", line 336, in wrapper
    return wrapped(*args, **kwargs)
  File "/app/shahry/core/tests/test_utils.py", line 783, in test_time2
    self.assertEqual(datetime.date(2020, 6, 1), timezone.now().date())
AssertionError: datetime.date(2020, 6, 1) != datetime.date(2020, 5, 31)

for some reason when using"2020-06-01" the time is getting wrong

aboueleyes avatar May 18 '23 07:05 aboueleyes

This will be due to the way that dateutil parses the datetime, and probably interprets it with the current timezone: https://dateutil.readthedocs.io/en/stable/parser.html

I've been considering removing the dateutil parsing in favour of simpler support with date.fromisoformat() and datetime.isoformat().

If the date was interpreted as a local date oject, would that help you?

adamchainz avatar May 18 '23 08:05 adamchainz

yes, it would be helpful I guess freezegun have it treated it that way, so it will also make it easier to migrate from freezegun

aboueleyes avatar May 18 '23 08:05 aboueleyes

No, freezegun uses dateutil, so this would be an incompatibility.

adamchainz avatar May 18 '23 08:05 adamchainz

mm yes but its behavior is correct with me

class TempTest2(TestCase):
    @freeze_time(datetime.datetime(2020, 6, 1, 0, 0, 0))
    def test_time(self):
        print(timezone.now())
        self.assertEqual(datetime.date(2020, 6, 1), timezone.now().date())
        self.assertEqual(timezone.now().date(), datetime.date(2020, 6, 1))

    @freeze_time("2020-06-01")
    def test_time2(self):
        print(timezone.now())
        self.assertEqual(datetime.date(2020, 6, 1), timezone.now().date())
        self.assertEqual(timezone.now().date(), datetime.date(2020, 6, 1))
2020-06-01 00:00:00+00:00
.2020-06-01 00:00:00+00:00
.
----------------------------------------------------------------------
Ran 2 tests in 0.119s

OK

same tests

aboueleyes avatar May 18 '23 09:05 aboueleyes

See #306 . I actually prefer the interpretation of a string to be in the timezone from my settings.

P.S. If it is changed, please treat it as a breaking change; because I have now written many of my tests using the current interpretation.

Siburg avatar Jul 09 '23 08:07 Siburg