def testBasicFunctions(self):

        testDate = Date(2015, 7, 11)
        cal = Calendar('China.SSE')
        self.assertTrue(cal.isWeekEnd(testDate.weekday()),
                        "{0} is expected to be a weekend".format(testDate))
        testDate = Date(2015, 7, 13)
        self.assertTrue(not cal.isWeekEnd(testDate.weekday()),
                        "{0} is expected not to be a weekend".format(testDate))

        testDate = Date(2015, 5, 29)
        cal = Calendar('China.SSE')
        self.assertTrue(
            cal.isEndOfMonth(testDate),
            "{0} is expected to be a end of month".format(testDate))

        testDate = Date(2015, 5, 1)
        cal = Calendar('China.SSE')
        endOfMonth = cal.endOfMonth(testDate)
        self.assertEqual(
            endOfMonth, Date(2015, 5, 29),
            "The month end of 2015/5 is expected to be {0}".format(
                Date(2015, 5, 29)))

        bizDates1 = cal.bizDaysBetween(Date(2015, 1, 1), Date(2015, 12, 31),
                                       True, False)
        bizDates2 = cal.bizDaysBetween(Date(2015, 12, 31), Date(2015, 1, 1),
                                       False, True)
        self.assertEqual(bizDates1, bizDates2)
    def testParseDates(self):
        input_date = "2006-01-15"
        d = Date.strptime(input_date, "%Y-%m-%d")
        flag = d == Date(2006, 1, 15)

        self.assertTrue(
            flag, "date parsing failed\n"
            " input date:    {0:s}\n"
            " parsed:        {1}".format(input_date, d))

        input_date = "12/02/2012"
        d = Date.strptime(input_date, "%m/%d/%Y")
        flag = d == Date(2012, 12, 2)

        self.assertTrue(
            flag, "date parsing failed\n"
            " input date:    {0:s}\n"
            " parsed:        {1}".format(input_date, d))

        d = Date.strptime(input_date, "%d/%m/%Y")
        flag = d == Date(2012, 2, 12)

        self.assertTrue(
            flag, "date parsing failed\n"
            " input date:    {0:s}\n"
            " parsed:        {1}".format(input_date, d))

        input_date = "20011002"
        d = Date.strptime(input_date, "%Y%m%d")
        flag = d == Date(2001, 10, 2)

        self.assertTrue(
            flag, "date parsing failed\n"
            " input date:    {0:s}\n"
            " parsed:        {1}".format(input_date, d))
    def testDateAdvanceOutOfBounds(self):
        testDate = Date(2199, 12, 30)
        with self.assertRaises(ValueError):
            _ = testDate + '1w'

        testDate = Date(1901, 1, 1)
        with self.assertRaises(ValueError):
            _ = testDate - '1w'
 def testScheduleInitializeWithYearly(self):
     startDate = Date(2012, 2, 29)
     endDate = Date(2013, 3, 1)
     tenor = Period('1y')
     cal = Calendar('NullCalendar')
     sch = Schedule(startDate, endDate, tenor, cal)
     expected = [Date(2012, 2, 29), Date(2013, 2, 28), Date(2013, 3, 1)]
     for i in range(sch.size()):
         self.assertEqual(expected[i], sch[i])
    def testScheduleDeepCopy(self):
        startDate = Date(2013, 3, 31)
        endDate = Date(2013, 6, 30)
        tenor = Period('1m')
        cal = Calendar('NullCalendar')
        sch = Schedule(startDate, endDate, tenor, cal)
        copied_sch = copy.deepcopy(sch)

        self.assertEqual(sch, copied_sch)
    def testAdvanceDate(self):
        referenceDate = Date(2014, 1, 31)
        sseCal = Calendar('China.SSE')
        ibCal = Calendar('China.IB')

        bizDayConv = BizDayConventions.Following

        # test null period
        self.assertEqual(sseCal.advanceDate(referenceDate, '0b', bizDayConv),
                         Date(2014, 2, 7))

        # test negative period
        self.assertEqual(sseCal.advanceDate(referenceDate, '-5b', bizDayConv),
                         Date(2014, 1, 24))

        # The difference is caused by Feb 8 is SSE holiday but a working day for IB market
        self.assertEqual(sseCal.advanceDate(referenceDate, '2b', bizDayConv),
                         Date(2014, 2, 10))
        self.assertEqual(sseCal.advanceDate(referenceDate, '2d', bizDayConv),
                         Date(2014, 2, 7))
        self.assertEqual(ibCal.advanceDate(referenceDate, '2b', bizDayConv),
                         Date(2014, 2, 8))
        self.assertEqual(ibCal.advanceDate(referenceDate, '2d', bizDayConv),
                         Date(2014, 2, 7))

        bizDayConv = BizDayConventions.ModifiedFollowing
        # May 31, 2014 is a holiday
        self.assertEqual(
            sseCal.advanceDate(referenceDate, '4m', bizDayConv, True),
            Date(2014, 5, 30))
    def testSchedulePickle(self):
        startDate = Date(2013, 3, 31)
        endDate = Date(2013, 6, 30)
        tenor = Period('1m')
        cal = Calendar('NullCalendar')
        sch = Schedule(startDate, endDate, tenor, cal)

        f = tempfile.NamedTemporaryFile('w+b', delete=False)
        pickle.dump(sch, f)
        f.close()

        with open(f.name, 'rb') as f2:
            pickled_sch = pickle.load(f2)
            self.assertEqual(sch, pickled_sch)

        os.unlink(f.name)
    def testEvaluationDate(self):
        referenceDate = Date(2015, 1, 1)
        Settings.evaluationDate = referenceDate
        self.assertEqual(
            Settings.evaluationDate, referenceDate,
            "Evaluation Date should be \n"
            "expected:   {0}\n"
            "calculated: {1}".format(referenceDate, Settings.evaluationDate))

        # check wrong input
        with self.assertRaises(ValueError):
            Settings.evaluationDate = 2

        # settings should be a singleton by default
        secondeSettings = SettingsFactory()
        self.assertEqual(
            Settings.evaluationDate, secondeSettings.evaluationDate,
            "Settings should be a singleton\n"
            "expected:   {0}\n"
            "calculated: {1}".format(Settings.evaluationDate,
                                     secondeSettings.evaluationDate))

        # test forced building
        newSettings = SettingsFactory(forcedBuild=True)
        self.assertEqual(
            newSettings.evaluationDate, Date.todaysDate(),
            "Forced built settings should be a fresh object\n"
            "expected:   {0}\n"
            "calculated: {1}".format(Date.todaysDate(),
                                     newSettings.evaluationDate))
