mirror of
https://github.com/jbranchaud/til
synced 2026-08-31 16:51:46 +00:00
Add Override Timezone That All Tests Run In as a Python TIL
This commit is contained in:
@@ -10,7 +10,7 @@ working across different projects via [VisualMode](https://www.visualmode.dev/).
|
||||
|
||||
For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter).
|
||||
|
||||
_1862 TILs and counting..._
|
||||
_1863 TILs and counting..._
|
||||
|
||||
See some of the other learning resources I work on:
|
||||
|
||||
@@ -1115,6 +1115,7 @@ If you've learned something here, support my efforts writing daily TILs by
|
||||
- [Make Dataclass Sortable By Specific Field](python/make-dataclass-sortable-by-specific-field.md)
|
||||
- [Make Secure Temp File For Atomic Write](python/make-secure-temp-file-for-atomic-write.md)
|
||||
- [Override The Boolean Context Of A Class](python/override-the-boolean-context-of-a-class.md)
|
||||
- [Override Timezone That All Tests Run In](python/override-timezone-that-all-tests-run-in.md)
|
||||
- [Parse Relative Time To datetime Object](python/parse-relative-time-to-datetime-object.md)
|
||||
- [Publish A Package To A Test Env As A Dry Run](python/publish-a-package-to-a-test-env-as-a-dry-run.md)
|
||||
- [Reclassify Certain Packages As Dev Dependencies](python/reclassify-certain-packages-as-dev-dependencies.md)
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
# Override Timezone That All Tests Run In
|
||||
|
||||
I was building out the initial functionality of a Python project in my local dev
|
||||
env. I was writing tests along the way to guard against regressions. Many of the
|
||||
tests assert things about time, durations, and formatted timestamps. All was
|
||||
well.
|
||||
|
||||
Then I was getting the project setup on GitHub with a CI workflow to run the
|
||||
test suite. And a ton of tests were failing. It was all the tests dealing with
|
||||
time. They were all off by 5 hours.
|
||||
|
||||
I recognized that time difference immediately. That's the difference between my
|
||||
time here in Chicago and UTC. When I run the tests locally, they are all
|
||||
formatted the timezone setting on my machine -- `America/Chicago`. Whereas when
|
||||
I run them in this CI environment, they were suddenly being formatted to `UTC`.
|
||||
|
||||
What I needed to do was explicitly set the timezone that all the tests would
|
||||
always run in, regardless of environment.
|
||||
|
||||
I added the following to my `conftest.py` file:
|
||||
|
||||
```python
|
||||
import os
|
||||
import time
|
||||
|
||||
|
||||
# Set the timezone that all tests run in
|
||||
os.environ["TZ"] = "America/Chicago"
|
||||
time.tzset()
|
||||
```
|
||||
|
||||
First, I set the timezone (`TZ`) in `os.environ` to `America/Chicago`. That's a
|
||||
copy of the executing environment that Python makes when it starts up.
|
||||
|
||||
Second, because `TZ` may be tied to some cached info under the hood, I have to
|
||||
also run `time.tzset()`.
|
||||
|
||||
> Initialize, or reinitialize, the local timezone to the value stored in
|
||||
> os.environ[‘TZ’]. The TZ environment variable should be specified in standard
|
||||
> Unix timezone format as documented in the tzset man page (eg. ‘US/Eastern’,
|
||||
> ‘Europe/Amsterdam’).
|
||||
|
||||
Now, all of my tests run reliably in `America/Chicago`.
|
||||
|
||||
To demonstrate all of this from the Python repl, here is an example:
|
||||
|
||||
```python
|
||||
>>> from datetime import datetime
|
||||
>>> import os
|
||||
>>> import time
|
||||
>>> os.environ["TZ"] = "America/Los_Angeles"
|
||||
>>> time.tzset()
|
||||
>>> datetime.now()
|
||||
datetime.datetime(2026, 8, 15, 14, 3, 19, 373682)
|
||||
>>> os.environ["TZ"] = "America/Chicago"
|
||||
>>> time.tzset()
|
||||
>>> datetime.now()
|
||||
datetime.datetime(2026, 8, 15, 16, 3, 33, 322803)
|
||||
```
|
||||
|
||||
Notice the two hour difference between those two.
|
||||
Reference in New Issue
Block a user