亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

利用Python將時(shí)間或時(shí)間間隔轉(zhuǎn)為ISO 8601格式方法示例

 更新時(shí)間:2017年09月05日 11:01:46   作者:TheOneGIS  
國(guó)際標(biāo)準(zhǔn)化組織的國(guó)際標(biāo)準(zhǔn)ISO8601是日期和時(shí)間的表示方法,全稱為《數(shù)據(jù)存儲(chǔ)和交換形式·信息交換·日期和時(shí)間的表示方法》,下面這篇文章主要給大家介紹了關(guān)于利用Python將時(shí)間或時(shí)間間隔轉(zhuǎn)為ISO 8601格式的相關(guān)資料,需要的朋友可以參考下。

前言

大家都知道,Python自帶的datetime庫(kù)提供了將datetime轉(zhuǎn)為ISO 8610格式的函數(shù),但是對(duì)于時(shí)間間隔(inteval)并沒(méi)有提供轉(zhuǎn)換的函數(shù),下面我們動(dòng)手寫(xiě)一個(gè)。 下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧。

對(duì)于時(shí)間間隔,ISO 8601的表示形式如下:

P表示的是時(shí)間間隔的前綴。YMDHMS分別表示年月日時(shí)分秒,W表示周。T表示后面的字符是精確到天的,也就是以小時(shí)表示開(kāi)始的前綴。

英文解釋如下 :

[P] is used as time-interval (period) designator, preceding a data element which represents a given duration of a time-interval;

The character [T] shall be used as time designator to indicate the start of the representation of time of the day in date and time expressions.

下面是實(shí)現(xiàn)代碼:

# -*- encoding: utf-8 -*-

import datetime


def isoformat(time):
 '''
 將datetime或者timedelta對(duì)象轉(zhuǎn)換成ISO 8601時(shí)間標(biāo)準(zhǔn)格式字符串
 :param time: 給定datetime或者timedelta
 :return: 根據(jù)ISO 8601時(shí)間標(biāo)準(zhǔn)格式進(jìn)行輸出
 '''
 if isinstance(time, datetime.datetime): # 如果輸入是datetime
  return time.isoformat();
 elif isinstance(time, datetime.timedelta): # 如果輸入時(shí)timedelta,計(jì)算其代表的時(shí)分秒
  hours = time.seconds // 3600
  minutes = time.seconds % 3600 // 60
  seconds = time.seconds % 3600 % 60
  return 'P%sDT%sH%sM%sS' % (time.days, hours, minutes, seconds) # 將字符串進(jìn)行連接


if __name__ == '__main__':
 dtnow = datetime.datetime.now() # 獲取現(xiàn)在時(shí)間
 print(isoformat(dtnow))

 dtpast = datetime.datetime(1990, 12, 31) # 獲取過(guò)去某個(gè)時(shí)間點(diǎn)
 print(isoformat(dtpast))

 interval = dtnow - dtpast # 得到時(shí)間差
 print(interval)
 print(isoformat(interval))

輸出結(jié)果如下:

2017-01-14T10:54:28.323000
1990-12-31T00:00:00
9511 days, 10:54:28.323000
P9511DT10H54M28S

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論