Exemplo n.º 1
0
 def test_div(self):
     self.assertEqual(relativedelta(years=+3, bdays=-9) / 3,
                      relativedelta(years=+1, bdays=-3))
     self.assertEqual(relativedelta(years=+3, bdays=-9) / -3,
                      relativedelta(years=-1, bdays=+3))
     self.assertRaises(ZeroDivisionError,
                       lambda: relativedelta(bdays=-3) / 0)
Exemplo n.º 2
0
 def testMonthsOfDiffNumOfDays(self):
     self.assertEqual(date(2003, 1, 27)+relativedelta(months=+1),
                      date(2003, 2, 27))
     self.assertEqual(date(2003, 1, 31)+relativedelta(months=+1),
                      date(2003, 2, 28))
     self.assertEqual(date(2003, 1, 31)+relativedelta(months=+2),
                      date(2003, 3, 31))
Exemplo n.º 3
0
 def test_div(self):
     self.assertEqual(relativedelta(years=+3, bdays=-9) / 3,
                      relativedelta(years=+1, bdays=-3))
     self.assertEqual(relativedelta(years=+3, bdays=-9) / -3,
                      relativedelta(years=-1, bdays=+3))
     self.assertRaises(ZeroDivisionError,
                       lambda: relativedelta(bdays=-3) / 0)
    def get_start_date(self, past_months):
        """
        Returns the date user wants to start collecting information.
        :param past_months: number of months back user wants to research
        """

        current_date = list(self.stock_dates.keys())[
            0]  # Gets most current day of the stock market

        year = int(current_date[:4])
        month = int(current_date[5:7])
        day = int(current_date[8:10])

        begin_date = date(year, month, day) - relativedelta(months=past_months)

        year = begin_date.year
        month = begin_date.month
        day = begin_date.day

        # Changes single digit to double.
        if len(str(month)) is 1:
            month = "0" + str(month)

        if len(str(day)) is 1:
            day = "0" + str(day)

        # Date changes format.
        start_date = str(year) + "-" + str(month) + "-" + str(day)

        # Checks to make sure start date is a business day, if not moves to next business day.
        if not isbday(start_date, holidays=holidays.US()):
            start_date += relativedelta(bdays=+1, holidays=holidays.US())

        return str(start_date)[:10]
 def testMultiplication(self):
     self.assertEqual(
         datetime(2000, 1, 1) + relativedelta(days=1) * 28,
         datetime(2000, 1, 29))
     self.assertEqual(
         datetime(2000, 1, 1) + 28 * relativedelta(days=1),
         datetime(2000, 1, 29))
    def testComparison(self):
        d1 = relativedelta(years=1,
                           months=1,
                           days=1,
                           leapdays=0,
                           hours=1,
                           minutes=1,
                           seconds=1,
                           microseconds=1)
        d2 = relativedelta(years=1,
                           months=1,
                           days=1,
                           leapdays=0,
                           hours=1,
                           minutes=1,
                           seconds=1,
                           microseconds=1)
        d3 = relativedelta(years=1,
                           months=1,
                           days=1,
                           leapdays=0,
                           hours=1,
                           minutes=1,
                           seconds=1,
                           microseconds=2)

        self.assertEqual(d1, d2)
        self.assertNotEqual(d1, d3)
Exemplo n.º 7
0
    def testRelativeDeltaFractionalNegativeOverflow(self):
        # Equivalent to (days=-1)
        rd1 = relativedelta(days=-0.5, hours=-12)
        self.assertEqual(rd1.normalized(),
            relativedelta(days=-1))

        # Equivalent to (days=-1)
        rd2 = relativedelta(days=-1.5, hours=12)
        self.assertEqual(rd2.normalized(),
            relativedelta(days=-1))

        # Equivalent to (days=-1, hours=-14, minutes=-45)
        rd3 = relativedelta(days=-1.5, hours=-2.5, minutes=-15)
        self.assertEqual(rd3.normalized(),
            relativedelta(days=-1, hours=-14, minutes=-45))

        # Equivalent to (days=-1, hours=-14, minutes=+15)
        rd4 = relativedelta(days=-1.5, hours=-2.5, minutes=45)
        self.assertEqual(rd4.normalized(),
            relativedelta(days=-1, hours=-14, minutes=+15))

        # Carry back up - equivalent to:
        # (days=-2, hours=-2, minutes=0, seconds=-2, microseconds=-3)
        rd3 = relativedelta(days=-1.5, hours=-13, minutes=-59.50045,
                            seconds=-31.473, microseconds=-500003)
        self.assertEqual(rd3.normalized(),
            relativedelta(days=-2, hours=-2, minutes=0,
                          seconds=-2, microseconds=-3))
 def testMonthsOfDiffNumOfDays(self):
     self.assertEqual(
         date(2003, 1, 27) + relativedelta(months=+1), date(2003, 2, 27))
     self.assertEqual(
         date(2003, 1, 31) + relativedelta(months=+1), date(2003, 2, 28))
     self.assertEqual(
         date(2003, 1, 31) + relativedelta(months=+2), date(2003, 3, 31))
