Python處理時(shí)間戳和時(shí)間計(jì)算等的腳本分享
由于實(shí)際需要,簡(jiǎn)要寫了個(gè)小腳本,并打包生成exe,供無網(wǎng)絡(luò)環(huán)境下使用
腳本1:顯示當(dāng)前時(shí)間與時(shí)間戳,以及10分鐘后的時(shí)間與時(shí)間戳
# -*- coding: utf-8 -*- """ Project: pyWorkspace Creator: Administrator -haochuang Create time: 2021-05-12 09:24 IDE: PyCharm Introduction: """
import time
import datetime
t=datetime.datetime.now()
#當(dāng)前日期
t1 =t.strftime('%Y-%m-%d %H:%M:%S')
#轉(zhuǎn)為秒級(jí)時(shí)間戳
ts1=time.mktime(time.strptime(t1, '%Y-%m-%d %H:%M:%S'))
#轉(zhuǎn)為毫秒級(jí)
end_time=int(str(ts1*1000).split(".")[0])
#10分鐘后
t2 = (t+datetime.timedelta(minutes=10)).strftime("%Y-%m-%d %H:%M:%S")
# t2=(t-datetime.timedelta(hours=1)).strftime("%Y-%m-%d %H:%M:%S")
#轉(zhuǎn)為秒級(jí)時(shí)間戳
ts2=time.mktime(time.strptime(t2, '%Y-%m-%d %H:%M:%S'))
#轉(zhuǎn)為毫秒級(jí)
start_time=int(str(ts2*1000).split(".")[0])
#print("\n","*"*30)
print("\n")
print("*"*30)
print("當(dāng)前時(shí)間戳:")
print(start_time)
print("當(dāng)前時(shí)間:")
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts2)))
print("*"*30,"\n")
print("10分鐘后的時(shí)間戳:")
print(end_time)
print("10分鐘后的時(shí)間:")
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts1)))
print("*"*30,"\n")腳本2:顯示當(dāng)前時(shí)間與時(shí)間戳,以及10分鐘后的時(shí)間與時(shí)間戳,允許根據(jù)輸入的指定時(shí)間,生成多久之后的時(shí)間戳
# -*- coding: utf-8 -*- """ Project: pyWorkspace Creator: Administrator -haochuang Create time: 2021-05-12 09:24 IDE: PyCharm Introduction: """
import time
import datetime
t=datetime.datetime.now()
#當(dāng)前日期
t1 =t.strftime('%Y-%m-%d %H:%M:%S')
#轉(zhuǎn)為秒級(jí)時(shí)間戳
ts1=time.mktime(time.strptime(t1, '%Y-%m-%d %H:%M:%S'))
#轉(zhuǎn)為毫秒級(jí)
end_time=int(str(ts1*1000).split(".")[0])
#10分鐘后
t2 = (t+datetime.timedelta(minutes=10)).strftime("%Y-%m-%d %H:%M:%S")
# t2=(t-datetime.timedelta(hours=1)).strftime("%Y-%m-%d %H:%M:%S")
#轉(zhuǎn)為秒級(jí)時(shí)間戳
ts2=time.mktime(time.strptime(t2, '%Y-%m-%d %H:%M:%S'))
#轉(zhuǎn)為毫秒級(jí)
start_time=int(str(ts2*1000).split(".")[0])
#print("\n","*"*30)
print("\n")
print("*"*30)
print("當(dāng)前時(shí)間戳:")
print(start_time)
print("當(dāng)前時(shí)間:")
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts2)))
print("*"*30,"\n")
# 10分鐘后的時(shí)間戳
print("10 分鐘后的時(shí)間戳:")
print(end_time)
print("10 分鐘后的時(shí)間:")
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts1)))
print("*"*30,"\n")
# 用戶自定義時(shí)間
time_user = input("需要多少分鐘后的時(shí)間戳(請(qǐng)輸入正確int類型數(shù)值):")
t3 = (t+datetime.timedelta(minutes=int(time_user))).strftime("%Y-%m-%d %H:%M:%S")
ts3=time.mktime(time.strptime(t3, '%Y-%m-%d %H:%M:%S'))
#轉(zhuǎn)為毫秒級(jí)
start_time=int(str(ts3*1000).split(".")[0])
print(time_user + " 分鐘后的時(shí)間戳:")
print(end_time)
print(time_user + " 分鐘后的時(shí)間:")
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts3)))
print("*"*30,"\n")腳本3:顯示部分時(shí)間與時(shí)間戳等
# -*- coding: utf-8 -*-
"""
Project: pyWorkspace
Creator: Administrator -haochuang
Create time: 2021-05-12 09:24
IDE: PyCharm
Introduction:
"""
import time
import datetime
from datetime import timezone
from datetime import timedelta
# 顯示當(dāng)前秒級(jí)時(shí)間戳與毫秒級(jí)時(shí)間戳、微秒級(jí)時(shí)間戳
t = time.time()
#print(t) # 原始時(shí)間數(shù)據(jù)
#print(int(t)) # 秒級(jí)時(shí)間戳
#print(int(round(t * 1000))) # 毫秒級(jí)時(shí)間戳
#print(int(round(t * 1000000))) # 微秒級(jí)時(shí)間戳
# 顯示當(dāng)前日期:
dt = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
dt_ms = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f') # 含微秒的日期時(shí)間,來源 比特量化
print("當(dāng)前日期(s): " + dt)
print("當(dāng)前日期(ms): " + dt_ms)
# 將日期轉(zhuǎn)為秒級(jí)時(shí)間戳
#dtt = '2018-01-01 10:40:30'
#dtts = int(time.mktime(time.strptime(dt, "%Y-%m-%d %H:%M:%S")))
#ts_ms = int(time.mktime(time.strptime(dt, "%Y-%m-%d %H:%M:%S")))
t=datetime.datetime.now()
print("當(dāng)前時(shí)間戳(s): " + t)
print("當(dāng)前時(shí)間戳(ms): " + (int(round(t * 1000))))
# 國際標(biāo)準(zhǔn)時(shí)間
print("國際標(biāo)準(zhǔn)時(shí)間: "+time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime()))
# 本地時(shí)間
print("本地當(dāng)前時(shí)間: "+time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
# 將當(dāng)前日期轉(zhuǎn)為秒級(jí)時(shí)間戳
dt = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
dt_ts = int(time.mktime(time.strptime(dt, "%Y-%m-%d %H:%M:%S")))
print("當(dāng)前時(shí)間: " + dt)
print("當(dāng)前時(shí)間戳: " + dt_ts)
# 將獲取十分鐘后的秒級(jí)時(shí)間戳
#dt_10 = int((datetime.datetime.now()+datetime.timedelta(minutes=10)).strftime("%Y-%m-%d %H:%M:%S"))
#ts_10 = int(time.mktime(time.strptime(dt_10, "%Y-%m-%d %H:%M:%S")))
after10 = (datetime.datetime.now()+datetime.timedelta(minutes=10)).strftime("%Y-%m-%d %H:%M:%S")
after10_ts = int(time.mktime(time.strptime(t1,after10)))
print("10分鐘后的時(shí)間: " + after10)
print("10分鐘后的時(shí)間戳: "腳本4:顯示部分時(shí)間與時(shí)間戳等
# -*- coding: utf-8 -*-
"""
Project: pyWorkspace
Creator: Administrator -haochuang
Create time: 2021-05-12 09:08
IDE: PyCharm
Introduction:
"""
import datetime
import time
print('*'*30 +"獲取時(shí)間方式")
#獲取當(dāng)前時(shí)間:Thu Nov 03 16:40:00 2016
print(time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()))
#獲取當(dāng)前時(shí)間:2016-11-03 16:40:00
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
#獲取年,月,日:2016-11-03
print(datetime.date.today())
#獲取當(dāng)前時(shí)間:2016-11-03 16:43:14.550000
print(datetime.datetime.now())
#不加參數(shù)是00:00,參數(shù)days=1表示一天:1 day, 0:00:00
print(datetime.timedelta(days=1))
#獲取昨天日期:2016-11-02
nowtime=datetime.date.today()
oldtime=datetime.timedelta(days=1)
print(nowtime-oldtime)
#獲取昨天的精確日期
oldtime=datetime.timedelta(days=1)
print (datetime.datetime.now() - oldtime)
print ('*'*30 + 'python時(shí)間處理之time模塊')
import time
# 返回時(shí)間戳
# print(time.time())
# 返回當(dāng)前時(shí)間
print(time.ctime())
# 返回一天前的時(shí)間
print(time.ctime(time.time()-86400))
# 函數(shù)返回time.struct_time類型的對(duì)象
time_obj = time.gmtime()
print(time_obj)
#結(jié)果:time.struct_time(tm_year=2016, tm_mon=7, tm_mday=27, tm_hour=8, tm_min=52, tm_sec=26, tm_wday=2, tm_yday=209, tm_isdst=0)
# 格式化輸出:
print(time_obj.tm_year,time_obj.tm_mon,time_obj.tm_mday)
print("{year}-{month}".format(year=time_obj.tm_year,month=time_obj.tm_mon))
# 以time.struct_time類型,打印本地時(shí)間
print(time.localtime())
# 轉(zhuǎn)換成時(shí)間戳
time_obj = time.gmtime()
print(time.mktime(time_obj))
# 延時(shí)2秒
time.sleep(2)
# 打印UTC,世界標(biāo)準(zhǔn)時(shí)間,北京時(shí)區(qū)是東八區(qū),領(lǐng)先UTC八個(gè)小時(shí)
print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()))
# 本地時(shí)間
print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()))
# 把time.struct_time類型時(shí)間,轉(zhuǎn)換成時(shí)間戳
tm = time.strptime("2016-05-6 15:06:33","%Y-%m-%d %H:%M:%S")
print(tm)
print(time.mktime(tm))
print ('*'*30 + '3-python時(shí)間處理之datetime模塊')
import datetime
# 打印當(dāng)前,年,月,日
print(datetime.date.today())
# 打印當(dāng)前時(shí)間,精確到微秒
current_time = datetime.datetime.now()
print(current_time)
# 轉(zhuǎn)成time.struct_time格式時(shí)間
current_time = datetime.datetime.now()
print(current_time.timetuple())
# 加十天
print(datetime.datetime.now() +datetime.timedelta(days=10))
# 減十天
print(datetime.datetime.now() +datetime.timedelta(days=-10))
# 減十個(gè)小時(shí)
print(datetime.datetime.now() +datetime.timedelta(hours=-10))
# 加120s
print(datetime.datetime.now() +datetime.timedelta(seconds=120))
# 替換成指定的時(shí)間
cr_time = datetime.datetime.now()
print(cr_time.replace(2014,9,12))
# 結(jié)果:2014-09-12 17:28:17.522893
# 格式化輸出
print(datetime.datetime.strptime("21/11/06 16:30","%d/%m/%y %H:%M"))
# 替換成指定時(shí)間后,類型是<class 'datetime.datetime'>
current_time = datetime.datetime.now()
time_obj = current_time.replace(2015,5)
print(time_obj,type(time_obj))
# 結(jié)果:2015-05-27 17:34:13.350245 <class 'datetime.datetime'>
# 對(duì)比時(shí)間大小,取指定時(shí)間范圍使用
current_time = datetime.datetime.now()
time_obj = current_time.replace(2015,5)
print(current_time>time_obj)
import datetime
def getYesterday():
today=datetime.date.today()
oneday=datetime.timedelta(days=1)
yesterday=today-oneday
return yesterday
# 輸出
print(getYesterday())腳本5:關(guān)于時(shí)間戳處理
# -*- coding: utf-8 -*-
"""
Project: pyWorkspace
Creator: Administrator -haochuang
Create time: 2021-05-12 09:24
IDE: PyCharm
Introduction:
"""
import time
import datetime
from datetime import timezone
from datetime import timedelta
# 顯示當(dāng)前秒級(jí)時(shí)間戳與毫秒級(jí)時(shí)間戳、微秒級(jí)時(shí)間戳
t = time.time()
print(t) # 原始時(shí)間數(shù)據(jù)
print(int(t)) # 秒級(jí)時(shí)間戳
print(int(round(t * 1000))) # 毫秒級(jí)時(shí)間戳
print(int(round(t * 1000000))) # 微秒級(jí)時(shí)間戳
# 顯示當(dāng)前日期:
dt = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
dt_ms = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f') # 含微秒的日期時(shí)間,來源 比特量化
print(dt)
print(dt_ms)
# 將日期轉(zhuǎn)為秒級(jí)時(shí)間戳
dt = '2018-01-01 10:40:30'
ts = int(time.mktime(time.strptime(dt, "%Y-%m-%d %H:%M:%S")))
print(ts)
# 將秒級(jí)時(shí)間戳轉(zhuǎn)為日期
ts = 1515774430
dt = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts))
print(dt)
# 時(shí)區(qū)轉(zhuǎn)換
# 顯示UTC時(shí)間
utc_now = datetime.datetime.utcnow()
print(utc_now)
# 世界標(biāo)準(zhǔn)時(shí)間
# utc_time = datetime(2019, 7, 30, 7, 50, 0)
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
# 北京時(shí)間UTC+8
# cst_time =utc_time.astimezone(timezone(timedelta(hours=-8))).strftime("%Y-%m-%d %H:%M:%S")
# 國際標(biāo)準(zhǔn)時(shí)間
print("國際標(biāo)準(zhǔn)時(shí)間:"+time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime()))
# 本地時(shí)間
print("本地時(shí)間:"+time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))到此這篇關(guān)于Python處理時(shí)間戳和時(shí)間計(jì)算等的腳本分享的文章就介紹到這了,更多相關(guān)Python 時(shí)間戳 時(shí)間計(jì)算內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python+VTK環(huán)境搭建及第一個(gè)簡(jiǎn)單程序代碼
這篇文章主要介紹了python+VTK環(huán)境搭建及第一個(gè)簡(jiǎn)單程序代碼,簡(jiǎn)單介紹了vtk,然后分享了安裝步驟,最后涉及一個(gè)簡(jiǎn)單的代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12
Python內(nèi)置函數(shù)Type()函數(shù)一個(gè)有趣的用法
這篇文章主要介紹了Python內(nèi)置函數(shù)Type()函數(shù)一個(gè)有趣的用法,本文講解的是個(gè)人發(fā)現(xiàn)在的一個(gè)有趣的用法,注意這種寫法會(huì)導(dǎo)致代碼很難讀,需要的朋友可以參考下2015-02-02
python suds訪問webservice服務(wù)實(shí)現(xiàn)
這篇文章主要介紹了python suds訪問webservice服務(wù)實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Python學(xué)習(xí)筆記之open()函數(shù)打開文件路徑報(bào)錯(cuò)問題
這篇文章主要介紹了Python學(xué)習(xí)筆記之open()函數(shù)打開文件路徑報(bào)錯(cuò)問題,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04
Python 中的多值傳遞、靈活參數(shù)與無名參數(shù)的使用技巧
Python 是一門高度抽象且易于使用的編程語言,函數(shù)作為其核心特性之一,具有非常強(qiáng)大的靈活性和可擴(kuò)展性,本篇文章將深入講解 Python 函數(shù)中的多值傳遞、靈活參數(shù)以及無名參數(shù)的使用技巧,讓你輕松解鎖 Python 函數(shù)的魔力,感興趣的朋友一起看看吧2025-04-04
Python 多繼承中的一個(gè)詭異現(xiàn)象 既是 Father又是grandfather
我們知道,在面向?qū)ο缶幊汤锩?,繼承是一個(gè)很重要的概念。子類可以使用父類的方法和屬性,接下來小編將用舉例的方式為大家講解Python 多繼承中的一個(gè)詭異現(xiàn)象 其即是爸爸又是爺爺?shù)钠孑猬F(xiàn)象,感興趣的小伙伴可以看下面文章具體了解2021-09-09
Python用內(nèi)置模塊來構(gòu)建REST服務(wù)與RPC服務(wù)實(shí)戰(zhàn)
這篇文章主要介紹了Python用內(nèi)置模塊來構(gòu)建REST服務(wù)與RPC服務(wù)實(shí)戰(zhàn),python在網(wǎng)絡(luò)方面封裝一些內(nèi)置模塊,可以用很簡(jiǎn)潔的代碼實(shí)現(xiàn)端到端的通信,比如HTTP、RPC服務(wù),下文實(shí)戰(zhàn)詳情,需要的朋友可以參考一下2022-09-09