def date_stamps(start_date, end_date):
    start_date = Date.fromDateTime(start_date)
    end_date = Date.fromDateTime(end_date)

    start_year = start_date.year()
    start_month = start_date.month()

    if start_month <= 3:
        start_month = 3
    elif start_month <= 6:
        start_month = 6
    elif start_month <= 9:
        start_month = 9
    else:
        start_month = 12

    stamps = []

    start_point = Date(start_year, start_month, 1)
    start_point = Date.endOfMonth(start_point)

    while start_point <= end_date:
        stamps.append(start_point.toDateTime().strftime('%Y%m%d'))
        start_point = Date.endOfMonth(start_point + '3m')

    return stamps
    def testNullCalendar(self):
        cal = Calendar("Null")

        testDate = Date(2015, 1, 1)
        self.assertTrue(cal.isBizDay(testDate))
        self.assertTrue(not cal.isHoliday(testDate))
        self.assertTrue(cal.isWeekEnd(Weekdays.Saturday))
        self.assertTrue(cal.isWeekEnd(Weekdays.Sunday))
        self.assertTrue(not cal.isWeekEnd(Weekdays.Friday))
    def testAdjustDate(self):
        # April 30, 2005 is a working day under IB, but a holiday under SSE
        referenceDate = Date(2005, Months.April, 30)

        sseCal = Calendar('China.SSE')
        ibCal = Calendar('China.IB')

        bizDayConv = BizDayConventions.Unadjusted
        self.assertEqual(sseCal.adjustDate(referenceDate, bizDayConv), referenceDate)
        self.assertEqual(ibCal.adjustDate(referenceDate, bizDayConv), referenceDate)

        bizDayConv = BizDayConventions.Following
        self.assertEqual(sseCal.adjustDate(referenceDate, bizDayConv), Date(2005, Months.May, 9))
        self.assertEqual(ibCal.adjustDate(referenceDate, bizDayConv), Date(2005, Months.April, 30))

        bizDayConv = BizDayConventions.ModifiedFollowing
        self.assertEqual(sseCal.adjustDate(referenceDate, bizDayConv), Date(2005, Months.April, 29))
        self.assertEqual(ibCal.adjustDate(referenceDate, bizDayConv), Date(2005, Months.April, 30))
