Example #1
0
    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)
Example #2
0
 def check_delta(self, start, target, years=0, months=0, days=0):
     assert_equal(
         start + macro_timedelta(
             start,
             years=years,
             months=months,
             days=days,
         ),
         target,
     )
Example #3
0
 def check_delta(self, start, target, years=0, months=0, days=0):
     assert_equal(
         start + macro_timedelta(
             start,
             years=years,
             months=months,
             days=days,
         ),
         target,
     )
Example #4
0
 def test_year_minus(self):
     for i in range(50):
         dt = self.now - timeutils.macro_timedelta(self.now, years=i)
         self._cmp_year('year-%s' % i, dt)
Example #5
0
 def test_month_minus(self):
     for i in range(50):
         dt = self.now - timeutils.macro_timedelta(self.now, months=i)
         self._cmp_month('month-%s' % i, dt)
Example #6
0
 def test_year_plus(self):
     for i in xrange(50):
         dt = self.now + timeutils.macro_timedelta(self.now, years=i)
         self._cmp_year('year+%s' % i, dt)
Example #7
0
 def test_month_plus(self):
     for i in xrange(50):
         dt = self.now + timeutils.macro_timedelta(self.now, months=i)
         self._cmp_month('month+%s' % i, dt)
Example #8
0
 def test_year_minus(self):
     for i in xrange(50):
         dt = self.now - timeutils.macro_timedelta(self.now, years=i)
         self._cmp_year('year-%s' % i, dt)
Example #9
0
 def test_month_minus(self):
     for i in xrange(50):
         dt = self.now - timeutils.macro_timedelta(self.now, months=i)
         self._cmp_month('month-%s' % i, dt)
Example #10
0
 def test_year_plus(self):
     for i in range(50):
         dt = self.now + timeutils.macro_timedelta(self.now, years=i)
         self._cmp_year('year+%s' % i, dt)
Example #11
0
 def test_month_plus(self):
     for i in range(50):
         dt = self.now + timeutils.macro_timedelta(self.now, months=i)
         self._cmp_month('month+%s' % i, dt)
Example #12
0
 def test_year_minus(self):
     self.action.command = "somescript -d %(year-1)s"
     ystr = self.now + timeutils.macro_timedelta(self.now, years=-1)
     assert_equal(self._cmd(), "somescript -d %.4d" % (ystr.year))
Example #13
0
 def test_year_plus(self):
     self.action.command = "somescript -d %(year+1)s"
     tmrw = self.now + timeutils.macro_timedelta(self.now, years=1)
     assert_equal(self._cmd(), "somescript -d %.4d" % (tmrw.year))
Example #14
0
 def test_month_minus(self):
     self.action.command = "somescript -d %(month-1)s"
     ystr = self.now + timeutils.macro_timedelta(self.now, months=-1)
     assert_equal(self._cmd(), "somescript -d %.2d" % (ystr.month))
Example #15
0
 def test_month_plus(self):
     self.action.command = "somescript -d %(month+1)s"
     tmrw = self.now + timeutils.macro_timedelta(self.now, months=1)
     assert_equal(self._cmd(), "somescript -d %.2d" % (tmrw.month))
Example #16
0
 def test_round_day(self):
     start = datetime.datetime(2019, 3, 30)
     delta = timeutils.macro_timedelta(start, months=-1)
     assert (start + delta).day == 28
Example #17
0
 def test_round_day(self):
     start = datetime.datetime(2019, 3, 30)
     delta = timeutils.macro_timedelta(start, months=-1)
     assert (start + delta).day == 28