1 from datetime import datetime, timedelta
7 def __init__(self, start = time.strptime('19960101000000', '%Y%m%d%H%M%S'),
8 end = datetime.now(), features = []):
11 self._features = features
13 self._dated_features = {}
23 def dated_features(self):
24 if self._dated_features == {}:
26 while date <= self._end:
28 Date = time.strptime(datetime.strftime(date, '%m/%d/%Y %H:%M:%S'), '%m/%d/%Y %H:%M:%S')
29 for feature in self._features:
31 dict_hour['hour'] = Date.tm_hour
32 elif feature == 'dayInWeek':
33 dict_hour['dayInWeek'] = Date.tm_wday
34 elif feature == 'dayInMonth':
35 dict_hour['dayInMonth'] = Date.tm_mday
36 elif feature == 'month':
37 dict_hour['month'] = Date.tm_mon
38 elif feature == 'year':
39 dict_hour['year'] = Date.tm_year
40 elif feature == 'dayInYear':
41 # Si c'est une année bissextile et qu'on est après le 29 février, on compte une journée
42 # dans l'année de moins, car on va supprimer les 29 févriers, de sorte que les 14 juillets,
43 # les 24 décembre... tombent toujours
44 if calendar.isleap(Date.tm_year) and Date >= time.strptime("29/02/"+str(Date.tm_year), "%d/%m/%Y"):
45 dict_hour['dayInYear'] = Date.tm_yday -1
47 dict_hour['dayInYear'] = Date.tm_yday
48 elif feature == 'weekInYear':
49 dict_hour['weekInYear'] = date.isocalendar()[1]
50 self._dated_features[date] = dict_hour
51 date += timedelta(hours=1)
52 return self._dated_features