From 4515679e28417cf240e7b555c807a915a9f160db Mon Sep 17 00:00:00 2001 From: oldnapalm <38410858+oldnapalm@users.noreply.github.com> Date: Wed, 6 Dec 2023 15:22:51 -0300 Subject: [PATCH] Replace deprecated functions Closes #280 --- standalone.py | 4 ++-- zwift_offline.py | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/standalone.py b/standalone.py index 37343b26..6eb8efea 100644 --- a/standalone.py +++ b/standalone.py @@ -16,7 +16,7 @@ from http.server import SimpleHTTPRequestHandler from http.cookies import SimpleCookie from collections import deque -from datetime import datetime, timedelta +from datetime import datetime, timedelta, timezone from Crypto.Cipher import AES import zwift_offline as zo @@ -464,7 +464,7 @@ def save_ghost(name, player_id): print('save_ghost: %s' % repr(exc)) return ghosts.rec.player_id = player_id - f = '%s/%s-%s.bin' % (folder, datetime.utcnow().strftime("%Y-%m-%d-%H-%M-%S"), name) + f = '%s/%s-%s.bin' % (folder, datetime.now(timezone.utc).strftime("%Y-%m-%d-%H-%M-%S"), name) with open(f, 'wb') as fd: fd.write(ghosts.rec.SerializeToString()) diff --git a/zwift_offline.py b/zwift_offline.py index 26c58cf7..7b3a40fa 100644 --- a/zwift_offline.py +++ b/zwift_offline.py @@ -168,7 +168,7 @@ def get_id(self): return self.player_id def get_token(self): - dt = datetime.datetime.utcnow() + datetime.timedelta(minutes=30) + dt = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(minutes=30) return jwt.encode({'user': self.player_id, 'exp': dt}, app.config['SECRET_KEY'], algorithm='HS256') @staticmethod @@ -434,7 +434,7 @@ def load_game_dictionary(): def get_utc_time(): - return datetime.datetime.utcnow().timestamp() + return datetime.datetime.now(datetime.timezone.utc).timestamp() def get_time(): return datetime.datetime.now().timestamp() @@ -2317,7 +2317,7 @@ def api_profiles_activities_rideon(receiving_player_id): return '{}', 200 def stime_to_timestamp(stime): - utc_offset = datetime.datetime.fromtimestamp(0) - datetime.datetime.utcfromtimestamp(0) + utc_offset = datetime.datetime.now().astimezone().utcoffset() try: return int((datetime.datetime.strptime(stime, '%Y-%m-%dT%H:%M:%SZ') + utc_offset).timestamp()) except: @@ -2688,7 +2688,7 @@ def unix_time_millis(dt): def fill_in_goal_progress(goal, player_id): local_now = datetime.datetime.now() - utc_offset = datetime.datetime.fromtimestamp(0) - datetime.datetime.utcfromtimestamp(0) + utc_offset = datetime.datetime.now().astimezone().utcoffset() if goal.periodicity == 0: # weekly first_dt, last_dt = get_week_range(local_now) else: # monthly @@ -2720,7 +2720,7 @@ def fill_in_goal_progress(goal, player_id): def set_goal_end_date_now(goal): local_now = datetime.datetime.now() - utc_offset = int((datetime.datetime.fromtimestamp(0) - datetime.datetime.utcfromtimestamp(0)).total_seconds()) + utc_offset = int(datetime.datetime.now().astimezone().utcoffset().total_seconds()) if goal.periodicity == 0: # weekly goal.period_end_date = unix_time_millis(get_week_range(local_now)[1]) - utc_offset else: # monthly @@ -2742,7 +2742,7 @@ def str_timestamp(ts): else: sec = int(ts/1000) ms = ts % 1000 - return datetime.datetime.utcfromtimestamp(sec).strftime('%Y-%m-%dT%H:%M:%S.') + str(ms).zfill(3) + "+0000" + return datetime.datetime.fromtimestamp(sec, datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%S.') + str(ms).zfill(3) + "+0000" def str_timestamp_json(ts): if ts == 0: @@ -2794,8 +2794,8 @@ def select_protobuf_goals(player_id, limit): for row in rows: goal = goals.goals.add() row_to_protobuf(row, goal) - end_dt = datetime.datetime.fromtimestamp(goal.period_end_date / 1000) - if end_dt < datetime.datetime.utcnow(): + end_dt = datetime.datetime.fromtimestamp(goal.period_end_date / 1000, datetime.timezone.utc) + if end_dt < datetime.datetime.now(datetime.timezone.utc): need_update.append(goal) fill_in_goal_progress(goal, player_id) for goal in need_update: @@ -2826,7 +2826,7 @@ def api_profiles_goals(player_id): str_goal = request.stream.read() json_goal = json.loads(str_goal) goal = goalJsonToProtobuf(json_goal) - goal.created_on = unix_time_millis(datetime.datetime.utcnow()) + goal.created_on = unix_time_millis(datetime.datetime.now(datetime.timezone.utc)) set_goal_end_date_now(goal) fill_in_goal_progress(goal, player_id) goal.id = insert_protobuf_into_db(Goal, goal) @@ -3163,7 +3163,7 @@ def api_segment_results(): if result.segment_id == 1: return '', 400 result.world_time = world_time() - result.finish_time_str = datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ") + result.finish_time_str = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") result.sport = 0 result.id = insert_protobuf_into_db(SegmentResult, result)