乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      python工具模塊介紹-time 時(shí)間訪問和轉(zhuǎn)換!

       靜幻堂 2018-09-19
      徒手憶歲月 2018-09-18 21:28:14

      快速入門(私信小編007就能自動領(lǐng)取Python學(xué)習(xí)大禮包?。?/strong>

      In [1]: import time
      # 獲取當(dāng)前時(shí)間
      In [25]: time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime())
      Out[25]: '2018-06-17_20-05-36'
      # 停頓0.5秒
      In [26]: time.sleep(0.5)

      簡介

      功能:時(shí)間訪問和轉(zhuǎn)換。

      相關(guān)模塊:

      datetime 標(biāo)準(zhǔn)模塊。

      calendar 標(biāo)準(zhǔn)模塊。

      下面介紹一些術(shù)語和約定:

      epoch 是時(shí)間開始點(diǎn)。對于Unix ,時(shí)代是1970年1月1日0點(diǎn)。通過time.gmtime(0)可以查看時(shí)間的起點(diǎn):

      In [1]: import time
      In [2]: time.gmtime(0)
      Out[2]: time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)
      In [3]: time.gmtime(time.time() + 786041553) # 32位會報(bào)錯(cuò)
      Out[3]: time.struct_time(tm_year=2043, tm_mon=5, tm_mday=8, tm_hour=6, tm_min=26, tm_sec=50, tm_wday=4, tm_yday=128, tm_isdst=0)

      對于32位的linux系統(tǒng),時(shí)間只能處理到2038年?,F(xiàn)在新發(fā)布的主流已經(jīng)全部是64位版本。

      UTC是協(xié)調(diào)世界時(shí)(前身為格林威治標(biāo)準(zhǔn)時(shí)間或GMT)。

      DST為夏令時(shí),通常是根據(jù)當(dāng)?shù)胤稍谝荒陜?nèi)的部分時(shí)間進(jìn)行一小時(shí)的調(diào)整。 C庫包含有當(dāng)?shù)匾?guī)則的表。

      實(shí)時(shí)函數(shù)的精度可能比建議的要低。例如在大多數(shù)Unix系統(tǒng)中,時(shí)鐘“滴答”只有50或100次每秒。

      不過time()和sleep()比Unix的更好:時(shí)間為浮點(diǎn)數(shù),time()的返回確保最精確(盡量使用Unix的函數(shù)gettimeofday()) ,sleep()接受的時(shí)間為非零分?jǐn)?shù)(盡量用select()實(shí)現(xiàn)) 。

      gmtime(), localtime()和strptime()的返回是包含9個(gè)整數(shù)的序列,可以作為asctime(), mktime() and strftime()的輸入,每個(gè)域都有自己的屬性,實(shí)際上是一個(gè)結(jié)構(gòu)體struct_time,參見上面的例子。

      時(shí)間轉(zhuǎn)換:gmtime()把浮點(diǎn)時(shí)間轉(zhuǎn)為UTC的struct_time,反之calendar.timegm();localtime()把浮點(diǎn)時(shí)間轉(zhuǎn)為local的struct_time,反之mktime()。實(shí)際上calendar.timegm()和mktime()是等效的,不過前者返回整數(shù),后者返回浮點(diǎn)數(shù)。

      時(shí)間生成與轉(zhuǎn)換

      生成epoch的浮點(diǎn)數(shù),注意不同的系統(tǒng)精度不同,linux一般是小數(shù)點(diǎn)后面7為,windows一般是小數(shù)點(diǎn)后3位。Time函數(shù)是沒有參數(shù)的??梢灾苯訉Ψ祷氐母↑c(diǎn)數(shù)進(jìn)行計(jì)算。

      gmtime([secs])把浮點(diǎn)時(shí)間轉(zhuǎn)為UTC的struct_time,如果無輸入?yún)?shù)為空會調(diào)用time()讀取當(dāng)前時(shí)間。

      gmtime顯示的是世界協(xié)調(diào)時(shí)間, localtime([secs])可以顯示本地時(shí)間。

      注意夏時(shí)制要設(shè)置dst。asctime([t])顯示時(shí)間為可讀性好的格式,它把gmtime(), localtime()和strptime()的返回的struct_time類型轉(zhuǎn)換為可讀性較好的格式。如果輸入?yún)?shù)為空則調(diào)用localtime()的返回結(jié)果。它和c函數(shù)不同的地方是末尾不會添加換行。asctime不會使用Locale信息。

      ctime([secs])在asctime上更進(jìn)一步,轉(zhuǎn)換浮點(diǎn)數(shù)為可讀性較好的格式,相當(dāng)于asctime(localtime(secs)), 這個(gè)功能很常用。ctime不會使用Locale信息。

      In [1]: import time
      In [2]: time.gmtime(0)
      Out[2]: time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)
      In [3]: time.gmtime(time.time() + 786041553)
      Out[3]: time.struct_time(tm_year=2043, tm_mon=5, tm_mday=8, tm_hour=6, tm_min=26, tm_sec=50, tm_wday=4, tm_yday=128, tm_isdst=0)
      In [4]: time.time()
      Out[4]: 1528637996.277831
      In [5]: time.gmtime()
      Out[5]: time.struct_time(tm_year=2018, tm_mon=6, tm_mday=10, tm_hour=13, tm_min=42, tm_sec=47, tm_wday=6, tm_yday=161, tm_isdst=0)
      In [6]: time.localtime()
      Out[6]: time.struct_time(tm_year=2018, tm_mon=6, tm_mday=10, tm_hour=21, tm_min=43, tm_sec=54, tm_wday=6, tm_yday=161, tm_isdst=0)
      In [7]: time.asctime()
      Out[7]: 'Sun Jun 10 22:10:14 2018'
      In [8]: time.ctime()
      Out[8]: 'Sun Jun 10 22:12:25 2018'

      Sleep

      sleep(secs)暫停執(zhí)行指定秒數(shù)。參數(shù)可以是整數(shù)或浮點(diǎn)數(shù)。實(shí)際的中止時(shí)間可能小于請求時(shí)間,因?yàn)槔械男盘柌蹲娇赡芙K止sleep。此外中止時(shí)間可能長于請求時(shí)間,因?yàn)橐驗(yàn)橄到y(tǒng)調(diào)度也是需要時(shí)間的。

      In [36]: time.sleep(3)

      處理器時(shí)間

      clock()在Unix上,返回當(dāng)前的處理器時(shí)間,為以秒表示的浮點(diǎn)數(shù)。精度決于同名的C函數(shù),通常用于基準(zhǔn)Python或定時(shí)的算法。我們書寫一個(gè)不耗cpu和耗cpu的腳本對比:

      import time
      template = '{} - {:0.2f} - {:0.2f}'
      print(template.format(
      time.ctime(), time.time(), time.clock())
      )
      for i in range(3, 0, -1):
      print('Sleeping', i)
      time.sleep(i)
      print(template.format(
      time.ctime(), time.time(), time.clock())
      )

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

      $ python3 time_clock_sleep.py 
      Mon Jun 18 01:27:52 2018 - 1529256472.83 - 0.05
      Sleeping 3
      Mon Jun 18 01:27:55 2018 - 1529256475.83 - 0.05
      Sleeping 2
      Mon Jun 18 01:27:57 2018 - 1529256477.83 - 0.05
      Sleeping 1
      Mon Jun 18 01:27:58 2018 - 1529256478.83 - 0.05
      import hashlib
      import time
      # Data to use to calculate md5 checksums
      data = open(__file__, 'rb').read()
      for i in range(5):
      h = hashlib.sha1()
      print(time.ctime(), ': {:0.3f} {:0.3f}'.format(
      time.time(), time.clock()))
      for i in range(300000):
      h.update(data)
      cksum = h.digest()

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

      $ python3 time_clock.py 
      Mon Jun 18 01:31:35 2018 : 1529256695.695 0.048
      Mon Jun 18 01:31:36 2018 : 1529256696.166 0.519
      Mon Jun 18 01:31:36 2018 : 1529256696.635 0.987
      Mon Jun 18 01:31:37 2018 : 1529256697.110 1.461
      Mon Jun 18 01:31:37 2018 : 1529256697.587 1.936

      struct_time類

      struct_time是的命名元組,結(jié)構(gòu)如下:

      | 索引(Index) | 屬性(Attribute) | 值(Values) |

      | 0 | tm_year(年 | 比如2013 |

      | 1 | tm_mon(月) | 1 - 12 |

      | 2 | tm_mday(日) | 1 - 31 |

      | 3 | tm_hour(時(shí)) | 0 - 23 |

      | 4 | tm_min(分) | 0 - 59 |

      | 5 | tm_sec(秒) | 0 - 61 |

      | 6 | tm_wday(weekday | 0 - 6(0表示周日 |

      | 7 | tm_yday(一年中的第幾天) | 1 - 366 |

      | 8 | tm_isdst(是否是夏令時(shí)) | 默認(rèn)為-1 |

      import timedef show_struct(s):
      print ' tm_year :', s.tm_year print ' tm_mon :', s.tm_mon print ' tm_mday :', s.tm_mday print ' tm_hour :', s.tm_hour print ' tm_min :', s.tm_min print ' tm_sec :', s.tm_sec print ' tm_wday :', s.tm_wday print ' tm_yday :', s.tm_yday print ' tm_isdst:', s.tm_isdstprint 'gmtime:'show_struct(time.gmtime())print ' localtime:'show_struct(time.localtime())print ' mktime:', time.mktime(time.localtime())

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

      $ python3 time_struct.py 
      gmtime:
      tm_year : 2018
      tm_mon : 6
      tm_mday : 17
      tm_hour : 17
      tm_min : 32
      tm_sec : 54
      tm_wday : 6
      tm_yday : 168
      tm_isdst: 0
      localtime:
      tm_year : 2018
      tm_mon : 6
      tm_mday : 18
      tm_hour : 1
      tm_min : 32
      tm_sec : 54
      tm_wday : 0
      tm_yday : 169
      tm_isdst: 0
      mktime: 1529256774.0

      時(shí)區(qū)

      重置庫函數(shù)的時(shí)間轉(zhuǎn)換規(guī)則。實(shí)際上是修改環(huán)境變量TZ,python 2.3以后類linux支持該功能,這個(gè)功能相對不是那么常用。TZ環(huán)境變量的格式如下:

      std offset [dst [offset [,start[/time], end[/time]]]]

      STD和DST為時(shí)區(qū)縮寫。hh[:mm[:ss]],表示加上這個(gè)時(shí)間可以得到UTC時(shí)間。偏移量的形式為: HH [ : MM [ : SS] ],夏時(shí)制增加1小時(shí)。

      start time , end time 表示使用夏時(shí)制的區(qū)間。time和偏移類似,默認(rèn)時(shí)間是02:00:00。比如:

      In [1]: import os
      In [2]: import time
      In [3]: os.environ['TZ'] = 'EST+05EDT,M4.1.0,M10.5.0'
      In [4]: time.tzset()
      In [5]: time.strftime('%X %x %Z')
      Out[5]: '13:38:26 06/17/18 EDT'
      In [6]: os.environ['TZ'] = 'AEST-10AEDT-11,M10.5.0,M3.5.0'
      In [7]: time.tzset()
      In [8]: time.strftime('%X %x %Z')
      Out[8]: '03:38:46 06/18/18 AEST'

      在許多Unix系統(tǒng)(包括* BSD,Linux和Solaris,和Darwin),使用系統(tǒng)時(shí)區(qū)數(shù)據(jù)庫更方便。

      In [9]: os.environ['TZ'] = 'US/Eastern'
      In [10]: time.tzset()
      In [11]: time.tzname
      Out[11]: ('EST', 'EDT')
      In [12]: os.environ['TZ'] = 'Egypt'
      In [13]: time.tzset()
      In [14]: ('EET', 'EEST')
      Out[14]: ('EET', 'EEST')

      另一實(shí)例:

      import time
      import os
      def show_zone_info():
      print ' TZ :', os.environ.get('TZ', '(not set)')
      print ' tzname:', time.tzname print ' Zone : %d (%d)' % (time.timezone,
      (time.timezone / 3600))
      print ' DST :', time.daylight print ' Time :', time.ctime()
      printprint 'Default :'show_zone_info()ZONES = [ 'GMT',
      'Europe/Amsterdam',
      ]for zone in ZONES:
      os.environ['TZ'] = zone
      time.tzset()
      print zone, ':'
      show_zone_info()

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

      $ python3 time_timezone.py 
      Default :
      TZ : (not set)
      tzname: ('CST', 'CST')
      Zone : -28800 (-8.0)
      DST : 0
      Time : Mon Jun 18 01:40:39 2018
      GMT :
      TZ : GMT
      tzname: ('GMT', 'GMT')
      Zone : 0 (0.0)
      DST : 0
      Time : Sun Jun 17 17:40:39 2018
      Europe/Amsterdam :
      TZ : Europe/Amsterdam
      tzname: ('CET', 'CEST')
      Zone : -3600 (-1.0)
      DST : 1
      Time : Sun Jun 17 19:40:39 2018

      格式化

      time.strftime(format[, t]):把一個(gè)代表時(shí)間的元組或者struct_tim轉(zhuǎn)為格式化的時(shí)間字符串。如果t未指定,將調(diào)用time.localtime()的返回作為輸入。如果輸入中任何一個(gè)元素越界將報(bào)ValueError異常。格式化參數(shù)如下:

      | 格式 | 含義 | 備注 |

      | %a | 本地簡化星期名 | |

      | %A | 本地完整星期名 | |

      | %b | 本地簡化月份名 | |

      | %B | 本地完整月份名稱 | |

      | %c | 本地相應(yīng)的日期和時(shí)間表示 | |

      | %d | 日期(01 - 31) | |

      | %H | 小時(shí)(24小時(shí)制,00 - 23) | |

      | %I | 小時(shí)(12小時(shí)制,01 - 12) | |

      | %j | 天數(shù)(基于年)(001 - 366) | |

      | %m | 月份(01 - 12) | |

      | %M | 分鐘(00 - 59) | |

      | %p | 顯示am或pm的標(biāo)識 | |

      | %S | 秒(01 - 61) | |

      | %U | 周數(shù)(基于年)(00 – 53周日是星期的開始。)第一個(gè)周日之前的所有天數(shù)都放在第0周。 | |

      | %w | 星期中的天數(shù)(0 - 6,0是星期天) | |

      | %W | 和%U基本相同,以星期一為星期的開始。 | |

      | %x | 本地相應(yīng)日期表示 | |

      | %X | 本地相應(yīng)時(shí)間表示 | |

      | %y | 去掉世紀(jì)的年份(00 - 99) | |

      | %Y | 完整的年份 | |

      | %Z | 時(shí)區(qū)的名字(如果不存在為空字符) | |

      | %% | '%’字符 | |

      備注:

      • “%p”只有與“%I”配合使用才有效果。
      • 秒是0 - 61,而不是59,以處理閏秒和雙閏秒。
      • 當(dāng)使用strptime()函數(shù)時(shí),只有當(dāng)在這年中的周數(shù)和天數(shù)被確定的時(shí)候%U和%W才會被計(jì)算。

      比如:

      In [15]: time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())
      Out[15]: 'Sun, 17 Jun 2018 17:44:12 +0000'

      下面方式在給文件名等添加時(shí)間戳比較有用:

      In [17]: time.strftime("%Y-%m-%d_%H:%M:%S", time.gmtime())
      Out[17]: '2018-06-17_17:46:18'

      顯示格式可能因系統(tǒng)而又不同的差異。

      time.strptime(string[, format]):把一個(gè)格式化時(shí)間字符串轉(zhuǎn)化為struct_time。實(shí)際上它和strftime()是逆操作,參數(shù)參見strftime。Format默認(rèn)為"%a %b %d %H:%M:%S %Y",和ctime的返回格式一致,沒有提供的值會采用默認(rèn)值(1900, 1, 1, 0, 0, 0, 0, 1, -1)。

      In [19]: time.strptime("30 Nov 18", "%d %b %y")
      ...:
      Out[19]: time.struct_time(tm_year=2018, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=334, tm_isdst=-1)

      其他

      altzone屬性查看當(dāng)前夏時(shí)制時(shí)間的偏移。daylight屬性查看是否使用了夏時(shí)制。timezone查看當(dāng)前時(shí)區(qū)的偏移。Tzname返回本地時(shí)區(qū)和夏時(shí)制對應(yīng)的時(shí)區(qū)。

      In [3]: time.altzone
      Out[3]: -28800
      In [4]: time.daylight
      Out[4]: 0
      In [5]: time.timezone
      Out[5]: -28800
      In [6]: time.tzname
      Out[6]: ('CST', 'CST')

        本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多