def props_to_date(self, props, dt): if self.fn: return self.fn(props, dt) else: args = {} for key in adatetime.units: args[key] = props.get(key) return adatetime(**args)
def parse(self, text, dt, pos=0, debug=-9999): try: d, pos = self.element.parse(text, dt, pos, debug + 1) except TimeError: d, pos = None, None if d: return (d, pos) else: return (adatetime(), pos)
def parse(self, text, dt, pos=0, debug=-9999): first = True d = adatetime() seen = [False] * len(self.elements) while True: newpos = pos print_debug(debug, "Bag %s text=%r", self.name, text[pos:]) if not first: print_debug(debug, "Bag %s looking for sep", self.name) m = self.sep_expr.match(text, pos) if m: newpos = m.end() else: print_debug(debug, "Bag %s didn't find sep", self.name) break for i, e in enumerate(self.elements): print_debug(debug, "Bag %s trying=%r", self.name, e) try: at, xpos = e.parse(text, dt, newpos, debug + 1) except TimeError: at, xpos = None, None print_debug(debug, "Bag %s result=%r", self.name, at) if at: if self.onceper and seen[i]: return (None, None) d = fill_in(d, at) newpos = xpos seen[i] = True break else: break pos = newpos if self.onceper and all(seen): break first = False if (not any(seen) or (self.allof and not all(seen[pos] for pos in self.allof)) or (self.anyof and not any(seen[pos] for pos in self.anyof)) or (self.requireall and not all(seen))): return (None, None) print_debug(debug, "Bag %s final=%r", self.name, d) return (d, pos)
def parse(self, text, dt, pos=0, debug=-9999): d = adatetime() first = True foundall = False failed = False print_debug(debug, "Seq %s sep=%r text=%r", self.name, self.sep_pattern, text[pos:]) for e in self.elements: print_debug(debug, "Seq %s text=%r", self.name, text[pos:]) if self.sep_expr and not first: print_debug(debug, "Seq %s looking for sep", self.name) m = self.sep_expr.match(text, pos) if m: pos = m.end() else: print_debug(debug, "Seq %s didn't find sep", self.name) break print_debug(debug, "Seq %s trying=%r at=%s", self.name, e, pos) try: at, newpos = e.parse(text, dt, pos=pos, debug=debug + 1) except TimeError: failed = True break print_debug(debug, "Seq %s result=%r", self.name, at) if not at: break pos = newpos print_debug(debug, "Seq %s adding=%r to=%r", self.name, at, d) try: d = fill_in(d, at) except TimeError: print_debug(debug, "Seq %s Error in fill_in", self.name) failed = True break print_debug(debug, "Seq %s filled date=%r", self.name, d) first = False else: foundall = True if not failed and (foundall or (not first and self.progressive)): print_debug(debug, "Seq %s final=%r", self.name, d) return (d, pos) else: print_debug(debug, "Seq %s failed", self.name) return (None, None)
def props_to_date(self, p, dt): isam = p.ampm.lower().startswith("a") if p.hour == 12: if isam: hr = 0 else: hr = 12 else: hr = p.hour if not isam: hr += 12 return adatetime(hour=hr, minute=p.mins, second=p.secs, microsecond=p.usecs)
def props_to_date(self, p, dt): if re.match(p.dir, self.last_pattern): dir = -1 else: dir = 1 for daynum, expr in enumerate(self._dayname_exprs): m = expr.match(p.day) if m: break current_daynum = dt.weekday() days_delta = relative_days(current_daynum, daynum, dir) d = dt.date() + timedelta(days=days_delta) return adatetime(year=d.year, month=d.month, day=d.day)
def tomorrow_to_date(p, dt): d = dt.date() + timedelta(days=+1) return adatetime(year=d.year, month=d.month, day=d.day)
def setup(self): self.plusdate = PlusMinus( "years|year|yrs|yr|ys|y", "months|month|mons|mon|mos|mo", "weeks|week|wks|wk|ws|w", "days|day|dys|dy|ds|d", "hours|hour|hrs|hr|hs|h", "minutes|minute|mins|min|ms|m", "seconds|second|secs|sec|s") self.dayname = Daynames( "next", "last", ("monday|mon|mo", "tuesday|tues|tue|tu", "wednesday|wed|we", "thursday|thur|thu|th", "friday|fri|fr", "saturday|sat|sa", "sunday|sun|su")) midnight_l = lambda p, dt: adatetime( hour=0, minute=0, second=0, microsecond=0) midnight = Regex("midnight", midnight_l) noon_l = lambda p, dt: adatetime( hour=12, minute=0, second=0, microsecond=0) noon = Regex("noon", noon_l) now = Regex("now", lambda p, dt: dt) self.time = Choice((self.time12, self.time24, midnight, noon, now), name="time") def tomorrow_to_date(p, dt): d = dt.date() + timedelta(days=+1) return adatetime(year=d.year, month=d.month, day=d.day) tomorrow = Regex("tomorrow", tomorrow_to_date) def yesterday_to_date(p, dt): d = dt.date() + timedelta(days=-1) return adatetime(year=d.year, month=d.month, day=d.day) yesterday = Regex("yesterday", yesterday_to_date) thisyear = Regex("this year", lambda p, dt: adatetime(year=dt.year)) thismonth = Regex( "this month", lambda p, dt: adatetime(year=dt.year, month=dt.month)) today = Regex( "today", lambda p, dt: adatetime(year=dt.year, month=dt.month, day=dt.day)) self.month = Month("january|jan", "february|febuary|feb", "march|mar", "april|apr", "may", "june|jun", "july|jul", "august|aug", "september|sept|sep", "october|oct", "november|nov", "december|dec") self.of = Regex("of") # If you specify a day number you must also specify a month... this # Choice captures that constraint self.dmy = Choice(( Sequence((self.day, self.month, self.year), name="dmy"), Sequence( (self.day, self.of, self.month, self.year), name="d of my"), Sequence((self.month, self.day, self.year), name="mdy"), Sequence((self.year, self.month, self.day), name="ymd"), Sequence((self.year, self.day, self.month), name="ydm"), Sequence((self.day, self.month), name="dm"), Sequence((self.day, self.of, self.month), name="d of m"), Sequence((self.month, self.day), name="md"), Sequence((self.month, self.year), name="my"), self.month, self.year, self.dayname, tomorrow, yesterday, thisyear, thismonth, today, now, ), name="date") self.datetime = Bag((self.time, self.dmy), name="datetime") self.bundle = Choice((self.plusdate, self.simple, self.datetime), name="bundle") self.torange = Combo((self.bundle, "to", self.bundle), name="torange") self.all = Choice((self.torange, self.bundle), name="all")
class DateParser(object): """Base class for locale-specific parser classes. """ day = Regex("(?P<day>([123][0-9])|[1-9])(?=(\\W|$))(?!=:)", lambda p, dt: adatetime(day=p.day)) year = Regex("(?P<year>[0-9]{4})(?=(\\W|$))", lambda p, dt: adatetime(year=p.year)) time24 = Regex( "(?P<hour>([0-1][0-9])|(2[0-3])):(?P<mins>[0-5][0-9])" "(:(?P<secs>[0-5][0-9])(\\.(?P<usecs>[0-9]{1,5}))?)?" "(?=(\\W|$))", lambda p, dt: adatetime( hour=p.hour, minute=p.mins, second=p.secs, microsecond=p.usecs)) time12 = Time12() def __init__(self): simple_year = "(?P<year>[0-9]{4})" simple_month = "(?P<month>[0-1][0-9])" simple_day = "(?P<day>[0-3][0-9])" simple_hour = "(?P<hour>([0-1][0-9])|(2[0-3]))" simple_minute = "(?P<minute>[0-5][0-9])" simple_second = "(?P<second>[0-5][0-9])" simple_usec = "(?P<microsecond>[0-9]{6})" tup = (simple_year, simple_month, simple_day, simple_hour, simple_minute, simple_second, simple_usec) simple_seq = Sequence(tup, sep="[- .:/]*", name="simple", progressive=True) self.simple = Sequence((simple_seq, "(?=(\\s|$))"), sep='') self.setup() def setup(self): raise NotImplementedError # def get_parser(self): return self.all def parse(self, text, dt, pos=0, debug=-9999): parser = self.get_parser() d, newpos = parser.parse(text, dt, pos=pos, debug=debug) if isinstance(d, (adatetime, timespan)): d = d.disambiguated(dt) return (d, newpos) def date_from(self, text, basedate=None, pos=0, debug=-9999, toend=True): if basedate is None: basedate = datetime.utcnow() parser = self.get_parser() if toend: parser = ToEnd(parser) d = parser.date_from(text, basedate, pos=pos, debug=debug) if isinstance(d, (adatetime, timespan)): d = d.disambiguated(basedate) return d
def setup(self): self.plusdate = PlusMinus("years|year|yrs|yr|ys|y", "months|month|mons|mon|mos|mo", "weeks|week|wks|wk|ws|w", "days|day|dys|dy|ds|d", "hours|hour|hrs|hr|hs|h", "minutes|minute|mins|min|ms|m", "seconds|second|secs|sec|s") self.dayname = Daynames("next", "last", ("monday|mon|mo", "tuesday|tues|tue|tu", "wednesday|wed|we", "thursday|thur|thu|th", "friday|fri|fr", "saturday|sat|sa", "sunday|sun|su")) midnight_l = lambda p, dt: adatetime(hour=0, minute=0, second=0, microsecond=0) midnight = Regex("midnight", midnight_l) noon_l = lambda p, dt: adatetime(hour=12, minute=0, second=0, microsecond=0) noon = Regex("noon", noon_l) now = Regex("now", lambda p, dt: dt) self.time = Choice((self.time12, self.time24, midnight, noon, now), name="time") def tomorrow_to_date(p, dt): d = dt.date() + timedelta(days=+1) return adatetime(year=d.year, month=d.month, day=d.day) tomorrow = Regex("tomorrow", tomorrow_to_date) def yesterday_to_date(p, dt): d = dt.date() + timedelta(days=-1) return adatetime(year=d.year, month=d.month, day=d.day) yesterday = Regex("yesterday", yesterday_to_date) thisyear = Regex("this year", lambda p, dt: adatetime(year=dt.year)) thismonth = Regex("this month", lambda p, dt: adatetime(year=dt.year, month=dt.month)) today = Regex("today", lambda p, dt: adatetime(year=dt.year, month=dt.month, day=dt.day)) self.month = Month("january|jan", "february|febuary|feb", "march|mar", "april|apr", "may", "june|jun", "july|jul", "august|aug", "september|sept|sep", "october|oct", "november|nov", "december|dec") self.of = Regex("of") # If you specify a day number you must also specify a month... this # Choice captures that constraint self.dmy = Choice((Sequence((self.day, self.month, self.year), name="dmy"), Sequence((self.day, self.of, self.month, self.year), name="d of my"), Sequence((self.month, self.day, self.year), name="mdy"), Sequence((self.year, self.month, self.day), name="ymd"), Sequence((self.year, self.day, self.month), name="ydm"), Sequence((self.day, self.month), name="dm"), Sequence((self.day, self.of, self.month), name="d of m"), Sequence((self.month, self.day), name="md"), Sequence((self.month, self.year), name="my"), self.month, self.year, self.dayname, tomorrow, yesterday, thisyear, thismonth, today, now, ), name="date") self.datetime = Bag((self.time, self.dmy), name="datetime") self.bundle = Choice((self.plusdate, self.simple, self.datetime), name="bundle") self.torange = Combo((self.bundle, "to", self.bundle), name="torange") self.all = Choice((self.torange, self.bundle), name="all")
def yesterday_to_date(p, dt): d = dt.date() + timedelta(days=-1) return adatetime(year=d.year, month=d.month, day=d.day)