Exemplo n.º 9
0
 def test_sub(self):
     rd1 = relativedelta(years=+1,
                         months=+2,
                         bdays=+3,
                         days=+4,
                         bhours=+5,
                         bminutes=+6,
                         bseconds=+7,
                         hours=+8,
                         minutes=+9,
                         seconds=+10)
     rd2 = relativedelta(years=+10,
                         months=-9,
                         bdays=+8,
                         days=-7,
                         bhours=+6,
                         bminutes=-5,
                         bseconds=+4,
                         hours=-3,
                         minutes=+2,
                         seconds=-1)
     rd3 = relativedelta(years=-9,
                         months=+11,
                         bdays=-5,
                         days=+11,
                         bhours=-1,
                         bminutes=+11,
                         bseconds=+3,
                         hours=+11,
                         minutes=+7,
                         seconds=+11)
     self.assertEqual(rd1 - rd2, rd3)
Exemplo n.º 10
0
    def testRelativeDeltaFractionalNegativeOverflow(self):
        # Equivalent to (days=-1)
        rd1 = relativedelta(days=-0.5, hours=-12)
        self.assertEqual(rd1.normalized(), relativedelta(days=-1))

        # Equivalent to (days=-1)
        rd2 = relativedelta(days=-1.5, hours=12)
        self.assertEqual(rd2.normalized(), relativedelta(days=-1))

        # Equivalent to (days=-1, hours=-14, minutes=-45)
        rd3 = relativedelta(days=-1.5, hours=-2.5, minutes=-15)
        self.assertEqual(rd3.normalized(),
                         relativedelta(days=-1, hours=-14, minutes=-45))

        # Equivalent to (days=-1, hours=-14, minutes=+15)
        rd4 = relativedelta(days=-1.5, hours=-2.5, minutes=45)
        self.assertEqual(rd4.normalized(),
                         relativedelta(days=-1, hours=-14, minutes=+15))

        # Carry back up - equivalent to:
        # (days=-2, hours=-2, minutes=0, seconds=-2, microseconds=-3)
        rd3 = relativedelta(days=-1.5,
                            hours=-13,
                            minutes=-59.50045,
                            seconds=-31.473,
                            microseconds=-500003)
        self.assertEqual(
            rd3.normalized(),
            relativedelta(days=-2,
                          hours=-2,
                          minutes=0,
                          seconds=-2,
                          microseconds=-3))
Exemplo n.º 11
0
    def testRelativeDeltaFractionalRepr(self):
        rd = relativedelta(years=3, months=-2, days=1.25)

        self.assertEqual(repr(rd),
                         'relativedelta(years=+3, months=-2, days=+1.25)')

        rd = relativedelta(hours=0.5, seconds=9.22)
        self.assertEqual(repr(rd), 'relativedelta(hours=+0.5, seconds=+9.22)')
Exemplo n.º 12
0
    def testRelativeDeltaFractionalDays(self):
        rd1 = relativedelta(days=1.48)

        d1 = datetime(2009, 9, 3, 0, 0)
        self.assertEqual(d1 + rd1, datetime(2009, 9, 4, 11, 31, 12))

        rd2 = relativedelta(days=1.5)
        self.assertEqual(d1 + rd2, datetime(2009, 9, 4, 12, 0, 0))
Exemplo n.º 13
0
    def testMonthEndMonthBeginningLeapYear(self):
        self.assertEqual(relativedelta(datetime(2012, 1, 31, 23, 59, 59),
                                       datetime(2012, 3, 1, 0, 0, 0)),
                         relativedelta(months=-1, seconds=-1))

        self.assertEqual(relativedelta(datetime(2003, 3, 1, 0, 0, 0),
                                       datetime(2003, 1, 31, 23, 59, 59)),
                         relativedelta(months=1, seconds=1))
Exemplo n.º 14
0
    def testRelativeDeltaRepr(self):
        self.assertEqual(repr(relativedelta(years=1, months=-1, days=15)),
                         'relativedelta(years=+1, months=-1, days=+15)')

        self.assertEqual(repr(relativedelta(months=14, seconds=-25)),
                         'relativedelta(years=+1, months=+2, seconds=-25)')

        self.assertEqual(repr(relativedelta(month=3, hour=3, weekday=SU(3))),
                         'relativedelta(month=3, weekday=SU(+3), hour=3)')
Exemplo n.º 15
0
    def testRelativeDeltaFractionalNegativeDays(self):
        # Equivalent to (days=-1, hours=-1)
        rd1 = relativedelta(days=-1.5, hours=11)
        d1 = datetime(2009, 9, 3, 12, 0)
        self.assertEqual(d1 + rd1, datetime(2009, 9, 2, 11, 0, 0))

        # Equivalent to (days=-1, hours=-9)
        rd2 = relativedelta(days=-1.25, hours=-3)
        self.assertEqual(d1 + rd2, datetime(2009, 9, 2, 3))
Exemplo n.º 16
0
 def test_radd(self):
     self.assertEqual(date(2014, 1, 3) + relativedelta(bdays=2),
                      date(2014, 1, 7))
     self.assertEqual(date(2014, 1, 7) + relativedelta(bdays=-2),
                      date(2014, 1, 3))
     self.assertEqual(date(2014, 2, 3) + relativedelta(bdays=-19),
                      date(2014, 1, 7))
     self.assertEqual(date(2014, 1, 3) + relativedelta(bdays=1.5),
                      datetime(2014, 1, 6, 13, 0))
