mirror of
https://github.com/TeamPGM/PagerMaid_Plugins_Pyro.git
synced 2024-11-16 08:19:25 +00:00
unixtime Unix时间戳转换 (Sourcery refactored) (#58)
Co-authored-by: zhxy-CN <admin@owo.cab> Co-authored-by: Sourcery AI <> Co-authored-by: omg-xtao <100690902+omg-xtao@users.noreply.github.com>
This commit is contained in:
parent
81f63f8826
commit
9b36628237
45
unixtime/main.py
Normal file
45
unixtime/main.py
Normal file
@ -0,0 +1,45 @@
|
||||
import re
|
||||
import time
|
||||
|
||||
from pagermaid.listener import listener
|
||||
from pagermaid.enums import Message
|
||||
|
||||
|
||||
def time_to_unix(t: str) -> int:
|
||||
"""将时间转换为Unix时间戳"""
|
||||
return int(time.mktime(time.strptime(t, "%Y-%m-%d %H:%M:%S")))
|
||||
|
||||
|
||||
def unix_to_time(t: int) -> str:
|
||||
"""将Unix时间戳转换为时间"""
|
||||
return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(float(t)))
|
||||
|
||||
|
||||
def match_datetime(text):
|
||||
"""正则表达式提取文本所有日期+时间"""
|
||||
pattern = r"(\d{4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2}:\d{1,2})"
|
||||
pattern = re.compile(pattern)
|
||||
return pattern.findall(text)
|
||||
|
||||
|
||||
def format_time(t):
|
||||
if not t:
|
||||
t = time.time()
|
||||
try:
|
||||
t = int(t)
|
||||
except ValueError as e:
|
||||
if match := match_datetime(t):
|
||||
t = time_to_unix(match[0])
|
||||
else:
|
||||
raise ValueError from e
|
||||
return f"时间:`{unix_to_time(t)}`\n\n时间戳:`{t}`"
|
||||
|
||||
|
||||
@listener(command="unixtime",
|
||||
description="Unix时间戳转换\n参数缺省将当前服务器时间转换为Unix时间戳\n时间格式: `YYYY-MM-DD HH:MM:SS`",
|
||||
parameters="<缺省 / 时间 / Unix时间戳>")
|
||||
async def unix_time(message: Message):
|
||||
try:
|
||||
return await message.edit(format_time(message.arguments))
|
||||
except Exception as e:
|
||||
return await message.edit(f"出错了呜呜呜 ~ 无效的参数:{e}")
|
Loading…
Reference in New Issue
Block a user