]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/ide/activegrid/util/datetimeparser.py
   1 #---------------------------------------------------------------------------- 
   2 # Name:         datetimeparser.py 
   4 # Purpose:      - Instantiate datetime.datetime/date instance from a string 
   6 #                 Uses dateutil from http://labix.org/python-dateutil. 
   8 #               - Creates string representation of datetime/date instance. 
  15 # Copyright:    (c) 2005 ActiveGrid, Inc. 
  16 # License:      wxWindows License 
  17 #---------------------------------------------------------------------------- 
  22     import dateutil
.parser
 
  23     DATEUTIL_INSTALLED 
= True 
  25     DATEUTIL_INSTALLED 
= False 
  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
, 
  32 DEFAULT_DATETIME 
= datetime
.datetime(1, 1, 1, 0, 0, 0, 0) 
  35 def format(dateobj
, formatstr
=None): 
  36     if (formatstr 
!= None and _isDateTimeObject(dateobj
)): 
  37         return dateobj
.strftime(str(formatstr
)) 
  41 def parse(datestr
, formatstr
=None, asdate
=False, astime
=False): 
  42     """Instantiates and returns a datetime instance from the datestr datetime 
  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. 
  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 
  56     dayfirst
, yearfirst 
= _getDayFirstAndYearFirst(formatstr
) 
  61         if DATEUTIL_INSTALLED
: 
  62             rtn 
= dateutil
.parser
.parse(str(datestr
), fuzzy
=True, 
  63                                         dayfirst
=dayfirst
, yearfirst
=yearfirst
, 
  64                                         default
=DEFAULT_DATETIME
) 
  66             rtn 
= DEFAULT_DATETIME
 
  68         rtn 
= DEFAULT_DATETIME
 
  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
) 
  78 def _isDateTimeObject(obj
): 
  79     return (isinstance(obj
, datetime
.datetime
) or 
  80             isinstance(obj
, datetime
.date
) or 
  81             isinstance(obj
, datetime
.time
)) 
  84 def _getDayFirstAndYearFirst(formatstr
): 
  92     if (formatstr 
== None): 
  96         if (c
.lower() == "y"): 
  99             if (not gotDay 
and not gotMonth
): 
 103         elif (c
.lower() == "m"): 
 110         elif (c
.lower() == "d"): 
 118     return dayFirst
, yearFirst