Exemple #12
0
def get_pos_adj_date(start_date,
                     end_date,
                     formats="%Y-%m-%d",
                     calendar='China.SSE',
                     freq='m',
                     return_biz_day=False):
    """
    :param start_date: str/datetime.datetime, start date of strategy
    :param end_date: str/datetime.datetime, end date of strat egy
    :param formats: optional, formats of the string date
    :param calendar: str, optional, name of the calendar to use in dates math
    :param freq: str, optional, the frequency of data
    :param return_biz_day: bool, optional, if the return dates are biz days
    :return: list of datetime.datetime, pos adjust dates
    """
    if isinstance(start_date, str) and isinstance(end_date, str):
        d_start_date = Date.strptime(start_date, formats)
        d_end_date = Date.strptime(end_date, formats)
    elif isinstance(start_date, datetime.datetime) and isinstance(
            end_date, datetime.datetime):
        d_start_date = Date.fromDateTime(start_date)
        d_end_date = Date.fromDateTime(end_date)

    cal = Calendar(calendar)
    pos_adjust_date = Schedule(d_start_date, d_end_date,
                               Period(length=1, units=_freqDict[freq]), cal,
                               BizDayConventions.Unadjusted)
    # it fails if setting dStartDate to be first adjustment date, then use Schedule to compute the others
    # so i first compute dates list in each period, then compute the last date of each period
    # last day of that period(month) is the pos adjustment date
    if _freqDict[freq] == TimeUnits.Weeks:
        pos_adjust_date = [
            Date.nextWeekday(date, Weekdays.Friday)
            for date in pos_adjust_date[:-1]
        ]
    elif _freqDict[freq] == TimeUnits.Months:
        pos_adjust_date = [
            cal.endOfMonth(date) for date in pos_adjust_date[:-1]
        ]
    elif _freqDict[freq] == TimeUnits.Years:
        pos_adjust_date = [
            Date(date.year(), 12, 31) for date in pos_adjust_date[:-1]
        ]

    if return_biz_day:
        pos_adjust_date = [
            cal.adjustDate(date, BizDayConventions.Preceding)
            for date in pos_adjust_date
        ]
    pos_adjust_date = [Date.toDateTime(date) for date in pos_adjust_date]
    pos_adjust_date = [
        date for date in pos_adjust_date if date <= d_end_date.toDateTime()
    ]

    return pos_adjust_date
Exemple #13
0
    def testChinaIB(self):

        # China Inter Bank working weekend list in the year 2014
        expectedWorkingWeekEnd = [Date(2014, 1, 26),
                                  Date(2014, 2, 8),
                                  Date(2014, 5, 4),
                                  Date(2014, 9, 28),
                                  Date(2014, 10, 11),
                                  # China Inter Bank working weekend list in the year 2015
                                  Date(2015, 1, 4),
                                  Date(2015, 2, 15),
                                  Date(2015, 2, 28),
                                  Date(2015, 9, 6),
                                  Date(2015, 10, 10)]

        cal = Calendar('China.IB')

        for day in expectedWorkingWeekEnd:
            self.assertEqual(cal.isHoliday(day), False, "{0} is not expected to be a holiday in {1}".format(day, cal))
            self.assertEqual(cal.isBizDay(day), True, "{0} is expected to be a working day in {1} ".format(day, cal))
    def testEvaluationDate(self):
        referenceDate = Date(2015, 1, 1)
        Settings.evaluationDate = referenceDate
        self.assertEqual(Settings.evaluationDate, referenceDate, "Evaluation Date should be \n"
                                                                 "expected:   {0}\n"
                                                                 "calculated: {1}".format(referenceDate,
                                                                                          Settings.evaluationDate))

        # check wrong input
        with self.assertRaises(ValueError):
            Settings.evaluationDate = 2
    def testDatePickle(self):
        benchmark_date = Date(2016, 1, 2)

        f = tempfile.NamedTemporaryFile('w+b', delete=False)
        pickle.dump(benchmark_date, f)
        f.close()

        with open(f.name, 'rb') as f2:
            pickled_date = pickle.load(f2)
            self.assertEqual(benchmark_date, pickled_date)

        os.unlink(f.name)
    def testCalendarWithDayConvention(self):
        sseCal = Calendar('China.SSE')

        referenceDate = Date(2015, 2, 14)
        testDate = sseCal.adjustDate(referenceDate, BizDayConventions.HalfMonthModifiedFollowing)
        self.assertEqual(testDate, Date(2015, 2, 13))

        referenceDate = Date(2014, 2, 4)
        testDate = sseCal.adjustDate(referenceDate, BizDayConventions.ModifiedPreceding)
        self.assertEqual(testDate, Date(2014, 2, 7))

        referenceDate = Date(2014, 2, 3)
        testDate = sseCal.adjustDate(referenceDate, BizDayConventions.Nearest)
        self.assertEqual(testDate, Date(2014, 2, 7))

        referenceDate = Date(2014, 2, 2)
        testDate = sseCal.adjustDate(referenceDate, BizDayConventions.Nearest)
        self.assertEqual(testDate, Date(2014, 1, 30))

        with self.assertRaises(ValueError):
            _ = sseCal.adjustDate(referenceDate, -1)
 def testScheduleInitialize(self):
     startDate = Date(2013, 3, 31)
     endDate = Date(2013, 6, 30)
     tenor = Period('1m')
     cal = Calendar('NullCalendar')
     sch = Schedule(startDate, endDate, tenor, cal)
     expected = [
         Date(2013, 3, 31),
         Date(2013, 4, 30),
         Date(2013, 5, 31),
         Date(2013, 6, 30)
     ]
     for i in range(sch.size()):
         self.assertEqual(expected[i], sch[i])