Exemplo n.º 17
0
 def test_bdays_zero(self):
     self.assertEqual(date("2014-11-15") + relativedelta(bdays=0),
                      date(2014, 11, 17))
     self.assertEqual(date("2014-11-17") + relativedelta(bdays=0),
                      date(2014, 11, 17))
     self.assertEqual(date("2014-11-15") - relativedelta(bdays=0),
                      date(2014, 11, 14))
     self.assertEqual(date("2014-11-14") - relativedelta(bdays=0),
                      date(2014, 11, 14))
Exemplo n.º 18
0
 def test_bdays_zero(self):
     self.assertEqual(date("2014-11-15") + relativedelta(bdays=0),
                      date(2014, 11, 17))
     self.assertEqual(date("2014-11-17") + relativedelta(bdays=0),
                      date(2014, 11, 17))
     self.assertEqual(date("2014-11-15") - relativedelta(bdays=0),
                      date(2014, 11, 14))
     self.assertEqual(date("2014-11-14") - relativedelta(bdays=0),
                      date(2014, 11, 14))
Exemplo n.º 19
0
 def testNonLeapYearDay(self):
     self.assertEqual(
         date(2003, 1, 1) + relativedelta(nlyearday=260), date(2003, 9, 17))
     self.assertEqual(
         date(2002, 1, 1) + relativedelta(nlyearday=260), date(2002, 9, 17))
     self.assertEqual(
         date(2000, 1, 1) + relativedelta(nlyearday=260), date(2000, 9, 17))
     self.assertEqual(self.today + relativedelta(yearday=261),
                      date(2003, 9, 18))
Exemplo n.º 20
0
    def testRelativeDeltaRepr(self):
        self.assertEqual(repr(relativedelta(years=1, months=-1, days=15)),
                         'relativedelta(years=+1, months=-1, days=+15)')

        self.assertEqual(repr(relativedelta(months=14, seconds=-25)),
                         'relativedelta(years=+1, months=+2, seconds=-25)')

        self.assertEqual(repr(relativedelta(month=3, hour=3, weekday=SU(3))),
                         'relativedelta(month=3, weekday=SU(+3), hour=3)')
Exemplo n.º 21
0
 def testNonLeapYearDay(self):
     self.assertEqual(date(2003, 1, 1)+relativedelta(nlyearday=260),
                      date(2003, 9, 17))
     self.assertEqual(date(2002, 1, 1)+relativedelta(nlyearday=260),
                      date(2002, 9, 17))
     self.assertEqual(date(2000, 1, 1)+relativedelta(nlyearday=260),
                      date(2000, 9, 17))
     self.assertEqual(self.today+relativedelta(yearday=261),
                      date(2003, 9, 18))
Exemplo n.º 22
0
 def test_radd(self):
     self.assertEqual(date(2014, 1, 3) + relativedelta(bdays=2),
                      date(2014, 1, 7))
     self.assertEqual(date(2014, 1, 7) + relativedelta(bdays=-2),
                      date(2014, 1, 3))
     self.assertEqual(date(2014, 2, 3) + relativedelta(bdays=-19),
                      date(2014, 1, 7))
     self.assertEqual(date(2014, 1, 3) + relativedelta(bdays=1.5),
                      datetime(2014, 1, 6, 13, 0))
Exemplo n.º 23
0
    def testRelativeDeltaFractionalRepr(self):
        rd = relativedelta(years=3, months=-2, days=1.25)

        self.assertEqual(repr(rd),
                         'relativedelta(years=+3, months=-2, days=+1.25)')

        rd = relativedelta(hours=0.5, seconds=9.22)
        self.assertEqual(repr(rd),
                         'relativedelta(hours=+0.5, seconds=+9.22)')
Exemplo n.º 24
0
def get_duration_in_weeks(duration):
    ''' get start, end date for a week '''

    today = get_today()
    no_of_days = duration * 5 if duration > 0 else 2
    end_day = today + relativedelta(bdays=-1, holidays=holidays.US())
    start_day = today + relativedelta(bdays=-1 * no_of_days,
                                      holidays=holidays.US())
    return (str(start_day), str(end_day))
Exemplo n.º 25
0
 def testAddition(self):
     self.assertEqual(
         relativedelta(days=10) + relativedelta(
             years=1, months=2, days=3, hours=4, minutes=5, microseconds=6),
         relativedelta(years=1,
                       months=2,
                       days=13,
                       hours=4,
                       minutes=5,
                       microseconds=6))
Exemplo n.º 26
0
    def testComparison(self):
        d1 = relativedelta(years=1, months=1, days=1, leapdays=0, hours=1,
                           minutes=1, seconds=1, microseconds=1)
        d2 = relativedelta(years=1, months=1, days=1, leapdays=0, hours=1,
                           minutes=1, seconds=1, microseconds=1)
        d3 = relativedelta(years=1, months=1, days=1, leapdays=0, hours=1,
                           minutes=1, seconds=1, microseconds=2)

        self.assertEqual(d1, d2)
        self.assertNotEqual(d1, d3)
