From 6256af58345324a9be61992d31bdfb0b2ea6ade7 Mon Sep 17 00:00:00 2001 From: NY Date: Thu, 5 Jun 2025 16:40:35 +0800 Subject: [PATCH] timestamp --- scripts.db | Bin 32768 -> 32768 bytes scripts/时间戳.py | 41 ++++++++++++++++++ scripts/时间戳.ui.py | 98 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 scripts/时间戳.py create mode 100644 scripts/时间戳.ui.py diff --git a/scripts.db b/scripts.db index 6eaf2a8b207b04d95eeb0df36a2ad61c088eeffc..42aa739088c8e0b483186f05c466299e0dc9be68 100644 GIT binary patch delta 376 zcmZo@U}|V!njp>SI#I@%(RE|O5`I2b{vZbaApYC@>-i`0S8Wy)aNyTaXAWkJR*ES# zGfgv#DYZ0BGXb)VjE$9a6qHIbb5n~;5_1cbVp%s=$+rq{vhp%8FtGD)VA;%Ju#caQ zhn1N@{@L_xFQ;#L*0Fi=6?i#4RrpwWwlVNm^X=#T z#%s;94QTEXp87^6RxSp4Q^#NqYffpn;l;^CnFS@qF>nFBf=VL;Bh#llw-}f`o6})n o`fU1A1w%6{V@oR&Qz8NZ)4SJWS`3_>c2% List[str]: + """处理时间戳和文本的相互转换""" + try: + if len(input_strings) != 3: + return ["错误:输入格式不正确,期望 [mode, input, timezone]"] + mode, input_str, timezone = input_strings + + # 验证时区 + try: + tz = pytz.timezone(timezone) + except pytz.exceptions.UnknownTimeZoneError: + return ["错误:无效的时区"] + + if mode == "timestamp_to_text": + try: + timestamp = int(input_str) + dt = datetime.fromtimestamp(timestamp, tz=tz) + return [dt.strftime("%Y-%m-%d %H:%M:%S %Z")] + except ValueError: + return ["错误:请输入有效的时间戳"] + + elif mode == "text_to_timestamp": + try: + # 尝试两种格式 + try: + dt = datetime.strptime(input_str, "%Y-%m-%d %H:%M:%S") + except ValueError: + dt = datetime.strptime(input_str, "%Y年%m月%d日 %H:%M:%S") + dt = tz.localize(dt) + return [str(int(dt.timestamp()))] + except ValueError: + return ["错误:请输入有效的日期格式(YYYY-MM-DD HH:MM:SS 或 YYYY年MM月DD日 HH:MM:SS)"] + + else: + return ["错误:无效的转换模式"] + except Exception as e: + return [f"错误:{str(e)}"] \ No newline at end of file diff --git a/scripts/时间戳.ui.py b/scripts/时间戳.ui.py new file mode 100644 index 0000000..930c802 --- /dev/null +++ b/scripts/时间戳.ui.py @@ -0,0 +1,98 @@ +import tkinter as tk +from tkinter import ttk +import pyperclip +from tkinter import messagebox +from datetime import datetime +import pytz + +def create_ui(parent, run_callback): + """创建时间戳转换前端界面:时间戳转文本和文本转时间戳""" + frame = ttk.Frame(parent) + frame.grid(row=0, column=0, sticky=(tk.W, tk.E, tk.N, tk.S)) + 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"] + + # 上半部分:时间戳转文本 + 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.columnconfigure(1, weight=1) + + ttk.Label(timestamp_frame, text="时间戳:").grid(row=0, column=0, sticky=tk.W, pady=2) + timestamp_entry = ttk.Entry(timestamp_frame) + timestamp_entry.insert(0, str(int(datetime.now().timestamp()))) # 默认当前时间戳 + timestamp_entry.grid(row=0, column=1, sticky=(tk.W, tk.E), 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") + ttk.OptionMenu(timestamp_frame, timestamp_tz_var, "Asia/Shanghai", *timezones).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) + + def copy_timestamp_result(event): + output = timestamp_result.get("1.0", tk.END).strip() + if output: + pyperclip.copy(output) + messagebox.showinfo("提示", "结果已复制到剪贴板") + timestamp_result.bind("", copy_timestamp_result) + + def convert_timestamp(): + timestamp = timestamp_entry.get().strip() + if not timestamp: + messagebox.showerror("错误", "请输入时间戳") + return + result = run_callback(["timestamp_to_text", timestamp, timestamp_tz_var.get()]) + timestamp_result.configure(state="normal") + timestamp_result.delete("1.0", tk.END) + timestamp_result.insert("1.0", result[0] if result else "无结果") + timestamp_result.configure(state="disabled") + + ttk.Button(timestamp_frame, text="转换", command=convert_timestamp).grid(row=3, column=0, columnspan=2, pady=5) + + # 分割线 + 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.grid(row=2, column=0, columnspan=2, sticky=(tk.W, tk.E, tk.N), pady=5) + text_frame.columnconfigure(1, weight=1) + + ttk.Label(text_frame, text="日期时间:").grid(row=0, column=0, sticky=tk.W, pady=2) + text_entry = ttk.Entry(text_frame) + text_entry.insert(0, "2025-06-05 16:29:11") # 默认时间 + text_entry.grid(row=0, column=1, sticky=(tk.W, tk.E), 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") + ttk.OptionMenu(text_frame, text_tz_var, "Asia/Shanghai", *timezones).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) + + def copy_text_result(event): + output = text_result.get("1.0", tk.END).strip() + if output: + pyperclip.copy(output) + messagebox.showinfo("提示", "结果已复制到剪贴板") + text_result.bind("", copy_text_result) + + def convert_text(): + text = text_entry.get().strip() + if not text: + messagebox.showerror("错误", "请输入日期时间") + return + result = run_callback(["text_to_timestamp", text, text_tz_var.get()]) + text_result.configure(state="normal") + text_result.delete("1.0", tk.END) + text_result.insert("1.0", result[0] if result else "无结果") + text_result.configure(state="disabled") + + ttk.Button(text_frame, text="转换", command=convert_text).grid(row=3, column=0, columnspan=2, pady=5) + + return frame \ No newline at end of file