forked from turicas/covid19-br
-
Notifications
You must be signed in to change notification settings - Fork 0
/
date_utils.py
116 lines (96 loc) · 3.44 KB
/
date_utils.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import datetime
from functools import lru_cache
one_day = datetime.timedelta(days=1)
def today():
date = datetime.datetime.now()
return datetime.date(date.year, date.month, date.day)
def next_day(date):
return date + datetime.timedelta(days=1)
def next_week(date):
return date + datetime.timedelta(days=7)
def next_month(date):
return datetime.date(
year=date.year + (date.month // 12), month=(date.month % 12) + 1, day=date.day
)
def next_date(date, interval="daily"):
from_interval = {"daily": next_day, "weekly": next_week, "monthly": next_month}
return from_interval[interval](date)
def date_range(start, stop, interval="daily"):
current = start
while current < stop:
yield current
current = next_date(date=current, interval=interval)
def date_to_dict(date):
return {"year": date.year, "month": date.month, "day": date.day}
@lru_cache(4096)
def brazilian_epidemiological_week(date):
"""Calculate Brazilian epidemiological weeks
Information from:
<https://portalsinan.saude.gov.br/calendario-epidemiologico-2020/43-institucional>
>>> brazilian_epidemiological_week(datetime.date(2019, 1, 1))
(2019, 1)
>>> brazilian_epidemiological_week(datetime.date(2019, 1, 6))
(2019, 2)
>>> brazilian_epidemiological_week(datetime.date(2019, 12, 28))
(2019, 52)
>>> brazilian_epidemiological_week(datetime.date(2020, 1, 1))
(2020, 1)
>>> brazilian_epidemiological_week(datetime.date(2020, 1, 5))
(2020, 2)
>>> brazilian_epidemiological_week(datetime.date(2020, 12, 27))
(2020, 53)
>>> brazilian_epidemiological_week(datetime.date(2021, 1, 2))
(2020, 53)
"""
dates = {
2012: {
"start_date": datetime.date(2012, 1, 1),
"end_date": datetime.date(2012, 12, 29),
},
2013: {
"start_date": datetime.date(2012, 12, 30),
"end_date": datetime.date(2013, 12, 28),
},
2014: {
"start_date": datetime.date(2013, 12, 29),
"end_date": datetime.date(2015, 1, 3),
},
2015: {
"start_date": datetime.date(2015, 1, 4),
"end_date": datetime.date(2016, 1, 2),
},
2016: {
"start_date": datetime.date(2016, 1, 3),
"end_date": datetime.date(2016, 12, 31),
},
2017: {
"start_date": datetime.date(2017, 1, 1),
"end_date": datetime.date(2017, 12, 30),
},
2018: {
"start_date": datetime.date(2017, 12, 31),
"end_date": datetime.date(2018, 12, 29),
},
2019: {
"start_date": datetime.date(2018, 12, 30),
"end_date": datetime.date(2019, 12, 28),
},
2020: {
"start_date": datetime.date(2019, 12, 29),
"end_date": datetime.date(2021, 1, 2),
},
}
year = None
for possible_year, year_data in dates.items():
if year_data["start_date"] <= date <= year_data["end_date"]:
year = possible_year
break
if year is None:
raise ValueError(f"Cannot calculate year for date {date}")
start_date = dates[year]["start_date"]
end_date = dates[year]["end_date"]
week_range = date_range(start_date, end_date + one_day, interval="weekly")
for count, start in enumerate(week_range, start=1):
end = start + 6 * one_day
if start <= date <= end:
return year, count