Exemplo n.º 27
0
 def testSubtraction(self):
     self.assertEqual(
         relativedelta(days=10) - relativedelta(
             years=1, months=2, days=3, hours=4, minutes=5, microseconds=6),
         relativedelta(years=-1,
                       months=-2,
                       days=7,
                       hours=-4,
                       minutes=-5,
                       microseconds=-6))
Exemplo n.º 28
0
    def testRelativeDeltaFractionalDays(self):
        rd1 = relativedelta(days=1.48)

        d1 = datetime(2009, 9, 3, 0, 0)
        self.assertEqual(d1 + rd1,
                         datetime(2009, 9, 4, 11, 31, 12))

        rd2 = relativedelta(days=1.5)
        self.assertEqual(d1 + rd2,
                         datetime(2009, 9, 4, 12, 0, 0))
Exemplo n.º 29
0
    def testMonthEndMonthBeginningLeapYear(self):
        self.assertEqual(
            relativedelta(datetime(2012, 1, 31, 23, 59, 59),
                          datetime(2012, 3, 1, 0, 0, 0)),
            relativedelta(months=-1, seconds=-1))

        self.assertEqual(
            relativedelta(datetime(2003, 3, 1, 0, 0, 0),
                          datetime(2003, 1, 31, 23, 59, 59)),
            relativedelta(months=1, seconds=1))
Exemplo n.º 30
0
 def testMillenniumAge(self):
     self.assertEqual(
         relativedelta(self.now, date(2001, 1, 1)),
         relativedelta(years=+2,
                       months=+8,
                       days=+16,
                       hours=+20,
                       minutes=+54,
                       seconds=+47,
                       microseconds=+282310))
Exemplo n.º 31
0
 def testJohnAge(self):
     self.assertEqual(
         relativedelta(self.now, datetime(1978, 4, 5, 12, 0)),
         relativedelta(years=+25,
                       months=+5,
                       days=+12,
                       hours=+8,
                       minutes=+54,
                       seconds=+47,
                       microseconds=+282310))
Exemplo n.º 32
0
    def days_from_today(self, period, shift):
        if period == 'days':
            next_dates = self.today + bd.relativedelta(days=+shift)
            check = calendar.day_name[next_dates.weekday()]
            if check == 'Sunday':
                next_dates = next_dates + bd.relativedelta(days=+1)
            if check == 'Saturday':
                next_dates = next_dates + bd.relativedelta(
                    days=+2
                )  #TODO tak odliczanie od piatku tez nie dziala trzeba zrobic jak w mainie w przypadku days

        if period == 'months':
            next_dates = self.today + bd.relativedelta(months=+shift)
            check = calendar.day_name[next_dates.weekday()]
            if check == 'Sunday':
                next_dates = next_dates + bd.relativedelta(days=+1)
            if check == 'Saturday':
                next_dates = next_dates + bd.relativedelta(days=+2)
        if period == 'years':
            next_dates = self.today + bd.relativedelta(years=+shift)
            check = calendar.day_name[next_dates.weekday()]
            if check == 'Sunday':
                next_dates = next_dates + bd.relativedelta(days=+1)
            if check == 'Saturday':
                next_dates = next_dates + bd.relativedelta(days=+2)

        delta = next_dates.toordinal() - self.today.toordinal()
        return delta
 def year_fraction_fun(self, shift, period):
     if period == 'days':
         next_dates = self.today + bd.relativedelta(days=+shift)
         check = calendar.day_name[next_dates.weekday()]
         if check == 'Sunday':
             next_dates = next_dates + bd.relativedelta(days=+1)
         if check == 'Saturday':
             next_dates = next_dates + bd.relativedelta(days=+2)
     if period == 'months':
         next_dates = self.today + bd.relativedelta(months=+shift)
         check = calendar.day_name[next_dates.weekday()]
         if check == 'Sunday':
             next_dates = next_dates + bd.relativedelta(days=+1)
         if check == 'Saturday':
             next_dates = next_dates + bd.relativedelta(days=+2)
     if period == 'years':
         next_dates = self.today + bd.relativedelta(years=+shift)
         check = calendar.day_name[next_dates.weekday()]
         if check == 'Sunday':
             next_dates = next_dates + bd.relativedelta(days=+1)
         if check == 'Saturday':
             next_dates = next_dates + bd.relativedelta(days=+2)
     if self.day_convention == 'Actual/365':
         delta = (next_dates.toordinal() - self.today.toordinal()) / 365
     if self.day_convention == 'Actual/360':
         delta = (next_dates.toordinal() - self.today.toordinal()) / 360
     if self.day_convention == '30/360':
         delta = (max(30 - self.today.day, 0) + min(next_dates.day, 30) + 360 * (
                 next_dates.year - self.today.year) + 30 * (
                          next_dates.month - self.today.month - 1)) / 360
     return delta
    def days_from_today(self):
        if self.period == 'days':
            next_dates = self.today + bd.relativedelta(days=+self.shift)
            check = calendar.day_name[next_dates.weekday()]
            if check == 'Sunday':
                next_dates = next_dates + bd.relativedelta(days=+1)
            if check == 'Saturday':
                next_dates = next_dates + bd.relativedelta(days=+2)
        if self.period == 'months':
            next_dates = self.today + bd.relativedelta(months=+self.shift)
            check = calendar.day_name[next_dates.weekday()]
            if check == 'Sunday':
                next_dates = next_dates + bd.relativedelta(days=+1)
            if check == 'Saturday':
                next_dates = next_dates + bd.relativedelta(days=+2)
        if self.period == 'years':
            next_dates = self.today + bd.relativedelta(years=+self.shift)
            check = calendar.day_name[next_dates.weekday()]
            if check == 'Sunday':
                next_dates = next_dates + bd.relativedelta(days=+1)
            if check == 'Saturday':
                next_dates = next_dates + bd.relativedelta(days=+2)

        delta = next_dates.toordinal() - self.today.toordinal()
        return delta
