summaryrefslogtreecommitdiff
path: root/natlangdate.py
diff options
context:
space:
mode:
Diffstat (limited to 'natlangdate.py')
-rwxr-xr-xnatlangdate.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/natlangdate.py b/natlangdate.py
new file mode 100755
index 0000000..4fe7528
--- /dev/null
+++ b/natlangdate.py
@@ -0,0 +1,33 @@
+#!/usr/bin/env python
+import os
+import sys
+import re
+import parsedatetime.parsedatetime as pdt
+#import parsedatetime.parsedatetime_consts as pdc
+import datetime
+
+# Define Globals
+pyDate = os.getenv('KMVAR_myDate')
+
+# Function for parsing a string and returning a datetime value
+def datetimeFromString( s ):
+ #c = pdc.Constants()
+ p = pdt.Calendar()
+ result, what = p.parse( s )
+ dt = 0
+ # See: http://stackoverflow.com/questions/1810432/handling-the-different-results-from-parsedatetime
+ # what was returned (see http://code-bear.com/code/parsedatetime/docs/)
+ # 0 = failed to parse
+ # 1 = date (with current time, as a struct_time)
+ # 2 = time (with current date, as a struct_time)
+ # 3 = datetime
+ if what in (1,2,3):
+ # result is struct_time
+ dt = datetime.datetime( *result[:6] )
+ if what == 0:
+ # Failed to parse
+ raise ValueError, ("Don't understand date '"+s+"'")
+ dt = "Unrecognized Date"
+ return dt
+NLPDate = datetimeFromString(pyDate)
+print NLPDate