13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
17
from bzrlib import (
25
from bzrlib.osutils import local_time_offset
28
22
class TestPatchHeader(tests.TestCase):
30
24
def test_format_patch_date(self):
31
# epoch is always in utc
32
25
self.assertEqual('1970-01-01 00:00:00 +0000',
33
26
timestamp.format_patch_date(0))
34
self.assertEqual('1970-01-01 00:00:00 +0000',
27
self.assertEqual('1970-01-01 05:00:00 +0500',
35
28
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
# regular timestamp with typical timezone
29
self.assertEqual('1969-12-31 19:00:00 -0500',
30
timestamp.format_patch_date(0, -5 * 3600))
31
self.assertEqual('1969-12-31 19:00:00 -0500',
32
timestamp.format_patch_date(0, -5 * 3600))
39
33
self.assertEqual('2007-03-06 10:04:19 -0500',
40
34
timestamp.format_patch_date(1173193459, -5 * 3600))
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))
48
36
def test_parse_patch_date(self):
49
37
self.assertEqual((0, 0),
50
38
timestamp.parse_patch_date('1970-01-01 00:00:00 +0000'))
51
# even though we don't emit pre-epoch dates, we can parse them
52
39
self.assertEqual((0, -5 * 3600),
53
40
timestamp.parse_patch_date('1969-12-31 19:00:00 -0500'))
54
41
self.assertEqual((0, +5 * 3600),
55
42
timestamp.parse_patch_date('1970-01-01 05:00:00 +0500'))
56
43
self.assertEqual((1173193459, -5 * 3600),
57
44
timestamp.parse_patch_date('2007-03-06 10:04:19 -0500'))
58
# offset of three minutes
59
self.assertEqual((1173193459, +3 * 60),
60
timestamp.parse_patch_date('2007-03-06 15:07:19 +0003'))
63
class UnpackHighresDateTests(tests.TestCase):
65
def test_unpack_highres_date(self):
67
(1120153132.3508501, -18000),
68
timestamp.unpack_highres_date('Thu 2005-06-30 12:38:52.350850105 -0500'))
70
(1120153132.3508501, 0),
71
timestamp.unpack_highres_date('Thu 2005-06-30 17:38:52.350850105 +0000'))
73
(1120153132.3508501, 7200),
74
timestamp.unpack_highres_date('Thu 2005-06-30 19:38:52.350850105 +0200'))
76
(1152428738.867522, 19800),
77
timestamp.unpack_highres_date('Sun 2006-07-09 12:35:38.867522001 +0530'))
79
def test_random(self):
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
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))