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)化為TimeStamp
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) # 中國默認(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) # 中國默認(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)換問題
- 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í)間的訪問和轉(zhuǎn)換 time示例小結(jié)
相關(guān)文章
詳談Python高階函數(shù)與函數(shù)裝飾器(推薦)
下面小編就為大家?guī)硪黄斦凱ython高階函數(shù)與函數(shù)裝飾器(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09
Python類和對(duì)象的定義與實(shí)際應(yīng)用案例分析
這篇文章主要介紹了Python類和對(duì)象的定義與實(shí)際應(yīng)用,結(jié)合三個(gè)具體案例形式分析了Python面向?qū)ο蟪绦蛟O(shè)計(jì)中類與對(duì)象的定義、應(yīng)用、設(shè)計(jì)模式等相關(guān)操作技巧,需要的朋友可以參考下2018-12-12
pandas map(),apply(),applymap()區(qū)別解析
這篇文章主要介紹了pandas map(),apply(),applymap()區(qū)別解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
Python3控制路由器——使用requests重啟極路由.py
通過本文給大家介紹Python3控制路由器——使用requests重啟極路由.py的相關(guān)知識(shí),代碼寫了相應(yīng)的注釋,以后再寫成可以方便調(diào)用的模塊,感興趣的朋友一起學(xué)習(xí)吧2016-05-05
淺析python參數(shù)的知識(shí)點(diǎn)
在本文里小編給大家分享的是關(guān)于python參數(shù)的知識(shí)點(diǎn)內(nèi)容,正在學(xué)習(xí)的讀者們跟著思考下吧。2018-12-12
如何使用python獲取現(xiàn)在的日期與時(shí)間
學(xué)習(xí)了一段時(shí)間的python,不知道大家對(duì)于代碼的編寫有了屬于自己的一套思路了呢,下面這篇文章主要給大家介紹了關(guān)于如何使用python獲取現(xiàn)在的日期與時(shí)間的相關(guān)資料,需要的朋友可以參考下2022-11-11
python分布式系統(tǒng)Celery安裝使用實(shí)例講解
這篇文章主要為大家介紹了python分布式系統(tǒng)Celery安裝使用實(shí)例講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
利用pyecharts實(shí)現(xiàn)地圖可視化的例子
今天小編就為大家分享一篇利用pyecharts實(shí)現(xiàn)地圖可視化的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08

