diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/delta_time.py | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/examples/delta_time.py b/examples/delta_time.py index e079094..525f5df 100644 --- a/examples/delta_time.py +++ b/examples/delta_time.py @@ -52,6 +52,9 @@ today, tomorrow, yesterday, noon, midnight, now = map(CK, "today tomorrow yester def plural(s): return CK(s) | CK(s + 's').addParseAction(pp.replaceWith(s)) week, day, hour, minute, second = map(plural, "week day hour minute second".split()) +time_units = hour | minute | second +any_time_units = week | day | time_units + am = CL("am") pm = CL("pm") COLON = pp.Suppress(':') @@ -69,7 +72,7 @@ on_ = CK("on") couple = (pp.Optional(CK("a")) + CK("couple") + pp.Optional(CK("of"))).setParseAction(pp.replaceWith(2)) a_qty = (CK("a") | CK("an")).setParseAction(pp.replaceWith(1)) the_qty = CK("the").setParseAction(pp.replaceWith(1)) -qty = pp.ungroup(integer | couple | a_qty | the_qty) +qty = pp.ungroup(integer | couple | a_qty | the_qty).setName("qty") time_ref_present = pp.Empty().addParseAction(pp.replaceWith(True))('time_ref_present') def fill_24hr_time_fields(t): @@ -86,8 +89,10 @@ def fill_default_time_fields(t): weekday_name_list = list(calendar.day_name) weekday_name = pp.oneOf(weekday_name_list) -_24hour_time = pp.Word(pp.nums, exact=4).addParseAction(lambda t: [int(t[0][:2]),int(t[0][2:])], - fill_24hr_time_fields) +_24hour_time = (~(integer + any_time_units) + + pp.Word(pp.nums, exact=4).addParseAction(lambda t: [int(t[0][:2]),int(t[0][2:])], + fill_24hr_time_fields) + ) _24hour_time.setName("0000 time") ampm = am | pm timespec = (integer("HH") @@ -302,6 +307,10 @@ if __name__ == "__main__": 2pm next Sunday next Sunday at 2pm last Sunday at 2pm + 10 seconds ago + 100 seconds ago + 1000 seconds ago + 10000 seconds ago """ time_of_day = timedelta(hours=current_time.hour, @@ -309,6 +318,10 @@ if __name__ == "__main__": seconds=current_time.second) expected = { 'now' : timedelta(0), + "10 seconds ago": timedelta(seconds=-10), + "100 seconds ago": timedelta(seconds=-100), + "1000 seconds ago": timedelta(seconds=-1000), + "10000 seconds ago": timedelta(seconds=-10000), '10 minutes ago': timedelta(minutes=-10), '10 minutes from now': timedelta(minutes=10), 'in 10 minutes': timedelta(minutes=10), |