timestamp+1
This commit is contained in:
parent
6256af5834
commit
6d0ed6372e
BIN
scripts.db
BIN
scripts.db
Binary file not shown.
@ -2,6 +2,15 @@ from datetime import datetime
|
|||||||
import pytz
|
import pytz
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
|
# 预缓存常用时区
|
||||||
|
TIMEZONE_CACHE = {
|
||||||
|
"Asia/Shanghai": pytz.timezone("Asia/Shanghai"),
|
||||||
|
"UTC": pytz.timezone("UTC"),
|
||||||
|
"America/New_York": pytz.timezone("America/New_York"),
|
||||||
|
"Europe/London": pytz.timezone("Europe/London"),
|
||||||
|
"Australia/Sydney": pytz.timezone("Australia/Sydney")
|
||||||
|
}
|
||||||
|
|
||||||
def process(input_strings: List[str]) -> List[str]:
|
def process(input_strings: List[str]) -> List[str]:
|
||||||
"""处理时间戳和文本的相互转换"""
|
"""处理时间戳和文本的相互转换"""
|
||||||
try:
|
try:
|
||||||
@ -9,10 +18,9 @@ def process(input_strings: List[str]) -> List[str]:
|
|||||||
return ["错误:输入格式不正确,期望 [mode, input, timezone]"]
|
return ["错误:输入格式不正确,期望 [mode, input, timezone]"]
|
||||||
mode, input_str, timezone = input_strings
|
mode, input_str, timezone = input_strings
|
||||||
|
|
||||||
# 验证时区
|
# 从缓存获取时区
|
||||||
try:
|
tz = TIMEZONE_CACHE.get(timezone)
|
||||||
tz = pytz.timezone(timezone)
|
if not tz:
|
||||||
except pytz.exceptions.UnknownTimeZoneError:
|
|
||||||
return ["错误:无效的时区"]
|
return ["错误:无效的时区"]
|
||||||
|
|
||||||
if mode == "timestamp_to_text":
|
if mode == "timestamp_to_text":
|
||||||
@ -25,7 +33,6 @@ def process(input_strings: List[str]) -> List[str]:
|
|||||||
|
|
||||||
elif mode == "text_to_timestamp":
|
elif mode == "text_to_timestamp":
|
||||||
try:
|
try:
|
||||||
# 尝试两种格式
|
|
||||||
try:
|
try:
|
||||||
dt = datetime.strptime(input_str, "%Y-%m-%d %H:%M:%S")
|
dt = datetime.strptime(input_str, "%Y-%m-%d %H:%M:%S")
|
||||||
except ValueError:
|
except ValueError:
|
||||||
@ -35,7 +42,6 @@ def process(input_strings: List[str]) -> List[str]:
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
return ["错误:请输入有效的日期格式(YYYY-MM-DD HH:MM:SS 或 YYYY年MM月DD日 HH:MM:SS)"]
|
return ["错误:请输入有效的日期格式(YYYY-MM-DD HH:MM:SS 或 YYYY年MM月DD日 HH:MM:SS)"]
|
||||||
|
|
||||||
else:
|
return ["错误:无效的转换模式"]
|
||||||
return ["错误:无效的转换模式"]
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return [f"错误:{str(e)}"]
|
return [f"错误:{str(e)}"]
|
@ -3,22 +3,19 @@ from tkinter import ttk
|
|||||||
import pyperclip
|
import pyperclip
|
||||||
from tkinter import messagebox
|
from tkinter import messagebox
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import pytz
|
|
||||||
|
|
||||||
def create_ui(parent, run_callback):
|
def create_ui(parent, run_callback):
|
||||||
"""创建时间戳转换前端界面:时间戳转文本和文本转时间戳"""
|
"""创建时间戳转换前端界面:时间戳转文本和文本转时间戳"""
|
||||||
frame = ttk.Frame(parent)
|
frame = ttk.Frame(parent)
|
||||||
frame.grid(row=0, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
|
frame.grid(row=0, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
|
||||||
frame.columnconfigure(1, weight=1)
|
frame.columnconfigure(1, weight=1)
|
||||||
frame.rowconfigure(0, weight=1)
|
|
||||||
frame.rowconfigure(2, weight=1)
|
|
||||||
|
|
||||||
# 时区选项
|
# 时区选项
|
||||||
timezones = ["Asia/Shanghai", "UTC", "America/New_York", "Europe/London", "Australia/Sydney"]
|
timezones = ["Asia/Shanghai", "UTC", "America/New_York", "Europe/London", "Australia/Sydney"]
|
||||||
|
|
||||||
# 上半部分:时间戳转文本
|
# 上半部分:时间戳转文本
|
||||||
timestamp_frame = ttk.LabelFrame(frame, text="时间戳转文本", padding=5)
|
timestamp_frame = ttk.LabelFrame(frame, text="时间戳转文本", padding=5)
|
||||||
timestamp_frame.grid(row=0, column=0, columnspan=2, sticky=(tk.W, tk.E, tk.N), pady=5)
|
timestamp_frame.grid(row=0, column=0, columnspan=2, sticky=(tk.W, tk.E), pady=5)
|
||||||
timestamp_frame.columnconfigure(1, weight=1)
|
timestamp_frame.columnconfigure(1, weight=1)
|
||||||
|
|
||||||
ttk.Label(timestamp_frame, text="时间戳:").grid(row=0, column=0, sticky=tk.W, pady=2)
|
ttk.Label(timestamp_frame, text="时间戳:").grid(row=0, column=0, sticky=tk.W, pady=2)
|
||||||
@ -28,18 +25,17 @@ def create_ui(parent, run_callback):
|
|||||||
|
|
||||||
ttk.Label(timestamp_frame, text="时区:").grid(row=1, column=0, sticky=tk.W, pady=2)
|
ttk.Label(timestamp_frame, text="时区:").grid(row=1, column=0, sticky=tk.W, pady=2)
|
||||||
timestamp_tz_var = tk.StringVar(value="Asia/Shanghai")
|
timestamp_tz_var = tk.StringVar(value="Asia/Shanghai")
|
||||||
ttk.OptionMenu(timestamp_frame, timestamp_tz_var, "Asia/Shanghai", *timezones).grid(row=1, column=1, sticky=tk.W, pady=2)
|
ttk.Combobox(timestamp_frame, textvariable=timestamp_tz_var, values=timezones, state="readonly").grid(row=1, column=1, sticky=tk.W, pady=2)
|
||||||
|
|
||||||
timestamp_result = tk.Text(timestamp_frame, height=1, width=30, state="disabled")
|
|
||||||
timestamp_result.grid(row=2, column=1, sticky=(tk.W, tk.E), pady=2)
|
|
||||||
ttk.Label(timestamp_frame, text="结果:").grid(row=2, column=0, sticky=tk.W, pady=2)
|
ttk.Label(timestamp_frame, text="结果:").grid(row=2, column=0, sticky=tk.W, pady=2)
|
||||||
|
timestamp_result = ttk.Entry(timestamp_frame, state="readonly")
|
||||||
|
timestamp_result.grid(row=2, column=1, sticky=(tk.W, tk.E), pady=2)
|
||||||
|
|
||||||
def copy_timestamp_result(event):
|
def copy_timestamp_result():
|
||||||
output = timestamp_result.get("1.0", tk.END).strip()
|
output = timestamp_result.get()
|
||||||
if output:
|
if output:
|
||||||
pyperclip.copy(output)
|
pyperclip.copy(output)
|
||||||
messagebox.showinfo("提示", "结果已复制到剪贴板")
|
messagebox.showinfo("提示", "结果已复制到剪贴板")
|
||||||
timestamp_result.bind("<Button-1>", copy_timestamp_result)
|
|
||||||
|
|
||||||
def convert_timestamp():
|
def convert_timestamp():
|
||||||
timestamp = timestamp_entry.get().strip()
|
timestamp = timestamp_entry.get().strip()
|
||||||
@ -48,18 +44,19 @@ def create_ui(parent, run_callback):
|
|||||||
return
|
return
|
||||||
result = run_callback(["timestamp_to_text", timestamp, timestamp_tz_var.get()])
|
result = run_callback(["timestamp_to_text", timestamp, timestamp_tz_var.get()])
|
||||||
timestamp_result.configure(state="normal")
|
timestamp_result.configure(state="normal")
|
||||||
timestamp_result.delete("1.0", tk.END)
|
timestamp_result.delete(0, tk.END)
|
||||||
timestamp_result.insert("1.0", result[0] if result else "无结果")
|
timestamp_result.insert(0, result[0] if result else "无结果")
|
||||||
timestamp_result.configure(state="disabled")
|
timestamp_result.configure(state="readonly")
|
||||||
|
|
||||||
ttk.Button(timestamp_frame, text="转换", command=convert_timestamp).grid(row=3, column=0, columnspan=2, pady=5)
|
ttk.Button(timestamp_frame, text="转换", command=convert_timestamp).grid(row=3, column=0, columnspan=2, pady=5)
|
||||||
|
timestamp_result.bind("<Double-1>", lambda e: copy_timestamp_result()) # 双击复制
|
||||||
|
|
||||||
# 分割线
|
# 分割线
|
||||||
ttk.Separator(frame, orient=tk.HORIZONTAL).grid(row=1, column=0, columnspan=2, sticky=(tk.W, tk.E), pady=10)
|
ttk.Separator(frame, orient=tk.HORIZONTAL).grid(row=1, column=0, columnspan=2, sticky=(tk.W, tk.E), pady=10)
|
||||||
|
|
||||||
# 下半部分:文本转时间戳
|
# 下半部分:文本转时间戳
|
||||||
text_frame = ttk.LabelFrame(frame, text="文本转时间戳", padding=5)
|
text_frame = ttk.LabelFrame(frame, text="文本转时间戳", padding=5)
|
||||||
text_frame.grid(row=2, column=0, columnspan=2, sticky=(tk.W, tk.E, tk.N), pady=5)
|
text_frame.grid(row=2, column=0, columnspan=2, sticky=(tk.W, tk.E), pady=5)
|
||||||
text_frame.columnconfigure(1, weight=1)
|
text_frame.columnconfigure(1, weight=1)
|
||||||
|
|
||||||
ttk.Label(text_frame, text="日期时间:").grid(row=0, column=0, sticky=tk.W, pady=2)
|
ttk.Label(text_frame, text="日期时间:").grid(row=0, column=0, sticky=tk.W, pady=2)
|
||||||
@ -69,18 +66,17 @@ def create_ui(parent, run_callback):
|
|||||||
|
|
||||||
ttk.Label(text_frame, text="时区:").grid(row=1, column=0, sticky=tk.W, pady=2)
|
ttk.Label(text_frame, text="时区:").grid(row=1, column=0, sticky=tk.W, pady=2)
|
||||||
text_tz_var = tk.StringVar(value="Asia/Shanghai")
|
text_tz_var = tk.StringVar(value="Asia/Shanghai")
|
||||||
ttk.OptionMenu(text_frame, text_tz_var, "Asia/Shanghai", *timezones).grid(row=1, column=1, sticky=tk.W, pady=2)
|
ttk.Combobox(text_frame, textvariable=text_tz_var, values=timezones, state="readonly").grid(row=1, column=1, sticky=tk.W, pady=2)
|
||||||
|
|
||||||
text_result = tk.Text(text_frame, height=1, width=30, state="disabled")
|
|
||||||
text_result.grid(row=2, column=1, sticky=(tk.W, tk.E), pady=2)
|
|
||||||
ttk.Label(text_frame, text="结果:").grid(row=2, column=0, sticky=tk.W, pady=2)
|
ttk.Label(text_frame, text="结果:").grid(row=2, column=0, sticky=tk.W, pady=2)
|
||||||
|
text_result = ttk.Entry(text_frame, state="readonly")
|
||||||
|
text_result.grid(row=2, column=1, sticky=(tk.W, tk.E), pady=2)
|
||||||
|
|
||||||
def copy_text_result(event):
|
def copy_text_result():
|
||||||
output = text_result.get("1.0", tk.END).strip()
|
output = text_result.get()
|
||||||
if output:
|
if output:
|
||||||
pyperclip.copy(output)
|
pyperclip.copy(output)
|
||||||
messagebox.showinfo("提示", "结果已复制到剪贴板")
|
messagebox.showinfo("提示", "结果已复制到剪贴板")
|
||||||
text_result.bind("<Button-1>", copy_text_result)
|
|
||||||
|
|
||||||
def convert_text():
|
def convert_text():
|
||||||
text = text_entry.get().strip()
|
text = text_entry.get().strip()
|
||||||
@ -89,10 +85,11 @@ def create_ui(parent, run_callback):
|
|||||||
return
|
return
|
||||||
result = run_callback(["text_to_timestamp", text, text_tz_var.get()])
|
result = run_callback(["text_to_timestamp", text, text_tz_var.get()])
|
||||||
text_result.configure(state="normal")
|
text_result.configure(state="normal")
|
||||||
text_result.delete("1.0", tk.END)
|
text_result.delete(0, tk.END)
|
||||||
text_result.insert("1.0", result[0] if result else "无结果")
|
text_result.insert(0, result[0] if result else "无结果")
|
||||||
text_result.configure(state="disabled")
|
text_result.configure(state="readonly")
|
||||||
|
|
||||||
ttk.Button(text_frame, text="转换", command=convert_text).grid(row=3, column=0, columnspan=2, pady=5)
|
ttk.Button(text_frame, text="转换", command=convert_text).grid(row=3, column=0, columnspan=2, pady=5)
|
||||||
|
text_result.bind("<Double-1>", lambda e: copy_text_result()) # 双击复制
|
||||||
|
|
||||||
return frame
|
return frame
|
Loading…
x
Reference in New Issue
Block a user