nachfolgender Code generiert datetime-objecte. Hier ein paar Beispiele:
Code: Alles auswählen
In [263]: parse.generate_date('2012.9.16')
Out[263]: datetime.datetime(2012, 9, 16, 0, 0)
In [264]: parse.generate_date('10-3')
Out[264]: datetime.datetime(2012, 10, 3, 0, 0)
In [265]: parse.generate_date('30')
Out[265]: datetime.datetime(2012, 8, 30, 0, 0)
Code: Alles auswählen
import re
import datetime
FORMAT = 'ymd'
CHANGE_SHORT_YEAR = True
def generate_date(date_args, formatstring=None):
'''generate_date(date_args[, formatdate_args]) -> datetime-object
Extract potential date digits from 'date_args' and return
a valid datetime-object or raise a ValueError.
'date_args' must be a date_args with 1, 2 or 3 digit(s) seperated
by any char. These digit(s) represents year, month and day
in order of the given 'formatdate_args' or by 'parse.FORMAT'
by default.
If one or more digit(s) are not given it will be replaced
with the equivalent of the current day.
Note that 2 digits are always interpreted as day with month,
1 digit always as day.
'formatdate_args' specified the order in which the date digits
are contained in 'date_args'.
If 'parse.CHANGE_SHORT_YEAR' is True, a year-number larger
then 100 will be interpreted as a 21 century year, e. g.
the year 13 will be returned as year 2013.'''
if formatstring is None:
formatstring = FORMAT
date_args = re.findall('\d+', date_args)
arg_count = len(date_args)
year, month, day = datetime.datetime.now().timetuple()[0:3]
if arg_count == 3:
# positions of year, month, date
y_idx, m_idx, d_idx = formatcode_positions(formatstring, 'ymd')
year = int(date_args[y_idx])
if CHANGE_SHORT_YEAR and year < 100:
year += 2000
month = int(date_args[m_idx])
day = int(date_args[d_idx])
elif arg_count == 2:
# positions of month and date if no year is given
m_idx, d_idx = formatcode_positions(formatstring, 'md')
month = int(date_args[m_idx])
day = int(date_args[d_idx])
else:
day = int(date_args[0])
try:
return datetime.datetime(year, month, day)
except ValueError:
raise ValueError('{0}(y), {1}(m), {2}(d) seems not to be '
'a valid date.'.format(year, month, day))
def formatcode_positions(formatstring, formatcodes):
formatcodes = formatcodes.upper()
_ = [f.upper() for f in re.findall('[{0}]'.format(formatcodes),
formatstring, re.I)]
return [_.index(code) for code in formatcodes]
Was mich allerdings nicht so ganz glücklich macht ist diese 'arg_count'-Sache. Ich stelle mir da eher sowas wie ein template vor, das mit default-Werten gefüllt ist und je nach übergebenen year-, month- und day-Werten bestückt wird.
Die Hürde, über die ich nicht komme ist die veränderliche Reihenfolge der Werte.
Wäre für jegliche Inspiration sehr dankbar!
mutetella