def __getitem__(self, name): # We've got a complex getitem implementaiton because we want to suport crazy date arithmetic syntax for # the run time of the action. # This allows features like running a job with an argument that is the previous day by doing something like # ./my_job --run-date=%(shortdate-1)s run_time = self.action_run.run_time match = re.match(r'([\w]+)([+-]*)(\d*)', name) attr, op, value = match.groups() if attr == "shortdate": if value: delta = datetime.timedelta(days=int(value)) if op == "-": delta *= -1 run_date = run_time + delta else: run_date = run_time return "%.4d-%.2d-%.2d" % (run_date.year, run_date.month, run_date.day) elif attr == "unixtime": delta = 0 if value: delta = int(value) if op == "-": delta *= -1 return int(timeutils.to_timestamp(run_time)) + delta elif attr == "daynumber": delta = 0 if value: delta = int(value) if op == "-": delta *= -1 return run_time.toordinal() + delta else: raise KeyError(name)
def __getitem__(self, name): # We've got a complex getitem implementaiton because we want to suport # crazy date arithmetic syntax for the run time of the action. # This allows features like running a job with an argument that is the # previous day by doing something like # ./my_job --run-date=%(shortdate-1)s run_time = self.action_run.run_time match = re.match(r'([\w]+)([+-]*)(\d*)', name) attr, op, value = match.groups() if attr in ("shortdate", "year", "month", "day"): if value: int_value = int(value) if op == '-': int_value = -int_value if attr == "year": delta = timeutils.macro_timedelta(run_time, years=int_value) elif attr == "month": delta = timeutils.macro_timedelta(run_time, months=int_value) else: delta = timeutils.macro_timedelta(run_time, days=int_value) run_date = run_time + delta else: run_date = run_time if attr == "year": return run_date.strftime("%Y") elif attr == "month": return run_date.strftime("%m") elif attr == "day": return run_date.strftime("%d") else: return run_date.strftime("%Y-%m-%d") elif attr == "unixtime": delta = 0 if value: delta = int(value) if op == "-": delta *= -1 return int(timeutils.to_timestamp(run_time)) + delta elif attr == "daynumber": delta = 0 if value: delta = int(value) if op == "-": delta *= -1 return run_time.toordinal() + delta else: raise KeyError(name)
def test_unixtime_minus(self): timestamp = int(timeutils.to_timestamp(self.now)) - 99 assert_equal(DateArithmetic.parse('unixtime-99'), timestamp)
def test_unixtime_plus(self): timestamp = int(timeutils.to_timestamp(self.now)) + 100 assert_equal(DateArithmetic.parse('unixtime+100'), timestamp)
def test_unixtime(self): timestamp = int(timeutils.to_timestamp(self.now)) assert_equal(DateArithmetic.parse('unixtime'), timestamp)
def test_unixtime_minus(self): self.action.command = "somescript -t %(unixtime-100)s" timestamp = int(timeutils.to_timestamp(self.now)) - 100 assert_equal(self._cmd(), "somescript -t %d" % timestamp)