parsedatetime.tests.TestSimpleDateTimes

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

# Source Code for Module parsedatetime.tests.TestSimpleDateTimes

      1  #!/usr/bin/env python
      2
      3  """
      4  Test parsing of simple 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 testDays(self):
     30          s = datetime.datetime.now()
     31          t = s + datetime.timedelta(days=1)
     32
     33          start  = s.timetuple()
     34          target = t.timetuple()
     35
     36          d = self.wd + 1
     37
     38          if d > 6:
     39              d = 0
     40
     41          day = self.cal.ptc.Weekdays[d]
     42
     43          self.assertTrue(_compareResults(self.cal.parse(day, start), (target, 1)))
     44
     45          t = s + datetime.timedelta(days=6)
     46
     47          target = t.timetuple()
     48
     49          d = self.wd - 1
     50
     51          if d < 0:
     52              d = 6
     53
     54          day = self.cal.ptc.Weekdays[d]
     55
     56          self.assertTrue(_compareResults(self.cal.parse(day, start), (target, 1)))
     57
     58
     59 -    def testTimes(self):
     60          start  = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple()
     61          target = datetime.datetime(self.yr, self.mth, self.dy, 23, 0, 0).timetuple()
     62
     63          self.assertTrue(_compareResults(self.cal.parse('11:00:00 PM', start), (target, 2)))
     64          self.assertTrue(_compareResults(self.cal.parse('11:00 PM',    start), (target, 2)))
     65          self.assertTrue(_compareResults(self.cal.parse('11 PM',       start), (target, 2)))
     66          self.assertTrue(_compareResults(self.cal.parse('11PM',        start), (target, 2)))
     67          self.assertTrue(_compareResults(self.cal.parse('2300',        start), (target, 2)))
     68          self.assertTrue(_compareResults(self.cal.parse('23:00',       start), (target, 2)))
     69          self.assertTrue(_compareResults(self.cal.parse('11p',         start), (target, 2)))
     70          self.assertTrue(_compareResults(self.cal.parse('11pm',        start), (target, 2)))
     71
     72          target = datetime.datetime(self.yr, self.mth, self.dy, 11, 0, 0).timetuple()
     73
     74          self.assertTrue(_compareResults(self.cal.parse('11:00:00 AM', start), (target, 2)))
     75          self.assertTrue(_compareResults(self.cal.parse('11:00 AM',    start), (target, 2)))
     76          self.assertTrue(_compareResults(self.cal.parse('11 AM',       start), (target, 2)))
     77          self.assertTrue(_compareResults(self.cal.parse('11AM',        start), (target, 2)))
     78          self.assertTrue(_compareResults(self.cal.parse('1100',        start), (target, 2)))
     79          self.assertTrue(_compareResults(self.cal.parse('11:00',       start), (target, 2)))
     80          self.assertTrue(_compareResults(self.cal.parse('11a',         start), (target, 2)))
     81          self.assertTrue(_compareResults(self.cal.parse('11am',        start), (target, 2)))
     82
     83          target = datetime.datetime(self.yr, self.mth, self.dy, 7, 30, 0).timetuple()
     84
     85          self.assertTrue(_compareResults(self.cal.parse('730',  start), (target, 2)))
     86          self.assertTrue(_compareResults(self.cal.parse('0730', start), (target, 2)))
     87
     88          target = datetime.datetime(self.yr, self.mth, self.dy, 17, 30, 0).timetuple()
     89
     90          self.assertTrue(_compareResults(self.cal.parse('1730',   start), (target, 2)))
     91          self.assertTrue(_compareResults(self.cal.parse('173000', start), (target, 2)))
     92
     93
     94 -    def testDates(self):
     95          start  = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple()
     96          target = datetime.datetime(2006, 8, 25,  self.hr, self.mn, self.sec).timetuple()
     97
     98          self.assertTrue(_compareResults(self.cal.parse('08/25/2006',     start), (target, 1)))
     99          self.assertTrue(_compareResults(self.cal.parse('08.25.2006',     start), (target, 1)))
    100          self.assertTrue(_compareResults(self.cal.parse('8/25/06',        start), (target, 1)))
    101          self.assertTrue(_compareResults(self.cal.parse('August 25, 2006',start), (target, 1)))
    102          self.assertTrue(_compareResults(self.cal.parse('Aug 25, 2006',   start), (target, 1)))
    103
    104          if self.mth > 8 or (self.mth == 8 and self.dy > 25):
    105              target = datetime.datetime(self.yr + 1, 8, 25,  self.hr, self.mn, self.sec).timetuple()
    106          else:
    107              target = datetime.datetime(self.yr, 8, 25,  self.hr, self.mn, self.sec).timetuple()
    108          self.assertTrue(_compareResults(self.cal.parse('8/25',       start), (target, 1)))
    109          self.assertTrue(_compareResults(self.cal.parse('8.25',       start), (target, 1)))
    110          self.assertTrue(_compareResults(self.cal.parse('08/25',      start), (target, 1)))
    111          self.assertTrue(_compareResults(self.cal.parse('August 25',  start), (target, 1)))
    112          self.assertTrue(_compareResults(self.cal.parse('Aug 25',     start), (target, 1)))
    113
    114
    115 -    def testSpecialTimes(self):
    116          start  = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple()
    117          target = datetime.datetime(self.yr, self.mth, self.dy, 6, 0, 0).timetuple()
    118
    119          self.assertTrue(_compareResults(self.cal.parse('morning', start), (target, 2)))
    120
    121          target = datetime.datetime(self.yr, self.mth, self.dy, 8, 0, 0).timetuple()
    122
    123          self.assertTrue(_compareResults(self.cal.parse('breakfast', start), (target, 2)))
    124
    125          target = datetime.datetime(self.yr, self.mth, self.dy, 12, 0, 0).timetuple()
    126
    127          self.assertTrue(_compareResults(self.cal.parse('lunch', start), (target, 2)))
    128
    129          target = datetime.datetime(self.yr, self.mth, self.dy, 18, 0, 0).timetuple()
    130
    131          self.assertTrue(_compareResults(self.cal.parse('evening', start), (target, 2)))
    132
    133          target = datetime.datetime(self.yr, self.mth, self.dy, 19,0, 0).timetuple()
    134
    135          self.assertTrue(_compareResults(self.cal.parse('dinner', start), (target, 2)))
    136
    137          target = datetime.datetime(self.yr, self.mth, self.dy, 21, 0, 0).timetuple()
    138
    139          self.assertTrue(_compareResults(self.cal.parse('night',   start), (target, 2)))
    140          self.assertTrue(_compareResults(self.cal.parse('tonight', start), (target, 2)))
    141
    142 -    def testMidnight(self):
    143          start  = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple()
    144          target = datetime.datetime(self.yr, self.mth, self.dy, 0, 0, 0).timetuple()
    145
    146          self.assertTrue(_compareResults(self.cal.parse('midnight',    start), (target, 2)))
    147          self.assertTrue(_compareResults(self.cal.parse('12:00:00 AM', start), (target, 2)))
    148          self.assertTrue(_compareResults(self.cal.parse('12:00 AM',    start), (target, 2)))
    149          self.assertTrue(_compareResults(self.cal.parse('12 AM',       start), (target, 2)))
    150          self.assertTrue(_compareResults(self.cal.parse('12AM',        start), (target, 2)))
    151          self.assertTrue(_compareResults(self.cal.parse('12am',        start), (target, 2)))
    152          self.assertTrue(_compareResults(self.cal.parse('12a',         start), (target, 2)))
    153          self.assertTrue(_compareResults(self.cal.parse('0000',        start), (target, 2)))
    154          self.assertTrue(_compareResults(self.cal.parse('00:00',       start), (target, 2)))
    155
    156
    157 -    def testNoon(self):
    158          start  = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple()
    159          target = datetime.datetime(self.yr, self.mth, self.dy, 12, 0, 0).timetuple()
    160
    161          self.assertTrue(_compareResults(self.cal.parse('noon',        start), (target, 2)))
    162          self.assertTrue(_compareResults(self.cal.parse('12:00:00 PM', start), (target, 2)))
    163          self.assertTrue(_compareResults(self.cal.parse('12:00 PM',    start), (target, 2)))
    164          self.assertTrue(_compareResults(self.cal.parse('12 PM',       start), (target, 2)))
    165          self.assertTrue(_compareResults(self.cal.parse('12PM',        start), (target, 2)))
    166          self.assertTrue(_compareResults(self.cal.parse('12pm',        start), (target, 2)))
    167          self.assertTrue(_compareResults(self.cal.parse('12p',         start), (target, 2)))
    168          self.assertTrue(_compareResults(self.cal.parse('1200',        start), (target, 2)))
    169          self.assertTrue(_compareResults(self.cal.parse('12:00',       start), (target, 2)))
    170
    171
    172  if __name__ == "__main__":
    173      unittest.main()
    174