/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_timestamp.py

  • Committer: Jelmer Vernooij
  • Date: 2018-11-11 04:08:32 UTC
  • mto: (7143.16.20 even-more-cleanups)
  • mto: This revision was merged to the branch mainline in revision 7175.
  • Revision ID: jelmer@jelmer.uk-20181111040832-nsljjynzzwmznf3h
Run autopep8.

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
    def test_format_patch_date(self):
31
31
        # epoch is always in utc
32
32
        self.assertEqual('1970-01-01 00:00:00 +0000',
33
 
            timestamp.format_patch_date(0))
34
 
        self.assertEqual('1970-01-01 00:00:00 +0000',
35
 
            timestamp.format_patch_date(0, 5 * 3600))
36
 
        self.assertEqual('1970-01-01 00:00:00 +0000',
37
 
            timestamp.format_patch_date(0, -5 * 3600))
 
33
                         timestamp.format_patch_date(0))
 
34
        self.assertEqual('1970-01-01 00:00:00 +0000',
 
35
                         timestamp.format_patch_date(0, 5 * 3600))
 
36
        self.assertEqual('1970-01-01 00:00:00 +0000',
 
37
                         timestamp.format_patch_date(0, -5 * 3600))
38
38
        # regular timestamp with typical timezone
39
39
        self.assertEqual('2007-03-06 10:04:19 -0500',
40
 
            timestamp.format_patch_date(1173193459, -5 * 3600))
 
40
                         timestamp.format_patch_date(1173193459, -5 * 3600))
41
41
        # the timezone part is HHMM
42
42
        self.assertEqual('2007-03-06 09:34:19 -0530',
43
 
            timestamp.format_patch_date(1173193459, -5.5 * 3600))
 
43
                         timestamp.format_patch_date(1173193459, -5.5 * 3600))
44
44
        # timezones can be offset by single minutes (but no less)
45
45
        self.assertEqual('2007-03-06 15:05:19 +0001',
46
 
            timestamp.format_patch_date(1173193459, +1 * 60))
 
46
                         timestamp.format_patch_date(1173193459, +1 * 60))
47
47
 
48
48
    def test_parse_patch_date(self):
49
49
        self.assertEqual((0, 0),
50
 
            timestamp.parse_patch_date('1970-01-01 00:00:00 +0000'))
 
50
                         timestamp.parse_patch_date('1970-01-01 00:00:00 +0000'))
51
51
        # even though we don't emit pre-epoch dates, we can parse them
52
52
        self.assertEqual((0, -5 * 3600),
53
 
            timestamp.parse_patch_date('1969-12-31 19:00:00 -0500'))
 
53
                         timestamp.parse_patch_date('1969-12-31 19:00:00 -0500'))
54
54
        self.assertEqual((0, +5 * 3600),
55
 
            timestamp.parse_patch_date('1970-01-01 05:00:00 +0500'))
 
55
                         timestamp.parse_patch_date('1970-01-01 05:00:00 +0500'))
56
56
        self.assertEqual((1173193459, -5 * 3600),
57
 
            timestamp.parse_patch_date('2007-03-06 10:04:19 -0500'))
 
57
                         timestamp.parse_patch_date('2007-03-06 10:04:19 -0500'))
58
58
        # offset of three minutes
59
59
        self.assertEqual((1173193459, +3 * 60),
60
 
            timestamp.parse_patch_date('2007-03-06 15:07:19 +0003'))
 
60
                         timestamp.parse_patch_date('2007-03-06 15:07:19 +0003'))
61
61
        # No space between time and offset
62
62
        self.assertEqual((1173193459, -5 * 3600),
63
 
            timestamp.parse_patch_date('2007-03-06 10:04:19-0500'))
 
63
                         timestamp.parse_patch_date('2007-03-06 10:04:19-0500'))
64
64
        # Extra spacing
65
65
        self.assertEqual((1173193459, -5 * 3600),
66
 
            timestamp.parse_patch_date('2007-03-06     10:04:19     -0500'))
 
66
                         timestamp.parse_patch_date('2007-03-06     10:04:19     -0500'))
67
67
 
68
68
    def test_parse_patch_date_bad(self):
69
69
        self.assertRaises(ValueError, timestamp.parse_patch_date,
70
 
            'NOT A TIME')
 
70
                          'NOT A TIME')
71
71
        # Extra data at end
