+ special = ctx.specialDate().getText().lower()
+ self.context['special'] = special
+ except Exception as e:
+ raise ParseException(
+ f'Bad specialDate expression: {ctx.specialDate().getText()}'
+ ) from e
+ try:
+ mod = ctx.thisNextLast()
+ if mod is not None:
+ if mod.THIS() is not None:
+ self.context['special_next_last'] = 'this'
+ elif mod.NEXT() is not None:
+ self.context['special_next_last'] = 'next'
+ elif mod.LAST() is not None:
+ self.context['special_next_last'] = 'last'
+ except Exception as e:
+ raise ParseException(f'Bad specialDateNextLast expression: {ctx.getText()}') from e
+
+ def exitNFoosFromTodayAgoExpr(
+ self, ctx: dateparse_utilsParser.NFoosFromTodayAgoExprContext
+ ) -> None:
+ d = self.now_datetime
+ try:
+ count = DateParser._get_int(ctx.unsignedInt().getText())
+ unit = ctx.deltaUnit().getText().lower()
+ ago_from_now = ctx.AGO_FROM_NOW().getText()
+ except Exception as e:
+ raise ParseException(f'Bad NFoosFromTodayAgoExpr: {ctx.getText()}') from e
+
+ if "ago" in ago_from_now or "back" in ago_from_now:
+ count = -count
+
+ unit = self._figure_out_date_unit(unit)
+ d = n_timeunits_from_base(count, TimeUnit(unit), d)
+ self.context['year'] = d.year
+ self.context['month'] = d.month
+ self.context['day'] = d.day
+
+ def exitDeltaRelativeToTodayExpr(
+ self, ctx: dateparse_utilsParser.DeltaRelativeToTodayExprContext
+ ) -> None:
+ # When someone says "next week" they mean a week from now.
+ # Likewise next month or last year. These expressions are now
+ # +/- delta.
+ #
+ # But when someone says "this Friday" they mean "this coming
+ # Friday". It would be weird to say "this Friday" if today
+ # was already Friday but I'm parsing it to mean: the next day
+ # that is a Friday. So when you say "next Friday" you mean
+ # the Friday after this coming Friday, or 2 Fridays from now.
+ #
+ # This set handles this weirdness.
+ weekdays = set(
+ [
+ TimeUnit.MONDAYS,
+ TimeUnit.TUESDAYS,
+ TimeUnit.WEDNESDAYS,
+ TimeUnit.THURSDAYS,
+ TimeUnit.FRIDAYS,
+ TimeUnit.SATURDAYS,
+ TimeUnit.SUNDAYS,
+ ]
+ )
+ d = self.now_datetime
+ try:
+ mod = ctx.thisNextLast()
+ unit = ctx.deltaUnit().getText().lower()
+ unit = self._figure_out_date_unit(unit)
+ if mod.LAST():
+ count = -1
+ elif mod.THIS():
+ if unit in weekdays:
+ count = +1
+ else:
+ count = 0
+ elif mod.NEXT():
+ if unit in weekdays:
+ count = +2
+ else:
+ count = +1
+ else:
+ raise ParseException(f'Bad This/Next/Last modifier: {mod}')
+ except Exception as e:
+ raise ParseException(f'Bad DeltaRelativeToTodayExpr: {ctx.getText()}') from e
+ d = n_timeunits_from_base(count, TimeUnit(unit), d)
+ self.context['year'] = d.year
+ self.context['month'] = d.month
+ self.context['day'] = d.day
+
+ def exitSpecialTimeExpr(self, ctx: dateparse_utilsParser.SpecialTimeExprContext) -> None:
+ try:
+ txt = ctx.specialTime().getText().lower()
+ except Exception as e:
+ raise ParseException(f'Bad special time expression: {ctx.getText()}') from e