Skip to content

Ensure that timezone TZ env var is unmodified by django.setup() - #284

Merged
taldcroft merged 1 commit into
masterfrom
leave-timezone-unmodified
Apr 5, 2023
Merged

Ensure that timezone TZ env var is unmodified by django.setup()#284
taldcroft merged 1 commit into
masterfrom
leave-timezone-unmodified

Conversation

@taldcroft

Copy link
Copy Markdown
Member

Description

This fixes the issue described in #283, but instead by ensuring that the TZ environment variable is not modified by the call to django.setup(). For most unix machines this means that TZ will be undefined, but on GRETA it will remain as GMT.

Interface impacts

None

Testing

Unit tests

  • Mac

Independent check of unit tests by [REVIEWER NAME]

  • [PLATFORM]:

Functional tests

These tests use sot/cxotime#36.

Test 1 (no TZ defined)

(ska3) ➜  kadi git:(master) ✗ env PYTHONPATH=/Users/aldcroft/git/cxotime ipython

Python 3.10.8 | packaged by conda-forge | (main, Nov 22 2022, 08:27:35) [Clang 14.0.6 ]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.8.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import kadi.events
   ...: 
   ...: kadi.events.eclipses
Out[1]: <EventQuery: Eclipse>

In [3]: from cxotime import CxoTime

In [4]: t = CxoTime.now()

In [5]: t.print_conversions()
local       2023 Thu Mar 30 12:00:07 PM EDT 
iso_local   2023-03-30T12:00:07.241000-04:00
date        2023:089:16:00:07.241           
cxcsec      796579276.425                   
decimalyear 2023.24292                      
iso         2023-03-30 16:00:07.241         
unix        1680192007.241            

In [7]: import os
In [8]: os.environ.get("TZ")
In [9]:       

Test 2 (TZ=GMT)

(ska3) ➜  kadi git:(leave-timezone-unmodified) env TZ=GMT PYTHONPATH=/Users/aldcroft/git/cxotime ipython 
Python 3.10.8 | packaged by conda-forge | (main, Nov 22 2022, 08:27:35) [Clang 14.0.6 ]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.8.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import kadi.events

In [2]: kadi.events.eclipses
Out[2]: <EventQuery: Eclipse>

In [3]: import os

In [4]: os.environ.get("TZ")
Out[4]: 'GMT'

In [5]: from cxotime import CxoTime

In [6]: t = CxoTime("2022:300:01:00:00.000")

In [7]: t.print_conversions()
local       2022 Thu Oct 27 01:00:00 AM GMT
iso_local   2022-10-27T01:00:00+00:00      
date        2022:300:01:00:00.000          
cxcsec      783219669.184                  
decimalyear 2022.81929                     
iso         2022-10-27 01:00:00.000        
unix        1666832400.000                 

@taldcroft
taldcroft requested review from javierggt and jeanconn April 3, 2023 13:59

@javierggt javierggt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could merge it as is, but perhaps it would be good to have a test for this somewhere?

I tested using this:

imoprt os
import datetime

os.environ["TZ"] = "UTC"  # optional
dt = datetime.datetime(2020, 11, 1, 1)
dt1 = dt.astimezone()
import kadi.events
kadi.events.eclipses
dt2 = dt.astimezone().astimezone()
assert dt1 == dt2

@taldcroft
taldcroft merged commit 9cd0015 into master Apr 5, 2023
@taldcroft
taldcroft deleted the leave-timezone-unmodified branch April 5, 2023 17:27
This was referenced Jun 1, 2023
@javierggt javierggt mentioned this pull request Jul 12, 2023
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 this pull request may close these issues.

2 participants