/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: 2020-04-05 19:11:34 UTC
  • mto: (7490.7.16 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200405191134-0aebh8ikiwygxma5
Populate the .gitignore file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 Canonical Ltd
 
1
# Copyright (C) 2007, 2009, 2010, 2011, 2016 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
from bzrlib import (
 
17
import random
 
18
import time
 
19
 
 
20
 
 
21
from breezy import (
18
22
    tests,
19
23
    timestamp,
20
24
    )
 
25
from breezy.osutils import local_time_offset
 
26
 
21
27
 
22
28
class TestPatchHeader(tests.TestCase):
23
29
 
24
30
    def test_format_patch_date(self):
25
31
        # epoch is always in utc
26
32
        self.assertEqual('1970-01-01 00:00:00 +0000',
27
 
            timestamp.format_patch_date(0))
28
 
        self.assertEqual('1970-01-01 00:00:00 +0000',
29
 
            timestamp.format_patch_date(0, 5 * 3600))
30
 
        self.assertEqual('1970-01-01 00:00:00 +0000',
31
 
            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))
32
38
        # regular timestamp with typical timezone
33
39
        self.assertEqual('2007-03-06 10:04:19 -0500',
34
 
            timestamp.format_patch_date(1173193459, -5 * 3600))
 
40
                         timestamp.format_patch_date(1173193459, -5 * 3600))
35
41
        # the timezone part is HHMM
36
42
        self.assertEqual('2007-03-06 09:34:19 -0530',
37
 
            timestamp.format_patch_date(1173193459, -5.5 * 3600))
 
43
                         timestamp.format_patch_date(1173193459, -5.5 * 3600))
38
44
        # timezones can be offset by single minutes (but no less)
39
45
        self.assertEqual('2007-03-06 15:05:19 +0001',
40
 
            timestamp.format_patch_date(1173193459, +1 * 60))
 
46
                         timestamp.format_patch_date(1173193459, +1 * 60))
41
47
 
42
48
    def test_parse_patch_date(self):
