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.

can't freeze `datetime.now()`

See original GitHub issue

I decorated a test, and this test will call a func, take a look at the output:

somehow it successfully froze datetime.today() but not datetime.now()

image

this is more obvious:

import datetime as dtmodule

image

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:5
  • Comments:7

github_iconTop GitHub Comments

4reactions
andkoncommented, Oct 6, 2021

Same with Django’s timezone.now().

2reactions
abcdeniscommented, Apr 24, 2022

@gerrywd This is strange a bit to do some calculation as default parameter value. Use sentinel (None) instead:

#!/usr/bin/env python3
from datetime import datetime, date

from freezegun import freeze_time


def hello(initial_datetime=None, initial_date=date.today()):
    if initial_datetime is None:
        initial_datetime = datetime.now()
    print()
    print('initial_now', initial_datetime)
    print('inline__now', datetime.now())
    print('initial_today', datetime.now())
    print('inline__today', date.today())


@freeze_time("2021-05-05")
def test_hello():
    assert hello()
test2.py::test_hello FAILED                                              [100%]
initial_now 2021-05-05 00:00:00
inline__now 2021-05-05 00:00:00
initial_today 2021-05-05 00:00:00
inline__today 2021-05-05
Read more comments on GitHub >

github_iconTop Results From Across the Web

Trying to mock datetime.date.today(), but not working
Trying to mock datetime.date.today(), but not working · 1. Docs of the mock library: voidspace.org.uk/python/mock/examples.html#partial-mocking · 2. freezegun · 1.
Read more >
Stop Using datetime.now! | Haki Benita
This test will pass today, but it will fail on any other day. Another way to test the function is this: # Bad...
Read more >
Freezegun - Real Joy For Fake Dates In Python - PyBites
Freezegun is a library for mocking Python datetimes. It does one thing, does it well, and lets you get on with your life....
Read more >
Date and time in Google Sheets - Ablebits
Fill a few cells with the required date/time/date-time values. ... NOW() - returns the current date and time to a cell.
Read more >
How to work with dates and time with Python - Opensource.com
We can see how to get the current time via the now function of datetime. On the second call we pass a tzinfo...
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