~bzr-pqm/bzr/bzr.dev

5557.1.7 by John Arbash Meinel
Merge in the bzr.dev 5582
1
# Copyright (C) 2007, 2009, 2011 Canonical Ltd
1551.12.29 by Aaron Bentley
Copy and extend patch date formatting code, add patch-date parsing
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1551.12.29 by Aaron Bentley
Copy and extend patch date formatting code, add patch-date parsing
16
5580.1.1 by Jelmer Vernooij
Convert bzrlib.timestamp.unpack_highres_date doc test to a unit test.
17
import random
18
import time
19
20
1551.12.29 by Aaron Bentley
Copy and extend patch date formatting code, add patch-date parsing
21
from bzrlib import (
22
    tests,
23
    timestamp,
24
    )
5580.1.1 by Jelmer Vernooij
Convert bzrlib.timestamp.unpack_highres_date doc test to a unit test.
25
from bzrlib.osutils import local_time_offset
26
1551.12.29 by Aaron Bentley
Copy and extend patch date formatting code, add patch-date parsing
27
28
class TestPatchHeader(tests.TestCase):
29
1551.12.36 by Aaron Bentley
Fix failing tests
30
    def test_format_patch_date(self):
2425.6.1 by Martin Pool
Fix formatting of timezones in bundles and merge directives.
31
        # epoch is always in utc
1551.12.29 by Aaron Bentley
Copy and extend patch date formatting code, add patch-date parsing
32
        self.assertEqual('1970-01-01 00:00:00 +0000',
1551.12.36 by Aaron Bentley
Fix failing tests
33
            timestamp.format_patch_date(0))
2425.6.1 by Martin Pool
Fix formatting of timezones in bundles and merge directives.
34
        self.assertEqual('1970-01-01 00:00:00 +0000',
1551.12.36 by Aaron Bentley
Fix failing tests
35
            timestamp.format_patch_date(0, 5 * 3600))
2425.6.1 by Martin Pool
Fix formatting of timezones in bundles and merge directives.
36
        self.assertEqual('1970-01-01 00:00:00 +0000',
37
            timestamp.format_patch_date(0, -5 * 3600))
38
        # regular timestamp with typical timezone
1551.12.29 by Aaron Bentley
Copy and extend patch date formatting code, add patch-date parsing
39
        self.assertEqual('2007-03-06 10:04:19 -0500',
1551.12.36 by Aaron Bentley
Fix failing tests
40
            timestamp.format_patch_date(1173193459, -5 * 3600))
2425.6.1 by Martin Pool
Fix formatting of timezones in bundles and merge directives.
41
        # the timezone part is HHMM
42
        self.assertEqual('2007-03-06 09:34:19 -0530',
43
            timestamp.format_patch_date(1173193459, -5.5 * 3600))
44
        # timezones can be offset by single minutes (but no less)
45
        self.assertEqual('2007-03-06 15:05:19 +0001',
46
            timestamp.format_patch_date(1173193459, +1 * 60))
1551.12.29 by Aaron Bentley
Copy and extend patch date formatting code, add patch-date parsing
47
48
    def test_parse_patch_date(self):
49
        self.assertEqual((0, 0),
50
            timestamp.parse_patch_date('1970-01-01 00:00:00 +0000'))
2425.6.1 by Martin Pool
Fix formatting of timezones in bundles and merge directives.
51
        # even though we don't emit pre-epoch dates, we can parse them
1551.12.29 by Aaron Bentley
Copy and extend patch date formatting code, add patch-date parsing
52
        self.assertEqual((0, -5 * 3600),
53
            timestamp.parse_patch_date('1969-12-31 19:00:00 -0500'))
54
        self.assertEqual((0, +5 * 3600),
55
            timestamp.parse_patch_date('1970-01-01 05:00:00 +0500'))
56
        self.assertEqual((1173193459, -5 * 3600),
57
            timestamp.parse_patch_date('2007-03-06 10:04:19 -0500'))
2425.6.1 by Martin Pool
Fix formatting of timezones in bundles and merge directives.
58
        # offset of three minutes
59
        self.assertEqual((1173193459, +3 * 60),
60
            timestamp.parse_patch_date('2007-03-06 15:07:19 +0003'))
5580.1.1 by Jelmer Vernooij
Convert bzrlib.timestamp.unpack_highres_date doc test to a unit test.
61
62
63
class UnpackHighresDateTests(tests.TestCase):
64
65
    def test_unpack_highres_date(self):
66
        self.assertEquals(
67
            (1120153132.3508501, -18000),
68
            timestamp.unpack_highres_date('Thu 2005-06-30 12:38:52.350850105 -0500'))
69
        self.assertEquals(
70
            (1120153132.3508501, 0),
71
            timestamp.unpack_highres_date('Thu 2005-06-30 17:38:52.350850105 +0000'))
72
        self.assertEquals(
73
            (1120153132.3508501, 7200),
74
            timestamp.unpack_highres_date('Thu 2005-06-30 19:38:52.350850105 +0200'))
75
        self.assertEquals(
76
            (1152428738.867522, 19800),
77
            timestamp.unpack_highres_date('Sun 2006-07-09 12:35:38.867522001 +0530'))
78
79
    def test_random(self):
80
        t = time.time()
81
        o = local_time_offset()
82
        t2, o2 = timestamp.unpack_highres_date(timestamp.format_highres_date(t, o))
83
        self.assertEquals(t, t2)
84
        self.assertEquals(o, o2)
85
        t -= 24*3600*365*2 # Start 2 years ago
86
        o = -12*3600
87
        for count in xrange(500):
88
            t += random.random()*24*3600*30
89
            o = ((o/3600 + 13) % 25 - 12)*3600 # Add 1 wrap around from [-12, 12]
90
            date = timestamp.format_highres_date(t, o)
91
            t2, o2 = timestamp.unpack_highres_date(date)
92
            self.assertEquals(t, t2,
93
                'Failed on date %r, %s,%s diff:%s' % (date, t, o, t2-t))
94
            self.assertEquals(o, o2,
95
                'Failed on date %r, %s,%s diff:%s' % (date, t, o, t2-t))