日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

【Python基礎(chǔ)學(xué)習(xí)】—時間轉(zhuǎn)換函數(shù)

系統(tǒng) 2061 0

前言

python的時間格式分為多種,幾種格式之間的轉(zhuǎn)換方法時常是我們遇到的而且是經(jīng)常忘記的點,python不像php,時間字符串和datetime是一起的,只需要strtotime和date函數(shù)就可以相互轉(zhuǎn)化。雖然網(wǎng)上已經(jīng)有很多python時間轉(zhuǎn)換的文章,但是由于作者本人經(jīng)常做海外業(yè)務(wù),需要各種時區(qū)之間的轉(zhuǎn)換,所以這篇文章會對按時區(qū)轉(zhuǎn)換各種時間格式做一個總結(jié)。

?

轉(zhuǎn)換方法圖示(圖片轉(zhuǎn)自網(wǎng)絡(luò)):

【Python基礎(chǔ)學(xué)習(xí)】—時間轉(zhuǎn)換函數(shù)_第1張圖片

?

一、字符串轉(zhuǎn)時間戳
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、按時區(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()))


            
          


二、時間戳轉(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、按時區(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、按時區(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、按時區(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)時間戳
1、默認(rèn):

            
              import time

def datetime_to_timestamp(date):
    return int(time.mktime(date.timetuple()))

            
          

2、按時區(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()))

            
          

六、時間戳轉(zhuǎn)datetime
1、默認(rèn):

            
              import datetime

def timestamp_to_datetime(time_stamp):
    return datetime.datetime.fromtimestamp(time_stamp)

            
          

2、按時區(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元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 井冈山市| 西乡县| 民丰县| 满洲里市| 泗阳县| 龙州县| 利川市| 樟树市| 中方县| 锡林郭勒盟| 东山县| 缙云县| 营口市| 廊坊市| 贵定县| 孙吴县| 新丰县| 肥西县| 滨海县| 双流县| 曲水县| 潍坊市| 余江县| 西和县| 曲沃县| 惠水县| 易门县| 军事| 山阳县| 城市| 新密市| 鲁山县| 绥化市| 拜城县| 缙云县| 富民县| 三门县| 黄石市| 明光市| 万载县| 温宿县|