Exemplo n.º 35
0
 def test_init(self):
     self.assertEqual(relativedelta(date(2014, 1, 7), date(2014, 1, 3)),
                      relativedelta(days=4, bdays=2))
     self.assertEqual(relativedelta(date(2014, 1, 31), date(2014, 1, 1)),
                      relativedelta(days=30, bdays=22))
     self.assertEqual(relativedelta(date(2014, 2, 1), date(2014, 1, 1)),
                      relativedelta(months=1, bdays=23))
     self.assertEqual(relativedelta(date(2014, 2, 2), date(2014, 1, 1)),
                      relativedelta(months=1, days=1, bdays=23))
     self.assertEqual(relativedelta(date(2014, 1, 1), date(2014, 2, 2)),
                      relativedelta(months=-1, days=-1, bdays=-23))
Exemplo n.º 36
0
 def test_sub(self):
     rd1 = relativedelta(years=+1, months=+2, bdays=+3, days=+4,
                         bhours=+5, bminutes=+6, bseconds=+7,
                         hours=+8, minutes=+9, seconds=+10)
     rd2 = relativedelta(years=+10, months=-9, bdays=+8, days=-7,
                         bhours=+6, bminutes=-5, bseconds=+4,
                         hours=-3, minutes=+2, seconds=-1)
     rd3 = relativedelta(years=-9, months=+11, bdays=-5, days=+11,
                         bhours=-1, bminutes=+11, bseconds=+3,
                         hours=+11, minutes=+7, seconds=+11)
     self.assertEqual(rd1 - rd2, rd3)
Exemplo n.º 37
0
 def test_init(self):
     self.assertEqual(relativedelta(date(2014, 1, 7), date(2014, 1, 3)),
                      relativedelta(days=4, bdays=2))
     self.assertEqual(relativedelta(date(2014, 1, 31), date(2014, 1, 1)),
                      relativedelta(days=30, bdays=22))
     self.assertEqual(relativedelta(date(2014, 2, 1), date(2014, 1, 1)),
                      relativedelta(months=1, bdays=23))
     self.assertEqual(relativedelta(date(2014, 2, 2), date(2014, 1, 1)),
                      relativedelta(months=1, days=1, bdays=23))
     self.assertEqual(relativedelta(date(2014, 1, 1), date(2014, 2, 2)),
                      relativedelta(months=-1, days=-1, bdays=-23))
Exemplo n.º 38
0
    def testRelativeDeltaNormalizeFractionalDays(self):
        # Equivalent to (hours=1, minutes=30)
        rd1 = relativedelta(hours=1.5)

        self.assertEqual(rd1.normalized(), relativedelta(hours=1, minutes=30))

        # Equivalent to (hours=3, minutes=17, seconds=5, microseconds=100)
        rd2 = relativedelta(hours=3.28472225)

        self.assertEqual(rd2.normalized(),
            relativedelta(hours=3, minutes=17, seconds=5, microseconds=100))
Exemplo n.º 39
0
    def testRelativeDeltaNormalizeFractionalDays(self):
        # Equivalent to (days=2, hours=18)
        rd1 = relativedelta(days=2.75)

        self.assertEqual(rd1.normalized(), relativedelta(days=2, hours=18))

        # Equvalent to (days=1, hours=11, minutes=31, seconds=12)
        rd2 = relativedelta(days=1.48)

        self.assertEqual(rd2.normalized(),
            relativedelta(days=1, hours=11, minutes=31, seconds=12))
Exemplo n.º 40
0
    def testRelativeDeltaFractionalNegativeDays(self):
        # Equivalent to (days=-1, hours=-1)
        rd1 = relativedelta(days=-1.5, hours=11)
        d1 = datetime(2009, 9, 3, 12, 0)
        self.assertEqual(d1 + rd1,
                         datetime(2009, 9, 2, 11, 0, 0))

        # Equivalent to (days=-1, hours=-9)
        rd2 = relativedelta(days=-1.25, hours=-3)
        self.assertEqual(d1 + rd2,
            datetime(2009, 9, 2, 3))
