parsedatetime.tests.TestComplexDateTimes

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

# Source Code for Module parsedatetime.tests.TestComplexDateTimes

     1  #!/usr/bin/env python
     2
     3  """
     4  Test parsing of complex date and times
     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(result, check):
    14      target, t_flag = result
    15      value,  v_flag = check
    16
    17      t_yr, t_mth, t_dy, t_hr, t_min, _, _, _, _ = target
    18      v_yr, v_mth, v_dy, v_hr, v_min, _, _, _, _ = value
    19
    20      return ((t_yr == v_yr) and (t_mth == v_mth) and (t_dy == v_dy) and
    21              (t_hr == v_hr) and (t_min == v_min)) and (t_flag == v_flag)
    22
    23 -class test(unittest.TestCase):
    24 -    def setUp(self):
    25          self.cal = pt.Calendar()
    26          self.yr, self.mth, self.dy, self.hr, self.mn, self.sec, self.wd, self.yd, self.isdst = time.localtime()
    27
    28
    29 -    def testDates(self):
    30          start  = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple()
    31          target = datetime.datetime(2006, 8, 25,  17, 0, 0).timetuple()
    32
    33          self.assertTrue(_compareResults(self.cal.parse('08/25/2006 5pm',      start), (target, 3)))
    34          self.assertTrue(_compareResults(self.cal.parse('5pm on 08.25.2006',   start), (target, 3)))
    35          self.assertTrue(_compareResults(self.cal.parse('5pm August 25, 2006', start), (target, 3)))
    36          self.assertTrue(_compareResults(self.cal.parse('Aug 25, 2006 5pm',    start), (target, 3)))
    37
    38          if self.mth > 8 or (self.mth == 8 and self.dy > 5):
    39              target = datetime.datetime(self.yr + 1, 8, 5, 17, 0, 0).timetuple()
    40          else:
    41              target = datetime.datetime(self.yr, 8, 5, 17, 0, 0).timetuple()
    42          self.assertTrue(_compareResults(self.cal.parse('8/5 at 5pm',    start), (target, 3)))
    43          self.assertTrue(_compareResults(self.cal.parse('5pm 8.5',       start), (target, 3)))
    44          self.assertTrue(_compareResults(self.cal.parse('08/05 5pm',     start), (target, 3)))
    45          self.assertTrue(_compareResults(self.cal.parse('August 5 5pm',  start), (target, 3)))
    46          self.assertTrue(_compareResults(self.cal.parse('5pm Aug 05',    start), (target, 3)))
    47          self.assertTrue(_compareResults(self.cal.parse('Aug 05 5pm',    start), (target, 3)))
    48
    49
    50  if __name__ == "__main__":
    51      unittest.main()
    52