Exemple #18
0
def get_report_date(act_date, return_biz_day=True):
    """
    :param act_date: str/datetime.datetime, 任意日期
    :param return_biz_day: bool, 是否返回交易日
    :return: datetime, 对应应使用的报告日期, 从wind数据库中爬取
    此函数的目的是要找到,任意时刻可使用最新的季报数据的日期,比如2-20日可使用的最新季报是去年的三季报(对应日期为9-30),

    """

    if isinstance(act_date, str):
        act_date = Date.strptime(act_date)
    elif isinstance(act_date, datetime.datetime):
        act_date = Date.fromDateTime(act_date)
    act_month = act_date.month()
    act_year = act_date.year()
    if 1 <= act_month <= 3:  # 第一季度使用去年三季报的数据
        year = act_year - 1
        month = 9
        day = 30
    elif 4 <= act_month <= 7:  # 第二季度使用当年一季报
        year = act_year
        month = 3
        day = 31
    elif 8 <= act_month <= 9:  # 第三季度使用当年中报
        year = act_year
        month = 6
        day = 30
    else:
        year = act_year  # 第四季度使用当年三季报
        month = 9
        day = 30
    if return_biz_day:
        date_adj = Calendar('China.SSE').adjustDate(
            Date(year, month, day), BizDayConventions.Preceding)
        ret = date_adj.toDateTime()
    else:
        ret = datetime.datetime(year, month, day)
    return ret
    def testDailySchedule(self):
        # Jan 2 and Jan 3 are skipped as New Year holiday
        # Jan 7 is skipped as weekend
        # Jan 8 is adjusted to Jan 9 with following convention
        startDate = Date(2012, 1, 1)
        s = Schedule(startDate, startDate + 7,
                     Period(length=1, units=TimeUnits.Days),
                     Calendar("China.SSE"), BizDayConventions.Preceding)

        expected = [
            Date(2011, 12, 30),
            Date(2012, 1, 4),
            Date(2012, 1, 5),
            Date(2012, 1, 6),
            Date(2012, 1, 9)
        ]
        self.checkDates(s, expected)

        # The schedule should skip Saturday 21st and Sunday 22rd.
        # Previously, it would adjust them to Friday 20th, resulting
        # in three copies of the same date.
        startDate = Date(2012, 1, 17)
        s = Schedule(startDate, startDate + 7,
                     Period(length=1, units=TimeUnits.Days),
                     Calendar("Target"), BizDayConventions.Preceding)
        expected = [
            Date(2012, 1, 17),
            Date(2012, 1, 18),
            Date(2012, 1, 19),
            Date(2012, 1, 20),
            Date(2012, 1, 23),
            Date(2012, 1, 24)
        ]
        self.checkDates(s, expected)
    def testConsistency(self):
        minDate = Date.minDate().serialNumber + 1
        maxDate = Date.maxDate().serialNumber

        dyold = Date.fromExcelSerialNumber(minDate - 1).dayOfYear()
        dold = Date.fromExcelSerialNumber(minDate - 1).dayOfMonth()
        mold = Date.fromExcelSerialNumber(minDate - 1).month()
        yold = Date.fromExcelSerialNumber(minDate - 1).year()
        wdold = Date.fromExcelSerialNumber(minDate - 1).weekday()

        for i in range(minDate, maxDate + 1):
            t = Date.fromExcelSerialNumber(i)
            serial = t.serialNumber
            self.assertEqual(
                serial, i, "inconsistent serial number:\n"
                "   original:      {0:d}\n"
                "   serial number: {1:d}".format(i, serial))

            dy = t.dayOfYear()
            d = t.dayOfMonth()
            m = t.month()
            y = t.year()
            wd = t.weekday()

            flag = (dy == dyold + 1) or \
                   (dy == 1 and dyold == 365 and not Date.isLeap(yold)) or \
                   (dy == 1 and dyold == 366 and Date.isLeap(yold))

            self.assertTrue(
                flag, "wrong day of year increment: \n"
                "    date: {0}\n"
                "    day of year: {1:d}\n"
                "    previous:    {2:d}".format(t, dy, dyold))

            dyold = dy

            flag = (d == dold + 1 and m == mold and y == yold) or \
                   (d == 1 and m == mold + 1 and y == yold) or \
                   (d == 1 and m == 1 and y == yold + 1)

            self.assertTrue(
                flag, "wrong day,month,year increment: \n"
                "    date: {0}\n"
                "    year,month,day: {1:d}, {2:d}, {3:d}\n"
                "    previous:       {4:d}, {5:d}, {6:d}".format(
                    t, y, m, d, yold, mold, dold))
            dold = d
            mold = m
            yold = y

            self.assertTrue(
                d >= 1, "invalid day of month: \n"
                "    date:  {0}\n"
                "    day: {1:d}".format(t, d))

            flag = (m == 1 and d <= 31) or \
                   (m == 2 and d <= 28) or \
                   (m == 2 and d == 29 and Date.isLeap(y)) or \
                   (m == 3 and d <= 31) or \
                   (m == 4 and d <= 30) or \
                   (m == 5 and d <= 31) or \
                   (m == 6 and d <= 30) or \
                   (m == 7 and d <= 31) or \
                   (m == 8 and d <= 31) or \
                   (m == 9 and d <= 30) or \
                   (m == 10 and d <= 31) or \
                   (m == 11 and d <= 30) or \
                   (m == 12 and d <= 31)

            self.assertTrue(
                flag, "invalid day of month: \n"
                "    date:  {0}\n"
                "    day: {1:d}".format(t, d))

            flag = (wd == (wdold + 1)) or (wd == 1 or wdold == 7)

            self.assertTrue(
                flag, "invalid weekday: \n"
                "    date:  {0}\n"
                "    weekday:  {1:d}\n"
                "    previous: {2:d}".format(t, wd, wdold))
            wdold = wd

            s = Date(y, m, d)
            serial = s.serialNumber

            self.assertTrue(
                serial == i, "inconsistent serial number:\n"
                "    date:          {0}\n"
                "    serial number: {1:d}\n"
                "    cloned date:   {2}\n"
                "    serial number: {3:d}".format(t, i, s, serial))
 def testDateInputWithSerialNumberAndNotNullYearMonthDay(self):
     serialNumber = 45678
     _ = Date(year=2015, serialNumber=serialNumber)
    def testChinaSSE(self):
        # China Shanghai Securities Exchange holiday list in the year 2014
        expectedHol = [
            Date(2014, 1, 1),
            Date(2014, 1, 31),
            Date(2014, 2, 3),
            Date(2014, 2, 4),
            Date(2014, 2, 5),
            Date(2014, 2, 6),
            Date(2014, 4, 7),
            Date(2014, 5, 1),
            Date(2014, 5, 2),
            Date(2014, 6, 2),
            Date(2014, 9, 8),
            Date(2014, 10, 1),
            Date(2014, 10, 2),
            Date(2014, 10, 3),
            Date(2014, 10, 6),
            Date(2014, 10, 7),
            # China Shanghai Securities Exchange holiday list in the year 2015
            Date(2015, 1, 1),
            Date(2015, 1, 2),
            Date(2015, 2, 18),
            Date(2015, 2, 19),
            Date(2015, 2, 20),
            Date(2015, 2, 23),
            Date(2015, 2, 24),
            Date(2015, 4, 6),
            Date(2015, 5, 1),
            Date(2015, 6, 22),
            Date(2015, 9, 3),
            Date(2015, 9, 4),
            Date(2015, 10, 1),
            Date(2015, 10, 2),
            Date(2015, 10, 5),
            Date(2015, 10, 6),
            Date(2015, 10, 7),
            # China Shanghai Securities Exchange holiday list in the year 2016
            Date(2016, 1, 1),
            Date(2016, 2, 8),
            Date(2016, 2, 9),
            Date(2016, 2, 10),
            Date(2016, 2, 11),
            Date(2016, 2, 12),
            Date(2016, 4, 4),
            Date(2016, 5, 2),
            Date(2016, 6, 9),
            Date(2016, 6, 10),
            Date(2016, 9, 15),
            Date(2016, 9, 16),
            Date(2016, 10, 3),
            Date(2016, 10, 4),
            Date(2016, 10, 5),
            Date(2016, 10, 6),
            Date(2016, 10, 7),
            # China Shanghai Securities Exchange holiday list in the year 2017
            Date(2017, 1, 1),
            Date(2017, 1, 2),
            Date(2017, 1, 27),
            Date(2017, 1, 28),
            Date(2017, 1, 29),
            Date(2017, 1, 30),
            Date(2017, 1, 31),
            Date(2017, 2, 1),
            Date(2017, 2, 2),
            Date(2017, 4, 2),
            Date(2017, 4, 3),
            Date(2017, 4, 4),
            Date(2017, 5, 1),
            Date(2017, 5, 28),
            Date(2017, 5, 29),
            Date(2017, 5, 30),
            Date(2017, 10, 1),
            Date(2017, 10, 2),
            Date(2017, 10, 3),
            Date(2017, 10, 4),
            Date(2017, 10, 5),
            Date(2017, 10, 6),
            Date(2017, 10, 7),
            Date(2017, 10, 8)
        ]

        cal = Calendar('China.SSE')

        for day in expectedHol:
            self.assertEqual(
                cal.isHoliday(day), True,
                "{0} is expected to be a holiday in {1}".format(day, cal))
            self.assertEqual(
                cal.isBizDay(day), False,
                "{0} is expected not to be a working day in {1} ".format(
                    day, cal))
 def testDateInputWithoutCompleteInformationOnYearMonthDay(self):
     year = 2015
     month = None
     day = 18
     with self.assertRaises(TypeError):
         _ = Date(year=year, month=month, day=day)
    def testDatesList(self):

        fromDate = Date(2014, 1, 31)
        toDate = Date(2014, 2, 28)
        sseCal = Calendar('China.SSE')
        ibCal = Calendar('China.IB')

        benchmarkHol = [
            Date(2014, 1, 31),
            Date(2014, 2, 3),
            Date(2014, 2, 4),
            Date(2014, 2, 5),
            Date(2014, 2, 6)
        ]
        sseHolList = sseCal.holDatesList(fromDate, toDate, False)
        self.assertEqual(sseHolList, benchmarkHol)
        ibHolList = ibCal.holDatesList(fromDate, toDate, False)
        self.assertEqual(ibHolList, benchmarkHol)

        sseHolList = sseCal.holDatesList(fromDate, toDate, True)
        benchmarkHol = [
            Date(2014, 1, 31),
            Date(2014, 2, 1),
            Date(2014, 2, 2),
            Date(2014, 2, 3),
            Date(2014, 2, 4),
            Date(2014, 2, 5),
            Date(2014, 2, 6),
            Date(2014, 2, 8),
            Date(2014, 2, 9),
            Date(2014, 2, 15),
            Date(2014, 2, 16),
            Date(2014, 2, 22),
            Date(2014, 2, 23)
        ]
        self.assertEqual(sseHolList, benchmarkHol)
        ibHolList = ibCal.holDatesList(fromDate, toDate, True)
        benchmarkHol = [
            Date(2014, 1, 31),
            Date(2014, 2, 1),
            Date(2014, 2, 2),
            Date(2014, 2, 3),
            Date(2014, 2, 4),
            Date(2014, 2, 5),
            Date(2014, 2, 6),
            Date(2014, 2, 9),
            Date(2014, 2, 15),
            Date(2014, 2, 16),
            Date(2014, 2, 22),
            Date(2014, 2, 23)
        ]
        self.assertEqual(ibHolList, benchmarkHol)

        sseWorkingDayList = sseCal.bizDatesList(fromDate, toDate)
        d = fromDate
        while d <= toDate:
            if sseCal.isBizDay(d):
                self.assertTrue(d in sseWorkingDayList and d not in sseHolList)
            d += 1

        ibWorkingDayList = ibCal.bizDatesList(fromDate, toDate)
        d = fromDate
        while d <= toDate:
            if ibCal.isBizDay(d):
                self.assertTrue(d in ibWorkingDayList and d not in ibHolList)
            d += 1
    def testWrongInputOfHolidayCenter(self):
        with self.assertRaises(ValueError):
            _ = Calendar('NulCalendar')

        with self.assertRaises(ValueError):
            _ = Calendar(Date(2015, 1, 1))
    def testBasicFunctions(self):
        year = 2015
        month = 7
        day = 24
        strRepr = "{0}-{1:02d}-{2:02d}".format(year, month, day)
        innerRepr = "Date({0}, {1}, {2})".format(year, month, day)

        testDate = Date(year, month, day)
        self.assertEqual(
            str(testDate), strRepr, "date string:\n"
            "expected:   {0:s}\n"
            "calculated: {1:s}".format(strRepr, str(testDate)))

        self.assertEqual(
            repr(testDate), innerRepr, "date representation:\n"
            "expected:   {0:s}\n"
            "calculated: {1:s}".format(innerRepr, repr(testDate)))

        self.assertEqual(
            testDate.year(), year, "date year:\n"
            "expected:   {0:d}\n"
            "calculated: {1:d}".format(year, testDate.year()))

        self.assertEqual(
            testDate.month(), month, "date month:\n"
            "expected:   {0:d}\n"
            "calculated: {1:d}".format(month, testDate.month()))

        self.assertEqual(
            testDate.dayOfMonth(), day, "date day:\n"
            "expected:   {0:d}\n"
            "calculated: {1:d}".format(day, testDate.dayOfMonth()))

        self.assertEqual(
            testDate.dayOfYear(), testDate - Date(2015, 1, 1) + 1,
            "date day:\n"
            "expected:   {0:d}\n"
            "calculated: {1:d}".format(testDate - Date(2015, 1, 1) + 1,
                                       testDate.dayOfYear()))
        self.assertEqual(
            testDate.weekday(), 6, "date weekday:\n"
            "expected:   {0:d}\n"
            "calculated: {1:d}".format(5, testDate.weekday()))

        self.assertEqual(
            testDate.toDateTime(), dt.datetime(year, month, day),
            "date datetime representation\n"
            "expected:   {0}\n"
            "calculated: {1}".format(dt.datetime(year, month, day),
                                     testDate.toDateTime()))

        serialNumber = testDate.serialNumber
        serialDate = Date(serialNumber=serialNumber)

        self.assertEqual(
            serialDate, testDate, "date excel serial number representation\n"
            "expected:   {0:d}"
            "calculated: {1:d}".format(serialDate.serialNumber,
                                       testDate.serialNumber))

        # test comparisons
        previousDate = testDate - 1
        self.assertTrue(
            previousDate < testDate,
            "{0} is not earlier than {1}".format(previousDate, testDate))
        self.assertFalse(
            previousDate >= testDate,
            "{0} should not be later than or equal to {1}".format(
                previousDate, testDate))
        self.assertTrue((previousDate + 1) == testDate,
                        "{0} plus one day should be equal to {1}".format(
                            previousDate, testDate))

        # check static members
        self.assertEqual(Date.minDate(), Date(1901, 1, 1), "min date is wrong")
        self.assertEqual(Date.maxDate(), Date(2199, 12, 31),
                         "max date is wrong")
        self.assertEqual(Date.endOfMonth(testDate), Date(year, month, 31),
                         "end of month is wrong")
        self.assertTrue(Date.isEndOfMonth(Date(year, month, 31)),
                        "{0} should be the end of month")
        self.assertEqual(
            Date.nextWeekday(testDate, testDate.weekday()), testDate,
            "{0}'s next same week day should be {1}".format(
                testDate, testDate))
        expectedDate = dt.date.today()
        expectedDate = dt.datetime(expectedDate.year, expectedDate.month,
                                   expectedDate.day)
        self.assertEqual(
            Date.todaysDate().toDateTime(), expectedDate, "today's date\n"
            "expected:   {0}\n"
            "calculated: {1}".format(expectedDate, Date.todaysDate()))

        # nth-week day
        with self.assertRaises(ValueError):
            _ = Date.nthWeekday(0, Weekdays.Friday, 1, 2015)

        with self.assertRaises(ValueError):
            _ = Date.nthWeekday(6, Weekdays.Friday, 1, 2015)

        self.assertEqual(Date.nthWeekday(3, Weekdays.Wednesday, 8, 2015),
                         Date(2015, 8, 19))

        # check plus/sub

        threeWeeksAfter = testDate + '3W'
        expectedDate = testDate + 21
        self.assertEqual(
            threeWeeksAfter, expectedDate, "date + 3w period\n"
            "expected:   {0}\n"
            "calculated: {1}".format(expectedDate, threeWeeksAfter))

        threeMonthsBefore = testDate - "3M"
        expectedDate = Date(year, month - 3, day)
        self.assertEqual(
            threeMonthsBefore, expectedDate, "date - 3m period\n"
            "expected:   {0}\n"
            "calculated: {1}".format(expectedDate, threeMonthsBefore))

        threeMonthsBefore = testDate - Period("3M")
        expectedDate = Date(year, month - 3, day)
        self.assertEqual(
            threeMonthsBefore, expectedDate, "date - 3m period\n"
            "expected:   {0}\n"
            "calculated: {1}".format(expectedDate, threeMonthsBefore))

        threeMonthsAfter = testDate + "3m"
        expectedDate = Date(year, month + 3, day)
        self.assertEqual(
            threeMonthsAfter, expectedDate, "date + 3m period\n"
            "expected:   {0}\n"
            "calculated: {1}".format(expectedDate, threeMonthsAfter))

        oneYearAndTwoMonthsBefore = testDate - "14m"
        expectedDate = Date(year - 1, month - 2, day)
        self.assertEqual(
            oneYearAndTwoMonthsBefore, expectedDate, "date - 14m period\n"
            "expected:   {0}\n"
            "calculated: {1}".format(expectedDate, threeMonthsBefore))

        oneYearAndTwoMonthsBefore = testDate + "14m"
        expectedDate = Date(year + 1, month + 2, day)
        self.assertEqual(
            oneYearAndTwoMonthsBefore, expectedDate, "date + 14m period\n"
            "expected:   {0}\n"
            "calculated: {1}".format(expectedDate, threeMonthsBefore))

        fiveMonthsAfter = testDate + "5m"
        expectedDate = Date(year, month + 5, day)
        self.assertEqual(
            fiveMonthsAfter, expectedDate, "date + 5m period\n"
            "expected:   {0}\n"
            "calculated: {1}".format(expectedDate, fiveMonthsAfter))
