Welcome to mirror list, hosted at ThFree Co, Russian Federation.

test_datetime_parsing.py « unit « test - dev.gajim.org/gajim/python-nbxmpp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 89706110ff8525eaca07672fb5d10b893189bac1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import unittest
from datetime import datetime
from datetime import timezone
from datetime import timedelta

from nbxmpp.modules.date_and_time import parse_datetime
from nbxmpp.modules.date_and_time import LocalTimezone
from nbxmpp.modules.date_and_time import create_tzinfo


class TestDateTime(unittest.TestCase):

    def test_convert_to_utc(self):

        strings = {
            # Valid UTC strings and fractions
            '2017-11-05T01:41:20Z': 1509846080.0,
            '2017-11-05T01:41:20.123Z': 1509846080.123,
            '2017-11-05T01:41:20.123123123+00:00': 1509846080.123123,
            '2017-11-05T01:41:20.123123123123123-00:00': 1509846080.123123,

            # Invalid strings
            '2017-11-05T01:41:20Z+05:00': None,
            '2017-11-05T01:41:20+0000': None,
            '2017-11-05T01:41:20-0000': None,

            # Valid strings with offset
            '2017-11-05T01:41:20-05:00': 1509864080.0,
            '2017-11-05T01:41:20+05:00': 1509828080.0,
            '2017-11-05T01:41:20-00:00': 1509846080.0,
            '2017-11-05T01:41:20+00:00': 1509846080.0,
        }

        strings2 = {
            # Valid strings with offset
            '2017-11-05T07:41:20-05:00': datetime(2017, 11, 5, 12, 41, 20, 0, timezone.utc),
            '2017-11-05T07:41:20+05:00': datetime(2017, 11, 5, 2, 41, 20, 0, timezone.utc),
            '2017-11-05T01:41:20+00:00': datetime(2017, 11, 5, 1, 41, 20, 0, timezone.utc),
            '2017-11-05T01:41:20Z': datetime(2017, 11, 5, 1, 41, 20, 0, timezone.utc),
        }

        for time_string, expected_value in strings.items():
            result = parse_datetime(time_string, convert='utc', epoch=True)
            self.assertEqual(result, expected_value)

        for time_string, expected_value in strings2.items():
            result = parse_datetime(time_string, convert='utc')
            self.assertEqual(result, expected_value)

    def test_convert_to_local(self):

        strings = {
            # Valid UTC strings and fractions
            '2017-11-05T01:41:20Z': datetime(2017, 11, 5, 1, 41, 20, 0, timezone.utc),
            '2017-11-05T01:41:20.123Z': datetime(2017, 11, 5, 1, 41, 20, 123000, timezone.utc),
            '2017-11-05T01:41:20.123123123+00:00': datetime(2017, 11, 5, 1, 41, 20, 123123, timezone.utc),
            '2017-11-05T01:41:20.123123123123123-00:00': datetime(2017, 11, 5, 1, 41, 20, 123123, timezone.utc),

            # Valid strings with offset
            '2017-11-05T01:41:20-05:00': datetime(2017, 11, 5, 1, 41, 20, 0, create_tzinfo(hours=-5)),
            '2017-11-05T01:41:20+05:00': datetime(2017, 11, 5, 1, 41, 20, 0, create_tzinfo(hours=5)),
        }

        for time_string, expected_value in strings.items():
            result = parse_datetime(time_string, convert='local')
            self.assertEqual(result, expected_value.astimezone(LocalTimezone()))

    def test_no_convert(self):

        strings = {
            # Valid UTC strings and fractions
            '2017-11-05T01:41:20Z': timedelta(0),
            '2017-11-05T01:41:20.123Z': timedelta(0),
            '2017-11-05T01:41:20.123123123+00:00': timedelta(0),
            '2017-11-05T01:41:20.123123123123123-00:00': timedelta(0),

            # Valid strings with offset
            '2017-11-05T01:41:20-05:00': timedelta(hours=-5),
            '2017-11-05T01:41:20+05:00': timedelta(hours=5),
        }

        for time_string, expected_value in strings.items():
            result = parse_datetime(time_string, convert=None)
            assert result is not None
            self.assertEqual(result.utcoffset(), expected_value)

    def test_check_utc(self):

        strings = {
            # Valid UTC strings and fractions
            '2017-11-05T01:41:20Z': 1509846080.0,
            '2017-11-05T01:41:20.123Z': 1509846080.123,
            '2017-11-05T01:41:20.123123123+00:00': 1509846080.123123,
            '2017-11-05T01:41:20.123123123123123-00:00': 1509846080.123123,

            # Valid strings with offset
            '2017-11-05T01:41:20-05:00': None,
            '2017-11-05T01:41:20+05:00': None,
        }

        for time_string, expected_value in strings.items():
            result = parse_datetime(
                time_string, check_utc=True, epoch=True)
            self.assertEqual(result, expected_value)


if __name__ == '__main__':
    unittest.main()