OLD | NEW |
| (Empty) |
1 # Copyright 2015 The Chromium Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
5 import datetime | |
6 from pytz import timezone | |
7 import unittest | |
8 | |
9 from infra_libs.time_functions import zulu | |
10 | |
11 | |
12 class TestZuluTime(unittest.TestCase): | |
13 def _assert_parses_to(self, timestring, utc_ts_equivalent): | |
14 zuluparse = zulu.parse_zulu_ts(timestring) | |
15 self.assertIsInstance(zuluparse, float) | |
16 self.assertEqual(zuluparse, utc_ts_equivalent) | |
17 | |
18 | |
19 def testParseNonFractional(self): | |
20 timestring = '2015-06-11T23:17:26Z' | |
21 utc_ts_equivalent = 1434064646.0 | |
22 self._assert_parses_to(timestring, utc_ts_equivalent) | |
23 | |
24 def testParseFractional(self): | |
25 timestring = '2015-06-11T23:17:26.5Z' | |
26 utc_ts_equivalent = 1434064646.5 | |
27 self._assert_parses_to(timestring, utc_ts_equivalent) | |
28 | |
29 def testInvalidParse(self): | |
30 timestring = '2015-06-11T23:YOLO:17:26.5Z' | |
31 zuluparse = zulu.parse_zulu_ts(timestring) | |
32 self.assertIsNone(zuluparse) | |
33 | |
34 def testNaiveDTZuluStringNonFractional(self): | |
35 dt = datetime.datetime(2015, 06, 11, 23, 17, 26) | |
36 timestring = '2015-06-11T23:17:26.0Z' | |
37 self.assertEqual(zulu.to_zulu_string(dt), timestring) | |
38 | |
39 def testNaiveDTZuluStringFractional(self): | |
40 dt = datetime.datetime(2015, 06, 11, 23, 17, 26, 123) | |
41 timestring = '2015-06-11T23:17:26.000123Z' | |
42 self.assertEqual(zulu.to_zulu_string(dt), timestring) | |
43 | |
44 def testTZAwareDTZuluString(self): | |
45 # If you're confused why GMT+8 is -08:00, see | |
46 # http://askubuntu.com/questions/519550/ | |
47 # why-is-the-8-timezone-called-gmt-8-in-the-filesystem | |
48 dt = datetime.datetime(2015, 06, 11, 10, 17, 26, 123, | |
49 tzinfo=timezone('Etc/GMT+8')) | |
50 timestring = '2015-06-11T18:17:26.000123Z' | |
51 self.assertEqual(zulu.to_zulu_string(dt), timestring) | |
OLD | NEW |