-
Notifications
You must be signed in to change notification settings - Fork 1
/
cycle.py
83 lines (65 loc) · 2.05 KB
/
cycle.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
from datetime import datetime, timedelta, timezone
# returns (cycle 28, cycle 56)
# use 1 as future to get future cycle, and -1 for past, can be any number +-n
def calculate_cycle(future=0):
# starting conditions from a known date
start_utc = datetime(2020, 1, 2, 9, 0, 0, 0, timezone.utc)
cycle = 1
last_year = 2019
combined = 2001
is56 = True
# all in UTC and current date, use future to current time
now_utc = datetime.now(timezone.utc) + future * timedelta(28)
# find all cycles through a loop
while start_utc < now_utc:
if last_year != start_utc.year:
cycle = 1
last_year = start_utc.year
else:
cycle = cycle + 1
combined = (start_utc.year % 2000) * 100 + cycle
is56 = not is56
start_utc = start_utc + timedelta(days=28)
if is56:
return combined, combined
else:
# this is not 56, last must be, get it. should not recurse
x, y = calculate_cycle(future - 1)
return combined, x
def get_cycle():
te, fs = calculate_cycle(1)
print("Cycle to be put in manifest is " + str(te))
return str(te)
def get_cycle_download():
te, fs = calculate_cycle(1)
print("Cycle to be downloaded is " + str(fs))
return str(fs)
def get_first_date(year):
# Date for first cycle every year in January starting 2014
first_dates = {
2020: 2,
2021: 28,
2022: 27,
2023: 26,
2024: 25,
2025: 23,
2026: 22,
2027: 21,
2028: 20,
2029: 18
}
return first_dates.get(year, 0)
def get_version_start(cycle_name):
try:
cycle = int(cycle_name)
except ValueError:
return ""
cycle_upper = cycle // 100
cycle_lower = cycle - (cycle_upper * 100)
first_date = get_first_date(2000 + cycle_upper)
if first_date < 1:
return ""
epoch = datetime(2000 + cycle_upper, 1, first_date, 9, 0, 0)
epoch += timedelta(days=28 * (cycle_lower - 1))
fmt1 = epoch.strftime("%Y-%m-%d")
return f"{fmt1}"