欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)自動(dòng)計(jì)算特定格式的時(shí)間差

 更新時(shí)間:2021年12月07日 14:35:24   作者:幸福的達(dá)哥  
這篇文章主要介紹了利用Python實(shí)現(xiàn)在輸入一個(gè)特定格式的時(shí)間后,自動(dòng)獲取前進(jìn)或者后退多少小時(shí)之后的時(shí)間。感興趣的朋友可以了解一下

功能

輸入一個(gè)特定格式的時(shí)間戳,自動(dòng)獲取前進(jìn)或者后退多少小時(shí)之后的時(shí)間

附加函數(shù)

時(shí)間戳轉(zhuǎn)換函數(shù)

def date_time_str_to_long(input_date_time_string):
    '''
    標(biāo)準(zhǔn)時(shí)間格式轉(zhuǎn)10位時(shí)間戳,如:'2020-01-16 08:05:09' 轉(zhuǎn)為 '1579133109'
    :param input_date_time_string: string類型,輸入的標(biāo)準(zhǔn)時(shí)間,如:'2020-01-16 08:05:09'
    :return: 10位時(shí)間戳,如:'1579133109'
    '''
    # 字符類型的時(shí)間
    # input_date_time_string = '2013-03-06 08:05:09'
    # input_date_time_string = '2013-03-06 08:05:09'
    # 轉(zhuǎn)為時(shí)間數(shù)組
    timeArray = time.strptime(input_date_time_string, "%Y-%m-%d %H:%M:%S")
    
    # 轉(zhuǎn)為時(shí)間戳
    timeStamp = int(time.mktime(timeArray))
    print('標(biāo)準(zhǔn)時(shí)間={},10位時(shí)間戳={}'.format(input_date_time_string, timeStamp))  # 1381419600
    
    return timeStamp
 
 
def date_time_long_to_str(timeStamp='1579133109', utc_time=0):
    '''
    將10位時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)時(shí)間,如:'1579133109' 轉(zhuǎn)為 '2020-01-16 08:05:09'
    :param timeStamp: string類型,輸入的10位時(shí)間戳,如:'1579133109'
    :param utc_time: 選擇是否輸出為UTC時(shí)間,設(shè)置為1,則輸出UTC時(shí)間,UTC時(shí)間=當(dāng)前時(shí)間-8小時(shí);默認(rèn)為0,輸出為非UTC時(shí)間
    :return: 標(biāo)準(zhǔn)時(shí)間,如:'2020-01-16 08:05:09'
    '''
    # # 使用datetime
    
    # dateArray = datetime.datetime.fromtimestamp(timeStamp)
    # otherStyleTime = dateArray.strftime("%Y-%m-%d %H:%M:%S")
    # print(otherStyleTime)  # 2013-10-10 23:40:00
    
    if utc_time == 0:
        # 使用time
        timeStamp = int(timeStamp)
        timeArray = time.localtime(timeStamp)
        otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
        print('input_time={},output_StyleTime={}'.format(timeStamp, otherStyleTime))  # 2013-10-10 23:40:00
        
        return otherStyleTime
    else:
        # 使用datetime,指定utc時(shí)間,比現(xiàn)在時(shí)間少8小時(shí)
        import datetime
        dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
        otherStyleTime_utc = dateArray.strftime("%Y-%m-%d %H:%M:%S")
        print('input_time={},output_StyleTime_utc={}'.format(timeStamp, otherStyleTime_utc))  # 2013-10-10 15:40:00
        return otherStyleTime_utc
 

實(shí)例如下

一、獲取 "2030-12-02T17:00:00+08:00" 3小時(shí)后的時(shí)間格式 "2030-12-02T20:00:00+08:00"

代碼實(shí)現(xiàn)

