-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_time.py
69 lines (51 loc) · 1.93 KB
/
utils_time.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from datetime import date, datetime
def get_month_and_day_format():
return "%m-%d"
def get_short_date_format():
return "%Y-%m-%d"
def get_long_date_format():
return f"{get_short_date_format()}T%H:%M"
def convert_date_to_string(date_to_convert, format_type="short"):
if format_type == "short":
dateformat = get_short_date_format()
elif format_type == "long":
dateformat = get_long_date_format()
else:
raise f"Format type {format_type} not supported!"
return date_to_convert.strftime(dateformat)
def convert_string_to_date(date_string):
format_short = get_short_date_format()
formats = [format_short, get_long_date_format(), f"{format_short}T-%H:%M"]
dt = None
for f in formats:
try:
dt = datetime.strptime(date_string, f)
break
except:
dt = None
return dt
def aggregate_data_for_time_window(data_dict, data_for_timescale, variable_list):
for time_window in data_dict.keys():
for hour in data_dict[time_window]["hour_indices"]:
for variable in variable_list:
if variable not in data_dict[time_window]:
data_dict[time_window][variable] = []
datapoint = data_for_timescale[variable][hour]
data_dict[time_window][variable].append(datapoint)
return data_dict
def get_weekday_and_hours(date_string, no_year=False):
dt = convert_string_to_date(date_string)
if no_year:
date_format = get_month_and_day_format()
else:
date_format = f"{get_short_date_format()},%A"
weekday = dt.strftime(date_format)
hours = dt.hour
return weekday, hours
def get_current_date():
today = date.today()
return today.strftime("%Y-%m-%d")
def get_week_number(date_string):
dt = convert_string_to_date(date_string)
week_number = date(dt.year, dt.month, dt.day).isocalendar()[1]
return f"{dt.year} {week_number}"