Python中實(shí)現(xiàn)對(duì)Timestamp和Datetime及UTC時(shí)間之間的轉(zhuǎn)換
Python項(xiàng)目中很多時(shí)候會(huì)需要將時(shí)間在Datetime格式和TimeStamp格式之間轉(zhuǎn)化,又或者你需要將UTC時(shí)間轉(zhuǎn)化為本地時(shí)間,本文總結(jié)了這幾個(gè)時(shí)間之間轉(zhuǎn)化的函數(shù),供大家參考。
一、Datetime轉(zhuǎn)化為T(mén)imeStamp
def datetime2timestamp(dt, convert_to_utc=False): ''' Converts a datetime object to UNIX timestamp in milliseconds. ''' if isinstance(dt, datetime.datetime): if convert_to_utc: # 是否轉(zhuǎn)化為UTC時(shí)間 dt = dt + datetime.timedelta(hours=-8) # 中國(guó)默認(rèn)時(shí)區(qū) timestamp = total_seconds(dt - EPOCH) return long(timestamp) return dt
二、TimeStamp轉(zhuǎn)化為Datetime
def timestamp2datetime(timestamp, convert_to_local=False): ''' Converts UNIX timestamp to a datetime object. ''' if isinstance(timestamp, (int, long, float)): dt = datetime.datetime.utcfromtimestamp(timestamp) if convert_to_local: # 是否轉(zhuǎn)化為本地時(shí)間 dt = dt + datetime.timedelta(hours=8) # 中國(guó)默認(rèn)時(shí)區(qū) return dt return timestamp
三、當(dāng)前UTC時(shí)間的TimeStamp
def timestamp_utc_now(): return datetime2timestamp(datetime.datetime.utcnow())
四、當(dāng)前本地時(shí)間的TimeStamp
def timestamp_now(): return datetime2timestamp(datetime.datetime.now())
五、UTC時(shí)間轉(zhuǎn)化為本地時(shí)間
# 需要安裝python-dateutil # Ubuntu下:sudo apt-get install python-dateutil # 或者使用PIP:sudo pip install python-dateutil from dateutil import tz from dateutil.tz import tzlocal from datetime import datetime # get local time zone name print datetime.now(tzlocal()).tzname() # UTC Zone from_zone = tz.gettz('UTC') # China Zone to_zone = tz.gettz('CST') utc = datetime.utcnow() # Tell the datetime object that it's in UTC time zone utc = utc.replace(tzinfo=from_zone) # Convert time zone local = utc.astimezone(to_zone) print datetime.strftime(local, "%Y-%m-%d %H:%M:%S")
- python datetime 和時(shí)間戳互相轉(zhuǎn)換問(wèn)題
- Python之time模塊的時(shí)間戳,時(shí)間字符串格式化與轉(zhuǎn)換方法(13位時(shí)間戳)
- python中時(shí)間轉(zhuǎn)換datetime和pd.to_datetime詳析
- Python datetime和unix時(shí)間戳之間相互轉(zhuǎn)換的講解
- Python中時(shí)間datetime的處理與轉(zhuǎn)換用法總結(jié)
- python utc datetime轉(zhuǎn)換為時(shí)間戳的方法
- python sys,os,time模塊的使用(包括時(shí)間格式的各種轉(zhuǎn)換)
- python 時(shí)間的訪(fǎng)問(wèn)和轉(zhuǎn)換 time示例小結(jié)
相關(guān)文章
利用Python制作本地Excel的查詢(xún)與生成的程序問(wèn)題
最近遇到這樣一個(gè)項(xiàng)目需求制作一個(gè)程序有一個(gè)簡(jiǎn)單的查詢(xún)?nèi)肟趯?shí)現(xiàn)Excel的查詢(xún)與生成,今天教大家利用Python制作本地Excel的查詢(xún)與生成的程序,感興趣的朋友跟隨小編一起看看吧2022-06-06python 內(nèi)置庫(kù)wsgiref的使用(WSGI基礎(chǔ)入門(mén))
WSGI(web服務(wù)器網(wǎng)關(guān)接口)主要規(guī)定了服務(wù)器端和應(yīng)用程序之間的接口,即規(guī)定了請(qǐng)求的URL到后臺(tái)處理函數(shù)之間的映射該如何實(shí)現(xiàn)。wsgiref是一個(gè)幫助開(kāi)發(fā)者開(kāi)發(fā)測(cè)試的Python內(nèi)置庫(kù),程序員可以通過(guò)這個(gè)庫(kù)了解WSGI的基本運(yùn)行原理,但是不能把它用在生產(chǎn)環(huán)境上。2021-06-06pip安裝py_zipkin時(shí)提示的SSL問(wèn)題對(duì)應(yīng)
今天小編就為大家分享一篇關(guān)于pip安裝py_zipkin時(shí)提示的SSL問(wèn)題對(duì)應(yīng),小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12Python自動(dòng)化構(gòu)建工具scons使用入門(mén)筆記
這篇文章主要介紹了Python自動(dòng)化構(gòu)建工具scons使用入門(mén)筆記,本文講解了安裝scons、scons常用命令、scons使用示例等內(nèi)容,需要的朋友可以參考下2015-03-03Pandas DataFrame分組求和、分組乘積的實(shí)例
這篇文章主要介紹了Pandas DataFrame分組求和、分組乘積的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02Python實(shí)現(xiàn)保證只能運(yùn)行一個(gè)腳本實(shí)例
這篇文章主要介紹了Python實(shí)現(xiàn)保證只能運(yùn)行一個(gè)腳本實(shí)例,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-06-06python socket模塊創(chuàng)建和使用套接字示例詳解
這篇文章主要為大家介紹了python socket模塊來(lái)創(chuàng)建和使用套接字示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06