Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add timezone offset config. #53

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ Tips:拓展功能需要项目已安装该插件,更多自定义插件支持

#检测频率(默认1秒一次,注意不建议修改!!如果任务带秒钟,则可能会被跳过)
"time_check_rate": 1,

#服务器所在时区偏移, 默认为0(计算方法为当前时区减去服务器时区,可为负数,如:用户当前时区GMT+8,服务器所在时区为GMT-4,则偏移量为 +8-(-4)=12. 假如用户当前时区为GMT-4,服务器所在时区为GMT+8,则偏移量为-4-(+8)=-12)
"timezone_offset": 0,

#Excel中迁移任务的时间(默认在凌晨4点将Excel 任务列表sheet 中失效的任务 迁移至 -> 历史任务sheet中)
"move_historyTask_time": "04:00:00",
Expand Down
12 changes: 7 additions & 5 deletions TimeTaskTool.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def pingTimeTask_in_sub_thread(self):
load_config()
self.conf = conf()
self.debug = self.conf.get("debug", False)
# 时区偏移(客户端时间与服务器时间的偏移量,单位:小时)
self.timezone_offset = self.conf.get("timezone_offset", 0)
#迁移任务的时间
self.move_historyTask_time = self.conf.get("move_historyTask_time", "04:00:00")
#默认每秒检测一次
Expand Down Expand Up @@ -196,7 +198,7 @@ def moveTask_toHistory(self, modelArray):
return

#当前时间的小时:分钟
current_time_hour_min = arrow.now().format('HH:mm')
current_time_hour_min = arrow.now().shift(hours=self.timezone_offset).format('HH:mm')
#执行中 - 标识符
identifier_running = f"{current_time_hour_min}_running"
#结束 - 标识符
Expand Down Expand Up @@ -230,7 +232,7 @@ def moveTask_toHistory(self, modelArray):
elif "_end" in current_task_state:
#标识符中的时间
tempTimeStr = current_task_state.replace("_end", ":00")
current_time = arrow.now().replace(second=0, microsecond=0).time()
current_time = arrow.now().shift(hours=self.timezone_offset).replace(second=0, microsecond=0).time()
task_time = arrow.get(tempTimeStr, "HH:mm:ss").replace(second=0, microsecond=0).time()
tempValue = task_time < current_time
if tempValue:
Expand All @@ -240,7 +242,7 @@ def moveTask_toHistory(self, modelArray):
#刷新c任务
def refresh_times(self, modelArray):
#当前时间的小时:分钟
current_time_hour_min = arrow.now().format('HH:mm')
current_time_hour_min = arrow.now().shift(hours=self.timezone_offset).format('HH:mm')
#执行中 - 标识符
identifier_running = f"{current_time_hour_min}_running"
#结束 - 标识符
Expand Down Expand Up @@ -278,7 +280,7 @@ def refresh_times(self, modelArray):
elif "_end" in current_task_state:
#标识符中的时间
tempTimeStr = current_task_state.replace("_end", ":00")
current_time = arrow.now().replace(second=0, microsecond=0).time()
current_time = arrow.now().shift(hours=self.timezone_offset).replace(second=0, microsecond=0).time()
task_time = arrow.get(tempTimeStr, "HH:mm:ss").replace(second=0, microsecond=0).time()
tempValue = task_time < current_time
if tempValue:
Expand Down Expand Up @@ -385,7 +387,7 @@ def convetDataToModelArray(self, dataArray):
def is_targetTime(self, timeStr):
tempTimeStr = timeStr
#对比精准到分(忽略秒)
current_time = arrow.now().format('HH:mm')
current_time = arrow.now().shift(hours=self.timezone_offset).format('HH:mm')

