簡単にPython timeモジュールを勉強します。


この文章はPython timeモジュールに対して分類学習を行います。皆さんの学習に役に立つと思います。
壁掛け時計の時間
1.タイム()
timeモジュールのコア関数time()は、紀元開始の秒数を返します。戻り値は浮動小数点で、具体的な精度はプラットフォームに依存します。

>>>import time

>>>time.time()

1460599046.85416
2.ctime()
浮動小数点数は一般的に日付の保存と比較に用いられますが、人間に対して友好的ではなく、記録と印刷時間には、ctime()が使えます。

>>>import time

>>>time.ctime()

'Thu Apr 14 10:03:58 2016'

>>> later = time.time()+5

>>> time.ctime(later)

'Thu Apr 14 10:05:57 2016'

二.プロセッサクロック時間
clock()はプロセッサクロック時間に戻り、その戻り値は一般に性能試験と基準試験に用いられる。したがって、プログラムの実際の実行時間が反映されます。

>>>import time

>>>time.clock()

0.07

三.時間構成
タイムモジュールはstructを定義しています。タイムは時間と日付を守るために、それぞれのコンポーネントを別々に記憶して訪問します。

import time

def 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

show_struct(time.gmtime())

show_struct(time.localtime())

gmtimeはUTC時間を取得するために、locatimeは現在のタイムゾーンの現在の時間を取得します。UTC時間は実際にグリニッジ時間です。中国時間との時差は8時間です。
locatime()=gmtime()+8 hour
四.処理タイムゾーン
1.取得時間差

>>>import time

>>>time.timezone/3600

-8
2.タイムゾーンの設定

ZONES = ["GMT", "EUROPE/Amsterdam']

for zone in ZONES:

   os.environ["TZ"] = zone

   time.tzset()

五.解析と書式設定時間
timeモジュールは二つの関数streptime()とstreftme()を提供しています。時間値文字列と時間値文字列を変換します。
1.streptime()
文字列時間をstruct_に変換するのに使います。タイムフォーマット:

>>> now=time.ctime()

>>> time.strptime(now)

time.struct_time(tm_year=2016, tm_mon=4, tm_mday=14, tm_hour=10, tm_min=48, tm_sec=40, tm_wday=3, tm_yday=105, tm_isdst=-1)

 
2.striftime()
時間の書式設定出力

>>> from time import gmtime, strftime

>>> strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())

'Thu, 28 Jun 2001 14:17:15 +0000'

3.mktime()
struct_に使うタイムを時間の浮動小数点表示に変換します。

>>>from time import mktime, gmtime

>>>mktime(gmtime())

1460573789.0

六.sleep()
sleep関数は、現在のスレッドを繰り出すために、システムが再度起動するのを待つ必要があります。プログラムを書くと、スレッドが一つしかないと、実際にはプロセスをブロックしてしまいます。何もしません。

import time

def fucn():

   time.sleep(5)

   print "hello, world"

上のコードを実行して5秒後に情報を出力します。
以上が本文の全部です。Python timeモジュールについて大体の理解がほしいです。