Exemplo n.º 41
0
 def test_repr(self):
     rd1 = relativedelta(years=+1, months=+2, days=-3)
     self.assertEqual(str(rd1),
                      "relativedelta(years=+1, months=+2, days=-3)")
     rd2 = relativedelta(years=+1, months=+2, bdays=-7)
     self.assertEqual(str(rd2),
                      "relativedelta(years=+1, months=+2, bdays=-7)")
     rd3 = relativedelta(years=-1, months=-2, bdays=+7)
     self.assertEqual(str(rd3),
                      "relativedelta(years=-1, months=-2, bdays=+7)")
     rd4 = relativedelta(year=2014, month=1, day=2)
     self.assertEqual(str(rd4), "relativedelta(year=2014, month=1, day=2)")
Exemplo n.º 42
0
    def testRelativeDeltaNormalizeFractionalDays(self):
        # Equivalent to (days=2, hours=18)
        rd1 = relativedelta(days=2.75)

        self.assertEqual(rd1.normalized(), relativedelta(days=2, hours=18))

        # Equvalent to (days=1, hours=11, minutes=31, seconds=12)
        rd2 = relativedelta(days=1.48)

        self.assertEqual(
            rd2.normalized(),
            relativedelta(days=1, hours=11, minutes=31, seconds=12))
Exemplo n.º 43
0
    def testRelativeDeltaNormalizeFractionalDays(self):
        # Equivalent to (hours=1, minutes=30)
        rd1 = relativedelta(hours=1.5)

        self.assertEqual(rd1.normalized(), relativedelta(hours=1, minutes=30))

        # Equivalent to (hours=3, minutes=17, seconds=5, microseconds=100)
        rd2 = relativedelta(hours=3.28472225)

        self.assertEqual(
            rd2.normalized(),
            relativedelta(hours=3, minutes=17, seconds=5, microseconds=100))
Exemplo n.º 44
0
    def testRelativeDeltaNormalizeFractionalMinutes(self):
        # Equivalent to (minutes=15, seconds=36)
        rd1 = relativedelta(minutes=15.6)

        self.assertEqual(rd1.normalized(),
            relativedelta(minutes=15, seconds=36))

        # Equivalent to (minutes=25, seconds=20, microseconds=25000)
        rd2 = relativedelta(minutes=25.33375)

        self.assertEqual(rd2.normalized(),
            relativedelta(minutes=25, seconds=20, microseconds=25000))
Exemplo n.º 45
0
 def test_repr(self):
     rd1 = relativedelta(years=+1, months=+2, days=-3)
     self.assertEqual(str(rd1),
                      "relativedelta(years=+1, months=+2, days=-3)")
     rd2 = relativedelta(years=+1, months=+2, bdays=-7)
     self.assertEqual(str(rd2),
                      "relativedelta(years=+1, months=+2, bdays=-7)")
     rd3 = relativedelta(years=-1, months=-2, bdays=+7)
     self.assertEqual(str(rd3),
                      "relativedelta(years=-1, months=-2, bdays=+7)")
     rd4 = relativedelta(year=2014, month=1, day=2)
     self.assertEqual(str(rd4),
                      "relativedelta(year=2014, month=1, day=2)")
Exemplo n.º 46
0
    def testRelativeDeltaNormalizeFractionalMinutes(self):
        # Equivalent to (minutes=15, seconds=36)
        rd1 = relativedelta(minutes=15.6)

        self.assertEqual(rd1.normalized(), relativedelta(minutes=15,
                                                         seconds=36))

        # Equivalent to (minutes=25, seconds=20, microseconds=25000)
        rd2 = relativedelta(minutes=25.33375)

        self.assertEqual(
            rd2.normalized(),
            relativedelta(minutes=25, seconds=20, microseconds=25000))
Exemplo n.º 47
0
 def test_radd_time(self):
     self.assertEqual("2015-01-02 16:45" + relativedelta(bminutes=+30),
                      datetime(2015, 1, 5, 9, 15))
     self.assertEqual(date(2015, 1, 2) + relativedelta(bminutes=+30),
                      datetime(2015, 1, 2, 9, 30))
     self.assertEqual(date(2014, 1, 3) + relativedelta(bdays=1, bhours=4),
                      datetime(2014, 1, 6, 13, 0))
     relativedelta.btstart = time(7, 30)
     self.assertEqual("2015-01-02 16:45" + relativedelta(bminutes=+30),
                      datetime(2015, 1, 5, 7, 45))
     self.assertEqual("2015-01-02 16:45" + relativedelta(bhours=+0.5),
                      datetime(2015, 1, 5, 7, 45))
     del relativedelta.btstart
Exemplo n.º 48
0
 def test_ne(self):
     r1 = relativedelta(years=1,
                        months=2,
                        days=3,
                        bdays=1,
                        hours=4,
                        minutes=5,
                        seconds=6,
                        microseconds=7)
     r2 = relativedelta(years=1,
                        months=2,
                        days=3,
                        bdays=1,
                        hours=4,
                        minutes=5,
                        seconds=6,
                        microseconds=7)
     self.assertFalse(r1 != r2)
     r2.days = 4
     self.assertTrue(r1 != r2)
     r2.days = 3
     r2.bdays = 0
     self.assertTrue(r1 != r2)
     self.assertFalse(relativedelta() != relativedelta())
     self.assertTrue(relativedelta() != relativedelta(months=1))
     self.assertTrue(relativedelta() != relativedelta(months=1))
