From 04b69eb0f3fa8ad3fab6b79a95645a9a0058ba5f Mon Sep 17 00:00:00 2001 From: ionic-bond Date: Wed, 25 Dec 2024 01:28:10 +0800 Subject: [PATCH] Update datetime usage --- stream_translator_gpt/common.py | 4 ++-- stream_translator_gpt/llm_translator.py | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/stream_translator_gpt/common.py b/stream_translator_gpt/common.py index d5e31ad..fb12826 100644 --- a/stream_translator_gpt/common.py +++ b/stream_translator_gpt/common.py @@ -1,6 +1,6 @@ import os from abc import ABC, abstractmethod -from datetime import datetime +from datetime import datetime, timezone import google.generativeai as genai import numpy as np @@ -37,7 +37,7 @@ def work(cls, **kwargs): def sec2str(second: float): - dt = datetime.utcfromtimestamp(second) + dt = datetime.fromtimestamp(second, tz=timezone.utc) result = dt.strftime('%H:%M:%S') result += ',' + str(int(second * 10 % 10)) return result diff --git a/stream_translator_gpt/llm_translator.py b/stream_translator_gpt/llm_translator.py index 7ebb1de..1388353 100644 --- a/stream_translator_gpt/llm_translator.py +++ b/stream_translator_gpt/llm_translator.py @@ -4,7 +4,7 @@ import time import re from collections import deque -from datetime import datetime, timedelta +from datetime import datetime, timedelta, timezone import google.generativeai as genai from google.api_core.exceptions import InternalServerError, ResourceExhausted, TooManyRequests @@ -50,7 +50,7 @@ def _parse_json_completion(completion): def _is_task_timeout(task: TranslationTask, timeout: float) -> bool: - return datetime.utcnow() - task.start_time > timedelta(seconds=timeout) + return datetime.now(timezone.utc) - task.start_time > timedelta(seconds=timeout) class LLMClint(): @@ -175,7 +175,7 @@ def __init__(self, llm_client: LLMClint, timeout: int, retry_if_translation_fail def _trigger(self, translation_task: TranslationTask): if not translation_task.start_time: - translation_task.start_time = datetime.utcnow() + translation_task.start_time = datetime.now(timezone.utc) translation_task.translation_failed = False thread = threading.Thread(target=self.llm_client.translate, args=(translation_task,)) thread.daemon = True @@ -225,7 +225,7 @@ def __init__(self, llm_client: LLMClint, timeout: int, retry_if_translation_fail def _trigger(self, translation_task: TranslationTask): if not translation_task.start_time: - translation_task.start_time = datetime.utcnow() + translation_task.start_time = datetime.now(timezone.utc) translation_task.translation_failed = False thread = threading.Thread(target=self.llm_client.translate, args=(translation_task,)) thread.daemon = True