Date/time parsing and manipulation functions
Failure to parse a date or time.
Given a line of text, get a date/time formatted as dateformat, and return a datetime object rounded to the nearest resolution seconds. If line fails to match dateformat, a CannotParse exception is raised.
Examples:
>>> date_chop('1976/05/19 12:05:17', '%Y/%m/%d %H:%M:%S', 60)
datetime.datetime(1976, 5, 19, 12, 5)
>>> date_chop('1976/05/19 12:05:17', '%Y/%m/%d %H:%M:%S', 3600)
datetime.datetime(1976, 5, 19, 12, 0)
Given a simplified date or time format string, return (format, regexp), where format is a strptime-compatible format string, and regexp is a regular expression that matches dates or times in that format.
The simple_format string supports a subset of strptime formatting directives, with the leading % characters removed.
Examples:
>>> format_regexp('Y/m/d')
('%Y/%m/%d', '(?P<Y>\\d\\d\\d\\d)/(?P<m>\\d\\d?)/(?P<d>\\d\\d?)')
>>> format_regexp('H:M:S')
('%H:%M:%S', '(?P<H>[01]?[0-9]|2[0-3]):(?P<M>[0-5]\\d):(?P<S>[0-5]\\d)')
Open the given file and use guess_format to look for a date/time at the beginning of each line. Return the format string for the first one that’s found. Raise CannotParse if none is found.
Try to guess the date/time format of string, or raise a CannotParse exception.
Examples:
>>> guess_format('2010/01/28 13:25:49')
'%Y/%m/%d %H:%M:%S'
>>> guess_format('01/28/10 1:25:49 PM')
'%m/%d/%y %I:%M:%S %p'
>>> guess_format('01/28/2010 13:25:49.123')
'%m/%d/%Y %H:%M:%S.%f'
>>> guess_format('Aug 15 2009 15:24')
'%b %d %Y %H:%M'
>>> guess_format('3-14-15 9:26:53.589')
'%m-%d-%y %H:%M:%S.%f'
Leading and trailing text may be present:
>>> guess_format('FOO April 1, 2007 3:45 PM BAR')
'%B %d, %Y %I:%M %p'
>>> guess_format('[[2010-09-25 14:19:24]]')
'%Y-%m-%d %H:%M:%S'
Attempt to parse the given string as a date in the given format. This is similar to datetime.strptime, but this can handle date strings with trailing characters. If it still fails to parse, raise a CannotParse exception.
Examples:
>>> parse('2010/08/28', '%Y/%m/%d')
datetime.datetime(2010, 8, 28, 0, 0)
>>> parse('2010/08/28 extra stuff', '%Y/%m/%d')
datetime.datetime(2010, 8, 28, 0, 0)
>>> parse('2010/08/28', '%m/%d/%y')
Traceback (most recent call last):
CannotParse: time data '2010/08/28' does not match format '%m/%d/%y'