diff options
| author | Russell Bryant <rbryant@redhat.com> | 2012-04-04 10:40:42 -0400 |
|---|---|---|
| committer | Russell Bryant <rbryant@redhat.com> | 2012-04-04 10:40:42 -0400 |
| commit | 1dde301866bcc5c40fd41aaaf82cf8542c69cb2c (patch) | |
| tree | 6bfc98a7e3ca46f0f99491a3935e7d2dbd7272b9 /tests/unit/test_timeutils.py | |
| parent | dcd50714c4f1a207ff53b20475b620de39c0ed4a (diff) | |
| download | oslo-1dde301866bcc5c40fd41aaaf82cf8542c69cb2c.tar.gz oslo-1dde301866bcc5c40fd41aaaf82cf8542c69cb2c.tar.xz oslo-1dde301866bcc5c40fd41aaaf82cf8542c69cb2c.zip | |
Create openstack.common.timeutils.
Split time related utility functions out of openstack.common.utils into
a new module, timeutils.
Change-Id: Ic09952cd48e0b4548e410926cc456cbd515a4e56
Diffstat (limited to 'tests/unit/test_timeutils.py')
| -rw-r--r-- | tests/unit/test_timeutils.py | 157 |
1 files changed, 157 insertions, 0 deletions
diff --git a/tests/unit/test_timeutils.py b/tests/unit/test_timeutils.py new file mode 100644 index 0000000..59a5032 --- /dev/null +++ b/tests/unit/test_timeutils.py @@ -0,0 +1,157 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2011 OpenStack LLC. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import datetime +import unittest + +import iso8601 +import mock + +from openstack.common import timeutils + + +class TimeUtilsTest(unittest.TestCase): + + def test_isotime(self): + skynet_self_aware_time_str = '1997-08-29T06:14:00Z' + skynet_self_aware_time = datetime.datetime(1997, 8, 29, 6, 14, 0, + tzinfo=iso8601.iso8601.UTC) + with mock.patch('datetime.datetime') as datetime_mock: + datetime_mock.utcnow.return_value = skynet_self_aware_time + dt = timeutils.isotime() + self.assertEqual(dt, skynet_self_aware_time_str) + + def test_parse_isotime(self): + skynet_self_aware_time_str = '1997-08-29T06:14:00Z' + skynet_self_aware_time = datetime.datetime(1997, 8, 29, 6, 14, 0, + tzinfo=iso8601.iso8601.UTC) + self.assertEqual(skynet_self_aware_time, + timeutils.parse_isotime(skynet_self_aware_time_str)) + + def test_utcnow(self): + timeutils.set_time_override(mock.sentinel.utcnow) + self.assertEqual(timeutils.utcnow(), mock.sentinel.utcnow) + + timeutils.clear_time_override() + self.assertFalse(timeutils.utcnow() == mock.sentinel.utcnow) + + self.assertTrue(timeutils.utcnow()) + + +class TestIso8601Time(unittest.TestCase): + + def _instaneous(self, timestamp, yr, mon, day, hr, min, sec, micro): + self.assertEquals(timestamp.year, yr) + self.assertEquals(timestamp.month, mon) + self.assertEquals(timestamp.day, day) + self.assertEquals(timestamp.hour, hr) + self.assertEquals(timestamp.minute, min) + self.assertEquals(timestamp.second, sec) + self.assertEquals(timestamp.microsecond, micro) + + def _do_test(self, str, yr, mon, day, hr, min, sec, micro, shift): + DAY_SECONDS = 24 * 60 * 60 + timestamp = timeutils.parse_isotime(str) + self._instaneous(timestamp, yr, mon, day, hr, min, sec, micro) + offset = timestamp.tzinfo.utcoffset(None) + self.assertEqual(offset.seconds + offset.days * DAY_SECONDS, shift) + + def test_zulu(self): + str = '2012-02-14T20:53:07Z' + self._do_test(str, 2012, 02, 14, 20, 53, 7, 0, 0) + + def test_zulu_micros(self): + str = '2012-02-14T20:53:07.123Z' + self._do_test(str, 2012, 02, 14, 20, 53, 7, 123000, 0) + + def test_offset_east(self): + str = '2012-02-14T20:53:07+04:30' + offset = 4.5 * 60 * 60 + self._do_test(str, 2012, 02, 14, 20, 53, 7, 0, offset) + + def test_offset_east_micros(self): + str = '2012-02-14T20:53:07.42+04:30' + offset = 4.5 * 60 * 60 + self._do_test(str, 2012, 02, 14, 20, 53, 7, 420000, offset) + + def test_offset_west(self): + str = '2012-02-14T20:53:07-05:30' + offset = -5.5 * 60 * 60 + self._do_test(str, 2012, 02, 14, 20, 53, 7, 0, offset) + + def test_offset_west_micros(self): + str = '2012-02-14T20:53:07.654321-05:30' + offset = -5.5 * 60 * 60 + self._do_test(str, 2012, 02, 14, 20, 53, 7, 654321, offset) + + def test_compare(self): + zulu = timeutils.parse_isotime('2012-02-14T20:53:07') + east = timeutils.parse_isotime('2012-02-14T20:53:07-01:00') + west = timeutils.parse_isotime('2012-02-14T20:53:07+01:00') + self.assertTrue(east > west) + self.assertTrue(east > zulu) + self.assertTrue(zulu > west) + + def test_compare_micros(self): + zulu = timeutils.parse_isotime('2012-02-14T20:53:07.6544') + east = timeutils.parse_isotime('2012-02-14T19:53:07.654321-01:00') + west = timeutils.parse_isotime('2012-02-14T21:53:07.655+01:00') + self.assertTrue(east < west) + self.assertTrue(east < zulu) + self.assertTrue(zulu < west) + + def test_zulu_roundtrip(self): + str = '2012-02-14T20:53:07Z' + zulu = timeutils.parse_isotime(str) + self.assertEquals(zulu.tzinfo, iso8601.iso8601.UTC) + self.assertEquals(timeutils.isotime(zulu), str) + + def test_east_roundtrip(self): + str = '2012-02-14T20:53:07-07:00' + east = timeutils.parse_isotime(str) + self.assertEquals(east.tzinfo.tzname(None), '-07:00') + self.assertEquals(timeutils.isotime(east), str) + + def test_west_roundtrip(self): + str = '2012-02-14T20:53:07+11:30' + west = timeutils.parse_isotime(str) + self.assertEquals(west.tzinfo.tzname(None), '+11:30') + self.assertEquals(timeutils.isotime(west), str) + + def test_now_roundtrip(self): + str = timeutils.isotime() + now = timeutils.parse_isotime(str) + self.assertEquals(now.tzinfo, iso8601.iso8601.UTC) + self.assertEquals(timeutils.isotime(now), str) + + def test_zulu_normalize(self): + str = '2012-02-14T20:53:07Z' + zulu = timeutils.parse_isotime(str) + normed = timeutils.normalize_time(zulu) + self._instaneous(normed, 2012, 2, 14, 20, 53, 07, 0) + + def test_east_normalize(self): + str = '2012-02-14T20:53:07-07:00' + east = timeutils.parse_isotime(str) + normed = timeutils.normalize_time(east) + self._instaneous(normed, 2012, 2, 15, 03, 53, 07, 0) + + def test_west_normalize(self): + str = '2012-02-14T20:53:07+21:00' + west = timeutils.parse_isotime(str) + normed = timeutils.normalize_time(west) + self._instaneous(normed, 2012, 2, 13, 23, 53, 07, 0) |
