240
240
def string_to_delta(interval):
241
241
"""Parse a string and return a datetime.timedelta
243
>>> string_to_delta('7d')
244
datetime.timedelta(7)
245
>>> string_to_delta('60s')
246
datetime.timedelta(0, 60)
247
>>> string_to_delta('60m')
248
datetime.timedelta(0, 3600)
249
>>> string_to_delta('24h')
250
datetime.timedelta(1)
251
>>> string_to_delta('1w')
252
datetime.timedelta(7)
253
>>> string_to_delta('5m 30s')
254
datetime.timedelta(0, 330)
258
245
return rfc3339_duration_to_delta(interval)
246
except ValueError as e:
247
log.warning("%s - Parsing as pre-1.6.1 interval instead",
249
return parse_pre_1_6_1_interval(interval)
252
def parse_pre_1_6_1_interval(interval):
253
"""Parse an interval string as documented by Mandos before 1.6.1, and
254
return a datetime.timedelta
255
>>> parse_pre_1_6_1_interval('7d')
256
datetime.timedelta(7)
257
>>> parse_pre_1_6_1_interval('60s')
258
datetime.timedelta(0, 60)
259
>>> parse_pre_1_6_1_interval('60m')
260
datetime.timedelta(0, 3600)
261
>>> parse_pre_1_6_1_interval('24h')
262
datetime.timedelta(1)
263
>>> parse_pre_1_6_1_interval('1w')
264
datetime.timedelta(7)
265
>>> parse_pre_1_6_1_interval('5m 30s')
266
datetime.timedelta(0, 330)
267
>>> parse_pre_1_6_1_interval('')
268
datetime.timedelta(0)
269
>>> # Ignore unknown characters, allow any order and repetitions
270
>>> parse_pre_1_6_1_interval('2dxy7zz11y3m5m')
271
datetime.timedelta(2, 480, 18000)
262
275
value = datetime.timedelta(0)
263
276
regexp = re.compile(r"(\d+)([dsmhw]?)")