Philipp Hörist pushed to branch master at gajim / python-nbxmpp

Commits:

1 changed file:

Changes:

  • test/unit/test_datetime_parsing.py
    1
    +import unittest
    
    2
    +from datetime import datetime
    
    3
    +from datetime import timezone
    
    4
    +from datetime import timedelta
    
    5
    +
    
    6
    +from nbxmpp.modules.date_and_time import parse_datetime
    
    7
    +from nbxmpp.modules.date_and_time import LocalTimezone
    
    8
    +from nbxmpp.modules.date_and_time import create_tzinfo
    
    9
    +
    
    10
    +
    
    11
    +class TestDateTime(unittest.TestCase):
    
    12
    +
    
    13
    +    def test_convert_to_utc(self):
    
    14
    +
    
    15
    +        strings = {
    
    16
    +            # Valid UTC strings and fractions
    
    17
    +            '2017-11-05T01:41:20Z': 1509846080.0,
    
    18
    +            '2017-11-05T01:41:20.123Z': 1509846080.123,
    
    19
    +            '2017-11-05T01:41:20.123123123+00:00': 1509846080.123123,
    
    20
    +            '2017-11-05T01:41:20.123123123123123-00:00': 1509846080.123123,
    
    21
    +
    
    22
    +            # Invalid strings
    
    23
    +            '2017-11-05T01:41:20Z+05:00': None,
    
    24
    +            '2017-11-05T01:41:20+0000': None,
    
    25
    +            '2017-11-05T01:41:20-0000': None,
    
    26
    +
    
    27
    +            # Valid strings with offset
    
    28
    +            '2017-11-05T01:41:20-05:00': 1509864080.0,
    
    29
    +            '2017-11-05T01:41:20+05:00': 1509828080.0,
    
    30
    +        }
    
    31
    +
    
    32
    +        strings2 = {
    
    33
    +            # Valid strings with offset
    
    34
    +            '2017-11-05T01:41:20-05:00': datetime(2017, 11, 5, 1, 41, 20, 0, create_tzinfo(hours=-5)),
    
    35
    +            '2017-11-05T01:41:20+05:00': datetime(2017, 11, 5, 1, 41, 20, 0, create_tzinfo(hours=5)),
    
    36
    +        }
    
    37
    +
    
    38
    +        for time_string, expected_value in strings.items():
    
    39
    +            result = parse_datetime(time_string, convert='utc', epoch=True)
    
    40
    +            self.assertEqual(result, expected_value)
    
    41
    +
    
    42
    +        for time_string, expected_value in strings2.items():
    
    43
    +            result = parse_datetime(time_string, convert='utc')
    
    44
    +            self.assertEqual(result, expected_value.astimezone(timezone.utc))
    
    45
    +
    
    46
    +    def test_convert_to_local(self):
    
    47
    +
    
    48
    +        strings = {
    
    49
    +            # Valid UTC strings and fractions
    
    50
    +            '2017-11-05T01:41:20Z': datetime(2017, 11, 5, 1, 41, 20, 0, timezone.utc),
    
    51
    +            '2017-11-05T01:41:20.123Z': datetime(2017, 11, 5, 1, 41, 20, 123000, timezone.utc),
    
    52
    +            '2017-11-05T01:41:20.123123123+00:00': datetime(2017, 11, 5, 1, 41, 20, 123123, timezone.utc),
    
    53
    +            '2017-11-05T01:41:20.123123123123123-00:00': datetime(2017, 11, 5, 1, 41, 20, 123123, timezone.utc),
    
    54
    +
    
    55
    +            # Valid strings with offset
    
    56
    +            '2017-11-05T01:41:20-05:00': datetime(2017, 11, 5, 1, 41, 20, 0, create_tzinfo(hours=-5)),
    
    57
    +            '2017-11-05T01:41:20+05:00': datetime(2017, 11, 5, 1, 41, 20, 0, create_tzinfo(hours=5)),
    
    58
    +        }
    
    59
    +
    
    60
    +        for time_string, expected_value in strings.items():
    
    61
    +            result = parse_datetime(time_string, convert='local')
    
    62
    +            self.assertEqual(result, expected_value.astimezone(LocalTimezone()))
    
    63
    +
    
    64
    +    def test_no_convert(self):
    
    65
    +
    
    66
    +        strings = {
    
    67
    +            # Valid UTC strings and fractions
    
    68
    +            '2017-11-05T01:41:20Z': timedelta(0),
    
    69
    +            '2017-11-05T01:41:20.123Z': timedelta(0),
    
    70
    +            '2017-11-05T01:41:20.123123123+00:00': timedelta(0),
    
    71
    +            '2017-11-05T01:41:20.123123123123123-00:00': timedelta(0),
    
    72
    +
    
    73
    +            # Valid strings with offset
    
    74
    +            '2017-11-05T01:41:20-05:00': timedelta(hours=-5),
    
    75
    +            '2017-11-05T01:41:20+05:00': timedelta(hours=5),
    
    76
    +        }
    
    77
    +
    
    78
    +        for time_string, expected_value in strings.items():
    
    79
    +            result = parse_datetime(time_string, convert=None)
    
    80
    +            self.assertEqual(result.utcoffset(), expected_value)
    
    81
    +
    
    82
    +    def test_check_utc(self):
    
    83
    +
    
    84
    +        strings = {
    
    85
    +            # Valid UTC strings and fractions
    
    86
    +            '2017-11-05T01:41:20Z': 1509846080.0,
    
    87
    +            '2017-11-05T01:41:20.123Z': 1509846080.123,
    
    88
    +            '2017-11-05T01:41:20.123123123+00:00': 1509846080.123123,
    
    89
    +            '2017-11-05T01:41:20.123123123123123-00:00': 1509846080.123123,
    
    90
    +
    
    91
    +            # Valid strings with offset
    
    92
    +            '2017-11-05T01:41:20-05:00': None,
    
    93
    +            '2017-11-05T01:41:20+05:00': None,
    
    94
    +        }
    
    95
    +
    
    96
    +        for time_string, expected_value in strings.items():
    
    97
    +            result = parse_datetime(
    
    98
    +                time_string, check_utc=True, epoch=True)
    
    99
    +            self.assertEqual(result, expected_value)