def get_now_ISO_time_count(first_time_string,count_int=3600):
    #"2030-12-02T17:00:00+08:00"
 
    if first_time_string =='':
        print('初始時(shí)間不能為空,請(qǐng)確認(rèn)...')
        return -1
 
    first_time_string=str(first_time_string).replace('T',' ').replace('+08:00','')
    print('first_time_string={}'.format(first_time_string))
 
    count_time=int(date_time_str_to_long(first_time_string))+int(count_int)
    count_str=str(date_time_long_to_str(count_time))
    result=count_str[0:10]+'T'+count_str[11:]+'+08:00'
    print("get_now_ISO_time_count_08={}".format(result))
    return str(result)

代碼調(diào)用

if __name__ == '__main__':
    print(get_now_ISO_time_count("2030-12-02T17:00:00+08:00", 10800))

執(zhí)行結(jié)果

first_time_string=2030-12-02 17:00:00

標(biāo)準(zhǔn)時(shí)間=2030-12-02 17:00:00,10位時(shí)間戳=1922432400

input_time=1922443200,output_StyleTime=2030-12-02 20:00:00

get_now_ISO_time_count_08=2030-12-02T20:00:00+08:00

2030-12-02T20:00:00+08:00

二、計(jì)算兩個(gè)時(shí)間之間的差值,精確到秒

def date_time_count(startTime, endTime):
    '''
    計(jì)算兩個(gè)時(shí)間之間的差值,精確到秒
    :param startTime:
    :param endTime:
    :return:
    '''
    spent_times = int(date_time_str_to_long(endTime)) - int(date_time_str_to_long(startTime))
    print('endTime={},startTime={},時(shí)間差值={}'.format(endTime, startTime, spent_times))
    return spent_times

代碼調(diào)用

if __name__ == '__main__':
    print(date_time_count("2021-12-06 19:40:00","2021-12-09 14:26:40"))

執(zhí)行結(jié)果

標(biāo)準(zhǔn)時(shí)間=2021-12-09 14:26:40,10位時(shí)間戳=1639031200

標(biāo)準(zhǔn)時(shí)間=2021-12-06 19:40:00,10位時(shí)間戳=1638790800

endTime=2021-12-09 14:26:40,startTime=2021-12-06 19:40:00,時(shí)間差值=240400

240400

三、計(jì)算兩個(gè)時(shí)間之間的差值,可以精確到毫秒

代碼實(shí)現(xiàn)

def date_time_count_ms(startTime, endTime):
    '''
    計(jì)算兩個(gè)時(shí)間之間的差值,可以精確到毫秒
    :param startTime:
    :param endTime:
    :return:
    '''
    # 輸入時(shí)間格式
    # a = parse('2019-10-30 23:43:10.123')
    # b = parse("2019-10-28/09:08:13.56212")
    a = parse(str(endTime))
    b = parse(str(startTime))
    
    d_count = (a - b).days  # 獲取天數(shù)的時(shí)間差
    s_count = (a - b).seconds  # 獲取時(shí)間差中的秒數(shù),也就是23:43:10到09:08:13,不包括前面的天數(shù)和秒后面的小數(shù)
    total_seconds = (a - b).total_seconds()  # 包括天數(shù),小時(shí),微秒等在內(nèi)的所有秒數(shù)差
    microseconds = (a - b).microseconds  # 秒小數(shù)點(diǎn)后面的差值
    print('endTime={},startTime={},時(shí)間差值={}'.format(endTime, startTime, total_seconds))
    return total_seconds

代碼調(diào)用

if __name__ == '__main__':
    print(date_time_count_ms("2021-12-06 19:40:44.123","2021-12-08 14:26:40.567"))

執(zhí)行結(jié)果

endTime=2021-12-08 14:26:40.567,startTime=2021-12-06 19:40:44.123,時(shí)間差值=153956.444

153956.444?

到此這篇關(guān)于Python實(shí)現(xiàn)自動(dòng)計(jì)算特定格式的時(shí)間差的文章就介紹到這了,更多相關(guān)Python 時(shí)間差計(jì)算內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論