diff --git a/swift/common/utils.py b/swift/common/utils.py index a008a344eb..4dacae24fb 100644 --- a/swift/common/utils.py +++ b/swift/common/utils.py @@ -696,16 +696,25 @@ def normalize_timestamp(timestamp): return Timestamp(timestamp).normal +EPOCH = datetime.datetime(1970, 1, 1) + + def last_modified_date_to_timestamp(last_modified_date_str): """ Convert a last modified date (like you'd get from a container listing, e.g. 2014-02-28T23:22:36.698390) to a float. """ - return Timestamp( - datetime.datetime.strptime( - last_modified_date_str, '%Y-%m-%dT%H:%M:%S.%f' - ).strftime('%s.%f') - ) + start = datetime.datetime.strptime(last_modified_date_str, + '%Y-%m-%dT%H:%M:%S.%f') + delta = start - EPOCH + # TODO(sam): after we no longer support py2.6, this expression can + # simplify to Timestamp(delta.total_seconds()). + # + # This calculation is based on Python 2.7's Modules/datetimemodule.c, + # function delta_to_microseconds(), but written in Python. + return Timestamp(delta.days * 86400 + + delta.seconds + + delta.microseconds / 1000000.0) def normalize_delete_at_timestamp(timestamp): diff --git a/test/unit/common/test_utils.py b/test/unit/common/test_utils.py index 1ea3a2dcec..7093249ae8 100644 --- a/test/unit/common/test_utils.py +++ b/test/unit/common/test_utils.py @@ -859,6 +859,24 @@ class TestUtils(unittest.TestCase): real = utils.last_modified_date_to_timestamp(last_modified) self.assertEqual(real, ts, "failed for %s" % last_modified) + def test_last_modified_date_to_timestamp_when_system_not_UTC(self): + try: + old_tz = os.environ.get('TZ') + # Western Argentina Summer Time. Found in glibc manual; this + # timezone always has a non-zero offset from UTC, so this test is + # always meaningful. + os.environ['TZ'] = 'WART4WARST,J1/0,J365/25' + + self.assertEqual(utils.last_modified_date_to_timestamp( + '1970-01-01T00:00:00.000000'), + 0.0) + + finally: + if old_tz is not None: + os.environ['TZ'] = old_tz + else: + os.environ.pop('TZ') + def test_backwards(self): # Test swift.common.utils.backward