Exemplo n.º 49
0
    def testRelativeDeltaFractionalPositiveOverflow(self):
        # Equivalent to (days=1, hours=14)
        rd1 = relativedelta(days=1.5, hours=2)
        d1 = datetime(2009, 9, 3, 0, 0)
        self.assertEqual(d1 + rd1, datetime(2009, 9, 4, 14, 0, 0))

        # Equivalent to (days=1, hours=14, minutes=45)
        rd2 = relativedelta(days=1.5, hours=2.5, minutes=15)
        d1 = datetime(2009, 9, 3, 0, 0)
        self.assertEqual(d1 + rd2, datetime(2009, 9, 4, 14, 45))

        # Carry back up - equivalent to (days=2, hours=2, minutes=0, seconds=1)
        rd3 = relativedelta(days=1.5, hours=13, minutes=59.5, seconds=31)
        self.assertEqual(d1 + rd3, datetime(2009, 9, 5, 2, 0, 1))
Exemplo n.º 50
0
    def testAddTimedeltaToPopulatedRelativeDelta(self):
        td = timedelta(
            days=1,
            seconds=1,
            microseconds=1,
            milliseconds=1,
            minutes=1,
            hours=1,
            weeks=1
        )

        rd = relativedelta(
            year=1,
            month=1,
            day=1,
            hour=1,
            minute=1,
            second=1,
            microsecond=1,
            years=1,
            months=1,
            days=1,
            weeks=1,
            hours=1,
            minutes=1,
            seconds=1,
            microseconds=1
        )

        expected = relativedelta(
            year=1,
            month=1,
            day=1,
            hour=1,
            minute=1,
            second=1,
            microsecond=1,
            years=1,
            months=1,
            weeks=2,
            days=2,
            hours=2,
            minutes=2,
            seconds=2,
            microseconds=1002,
        )

        self.assertEqual(expected, rd + td)
Exemplo n.º 51
0
 def test_WORKDAYS(self):
     self.assertEqual(date(2017, 1, 4) + relativedelta(bdays=3),
                      date(2017, 1, 9))
     bdateutil.WORKDAYS = (0, 1, 2)  # Mon, Tues, Wed
     self.assertEqual(date(2017, 1, 4) + relativedelta(bdays=3),
                      date(2017, 1, 11))
     self.assertEqual(date(2017, 1, 4) + relativedelta(bdays=3,
                                                       workdays=(0,)),
                      date(2017, 1, 30))
     bdateutil.WORKDAYS = range(5)
     self.assertRaises(ValueError,
                       lambda: date(2017, 1, 4) +
                       relativedelta(bdays=3, workdays=()))
     self.assertRaises(ValueError,
                       lambda: date(2017, 1, 4) +
                       relativedelta(bdays=3, workdays=('x', 'y')))
Exemplo n.º 52
0
 def test_radd_time(self):
     self.assertEqual(datetime("2015-01-02 16:45") +
                      relativedelta(bminutes=+30),
                      datetime(2015, 1, 5, 9, 15))
     self.assertEqual(date(2015, 1, 2) + relativedelta(bminutes=+30),
                      datetime(2015, 1, 2, 9, 30))
     self.assertEqual(date(2014, 1, 3) + relativedelta(bdays=1, bhours=4),
                      datetime(2014, 1, 6, 13, 0))
     bdateutil.BTSTART = time(7, 30)
     self.assertEqual(datetime("2015-01-02 16:45") +
                      relativedelta(bminutes=+30),
                      datetime(2015, 1, 5, 7, 45))
     self.assertEqual(datetime("2015-01-02 16:45") +
                      relativedelta(bhours=+0.5),
                      datetime(2015, 1, 5, 7, 45))
     bdateutil.BTSTART = time(9, 0)
Exemplo n.º 53
0
    def testWeeks(self):
        # Test that the weeks property is working properly.
        rd = relativedelta(years=4, months=2, weeks=8, days=6)
        self.assertEqual((rd.weeks, rd.days), (8, 8 * 7 + 6))

        rd.weeks = 3
        self.assertEqual((rd.weeks, rd.days), (3, 3 * 7 + 6))
Exemplo n.º 54
0
    def testInheritance(self):
        # Ensure that relativedelta is inheritance-friendly.
        class rdChildClass(relativedelta):
            pass

        ccRD = rdChildClass(years=1, months=1, days=1, leapdays=1, weeks=1,
                            hours=1, minutes=1, seconds=1, microseconds=1)

        rd = relativedelta(years=1, months=1, days=1, leapdays=1, weeks=1,
                           hours=1, minutes=1, seconds=1, microseconds=1)

        self.assertEqual(type(ccRD + rd), type(ccRD),
                         msg='Addition does not inherit type.')

        self.assertEqual(type(ccRD - rd), type(ccRD),
                         msg='Subtraction does not inherit type.')

        self.assertEqual(type(-ccRD), type(ccRD),
                         msg='Negation does not inherit type.')

        self.assertEqual(type(ccRD * 5.0), type(ccRD)) #,
                         #msg='Multiplication does not inherit type.')

        self.assertEqual(type(ccRD / 5.0), type(ccRD),
                         msg='Division does not inherit type.')
