Project

General

Profile

« Previous | Next » 

Revision 287

xml_func.py: Added support for parsing dates

View differences:

lib/xml_func.py
36 36

  
37 37
def date(items):
38 38
    items = dict(items)
39
    year = items['year']
40
    month = items.get('month', '1')
41
    day = items.get('day', '1')
42
    try:
43
        year = float(year)
44
        month = int(month)
45
        day = int(day)
46
    except ValueError, e: raise SyntaxException(e)
47
    date = (datetime.date(int(year), month, day) +
48
        datetime.timedelta(round((year % 1.)*365)))
39
    if 'date' in items:
40
        str_ = items['date']
41
        try: import dateutil.parser
42
        except ImportError: return str_
43
        try: date = dateutil.parser.parse(str_)
44
        except ValueError, e: raise SyntaxException(e)
45
    else:
46
        year = items['year']
47
        month = items.get('month', '1')
48
        day = items.get('day', '1')
49
        try:
50
            year = float(year)
51
            month = int(month)
52
            day = int(day)
53
        except ValueError, e: raise SyntaxException(e)
54
        date = (datetime.date(int(year), month, day) +
55
            datetime.timedelta(round((year % 1.)*365)))
49 56
    return date.strftime('%Y-%m-%d')
50 57

  
51 58
def name(items):

Also available in: Unified diff