43
 
        self.assertEqual((0, 0),
 
49
        self.assertEqual(
 
50
            (0, 0),
44
51
            timestamp.parse_patch_date('1970-01-01 00:00:00 +0000'))
45
52
        # even though we don't emit pre-epoch dates, we can parse them
46
 
        self.assertEqual((0, -5 * 3600),
 
53
        self.assertEqual(
 
54
            (0, -5 * 3600),
47
55
            timestamp.parse_patch_date('1969-12-31 19:00:00 -0500'))
48
 
        self.assertEqual((0, +5 * 3600),
 
56
        self.assertEqual(
 
57
            (0, +5 * 3600),
49
58
            timestamp.parse_patch_date('1970-01-01 05:00:00 +0500'))
50
 
        self.assertEqual((1173193459, -5 * 3600),
 
59
        self.assertEqual(
 
60
            (1173193459, -5 * 3600),
51
61
            timestamp.parse_patch_date('2007-03-06 10:04:19 -0500'))
52
62
        # offset of three minutes
53
 
        self.assertEqual((1173193459, +3 * 60),
 
63
        self.assertEqual(
 
64
            (1173193459, +3 * 60),
54
65
            timestamp.parse_patch_date('2007-03-06 15:07:19 +0003'))
 
66
        # No space between time and offset
 
67
        self.assertEqual(
 
68
            (1173193459, -5 * 3600),
 
69
            timestamp.parse_patch_date('2007-03-06 10:04:19-0500'))
 
70
        # Extra spacing
 
71
        self.assertEqual(
 
72
            (1173193459, -5 * 3600),
 
73
            timestamp.parse_patch_date('2007-03-06     10:04:19     -0500'))
 
74
 
 
75
    def test_parse_patch_date_bad(self):
 
76
        self.assertRaises(ValueError, timestamp.parse_patch_date,
 
77
                          'NOT A TIME')
 
78
        # Extra data at end
 
79
        self.assertRaises(ValueError, timestamp.parse_patch_date,
 
80
                          '2007-03-06 10:04:19 -0500x')
 
81
        # Missing day
 
82
        self.assertRaises(ValueError, timestamp.parse_patch_date,
 
83
                          '2007-03 10:04:19 -0500')
 
84
        # Missing seconds
 
85
        self.assertRaises(ValueError, timestamp.parse_patch_date,
 
86
                          '2007-03-06 10:04 -0500')
 
87
        # Missing offset
 
88
        self.assertRaises(ValueError, timestamp.parse_patch_date,
 
89
                          '2007-03-06 10:04:19')
 
90
        # Missing plus or minus in offset
 
91
        self.assertRaises(ValueError, timestamp.parse_patch_date,
 
92
                          '2007-03-06 10:04:19 0500')
 
93
        # Invalid hour in offset
 
94
        self.assertRaises(ValueError, timestamp.parse_patch_date,
 
95
                          '2007-03-06 10:04:19 +2400')
 
96
        self.assertRaises(ValueError, timestamp.parse_patch_date,
 
97
                          '2007-03-06 10:04:19 -2400')
 
98
        # Invalid minute in offset
 
99
        self.assertRaises(ValueError, timestamp.parse_patch_date,
 
100
                          '2007-03-06 10:04:19 -0560')
 
101
        # Too many digits in offset
 
102
        self.assertRaises(ValueError, timestamp.parse_patch_date,
 
103
                          '2007-03-06 10:04:19 79500')
 
104
        # Minus sign in middle of offset
 
105
        self.assertRaises(ValueError, timestamp.parse_patch_date,
 
106
                          '2007-03-06 10:04:19 +05-5')
 
107
 
 
108
 
 
109
class UnpackHighresDateTests(tests.TestCase):
 
110
 
 
111
    def test_unpack_highres_date(self):
 
112
        self.assertEqual(
 
113
            (1120153132.3508501, -18000),
 
114
            timestamp.unpack_highres_date(
 
115
                'Thu 2005-06-30 12:38:52.350850105 -0500'))
 
116
        self.assertEqual(
 
117
            (1120153132.3508501, 0),
 
118
            timestamp.unpack_highres_date(
 
119
                'Thu 2005-06-30 17:38:52.350850105 +0000'))
 
120
        self.assertEqual(
 
121
            (1120153132.3508501, 7200),
 
122
            timestamp.unpack_highres_date(
 
123
                'Thu 2005-06-30 19:38:52.350850105 +0200'))
 
124
        self.assertEqual(
 
125
            (1152428738.867522, 19800),
 
126
            timestamp.unpack_highres_date(
 
127
                'Sun 2006-07-09 12:35:38.867522001 +0530'))
 
128
 
 
129
    def test_random(self):
 
130
        t = time.time()
 
131
        o = local_time_offset()
 
132
        t2, o2 = timestamp.unpack_highres_date(
 
133
            timestamp.format_highres_date(t, o))
 
134
        self.assertEqual(t, t2)
 
135
        self.assertEqual(o, o2)
 
136
        t -= 24 * 3600 * 365 * 2  # Start 2 years ago
 
137
        o = -12 * 3600
 
138
        for count in range(500):
 
139
            t += random.random() * 24 * 3600 * 30
 
140
            try:
 
141
                time.gmtime(t + o)
 
142
            except (OverflowError, ValueError):
 
143
                # We've reached the maximum for time_t on this platform
 
144
                break
 
145
            if time.localtime(t).tm_year > 9998:
 
146
                # strptime doesn't always understand years with more than 4
 
147
                # digits.
 
148
                break
 
149
            # Add 1 wrap around from [-12, 12]
 
150
            o = ((o / 3600 + 13) % 25 - 12) * 3600
 
151
            date = timestamp.format_highres_date(t, o)
 
152
            t2, o2 = timestamp.unpack_highres_date(date)
 
153
            self.assertEqual(
 
154
                t, t2,
 
155
                'Failed on date %r, %s,%s diff:%s' % (date, t, o, t2 - t))
 
156
            self.assertEqual(
 
157
                o, o2,
 
158
                'Failed on date %r, %s,%s diff:%s' % (date, t, o, t2 - t))