]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/ide/activegrid/util/datetimeparser.py
DocView and ActiveGrid IDE updates from Morgan Hua:
[wxWidgets.git] / wxPython / samples / ide / activegrid / util / datetimeparser.py
1 #----------------------------------------------------------------------------
2 # Name: datetimeparser.py
3 #
4 # Purpose: - Instantiate datetime.datetime/date instance from a string
5 # date representation.
6 # Uses dateutil from http://labix.org/python-dateutil.
7 #
8 # - Creates string representation of datetime/date instance.
9 #
10 #
11 # Author: Simon Toens
12 #
13 # Created: 28-Feb-06
14 # CVS-ID:
15 # Copyright: (c) 2005 ActiveGrid, Inc.
16 # License: wxWindows License
17 #----------------------------------------------------------------------------
18
19 import datetime
20
21 try:
22 import dateutil.parser
23 DATEUTIL_INSTALLED = True
24 except ImportError:
25 DATEUTIL_INSTALLED = False
26
27 ISO_8601_DATE_FORMAT = "%Y-%m-%d"
28 ISO_8601_TIME_FORMAT = "%H:%M:%S"
29 ISO_8601_DATETIME_FORMAT = "%s %s" %(ISO_8601_DATE_FORMAT,
30 ISO_8601_TIME_FORMAT)
31
32 DEFAULT_DATETIME = datetime.datetime(1, 1, 1, 0, 0, 0, 0)
33
34
35 def format(dateobj, formatstr=None):
36 if (formatstr != None and _isDateTimeObject(dateobj)):
37 return dateobj.strftime(str(formatstr))
38 return str(dateobj)
39
40
41 def parse(datestr, formatstr=None, asdate=False, astime=False):
42 """Instantiates and returns a datetime instance from the datestr datetime
43 representation.
44
45 Optionally, a format string may be used. The format is only loosely
46 interpreted, its only purpose beeing to determine if the year is first
47 or last in datestr, and whether the day is in front or follows the
48 month. If no formatstr is passed in, dateutil tries its best to parse
49 the datestr. The default date format is YYYY-mm-dd HH:SS.
50
51 If asdate is True, returns a date instance instead of a datetime
52 instance, if astime is True, returns a time instance instead of a
53 datetime instance."""
54
55
56 dayfirst, yearfirst = _getDayFirstAndYearFirst(formatstr)
57
58 rtn = None
59
60 try:
61 if DATEUTIL_INSTALLED:
62 rtn = dateutil.parser.parse(str(datestr), fuzzy=True,
63 dayfirst=dayfirst, yearfirst=yearfirst,
64 default=DEFAULT_DATETIME)
65 else:
66 rtn = DEFAULT_DATETIME
67 except:
68 rtn = DEFAULT_DATETIME
69
70 if (asdate and isinstance(rtn, datetime.datetime)):
71 rtn = datetime.date(rtn.year, rtn.month, rtn.day)
72 elif (astime and isinstance(rtn, datetime.datetime)):
73 rtn = datetime.time(rtn.hour, rtn.minute, rtn.second, rtn.microsecond)
74
75 return rtn
76
77
78 def _isDateTimeObject(obj):
79 return (isinstance(obj, datetime.datetime) or
80 isinstance(obj, datetime.date) or
81 isinstance(obj, datetime.time))
82
83
84 def _getDayFirstAndYearFirst(formatstr):
85 dayFirst = False
86 yearFirst = False
87
88 gotYear = False
89 gotMonth = False
90 gotDay = False
91
92 if (formatstr == None):
93 formatstr = ""
94
95 for c in formatstr:
96 if (c.lower() == "y"):
97 if (gotYear):
98 continue
99 if (not gotDay and not gotMonth):
100 yearFirst = True
101 gotYear = True
102
103 elif (c.lower() == "m"):
104 if (gotMonth):
105 continue
106 if (not gotDay):
107 dayFirst = False
108 gotMonth = True
109
110 elif (c.lower() == "d"):
111 if (gotDay):
112 continue
113 if (not gotMonth):
114 dayFirst = True
115 gotDay = True
116
117
118 return dayFirst, yearFirst