Exemple #27
0
 def testDateInputWithSerialNumberAndNotNullYearMonthDay(self):
     serialNumber = 45678
     with self.assertRaises(ValueError):
         _ = Date(year=2015, serialNumber=serialNumber)
 def testDateInputWithSerialNumber(self):
     serialNumber = 45678
     testDate = Date(serialNumber=serialNumber)
     self.assertEqual(testDate.serialNumber, serialNumber)
Exemple #29
0
    def testChinaIB(self):

        # China Inter Bank working weekend list in the year 2014
        expectedWorkingWeekEnd = [
            Date(2014, 1, 26),
            Date(2014, 2, 8),
            Date(2014, 5, 4),
            Date(2014, 9, 28),
            Date(2014, 10, 11),
            # China Inter Bank working weekend list in the year 2015
            Date(2015, 1, 4),
            Date(2015, 2, 15),
            Date(2015, 2, 28),
            Date(2015, 9, 6),
            Date(2015, 10, 10),
            # China Inter Bank working weekend list in the year 2016
            Date(2016, 2, 6),
            Date(2016, 2, 14),
            Date(2016, 6, 12),
            Date(2016, 9, 18),
            Date(2016, 10, 8),
            Date(2016, 10, 9),
            # China Inter Bank working weekend list in the year 2017
            Date(2017, 1, 22),
            Date(2017, 2, 4),
            Date(2017, 4, 1),
            Date(2017, 5, 27),
            Date(2017, 9, 30),
            # China Inter Bank working weekend list in the year 2018
            Date(2018, 2, 11),
            Date(2018, 2, 24),
            Date(2018, 4, 8),
            Date(2018, 4, 28),
            Date(2018, 9, 29),
            Date(2018, 9, 30),
            # China Inter Bank working weekend list in the year 2019
            Date(2019, 2, 2),
            Date(2019, 2, 3),
            Date(2019, 4, 28),
            Date(2019, 5, 5),
            Date(2019, 9, 29),
            Date(2019, 10, 12),
            # China Inter Bank working weekend list in the year 2020
            Date(2020, 1, 19),
            Date(2020, 2, 1),
            Date(2020, 4, 26),
            Date(2020, 5, 9),
            Date(2020, 6, 28),
            Date(2020, 9, 27),
            Date(2020, 10, 10)
        ]

        cal = Calendar('China.IB')

        for day in expectedWorkingWeekEnd:
            self.assertEqual(
                cal.isHoliday(day), False,
                "{0} is not expected to be a holiday in {1}".format(day, cal))
            self.assertEqual(
                cal.isBizDay(day), True,
                "{0} is expected to be a working day in {1} ".format(day, cal))
    def testDateDeepCopy(self):
        benchmark_date = Date(2016, 1, 2)
        copied_date = copy.deepcopy(benchmark_date)

        self.assertEqual(benchmark_date, copied_date)