Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
testing Python library 'parsedatetime', from https://github.com/bear/parsedatetime
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, time, platform, calendar, subprocess as sp
from datetime import date
# from https://github.com/bear/parsedatetime
import parsedatetime as pdt
def print_parsedatetime_test_conversions(natural_langage_dates_list):
cal = pdt.Calendar()
for date in natural_langage_dates_list:
(struct_time_date, success) = cal.parse(date)
if success:
formal_date = time.strftime('%Y-%m-%d', struct_time_date)
else:
formal_date = '(conversion failed)'
print '{0:>15s} -> {1:10s}'.format(date, formal_date)
def print_OS_info():
if platform.system() == 'Windows':
sys.exit()
cmds = [ 'python --version', 'pip show parsedatetime', 'uname -a' ]
if platform.system() == 'Darwin':
cmds.append('sw_vers')
if platform.system() == 'Linux':
cmds.append('lsb_release -a')
for cmd in cmds:
print "'$ " + cmd + "':"
p = sp.Popen(cmd, shell=True)
p.communicate()
print
def print_todays_date():
todays_day_of_week = calendar.day_name[date.today().weekday()]
print "today's date = " + todays_day_of_week + ', ' + \
time.strftime('%Y-%m-%d') + '\n'
example_natural_langage_dates_list = \
[
'-1 thursday',
'last thursday',
'-1 thursday',
'- thursday',
'-thursday',
'1 thursday',
'+1 thursday',
'last thursday',
'next thursday',
'monday',
'-1 monday',
'last monday',
'-1 monday',
'-2 monday',
'- monday',
'monday',
'-1 sunday',
'- sunday',
'sunday',
'last sat',
'tomorrow',
'yesterday',
'- days',
'- day',
'-1d',
'2d',
'16month',
'-16month',
'6 months',
'3y',
'-1 days',
'-3 days',
' 3 d',
'-2 months',
' 2 month',
'-1 years',
' 1 yrs',
' 1 yr',
'-2 yr',
'thur',
'thu',
'th',
'mon',
'monday',
'next monday',
'next tues',
'last tues',
'-3 tues',
]
print_OS_info()
print_todays_date()
print_parsedatetime_test_conversions(example_natural_langage_dates_list)
@johnnyutahh

This comment has been minimized.

Copy link
Owner Author

commented May 13, 2015

The following shows command-line session + output executing the above script on a test machine. Notice the inconsistencies/inaccuracies on some of the conversions.

$ ./parsedatetime_unittest.py 
'$ python --version':
Python 2.7.8

'$ pip show parsedatetime':

---
Name: parsedatetime
Version: 1.4
Location: /usr/local/lib/python2.7/site-packages
Requires: 

'$ uname -a':
Darwin mba2mme 13.4.0 Darwin Kernel Version 13.4.0: Wed Mar 18 16:20:14 PDT 2015; root:xnu-2422.115.14~1/RELEASE_X86_64 x86_64 i386 MacBookAir6,2 Darwin

'$ sw_vers':
ProductName:    Mac OS X
ProductVersion: 10.9.5
BuildVersion:   13F1077

today's date = Wednesday, 2015-05-13

-1 thursday     -> 2015-05-14
last thursday   -> 2015-05-07
-1 thursday     -> 2015-05-07
- thursday      -> 2015-05-07
-thursday       -> (conversion failed)
1 thursday      -> 2015-05-07
+1 thursday     -> 2015-05-07
last thursday   -> 2015-05-07
next thursday   -> 2015-05-21
monday          -> 2015-05-11
-1 monday       -> 2015-05-13
last monday     -> 2015-05-11
-1 monday       -> 2015-05-13
-2 monday       -> 2015-05-13
- monday        -> 2015-05-11
monday          -> 2015-05-11
-1 sunday       -> 2015-05-13
- sunday        -> 2015-05-10
sunday          -> 2015-05-10
last sat        -> 2015-05-09
tomorrow        -> 2015-05-14
yesterday       -> 2015-05-12
- days          -> (conversion failed)
- day           -> (conversion failed)
-1 days         -> 2015-05-12
-3 days         -> 2015-05-10
 3 d            -> 2015-05-16
