124
125
date_fmt='%Y-%m-%d %H:%M:%S')
128
# Format for patch dates: %Y-%m-%d %H:%M:%S [+-]%H%M
129
# Groups: 1 = %Y-%m-%d %H:%M:%S; 2 = [+-]%H; 3 = %M
130
RE_PATCHDATE = re.compile("(\d+-\d+-\d+\s+\d+:\d+:\d+)\s*([+-]\d\d)(\d\d)$")
131
RE_PATCHDATE_NOOFFSET = re.compile("\d+-\d+-\d+\s+\d+:\d+:\d+$")
127
133
def parse_patch_date(date_str):
128
134
"""Parse a patch-style date into a POSIX timestamp and offset.
130
136
Inverse of format_patch_date.
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
138
match = RE_PATCHDATE.match(date_str)
140
if RE_PATCHDATE_NOOFFSET.match(date_str) is not None:
141
raise ValueError("time data %r is missing a timezone offset"
144
raise ValueError("time data %r does not match format " % date_str
145
+ "'%Y-%m-%d %H:%M:%S %z'")
146
secs_str = match.group(1)
147
offset_hours, offset_mins = int(match.group(2)), int(match.group(3))
148
if abs(offset_hours) >= 24 or offset_mins >= 60:
149
raise ValueError("invalid timezone %r" %
150
(match.group(2) + match.group(3)))
151
offset = offset_hours * 3600 + offset_mins * 60
139
152
tm_time = time.strptime(secs_str, '%Y-%m-%d %H:%M:%S')
140
153
# adjust seconds according to offset before converting to POSIX
141
154
# timestamp, to avoid edge problems