filled out Command.parseTimeSpec()

This commit is contained in:
Josh Holtrop 2010-12-13 19:28:55 -05:00
parent 8dc5d28cee
commit 7765a7cb0b

View File

@ -1,5 +1,6 @@
from datetime import datetime from datetime import datetime, timedelta
import re
class Command: class Command:
def __init__(self, timenow, cmdline): def __init__(self, timenow, cmdline):
@ -33,4 +34,34 @@ class Command:
self.argstr = ' '.join([command, rest]).strip() self.argstr = ' '.join([command, rest]).strip()
def parseTimeSpec(self, timespec): def parseTimeSpec(self, timespec):
pass the_time = self.time
m = re.match('^(?:(\d{4})[-/])?(\d{1,2})[-/](\d{1,2}),(.*)$', timespec)
if m is not None:
# a date was given
if m.group(1) != '':
the_time = the_time.replace(year = m.group(1))
the_time = the_time.replace(month = m.group(2), day = m.group(3))
timespec = m.group(4)
m = re.match('^(\d{1,2}):?(\d{2})?(am?|pm?)?$', timespec, re.I)
if m is not None:
# an absolute time was given
h = int(m.group(1))
mins = 0 if m.group(2) is None else int(m.group(2))
am_pm = m.group(3).lower()
if len(am_pm) >= 1 and am_pm[0] == 'p' and h < 12:
h += 12
elif len(am_pm) >= 1 and am_pm[0] == 'a' and h == 12:
h = 0
the_time = the_time.replace(hour = h, minute = mins, second = 0)
else:
m = re.match('^([-+])(\d+(?:\.\d+)?)([hms])?$', timespec, re.I)
if m is not None:
# a relative time was given
plus_minus = m.group(1)
hms = m.group(3)
if hms == 'm':
seconds = m.group(2) * 60
elif hms == 's':
seconds = m.group(2)
else: # hours
seconds = m.group(2) * 60 * 60