-
Notifications
You must be signed in to change notification settings - Fork 2
/
update-utcoffset.py
129 lines (86 loc) · 3.17 KB
/
update-utcoffset.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
117
118
119
120
121
122
123
124
125
126
127
128
'''
This script edits the default-schedule.csv file
to account for Daylight Savings Time and user edits.
It takes Local Time values and updates the UTC and Cron values.
Thus one only needs to find the best local time
(when the store's order page is active)
without having to worry about timezone conversion accuracy.
A Github Actions workflow should be set to activate once a day, and
on every pushed commit that edits the default-schedule.csv file
'''
# ======================================================= #
# Step 1: Import libraries and define necessary functions #
# ======================================================= #
import pytz
from datetime import datetime
import pandas as pd
import traceback
# Function to localize each local time string
# returns a datetime object with timezone included
def init_local(tz, t):
timezone = pytz.timezone(tz)
aware_dt = datetime.now(timezone).replace(hour=int(t[:2]), minute=int(t[3:]))
return aware_dt
# Function to get UTC offset (time difference between local time and UTC)
# in string format "[+-]hh:mm"
# DST offset, if applicable, has already been added
def utc_offset(tz, t):
hhmm = init_local(tz, t).strftime("%z")
hh_mm = hhmm[:3] + ":" + hhmm[3:]
return hh_mm
# Function to convert local time to UTC equivalent,
# returns in string format "hh:mm"
def utc_time(tz, t):
utc_dt = init_local(tz, t).astimezone(pytz.utc)
utc_t = utc_dt.strftime("%H:%M")
return utc_t
# Function to render converted UTC time in cron format
# returns a string "mm hh * * *"
def cron_time(tz, t):
utc_dt = init_local(tz, t).astimezone(pytz.utc)
cron = utc_dt.strftime("%M %H * * *")
return cron
# Function to adjust start and stop values of the Pandas DataFrame index
def reset_index(n):
df.index = pd.RangeIndex(
start=n, stop=(len(df.index) + n), step=1
)
# ======================================================= #
# Step 2: Import CSV data into DataFrame and clean it #
# ======================================================= #
'''
Column headers
0 1 2 3 4 5 6 7
| [index] | Code | Timezone | UTC | Cron | UTC Offset | Local Time | Library |
# '''
try:
df = pd.read_csv("default-schedule.csv", index_col=0)
reset_index(0)
# List comprehension returns series of values (list) after
# chosen function iterates through temporary DataFrame extract
def list_c(f):
return pd.Series([f(row[0], row[1]) for row in zip(df["Timezone"], df["Local Time"])])
df["UTC"] = list_c(utc_time)
df["Cron"] = list_c(cron_time)
df["UTC Offset"] = list_c(utc_offset)
# Sort rows by UTC time in ascending order
df = df.sort_values(
by=["UTC"], ascending=True, na_position="last"
)
reset_index(1)
# ======================================================= #
# Step 3: Export new data to overwrite file #
# ======================================================= #
df.to_csv("default-schedule.csv")
print("File checked.\nChanges, if any, will be automatically committed by the yml workflow\n\n\n")
except Exception:
print(
f'''
\n\n
---
One or more errors occurred:
{traceback.format_exc()}
---
\n\n
'''
)