python教程

Python时间戳互转

我的站长站 2023-08-12 人阅读

查询时间:可查询当前时间与时间戳
时间戳转时间:在前面输入框中输入时间戳点击按钮可转成时间
时间转时间戳:在前面输入框中输入时间点击按钮可转成时间戳,时间格式如果不知道可复制查询时间中的时间格式

import time
import os
import tkinter as tk
import threading
import datetime
 
 
def sjc():
    t.delete(1.0, tk.END)
    i = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    print(i)
    t.insert('end', '当前时间:')
    t.insert(tk.INSERT, '\n')
    t.insert('end', i)
    t.insert(tk.INSERT, '\n')
    # 转换成时间数组
    timeArray = time.strptime(i, "%Y-%m-%d %H:%M:%S")
    # 转换成时间戳
    timestamp = int(time.mktime(timeArray))
    print(timestamp)
    t.insert('end', '当前时间戳:')
    t.insert('end', timestamp)
    t.insert(tk.INSERT, '\n')
 
    # utc5_beginTime = (int(timestamp/86400)+1)*86400+18000
    # utc5_endTime = utc5_beginTime+86400*(int(hdts)-1)+79200
    # print("----------utc-5")
    # print(utc5_beginTime)
    # print(utc5_endTime)
    # t.insert('end',"----------utc-5")
    # t.insert(tk.INSERT, '\n')
    # t.insert('end', utc5_beginTime)
    # t.insert(tk.INSERT, '\n')
    # t.insert('end', utc5_endTime)
    # t.insert(tk.INSERT, '\n')
    # utc2_beginTime = (int(timestamp/86400)+1)*86400-7200
    # utc2_endTime = utc2_beginTime+86400*(int(hdts)-1)+79200
    # print("----------utc+2")
    # print(utc2_beginTime)
    # print(utc2_endTime)
    # t.insert('end',"----------utc+2")
    # t.insert(tk.INSERT, '\n')
    # t.insert('end', utc2_beginTime)
    # t.insert(tk.INSERT, '\n')
    # t.insert('end', utc2_endTime)
    # t.insert(tk.INSERT, '\n')
    # utc8_beginTime = (int(timestamp/86400)+1)*86400-28800
    # utc8_endTime = utc2_beginTime+86400*(int(hdts)-1)+79200
    # print("----------utc+8")
    # print(utc8_beginTime)
    # print(utc8_endTime)
    # t.insert('end',"----------utc+8")
    # t.insert(tk.INSERT, '\n')
    # t.insert('end', utc8_beginTime)
    # t.insert(tk.INSERT, '\n')
    # t.insert('end', utc8_endTime)
    # t.insert(tk.INSERT, '\n')
def zsj():
    hdts = txt.get()
    if hdts == "输入时间戳":
        t.insert('end', '输入时间戳')
        t.insert(tk.INSERT, '\n')
        return
    t.insert('end', '时间戳:')
    t.insert('end', hdts)
    t.insert(tk.INSERT, '\n')
    # 将时间戳转换为datetime对象
    dt_object = datetime.datetime.fromtimestamp(int(hdts))
 
    # 格式化输出时间
    formatted_time = dt_object.strftime("%Y-%m-%d %H:%M:%S")
 
    print("转换后的时间:", formatted_time)
    t.insert('end', '时间:')
    t.insert('end', formatted_time)
    t.insert(tk.INSERT, '\n')
def zsjc():
    sj = txt1.get()
    if sj == None:
        t.insert('end', '输入时间')
        t.insert(tk.INSERT, '\n')
        print('输入时间')
        return
    t.insert('end', '时间:')
    t.insert('end', sj)
    t.insert(tk.INSERT, '\n')
    # 转换成时间数组
    timeArray = time.strptime(sj, "%Y-%m-%d %H:%M:%S")
    print(timeArray)
    # 转换成时间戳
    timestamp = int(time.mktime(timeArray))
    print(timestamp)
    t.insert('end', '时间戳:')
    t.insert('end', timestamp)
    t.insert(tk.INSERT, '\n')
 
def thread_it(func, *args):
    '''将函数打包进线程'''
    k = threading.Thread(target=func, args=args)
    k.setDaemon(True)
    k.start()
 
 
def get_servertime(self):
    """
    获取服务器当前时间
    :return:当前服务器时间戳
    """
    req_body = self.get_request_body('gmGetServerTime')
    rsp = self.send_http_request(req_body).json()
    print(rsp['data']['serverTime'])
    return rsp['data']['serverTime']
 
 
if __name__ == '__main__':
    window = tk.Tk()
    window.title('sjc')
    window.geometry("200x300")
    t = tk.Text(window, width='250', height=15, bg='LightBlue')
    t.pack(side='bottom')
    btn1 = tk.Button(window, text="查询时间", bg="orange", command=lambda: thread_it(sjc))
    btn1.place(x=110, y=5)
    btn2 = tk.Button(window, text="时间戳转时间", bg="orange", command=lambda: thread_it(zsj))
    btn2.place(x=110, y=35)
    btn3 = tk.Button(window, text="时间转时间戳", bg="orange", command=lambda: thread_it(zsjc))
    btn3.place(x=110, y=65)
    txt = tk.Entry(window, width=13)
    txt.place(x=10, y=35)
    txt.insert('end', 2)
    txt1 = tk.Entry(window, width=13)
    txt1.place(x=10, y=65)
    txt1.insert('end','')
    # wz1 = tk.Label(window, text='输入开服天数:')
    # wz1.place(x=0, y=0)
    window.mainloop()


相关推荐
  • 时间戳
  • Python时间戳互转

    查询时间:可查询当前时间与时间戳时间戳转时间:在前面输入框中输入时间戳点击按钮可转成时间时间转时间戳:在前面输入框中输入时间点击按钮可转成时间戳,时间格式如果不知道可复制查询时间中的时间格式import timeimport osimport tkinter as tkimport t...

    python教程 4 8个月前
  • 七牛云oss时间戳图片防盗链api接口

    七牛云oss时间戳图片防盗链api接口,生成签名的 url 的时候,也要将新生成的 url 保存到 redis 中,防止每次访问都生成新的签名,即浪费服务器资源,又会增加 CDN 流量。签名代码:// Sign 每一个图片生成 2 分钟有效期的签名//// imgPath 是图片路径,不是完整链...

    php教程 117 2年前
  • php获取年月周时间戳代码

    php获取时区date_default_timezone_set("Asia/Shanghai"); date_default_timezone_set('PRC');//这两种方法效果相同时间戳转日期,可以用date(‘Y-m-s h:i:s’, 具体时间戳来实现)日期转换时间戳,用strtotime("date()").php获取时间戳//获取今...

    php教程 11 1年前
最新更新