Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flag that we're time traveling, with access to true datetime #206

Closed
pegler opened this issue Dec 13, 2021 · 3 comments · Fixed by #207
Closed

Flag that we're time traveling, with access to true datetime #206

pegler opened this issue Dec 13, 2021 · 3 comments · Fixed by #207

Comments

@pegler
Copy link

pegler commented Dec 13, 2021

Description

In some of our integration tests we make API calls to external services. Some of these require a timestamp for use in authentication. I know that excluding some modules from time-machine isn't possible due to the implementation, but having some signal that we're time traveling would suffice, along with access to the unpatched datetime module.

@adamchainz
Copy link
Owner

You can get the real time with _time_machine.original_now() etc., as the replacement functions do:

def now(tz: Optional[dt.tzinfo] = None) -> dt.datetime:
if not coordinates_stack:
return _time_machine.original_now(tz)
else:
return dt.datetime.fromtimestamp(time(), tz)

And you can check if time travelling based upon whether time_machine.coordinates_stack is empty or not.

Can you check those works for you first?

Then we could add an official API.

@pegler
Copy link
Author

pegler commented Dec 14, 2021

That worked perfectly. Thanks! Having it officially supported via the API would be awesome. I can work on a PR next week.

@adamchainz
Copy link
Owner

I added escape_hatch and released it in 2.5.0. Try it out.

bmwiedemann referenced this issue in bmwiedemann/openSUSE Jan 11, 2022
https://build.opensuse.org/request/show/945538
by user dirkmueller + dimstar_suse
- update to version 2.6.0:
  * Drop Python 3.6 support.

- update to version 2.5.0:
  * Add ``time_machine.escape_hatch``, which provides functions to bypass
    time-machine.
    Thanks to Matt Pegler for the feature request in `Issue #206
    <https://github.com/adamchainz/time-machine/issues/206>`__.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants