1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
1
# Copyright (C) 2007, 2008, 2009, 2011 Canonical Ltd
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
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
from __future__ import absolute_import
20
23
from bzrlib import osutils
47
50
>>> format_highres_date(1152428738.867522, 19800)
48
51
'Sun 2006-07-09 12:35:38.867522001 +0530'
50
assert isinstance(t, float)
53
if not isinstance(t, float):
52
56
# This has to be formatted for "original" date, so that the
53
57
# revision XML entry will be reproduced faithfully.
56
60
tt = time.gmtime(t + offset)
58
return (time.strftime("%a %Y-%m-%d %H:%M:%S", tt)
62
return (osutils.weekdays[tt[6]] +
63
time.strftime(" %Y-%m-%d %H:%M:%S", tt)
59
64
# Get the high-res seconds, but ignore the 0
60
65
+ ('%.9f' % (t - int(t)))[1:]
61
66
+ ' %+03d%02d' % (offset / 3600, (offset / 60) % 60))
70
75
:param date: A date formated by format_highres_date
73
>>> import time, random
74
>>> unpack_highres_date('Thu 2005-06-30 12:38:52.350850105 -0500')
75
(1120153132.3508501, -18000)
76
>>> unpack_highres_date('Thu 2005-06-30 17:38:52.350850105 +0000')
77
(1120153132.3508501, 0)
78
>>> unpack_highres_date('Thu 2005-06-30 19:38:52.350850105 +0200')
79
(1120153132.3508501, 7200)
80
>>> unpack_highres_date('Sun 2006-07-09 12:35:38.867522001 +0530')
81
(1152428738.867522, 19800)
82
>>> from bzrlib.osutils import local_time_offset
84
>>> o = local_time_offset()
85
>>> t2, o2 = unpack_highres_date(format_highres_date(t, o))
90
>>> t -= 24*3600*365*2 # Start 2 years ago
92
>>> for count in xrange(500):
93
... t += random.random()*24*3600*30
94
... o = ((o/3600 + 13) % 25 - 12)*3600 # Add 1 wrap around from [-12, 12]
95
... date = format_highres_date(t, o)
96
... t2, o2 = unpack_highres_date(date)
97
... if t != t2 or o != o2:
98
... print 'Failed on date %r, %s,%s diff:%s' % (date, t, o, t2-t)
79
# Weekday parsing is locale sensitive, so drop the weekday
80
space_loc = date.find(' ')
81
if space_loc == -1 or date[:space_loc] not in osutils.weekdays:
83
'Date string does not contain a day of week: %r' % date)
102
84
# Up until the first period is a datestamp that is generated
103
85
# as normal from time.strftime, so use time.strptime to
108
90
'Date string does not contain high-precision seconds: %r' % date)
109
base_time = time.strptime(date[:dot_loc], "%a %Y-%m-%d %H:%M:%S")
91
base_time = time.strptime(date[space_loc:dot_loc], " %Y-%m-%d %H:%M:%S")
110
92
fract_seconds, offset = date[dot_loc:].split()
111
93
fract_seconds = float(fract_seconds)
130
112
Inverse of parse_patch_date.
132
assert offset % 60 == 0, \
133
"can't represent timezone %s offset by fractional minutes" % offset
134
# so that we don't need to do calculations on pre-epoch times,
116
"can't represent timezone %s offset by fractional minutes" % offset)
117
# so that we don't need to do calculations on pre-epoch times,
135
118
# which doesn't work with win32 python gmtime, we always
136
119
# give the epoch in utc
144
127
date_fmt='%Y-%m-%d %H:%M:%S')
130
# Format for patch dates: %Y-%m-%d %H:%M:%S [+-]%H%M
131
# Groups: 1 = %Y-%m-%d %H:%M:%S; 2 = [+-]%H; 3 = %M
132
RE_PATCHDATE = re.compile("(\d+-\d+-\d+\s+\d+:\d+:\d+)\s*([+-]\d\d)(\d\d)$")
133
RE_PATCHDATE_NOOFFSET = re.compile("\d+-\d+-\d+\s+\d+:\d+:\d+$")
147
135
def parse_patch_date(date_str):
148
136
"""Parse a patch-style date into a POSIX timestamp and offset.
150
138
Inverse of format_patch_date.
152
secs_str = date_str[:-6]
153
offset_str = date_str[-5:]
154
assert len(offset_str) == 5, \
155
"invalid timezone %r" % offset_str
156
offset_hours, offset_mins = offset_str[:3], offset_str[3:]
157
offset = int(offset_hours) * 3600 + int(offset_mins) * 60
140
match = RE_PATCHDATE.match(date_str)
142
if RE_PATCHDATE_NOOFFSET.match(date_str) is not None:
143
raise ValueError("time data %r is missing a timezone offset"
146
raise ValueError("time data %r does not match format " % date_str
147
+ "'%Y-%m-%d %H:%M:%S %z'")
148
secs_str = match.group(1)
149
offset_hours, offset_mins = int(match.group(2)), int(match.group(3))
150
if abs(offset_hours) >= 24 or offset_mins >= 60:
151
raise ValueError("invalid timezone %r" %
152
(match.group(2) + match.group(3)))
153
offset = offset_hours * 3600 + offset_mins * 60
158
154
tm_time = time.strptime(secs_str, '%Y-%m-%d %H:%M:%S')
159
155
# adjust seconds according to offset before converting to POSIX
160
156
# timestamp, to avoid edge problems