Skip to content

Commit 231d782

Browse files
wcharginbileschi
authored andcommitted
uploader: make util_test py3-compatible internally (tensorflow#3179)
Summary: Follow-up to tensorflow#2978, which added explicit configuration of `TZ=UTC`. In cPython 3, the time zone used by `datetime.datetime.fromtimestamp` is [cached when `time` is imported][1] unless explicitly reset, so mocking the environment variable is no longer sufficient. We now call `tzset` ourselves; we could also just set `TZ=UTC` before importing `datetime`, but this seems cleaner. [1]: https://github.com/python/cpython/blob/2528a6c3d0660c03ae43d796628462ccf8e58190/Modules/timemodule.c#L1752-L1763 Test Plan: A test sync shows that this test passes in both Python 2 and Python 3, whereas it previously only passed in Python 2. wchargin-branch: util-test-tzset
1 parent 1a7dade commit 231d782

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

tensorboard/uploader/util_test.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import datetime
2222
import os
23+
import time
2324
import unittest
2425
import mock
2526

@@ -208,9 +209,13 @@ class FormatTimeTest(tb_test.TestCase):
208209
def _run(self, t=None, now=None):
209210
timestamp_pb = timestamp_pb2.Timestamp()
210211
util.set_timestamp(timestamp_pb, t)
211-
with mock.patch.dict(os.environ, {"TZ": "UTC"}):
212-
now = datetime.datetime.fromtimestamp(now)
213-
return util.format_time(timestamp_pb, now=now)
212+
try:
213+
with mock.patch.dict(os.environ, {"TZ": "UTC"}):
214+
time.tzset()
215+
now = datetime.datetime.fromtimestamp(now)
216+
return util.format_time(timestamp_pb, now=now)
217+
finally:
218+
time.tzset()
214219

215220
def test_just_now(self):
216221
base = 1546398245

0 commit comments

Comments
 (0)