Exemplo n.º 55
0
 def test_datetime(self):
     self.assertEqual(datetime("2015-03-25 12:34"),
                      dt.datetime(2015, 3, 25, 12, 34))
     self.assertEqual(datetime(2015, 3, 99, 23, 45),
                      datetime(2015, 3, 31, 23, 45))
     self.assertEqual(datetime.now().date(), dt.datetime.now().date())
     self.assertEqual(datetime.now(bdays=-45).date(),
                      (dt.datetime.now() - relativedelta(bdays=45)).date())
Exemplo n.º 56
0
 def test_date(self):
     self.assertEqual(date("2015-03-25"), dt.date(2015, 3, 25))
     self.assertEqual(date("1/2/2014"), dt.date(2014, 1, 2))
     self.assertEqual(date(1388577600), dt.date(2014, 1, 1))
     self.assertRaises(ValueError, lambda: date("abc"))
     self.assertRaises(TypeError, lambda: date(['a', 'b', 'c']))
     self.assertEqual(date(2015, 2, 99), date(2015, 2, 28))
     self.assertEqual(date.today(), dt.date.today())
     self.assertEqual(date.today(days=+1),
                      dt.date.today() + relativedelta(days=+1))
     self.assertEqual(date.today(bdays=+200, holidays=holidays.US()),
                      dt.date.today()
                      + relativedelta(bdays=+200, holidays=holidays.US()))
     relativedelta.holidays = holidays.US()
     self.assertEqual(date.today(bdays=+200),
                      dt.date.today() + relativedelta(bdays=+200))
     del relativedelta.holidays
Exemplo n.º 57
0
    def testRelativeDeltaFractionalPositiveOverflow(self):
        # Equivalent to (days=1, hours=14)
        rd1 = relativedelta(days=1.5, hours=2)
        d1 = datetime(2009, 9, 3, 0, 0)
        self.assertEqual(d1 + rd1,
                         datetime(2009, 9, 4, 14, 0, 0))

        # Equivalent to (days=1, hours=14, minutes=45)
        rd2 = relativedelta(days=1.5, hours=2.5, minutes=15)
        d1 = datetime(2009, 9, 3, 0, 0)
        self.assertEqual(d1 + rd2,
                         datetime(2009, 9, 4, 14, 45))

        # Carry back up - equivalent to (days=2, hours=2, minutes=0, seconds=1)
        rd3 = relativedelta(days=1.5, hours=13, minutes=59.5, seconds=31)
        self.assertEqual(d1 + rd3,
                         datetime(2009, 9, 5, 2, 0, 1))
Exemplo n.º 58
0
    def testInequalityWeekdays(self):
        # Different weekdays
        no_wday = relativedelta(year=1997, month=4)
        wday_mo_1 = relativedelta(year=1997, month=4, weekday=MO(+1))
        wday_mo_2 = relativedelta(year=1997, month=4, weekday=MO(+2))
        wday_tu = relativedelta(year=1997, month=4, weekday=TU)

        self.assertTrue(wday_mo_1 == wday_mo_1)

        self.assertFalse(no_wday == wday_mo_1)
        self.assertFalse(wday_mo_1 == no_wday)

        self.assertFalse(wday_mo_1 == wday_mo_2)
        self.assertFalse(wday_mo_2 == wday_mo_1)

        self.assertFalse(wday_mo_1 == wday_tu)
        self.assertFalse(wday_tu == wday_mo_1)
Exemplo n.º 59
0
 def test_BTSTART_BTEND(self):
     self.assertEqual(time(16, 30) + relativedelta(bminutes=60),
                      time(9, 30))
     bdateutil.BTSTART = time(10, 30)
     bdateutil.BTEND = time(16, 45)
     self.assertEqual(time(16, 30) + relativedelta(bminutes=60),
                      time(11, 15))
     self.assertEqual(time(16, 30) + relativedelta(bminutes=60,
                                                   btstart=time(11, 30)),
                      time(12, 15))
     bdateutil.BTEND = time(8, 0)
     self.assertRaises(ValueError,
                       lambda: time(16, 30) + relativedelta(bminutes=60))
     bdateutil.BTEND = None
     self.assertRaises(TypeError,
                       lambda: time(16, 30) + relativedelta(bminutes=60))
     bdateutil.BTSTART = time(9, 0)
     bdateutil.BTEND = time(17, 0)
Exemplo n.º 60
0
 def test_datetime(self):
     self.assertEqual(datetime("2015-03-25 12:34"),
                      dt.datetime(2015, 3, 25, 12, 34))
     self.assertEqual(datetime(2015, 3, 15, 23, 45).month_end(),
                      datetime(2015, 3, 31, 23, 59, 59, 999999))
     self.assertEqual(datetime.now().date(), dt.datetime.now().date())
     self.assertEqual(datetime.now(bdays=-45).date(),
                      (dt.datetime.now() - relativedelta(bdays=45)).date())
     self.assertEqual(datetime(time("3:40")),
                      dt.datetime.combine(dt.datetime.today(),
                                          dt.time(3, 40, 0)))