Last active
August 29, 2015 14:21
-
-
Save johnnyutahh/24f9a48a331fd36a4e97 to your computer and use it in GitHub Desktop.
testing Python library 'parsedatetime', from https://github.com/bear/parsedatetime
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() | |
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) |
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
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
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.