前言
python的時(shí)間格式分為多種,幾種格式之間的轉(zhuǎn)換方法時(shí)常是我們遇到的而且是經(jīng)常忘記的點(diǎn),python不像php,時(shí)間字符串和datetime是一起的,只需要strtotime和date函數(shù)就可以相互轉(zhuǎn)化。雖然網(wǎng)上已經(jīng)有很多python時(shí)間轉(zhuǎn)換的文章,但是由于作者本人經(jīng)常做海外業(yè)務(wù),需要各種時(shí)區(qū)之間的轉(zhuǎn)換,所以這篇文章會對按時(shí)區(qū)轉(zhuǎn)換各種時(shí)間格式做一個(gè)總結(jié)。
?
轉(zhuǎn)換方法圖示(圖片轉(zhuǎn)自網(wǎng)絡(luò)):
?
一、字符串轉(zhuǎn)時(shí)間戳
1、默認(rèn):
import time
def time_str_to_timestamp(string_time, _format="%Y-%m-%d %H:%M:%S"):
return int(time.mktime(time.strptime(string_time, _format)))
2、按時(shí)區(qū)轉(zhuǎn):
import time
import datetime
from pytz import timezone as tz
def time_str_to_timestamp_by_timezone(string_time, _format="%Y-%m-%d %H:%M:%S”, from_tz=“UTC”, to_tz="America/Los_Angeles"):
from_tz = tz(from_tz)
to_tz = tz(to_tz)
return int(time.mktime(
datetime.datetime.strptime(string_time, _format).replace(
tzinfo=from_tz).astimezone(to_tz).timetuple()))
二、時(shí)間戳轉(zhuǎn)字符串
1、默認(rèn):
import time
def timestamp_to_str(timestamp, _format="%Y-%m-%d %H:%M:%S"):
return time.strftime(_format, time.localtime(timestamp))
2、按時(shí)區(qū)轉(zhuǎn):
import datetime
from pytz import timezone as tz
def timestamp_to_str_by_timezone(timestamp, _format="%Y-%m-%d %H:%M:%S”, to_tz="America/Los_Angeles"):
to_tz = tz(to_tz)
return str(datetime.datetime.fromtimestamp(timestamp, to_tz).strftime(_format))
三、字符串轉(zhuǎn)datetime
1、默認(rèn):
import datetime
def datetime_str_to_datetime(string_time, _format="%Y-%m-%d %H:%M:%S"):
return datetime.datetime.strptime(string_time, _format)
2、按時(shí)區(qū)轉(zhuǎn):
import datetime
from pytz import timezone as tz
def datetime_str_to_datetime_by_timezone(string_time, from_tz=“UTC”, to_tz="America/Los_Angeles”, _format="%Y-%m-%d %H:%M:%S",):
from_tz = tz(from_tz)
to_tz = tz(to_tz)
return datetime.datetime.strptime(string_time, _format).replace(
tzinfo=from_tz).astimezone(to_tz)
四、datetime轉(zhuǎn)字符串
1、默認(rèn):
import datetime
def datetime_to_datetime_str(date, _format="%Y-%m-%d %H:%M:%S"):
return date.strftime(_format)
2、按時(shí)區(qū)轉(zhuǎn):
import datetime
from pytz import timezone as tz
def datetime_to_datetime_str_by_timezone(date, from_tz=“UTC”, to_tz="America/Los_Angeles”, _format="%Y-%m-%d %H:%M:%S"):
from_tz = tz(from_tz)
to_tz = tz(to_tz)
date = date.replace(tzinfo=from_tz).astimezone(to_tz)
return date.strftime(_format)
五、datetime轉(zhuǎn)時(shí)間戳
1、默認(rèn):
import time
def datetime_to_timestamp(date):
return int(time.mktime(date.timetuple()))
2、按時(shí)區(qū)轉(zhuǎn):
import time
from pytz import timezone as tz
def datetime_to_timestamp_by_timezone(date, from_tz=“UTC”, to_tz="America/Los_Angeles"):
from_tz = tz(from_tz)
to_tz = tz(to_tz)
return int(time.mktime(date.replace(
tzinfo=from_tz).astimezone(to_tz).timetuple()))
六、時(shí)間戳轉(zhuǎn)datetime
1、默認(rèn):
import datetime
def timestamp_to_datetime(time_stamp):
return datetime.datetime.fromtimestamp(time_stamp)
2、按時(shí)區(qū)轉(zhuǎn):
import datetime
from pytz import timezone as tz
def timestamp_to_datetime_by_timezone(time_stamp, to_tz="America/Los_Angeles"):
to_tz = tz(to_tz)
return datetime.datetime.fromtimestamp(time_stamp, to_tz)
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
