Fix utcnow_ts to return UTC timestamp

Fixes bug #1011121

Use calendar.timegm instead of time.mktime

Change-Id: Idb8d470f1ef90162641d21122699a026fa38faf1
This commit is contained in:
Zhongyue Luo
2012-06-10 18:42:18 +08:00
parent 5fc561c95f
commit be86feba38
2 changed files with 6 additions and 2 deletions

View File

@@ -19,6 +19,7 @@
Time related utilities and helper functions.
"""
import calendar
import datetime
import time
@@ -74,7 +75,7 @@ def is_older_than(before, seconds):
def utcnow_ts():
"""Timestamp version of our utcnow function."""
return time.mktime(utcnow().timetuple())
return calendar.timegm(utcnow().timetuple())
def utcnow():

View File

@@ -70,7 +70,10 @@ class TimeUtilsTest(unittest.TestCase):
self.assertFalse(expect_false)
def test_utcnow_ts(self):
skynet_self_aware_timestamp = 872806440.0
skynet_self_aware_timestamp = 872835240
dt = datetime.datetime.utcfromtimestamp(skynet_self_aware_timestamp)
expect = dt.replace(tzinfo=iso8601.iso8601.UTC)
self.assertEqual(self.skynet_self_aware_time, expect)
with mock.patch('datetime.datetime') as datetime_mock:
datetime_mock.utcnow.return_value = self.skynet_self_aware_time
ts = timeutils.utcnow_ts()