72
72
        self.assertRaises(ValueError, timestamp.parse_patch_date,
73
 
            '2007-03-06 10:04:19 -0500x')
 
73
                          '2007-03-06 10:04:19 -0500x')
74
74
        # Missing day
75
75
        self.assertRaises(ValueError, timestamp.parse_patch_date,
76
 
            '2007-03 10:04:19 -0500')
 
76
                          '2007-03 10:04:19 -0500')
77
77
        # Missing seconds
78
78
        self.assertRaises(ValueError, timestamp.parse_patch_date,
79
 
            '2007-03-06 10:04 -0500')
 
79
                          '2007-03-06 10:04 -0500')
80
80
        # Missing offset
81
81
        self.assertRaises(ValueError, timestamp.parse_patch_date,
82
 
            '2007-03-06 10:04:19')
 
82
                          '2007-03-06 10:04:19')
83
83
        # Missing plus or minus in offset
84
84
        self.assertRaises(ValueError, timestamp.parse_patch_date,
85
 
            '2007-03-06 10:04:19 0500')
 
85
                          '2007-03-06 10:04:19 0500')
86
86
        # Invalid hour in offset
87
87
        self.assertRaises(ValueError, timestamp.parse_patch_date,
88
 
            '2007-03-06 10:04:19 +2400')
 
88
                          '2007-03-06 10:04:19 +2400')
89
89
        self.assertRaises(ValueError, timestamp.parse_patch_date,
90
 
            '2007-03-06 10:04:19 -2400')
 
90
                          '2007-03-06 10:04:19 -2400')
91
91
        # Invalid minute in offset
92
92
        self.assertRaises(ValueError, timestamp.parse_patch_date,
93
 
            '2007-03-06 10:04:19 -0560')
 
93
                          '2007-03-06 10:04:19 -0560')
94
94
        # Too many digits in offset
95
95
        self.assertRaises(ValueError, timestamp.parse_patch_date,
96
 
            '2007-03-06 10:04:19 79500')
 
96
                          '2007-03-06 10:04:19 79500')
97
97
        # Minus sign in middle of offset
98
98
        self.assertRaises(ValueError, timestamp.parse_patch_date,
99
 
            '2007-03-06 10:04:19 +05-5')
 
99
                          '2007-03-06 10:04:19 +05-5')
100
100
 
101
101
 
102
102
class UnpackHighresDateTests(tests.TestCase):
118
118
    def test_random(self):
119
119
        t = time.time()
120
120
        o = local_time_offset()
121
 
        t2, o2 = timestamp.unpack_highres_date(timestamp.format_highres_date(t, o))
 
121
        t2, o2 = timestamp.unpack_highres_date(
 
122
            timestamp.format_highres_date(t, o))
122
123
        self.assertEqual(t, t2)
123
124
        self.assertEqual(o, o2)
124
 
        t -= 24*3600*365*2 # Start 2 years ago
125
 
        o = -12*3600
 
125
        t -= 24 * 3600 * 365 * 2  # Start 2 years ago
 
126
        o = -12 * 3600
126
127
        for count in range(500):
127
 
            t += random.random()*24*3600*30
 
128
            t += random.random() * 24 * 3600 * 30
128
129
            try:
129
130
                time.gmtime(t)
130
131
            except OverflowError:
133
134
            if time.localtime(t).tm_year > 9998:
134
135
                # strptime doesn't always understand years with more than 4 digits.
135
136
                break
136
 
            o = ((o/3600 + 13) % 25 - 12)*3600 # Add 1 wrap around from [-12, 12]
 
137
            # Add 1 wrap around from [-12, 12]
 
138
            o = ((o / 3600 + 13) % 25 - 12) * 3600
137
139
            date = timestamp.format_highres_date(t, o)
138
140
            t2, o2 = timestamp.unpack_highres_date(date)
139
141
            self.assertEqual(t, t2,
140
 
                'Failed on date %r, %s,%s diff:%s' % (date, t, o, t2-t))
 
142
                             'Failed on date %r, %s,%s diff:%s' % (date, t, o, t2 - t))
141
143
            self.assertEqual(o, o2,
142
 
                'Failed on date %r, %s,%s diff:%s' % (date, t, o, t2-t))
 
144
                             'Failed on date %r, %s,%s diff:%s' % (date, t, o, t2 - t))