def datedif(ctx, start_date, end_date, unit): """ Calculates the number of days, months, or years between two dates. """ start_date = conversions.to_date(start_date, ctx) end_date = conversions.to_date(end_date, ctx) unit = conversions.to_string(unit, ctx).lower() if start_date > end_date: raise ValueError("Start date cannot be after end date") if unit == 'y': return relativedelta(end_date, start_date).years elif unit == 'm': delta = relativedelta(end_date, start_date) return 12 * delta.years + delta.months elif unit == 'd': return (end_date - start_date).days elif unit == 'md': return relativedelta(end_date, start_date).days elif unit == 'ym': return relativedelta(end_date, start_date).months elif unit == 'yd': return (end_date - start_date.replace(year=end_date.year)).days raise ValueError("Invalid unit value: %s" % unit)
def evaluate(self, runner, run, context, text): try: date = conversions.to_date(text, context) if self.evaluate_for_date(runner, context, date): return Test.Result.match(date) except EvaluationError: pass return Test.Result.NO_MATCH
def evaluate_for_date(self, runner, context, date): test, errors = runner.substitute_variables(self.test, context) if not errors: try: test_val = conversions.to_date(test, context) return self.do_comparison(date, test_val) except Exception: pass return False
def datevalue(ctx, text): """ Converts date stored in text to an actual date """ return conversions.to_date(text, ctx)