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
17
from __future__ import absolute_import
23
20
from bzrlib import osutils
127
124
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+$")
135
127
def parse_patch_date(date_str):
136
128
"""Parse a patch-style date into a POSIX timestamp and offset.
138
130
Inverse of format_patch_date.
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
132
secs_str = date_str[:-6]
133
offset_str = date_str[-5:]
134
if len(offset_str) != 5:
136
"invalid timezone %r" % offset_str)
137
offset_hours, offset_mins = offset_str[:3], offset_str[3:]
138
offset = int(offset_hours) * 3600 + int(offset_mins) * 60
154
139
tm_time = time.strptime(secs_str, '%Y-%m-%d %H:%M:%S')
155
140
# adjust seconds according to offset before converting to POSIX
156
141
# timestamp, to avoid edge problems