【纯手工打造】时间戳转换工具(python)

打印 上一主题 下一主题

主题 532|帖子 532|积分 1596

1.背景

最近发现一个事情,如果日志中的时间戳,需要我们转换成时间,增加可读性。或者将时间转换成时间戳,来配置时间。相信大多人和我一样,都是打开网页,搜索在线时间戳转换工具,然后复制粘贴进去。个人认为可以手工打造一个python版本的时间戳转换工具,来解放双手,减少打开网页的时间,于是乎就产生了自己做个小工具的想法。
2.实际效果

废话不多说,直接上做出来的工具截图

是的,你没看错,这工具是有可视化界面的,而且是exe单文件,window上可以直接运行,而且拥有多个复制按钮,方便我们操作
3.实现原理

借用python的tmath库、datetime库轻松搞定,然后借助tkinter实现可视化界面,借用pyinstaller命令,将python项目一键构建exe单文件。这些库都可以用pip安装,不再赘述。
4.源码

废话不多说,直接上源码
  1. import tkinter as tk
  2. from datetime import datetime
  3. import math, pyperclip, os
  4. class DateTimeConverterApp:
  5.     def __init__(self, master):
  6.         self.master = master
  7.         self.master.title("Tom-时间戳工具")
  8.         # Left Frame
  9.         self.left_frame = tk.Frame(self.master)
  10.         self.left_frame.grid(row=0, column=0, padx=20, pady=20)
  11.         self.clock_canvas = tk.Canvas(self.left_frame, width=300, height=300)
  12.         self.clock_canvas.grid(row=0, column=0, columnspan=2)
  13.         # Current DateTime Label and Copy Button
  14.         self.current_datetime_label = tk.Label(self.left_frame, text="时间:")
  15.         self.current_datetime_label.grid(row=1, column=0, sticky="e", padx=10)
  16.         self.copy_datetime_button = tk.Button(self.left_frame, text="复制", command=self.copy_current_datetime)
  17.         self.copy_datetime_button.grid(row=1, column=1, sticky="w", pady=5)
  18.         # Current Timestamp Label and Copy Button
  19.         self.current_timestamp_label = tk.Label(self.left_frame, text="时间戳:")
  20.         self.current_timestamp_label.grid(row=2, column=0, sticky="e", padx=10)
  21.         self.copy_timestamp_button = tk.Button(self.left_frame, text="复制", command=self.copy_current_timestamp)
  22.         self.copy_timestamp_button.grid(row=2, column=1, sticky="w", pady=5)
  23.         # Right Frame
  24.         self.right_frame = tk.Frame(self.master)
  25.         self.right_frame.grid(row=0, column=2, padx=20, pady=20)
  26.         self.right_label1 = tk.Label(self.right_frame, text="请输入时间戳")
  27.         self.right_label1.grid(row=1, column=0, padx=10)
  28.         self.timestamp_entry = tk.Entry(self.right_frame)
  29.         self.timestamp_entry.grid(row=1, column=1, padx=10)
  30.         self.convert_button = tk.Button(self.right_frame, text="转为时间", command=self.convert_to_datetime)
  31.         self.convert_button.grid(row=1, column=2, padx=10)
  32.         self.right_label2 = tk.Label(self.right_frame, text="转换结果")
  33.         self.right_label2.grid(row=2, column=0, padx=10)
  34.         self.result_label = tk.Label(self.right_frame, text="1970-01-01 00:00:00")
  35.         self.result_label.grid(row=2, column=1, padx=10)
  36.         self.copy_result_button = tk.Button(self.right_frame, text="复制结果", command=self.copy_result_datetime)
  37.         self.copy_result_button.grid(row=2, column=2, padx=10)
  38.         tk.Label(self.right_frame, text="").grid(row=3)
  39.         tk.Label(self.right_frame, text="").grid(row=4)
  40.         self.right_label3 = tk.Label(self.right_frame, text="请输入日期时间")
  41.         self.right_label3.grid(row=5, column=0, padx=10)
  42.         
  43.         self.datetime_entry = tk.Entry(self.right_frame)
  44.         self.datetime_entry.grid(row=5, column=1, padx=10)
  45.         self.convert_button2 = tk.Button(self.right_frame, text="转为时间戳", command=self.convert_to_timestamp)
  46.         self.convert_button2.grid(row=5, column=2, padx=10)
  47.         self.right_label4 = tk.Label(self.right_frame, text="转换结果")
  48.         self.right_label4.grid(row=6, column=0, padx=10)
  49.         self.result_label2 = tk.Label(self.right_frame, text="0000000000", anchor="w")
  50.         self.result_label2.grid(row=6, column=1, padx=10)
  51.         self.copy_result_button2 = tk.Button(self.right_frame, text="复制结果", command=self.copy_result_timestamp)
  52.         self.copy_result_button2.grid(row=6, column=2, padx=10)
  53.         # Start updating the clock
  54.         self.update_clock()
  55.         # Start updating current time and timestamp
  56.         self.update_current_datetime_and_timestamp()
  57.     def update_current_datetime_and_timestamp(self):
  58.         current_datetime = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  59.         current_timestamp = self.datetime_to_timestamp(datetime.now())
  60.         self.current_datetime_label.config(text=f"时间: {current_datetime}")
  61.         self.current_timestamp_label.config(text=f"时间戳: {current_timestamp}")
  62.         # Schedule the update after 1000 milliseconds (1 second)
  63.         self.master.after(1000, self.update_current_datetime_and_timestamp)
  64.    
  65.     def update_clock(self):
  66.         # Clear the canvas
  67.         self.clock_canvas.delete("all")
  68.         # Get the current time
  69.         current_time = datetime.now()
  70.         hours = current_time.hour
  71.         minutes = current_time.minute
  72.         seconds = current_time.second
  73.         # Draw clock face
  74.         self.clock_canvas.create_oval(50, 50, 250, 250, fill="#C7DFEE")
  75.         # Draw hour hand
  76.         hour_angle = math.radians((hours % 12) * 30 - 90)
  77.         hour_length = 50
  78.         hour_x = 150 + hour_length * math.cos(hour_angle)
  79.         hour_y = 150 + hour_length * math.sin(hour_angle)
  80.         self.clock_canvas.create_line(150, 150, hour_x, hour_y, width=4, fill="blue")
  81.         # Draw minute hand
  82.         minute_angle = math.radians(minutes * 6 - 90)
  83.         minute_length = 80
  84.         minute_x = 150 + minute_length * math.cos(minute_angle)
  85.         minute_y = 150 + minute_length * math.sin(minute_angle)
  86.         self.clock_canvas.create_line(150, 150, minute_x, minute_y, width=3, fill="green")
  87.         # Draw second hand
  88.         second_angle = math.radians(seconds * 6 - 90)
  89.         second_length = 100
  90.         second_x = 150 + second_length * math.cos(second_angle)
  91.         second_y = 150 + second_length * math.sin(second_angle)
  92.         self.clock_canvas.create_line(150, 150, second_x, second_y, width=2, fill="red")
  93.         # Draw clock numbers
  94.         for i in range(12):
  95.             angle = math.radians(i * 30 - 60)
  96.             num_x = 150 + 90 * math.cos(angle)
  97.             num_y = 150 + 90 * math.sin(angle)
  98.             self.clock_canvas.create_text(num_x, num_y, text=str(i + 1), font=("Helvetica", 12, "bold"))
  99.         # Schedule the update after 1000 milliseconds (1 second)
  100.         self.master.after(1000, self.update_clock)
  101.     def convert_to_datetime(self):
  102.         input_str = self.timestamp_entry.get()
  103.         try:
  104.             timestamp = float(input_str)
  105.             result = self.timestamp_to_datetime(timestamp)
  106.             self.result_label.config(text=result)
  107.         except ValueError:
  108.             self.result_label.config(text="输入的格式错误")
  109.     def convert_to_timestamp(self):
  110.         input_str = self.datetime_entry.get()
  111.         try:
  112.             datetime_obj = datetime.strptime(input_str, '%Y-%m-%d %H:%M:%S')
  113.             result = self.datetime_to_timestamp(datetime_obj)
  114.             self.result_label2.config(text=result)
  115.         except ValueError:
  116.             self.result_label2.config(text="输入的格式错误")
  117.     def datetime_to_timestamp(self, dt):
  118.         timestamp = (dt - datetime(1970, 1, 1)).total_seconds()
  119.         return int(timestamp)
  120.     def timestamp_to_datetime(self, timestamp):
  121.         dt = datetime.utcfromtimestamp(timestamp)
  122.         return dt.strftime('%Y-%m-%d %H:%M:%S')
  123.    
  124.     def copy_result_datetime(self):
  125.         result_datetime = self.result_label.cget("text")
  126.         pyperclip.copy(result_datetime)
  127.     def copy_result_timestamp(self):
  128.         result_timestamp = self.result_label2.cget("text")
  129.         pyperclip.copy(result_timestamp)
  130.     def copy_current_datetime(self):
  131.         current_datetime = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  132.         pyperclip.copy(current_datetime)
  133.     def copy_current_timestamp(self):
  134.         current_timestamp = self.datetime_to_timestamp(datetime.now())
  135.         pyperclip.copy(str(current_timestamp))
  136. if __name__ == "__main__":
  137.     root = tk.Tk()
  138.     app = DateTimeConverterApp(root)
  139.     root.mainloop()
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

钜形不锈钢水箱

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表