-
?轉(zhuǎn)UTC世界標(biāo)準(zhǔn)時(shí)間(包含T和Z) 成為北京時(shí)間:
from datetime import datetime, timedelta
def time_handler(target_time: str):
"""
UTC世界標(biāo)準(zhǔn)時(shí)間(包含T和Z) 轉(zhuǎn) 北京時(shí)間
:param target_time:
:return:
"""
_date = datetime.strptime(target_time, "%Y-%m-%dT%H:%M:%S.%fZ")
local_time = _date + timedelta(hours=8)
end_time = local_time.strftime("%Y-%m-%d %H:%M:%S")
return end_time
if __name__ == '__main__':
start_time = "2020-05-08T11:16:51.000Z"
time_str = time_handler(start_time)
print(time_str) # 2020-05-08 19:16:51
-
本地時(shí)間轉(zhuǎn)為UTC時(shí)間
?文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-616397.html
import time
import arrow
def local_to_utc():
localtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
utctime = arrow.get(localtime).to("UTC")
utc_time_format = utctime.strftime("%Y-%m-%dT%H:%M:%SZ")
print("localtime:", localtime)
print("utc_date_time:",utc_time_format)
print("轉(zhuǎn)換前: ",str(localtime))
print("轉(zhuǎn)換后: ",str(utc_time_format))
return utc_time_format
local_to_utc()
運(yùn)行結(jié)果:
localtime: 2022-11-02 15:40:50
utctime: 2022-11-02T15:40:50+00:00
utc_date_time: 2022-11-02T15:40:50Z
轉(zhuǎn)換前: 2022-11-02 15:40:50
轉(zhuǎn)換后: 2022-11-02T15:40:50Z
轉(zhuǎn)換思路
1、使用arrow庫(kù)直接處理,import arrow
2、先獲取本地時(shí)間
3、使用arrow.get(localtime).to(“UTC”),這樣就轉(zhuǎn)成utc格式啦
4、轉(zhuǎn)成utc格式為:utctime: 2022-11-02T15:40:50+00:00
5、需要再格式化,跟傳入?yún)?shù)一樣,聲明格式:utctime.strftime(“%Y-%m-%dT%H:%M:%SZ”)
?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-616397.html
到了這里,關(guān)于【時(shí)間處理】Python轉(zhuǎn)UTC世界標(biāo)準(zhǔn)時(shí)間(包含T和Z) 成為北京時(shí)間、本地時(shí)間轉(zhuǎn)為UTC時(shí)間的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!