summaryrefslogtreecommitdiff
path: root/bin/natlangdate.py
blob: 4fe75287a923c6f24600246e9da79aef34883de1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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