-2 months       -> 2015-03-13
 2 month        -> 2015-07-13
-1 years        -> 2014-05-13
 1 yrs          -> (conversion failed)
 1 yr           -> 2016-05-13
-2 yr           -> 2013-05-13
thur            -> (conversion failed)
thu             -> (conversion failed)
th              -> 2015-05-07
mon             -> 2015-05-11
monday          -> 2015-05-11
next monday     -> 2015-05-18
next tues       -> 2015-05-19
last tues       -> 2015-05-12
-3 tues         -> 2015-05-12
$ 
@bear

This comment has been minimized.

Copy link

commented May 13, 2015

Some of these may have been corrected with a very recent commit - let me convert this to a python unit test format and give it a run against the dev version.

@johnnyutahh

This comment has been minimized.

Copy link
Owner Author

commented May 14, 2015

Thanks bear for your timely response. I'll await what you discover. In meantime, I made a minor (1-line) update to the above code gist, for more-readable output format, example below.

Also: I really appreciate the dateparsetime library. Saves me a lot of work.

$ ./parsedatetime_unittest.py
'$ python --version':
Python 2.7.8

'$ pip show parsedatetime':

---
Name: parsedatetime
Version: 1.4
Location: /usr/local/lib/python2.7/site-packages
Requires: 

'$ uname -a':
Darwin mba2mme 13.4.0 Darwin Kernel Version 13.4.0: Wed Mar 18 16:20:14 PDT 2015; root:xnu-2422.115.14~1/RELEASE_X86_64 x86_64 i386 MacBookAir6,2 Darwin

'$ sw_vers':
ProductName:    Mac OS X
ProductVersion: 10.9.5
BuildVersion:   13F1077

today's date = Thursday, 2015-05-14

    -1 thursday -> 2015-05-21
  last thursday -> 2015-05-07
    -1 thursday -> 2015-05-07
     - thursday -> 2015-05-07
      -thursday -> (conversion failed)
     1 thursday -> 2015-05-07
    +1 thursday -> 2015-05-07
  last thursday -> 2015-05-07
  next thursday -> 2015-05-21
         monday -> 2015-05-11
      -1 monday -> 2015-05-14
    last monday -> 2015-05-11
      -1 monday -> 2015-05-14
      -2 monday -> 2015-05-14
       - monday -> 2015-05-11
         monday -> 2015-05-11
      -1 sunday -> 2015-05-14
       - sunday -> 2015-05-10
         sunday -> 2015-05-10
       last sat -> 2015-05-09
       tomorrow -> 2015-05-15
      yesterday -> 2015-05-13
         - days -> (conversion failed)
          - day -> (conversion failed)
            -1d -> 2015-05-13
             2d -> 2015-05-16
        16month -> 2016-09-14
       -16month -> 2014-01-14
       6 months -> 2015-11-14
             3y -> 2018-05-14
        -1 days -> 2015-05-13
        -3 days -> 2015-05-11
            3 d -> 2015-05-17
      -2 months -> 2015-03-14
        2 month -> 2015-07-14
       -1 years -> 2014-05-14
          1 yrs -> (conversion failed)
           1 yr -> 2016-05-14
          -2 yr -> 2013-05-14
           thur -> (conversion failed)
            thu -> (conversion failed)
             th -> 2015-05-07
            mon -> 2015-05-11
         monday -> 2015-05-11
    next monday -> 2015-05-18
      next tues -> 2015-05-19
      last tues -> 2015-05-12
        -3 tues -> 2015-05-12
$ 
@bear

This comment has been minimized.

Copy link

commented Jul 5, 2015

All of these should be working now except for those that are +/- # format, like

-1 sunday

The reason for that is that the -1 is a modifier for a default unit of the phrase that follows but not a next/last modifier - so it will determine the date for "sunday" and then adjust it by a single day.

marking this as closed

@bear

This comment has been minimized.

Copy link

commented Jul 5, 2015

and... that should have been on the issue and not on your code - sorry!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.