{"action":"closed","issue":{"url":"https://api.github.com/repos/GoogleCloudPlatform/python-compat-runtime/issues/70","repository_url":"https://api.github.com/repos/GoogleCloudPlatform/python-compat-runtime","labels_url":"https://api.github.com/repos/GoogleCloudPlatform/python-compat-runtime/issues/70/labels{/name}","comments_url":"https://api.github.com/repos/GoogleCloudPlatform/python-compat-runtime/issues/70/comments","events_url":"https://api.github.com/repos/GoogleCloudPlatform/python-compat-runtime/issues/70/events","html_url":"https://github.com/GoogleCloudPlatform/python-compat-runtime/issues/70","id":144417220,"number":70,"title":"Diff from Standard: Accessing django.conf.settings at import time changes timezone","user":{"login":"evanj","id":675151,"avatar_url":"https://avatars.githubusercontent.com/u/675151?v=3","gravatar_id":"","url":"https://api.github.com/users/evanj","html_url":"https://github.com/evanj","followers_url":"https://api.github.com/users/evanj/followers","following_url":"https://api.github.com/users/evanj/following{/other_user}","gists_url":"https://api.github.com/users/evanj/gists{/gist_id}","starred_url":"https://api.github.com/users/evanj/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/evanj/subscriptions","organizations_url":"https://api.github.com/users/evanj/orgs","repos_url":"https://api.github.com/users/evanj/repos","events_url":"https://api.github.com/users/evanj/events{/privacy}","received_events_url":"https://api.github.com/users/evanj/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":8,"created_at":"2016-03-29T23:23:56Z","updated_at":"2016-04-06T21:16:42Z","closed_at":"2016-04-06T21:16:42Z","body":"If you access django.conf.settings at import time on Managed VMs it changes the timezone to America/Chicago. It does not do this on the Standard environment (see example program below). This is actually \"expected\" behaviour for Django, but it *is* a difference between the two environments that caused a bug for us that we didn't catch until we tried putting our GCE module in production.\r\n\r\nThe reason this happens: `django.conf.__init__` calls `time.tzset` to set the timezone to the value in `django.conf.global_settings.TIME_ZONE`, which defaults to `TIME_ZONE = 'America/Chicago'`! You have to do this at module import time, because after imports `os.environ` is monkey patched by `wsgi.py`.\r\n\r\nInteresting note: the same code is in `google.appengine._internal.django.conf`, but I was not able to trigger this by importing the things that may depend on it, e.g. through `google.appengine.ext.webapp.template`. Maybe there is some way to trigger this bug through those dependencies, but I wasn't able to reproduce it.\r\n\r\nSuggestions:\r\n\r\n* It might be worth changing the TIME_ZONE value of the included version of Django to UTC to ensure this can't happen by accidentally importing some `google.appengine.ext` module?\r\n\r\n* This may just need to be a documented difference, although if time.tzset really doesn't work on the Standard environment, maybe it should be monkey patched in wsgi.py to do nothing, as a \"compatibility\" hack? If a user doesn't want this behaviour, they can choose not to use `python-compat`?\r\n\r\n\r\nExample program that demonstrates this bug. On a Managed VM, the value for datetime.now() and datetime.utcnow() are different.\r\n\r\n```\r\nimport datetime\r\nimport logging\r\nimport os\r\nimport sys\r\nimport time\r\n\r\nfrom django.conf import settings\r\nimport webapp2\r\n\r\nprint \"### WTF\", settings.FOOBAR\r\n\r\nclass HelloWebapp2(webapp2.RequestHandler):\r\n  def get(self):\r\n    self.response.headers[\"Content-Type\"] = \"text/plain;encoding=UTF-8\"\r\n    self.response.write('settings.TIME_ZONE = %s\\n' % (repr(settings.TIME_ZONE)))\r\n    self.response.write('datetime.now = %s\\n' % (repr(datetime.datetime.now())))\r\n    self.response.write('datetime.utcnow = %s\\n' % (repr(datetime.datetime.utcnow())))\r\n    self.response.write('os.environ[\"TZ\"] = %s\\n' % (os.environ.get(\"TZ\", \"(not set)\")))\r\n    self.response.write('time.timezone = %s\\n' % (time.timezone))\r\n    self.response.write('time.tzname = %s\\n' % (repr(time.tzname)))\r\n    self.response.write('django dir(settings) = %s\\n' % (repr(dir(settings))))\r\n    self.response.write('django settings.USE_TZ = %s\\n' % (repr(settings.USE_TZ)))\r\n    self.response.write('django settings.TIME_ZONE = %s\\n' % (repr(settings.TIME_ZONE)))\r\n\r\napp = webapp2.WSGIApplication([\r\n  ('/', HelloWebapp2),\r\n], debug=True)\r\n```"}}