#如果是分钟
if tempTimeStr.count(":") == 1:
Expand Down
20 changes: 12 additions & 8 deletions Tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from typing import List
import time
from datetime import datetime
from plugins.timetask.config import conf, load_config
from lib import itchat
from lib.itchat.content import *
from channel.chat_message import ChatMessage
Expand Down Expand Up @@ -408,9 +409,12 @@ def __init__(self, item, msg:ChatMessage, isNeedFormat: bool, isNeedCalculateCro
self.isNeedCalculateCron = isNeedCalculateCron
self.taskId = item[0]
self.enable = item[1] == "1"
load_config()
self.conf = conf()

#是否今日已被消费
self.is_today_consumed = False
self.timezone_offset = self.conf.get("timezone_offset", 0)

#时间信息
timeValue = item[2]
Expand Down Expand Up @@ -524,7 +528,7 @@ def get_todayCron_times(self):
#校验cron格式
if self.isValid_Cron_time():
# 获取当前时间(忽略秒数)
current_time = arrow.now().replace(second=0, microsecond=0)
current_time = arrow.now().shift(hours=self.timezone_offset).replace(second=0, microsecond=0)
# 创建一个 croniter 对象
cron = croniter(self.cron_expression, current_time.datetime)
next_time = cron.get_next(datetime)
Expand Down Expand Up @@ -569,7 +573,7 @@ def get_short_id(self, string):
def is_nowTime(self):

# 获取当前时间(忽略秒数)
current_time = arrow.now().format('HH:mm')
current_time = arrow.now().shift(hours=self.timezone_offset).format('HH:mm')

#cron
if self.isCron_time():
Expand Down Expand Up @@ -600,7 +604,7 @@ def is_featureTime(self):
return True
else:
#对比精准到分(忽略秒)
current_time = arrow.now().replace(second=0, microsecond=0).time()
current_time = arrow.now().shift(hours=self.timezone_offset).replace(second=0, microsecond=0).time()
task_time = arrow.get(tempTimeStr, "HH:mm:ss").replace(second=0, microsecond=0).time()
tempValue = task_time > current_time
return tempValue
Expand All @@ -616,7 +620,7 @@ def is_featureDay(self):
tempValue = "每周" in tempStr or "每星期" in tempStr or "每天" in tempStr or "工作日" in tempStr
#日期
if self.is_valid_date(tempStr):
tempValue = arrow.get(tempStr, 'YYYY-MM-DD').date() > arrow.now().date()
tempValue = arrow.get(tempStr, 'YYYY-MM-DD').date() > arrow.now().shift(hours=self.timezone_offset).date()

return tempValue

Expand All @@ -627,7 +631,7 @@ def is_today(self):
return True

#当前时间
current_time = arrow.now()
current_time = arrow.now().shift(hours=self.timezone_offset)
#轮询信息
item_circle = self.circleTimeStr
if self.is_valid_date(item_circle):
Expand Down Expand Up @@ -656,7 +660,7 @@ def is_today(self):

elif "工作日" in item_circle:
# 判断星期几
weekday = arrow.now().weekday()
weekday = arrow.now().shift(hours=self.timezone_offset).weekday()
# 判断是否是工作日
is_weekday = weekday < 5
if is_weekday:
Expand All @@ -675,7 +679,7 @@ def is_today_weekday(self, weekday_str):
return False

# 判断今天是否是指定的星期几
today = arrow.now()
today = arrow.now().shift(hours=self.timezone_offset)
tempValue = today.weekday() == weekday_num - 1
return tempValue

Expand All @@ -697,7 +701,7 @@ def get_cicleDay(self, circleStr):
#如果可被解析为具体日期
if circleStr in ['今天', '明天', '后天']:
#今天
today = arrow.now('local')
today = arrow.now('local').shift(hours=self.timezone_offset)
if circleStr == '今天':
# 将日期格式化为 YYYY-MM-DD 的格式
formatted_today = today.format('YYYY-MM-DD')
Expand Down
1 change: 1 addition & 0 deletions config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"command_prefix": "$time",
"debug": false,
"time_check_rate": 1,
"timezone_offset": 0,
"move_historyTask_time": "04:00:00",
"is_open_route_everyReply": true,
"is_open_extension_function": true,
Expand Down
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"command_prefix": "$time",
"debug": false,
"time_check_rate": 1,
"timezone_offset": 0,
"move_historyTask_time": "04:00:00",
"is_open_route_everyReply": true,
"is_open_extension_function": true,
Expand Down
2 changes: 1 addition & 1 deletion timetask.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ def runTimeTask(self, model: TimeTaskModel):
#时间
if self.conf.get("is_need_currentTime_whenNormalReply", True):
# 获取当前时间
current_time = arrow.now()
current_time = arrow.now().shift(hours=self.conf.get("timezone_offset", 0))
# 去除秒钟
current_time_without_seconds = current_time.floor('minute')
# 转换为指定格式的字符串
Expand Down