← Back to team overview

dulwich team mailing list archive

[PATCH] Use datetime.utcfromtimestamp() in tests.

 

From: Dave Borowitz <dborowitz@xxxxxxxxxx>

datetime.fromtimestamp() has the confusing behavior of treating the
timestamp as a UTC time and then converting to local time, but not
setting the tzinfo member. As a result, the function returns different
values depending on the local timezone, which makes it hard to write a
test that passes everywhere.

The output of datetime.utcfromtimestamp() is stable across all
timezones, which is all we really need for tests. (Returning sensible
datetime objects from the Commit/Tag API would require substantially
more work, but that is not fixed in this change.)

Tested in both US-Pacific and US-Eastern.

Change-Id: I2db98932c849e71797e280158dabc05271656ad5
---
 dulwich/tests/test_objects.py |   10 +++++-----
 1 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/dulwich/tests/test_objects.py b/dulwich/tests/test_objects.py
index 37f8fb2..a209a7d 100644
--- a/dulwich/tests/test_objects.py
+++ b/dulwich/tests/test_objects.py
@@ -288,12 +288,12 @@ class CommitParseTests(ShaFileCheckTests):
         self.assertEquals(['ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd',
                            '4cffe90e0a41ad3f5190079d7c8f036bde29cbe6'],
                           c.parents)
-        expected_time = datetime.datetime(2007, 3, 24, 23, 1, 59).replace(tzinfo=None)
+        expected_time = datetime.datetime(2007, 3, 24, 22, 1, 59)
         self.assertEquals(expected_time,
-                          datetime.datetime.fromtimestamp(c.commit_time))
+                          datetime.datetime.utcfromtimestamp(c.commit_time))
         self.assertEquals(0, c.commit_timezone)
         self.assertEquals(expected_time,
-                          datetime.datetime.fromtimestamp(c.author_time))
+                          datetime.datetime.utcfromtimestamp(c.author_time))
         self.assertEquals(0, c.author_timezone)
         self.assertEquals(None, c.encoding)
 
@@ -455,8 +455,8 @@ class TagParseTests(ShaFileCheckTests):
         self.assertEquals("a38d6181ff27824c79fc7df825164a212eff6a3f",
                           object_sha)
         self.assertEquals(Commit, object_type)
-        self.assertEquals(datetime.datetime.fromtimestamp(x.tag_time),
-                          datetime.datetime(2007, 7, 1, 21, 54, 34, 0).replace(tzinfo=None))
+        self.assertEquals(datetime.datetime.utcfromtimestamp(x.tag_time),
+                          datetime.datetime(2007, 7, 1, 19, 54, 34))
         self.assertEquals(-25200, x.tag_timezone)
 
     def test_parse_no_tagger(self):
-- 
1.7.0.3.295.gd8fa2




Follow ups