parsedatetime.tests.TestInc

Package parsedatetime :: Package tests :: Module TestInc |
[hide private]
[frames] | no frames]

# Source Code for Module parsedatetime.tests.TestInc

     1  #!/usr/bin/env python

     2

     3  """

     4  Test Calendar.Inc() routine

     5  """
     6

     7  import unittest, time, datetime
     8  import parsedatetime.parsedatetime as pt
     9

    10

    11    # a special compare function is used to allow us to ignore the seconds as

    12    # the running of the test could cross a minute boundary

    13 -def _compareResults(target, value):
    14      t_yr, t_mth, t_dy, t_hr, t_min, _, _, _, _ = target
    15      v_yr, v_mth, v_dy, v_hr, v_min, _, _, _, _ = value
    16

    17      return ((t_yr == v_yr) and (t_mth == v_mth) and (t_dy == v_dy) and

    18              (t_hr == v_hr) and (t_min == v_min)) #and (t_wd == v_wd) and (t_yd == v_yd))
    19

    20 -class test(unittest.TestCase):
    21 -    def setUp(self):
    22          self.cal = pt.Calendar()
    23          self.yr, self.mth, self.dy, self.hr, self.mn, self.sec, self.wd, self.yd, self.isdst = time.localtime()
    24

    25 -    def testIncMonths(self):
    26          s = datetime.datetime(2006, 1, 1, 12, 0, 0)
    27          t = datetime.datetime(2006, 2, 1, 12, 0, 0)
    28          self.assertTrue(_compareResults(self.cal.inc(s, month=1).timetuple(), t.timetuple()))
    29

    30          s = datetime.datetime(2006, 12, 1, 12, 0, 0)
    31          t = datetime.datetime(2007,  1, 1, 12, 0, 0)
    32          self.assertTrue(_compareResults(self.cal.inc(s, month=1).timetuple(), t.timetuple()))
    33

    34          # leap year, Feb 1

    35          s = datetime.datetime(2008, 2, 1, 12, 0, 0)
    36          t = datetime.datetime(2008, 3, 1, 12, 0, 0)
    37          self.assertTrue(_compareResults(self.cal.inc(s, month=1).timetuple(), t.timetuple()))
    38

    39          # leap year, Feb 29

    40          s = datetime.datetime(2008, 2, 29, 12, 0, 0)
    41          t = datetime.datetime(2008, 3, 29, 12, 0, 0)
    42          self.assertTrue(_compareResults(self.cal.inc(s, month=1).timetuple(), t.timetuple()))
    43

    44          s = datetime.datetime(2006,  1, 1, 12, 0, 0)
    45          t = datetime.datetime(2005, 12, 1, 12, 0, 0)
    46          self.assertTrue(_compareResults(self.cal.inc(s, month=-1).timetuple(), t.timetuple()))
    47

    48          # End of month Jan 31 to Feb - Febuary only has 28 days

    49          s = datetime.datetime(2006, 1, 31, 12, 0, 0)
    50          t = datetime.datetime(2006, 2, 28, 12, 0, 0)
    51          self.assertTrue(_compareResults(self.cal.inc(s, month=1).timetuple(), t.timetuple()))
    52

    53          # walk thru months and make sure month increment doesn't set the day

    54          # to be past the last day of the new month

    55          # think Jan transition to Feb - 31 days to 28 days

    56          for m in range(1, 11):
    57              d = self.cal.ptc.DaysInMonthList[m - 1]
    58              s = datetime.datetime(2006, m, d, 12, 0, 0)
    59

    60              if d > self.cal.ptc.DaysInMonthList[m]:
    61                  d = self.cal.ptc.DaysInMonthList[m]
    62

    63              t = datetime.datetime(2006, m + 1, d, 12, 0, 0)
    64

    65              self.assertTrue(_compareResults(self.cal.inc(s, month=1).timetuple(), t.timetuple()))
    66

    67

    68 -    def testIncYears(self):
    69          s = datetime.datetime(2006, 1, 1, 12, 0, 0)
    70          t = datetime.datetime(2007, 1, 1, 12, 0, 0)
    71          self.assertTrue(_compareResults(self.cal.inc(s, year=1).timetuple(), t.timetuple()))
    72

    73          s = datetime.datetime(2006, 1, 1, 12, 0, 0)
    74          t = datetime.datetime(2008, 1, 1, 12, 0, 0)
    75          self.assertTrue(_compareResults(self.cal.inc(s, year=2).timetuple(), t.timetuple()))
    76

    77          s = datetime.datetime(2006, 12, 31, 12, 0, 0)
    78          t = datetime.datetime(2007, 12, 31, 12, 0, 0)
    79          self.assertTrue(_compareResults(self.cal.inc(s, year=1).timetuple(), t.timetuple()))
    80

    81          s = datetime.datetime(2006, 12, 31, 12, 0, 0)
    82          t = datetime.datetime(2005, 12, 31, 12, 0, 0)
    83          self.assertTrue(_compareResults(self.cal.inc(s, year=-1).timetuple(), t.timetuple()))
    84

    85          s = datetime.datetime(2008, 3, 1, 12, 0, 0)
    86          t = datetime.datetime(2009, 3, 1, 12, 0, 0)
    87          self.assertTrue(_compareResults(self.cal.inc(s, year=1).timetuple(), t.timetuple()))
    88

    89          s = datetime.datetime(2008, 3, 1, 12, 0, 0)
    90          t = datetime.datetime(2007, 3, 1, 12, 0, 0)
    91          self.assertTrue(_compareResults(self.cal.inc(s, year=-1).timetuple(), t.timetuple()))
    92

    93  if __name__ == "__main__":
    94      unittest.main()
    95