Python 時間モジュール
Python 時間モジュール
この記事では、時間モジュールについて詳しく説明します。例を使用して、time モジュールで定義されたさまざまな時間関連関数の使用方法を学習します。
Python には time
という名前のモジュールがあります 時間関連のタスクを処理します。モジュールで定義された関数を使用するには、最初にモジュールをインポートする必要があります。方法は次のとおりです:
import time
一般的に使用される時間関連の関数を次に示します。
Python time.time()
time()
関数はエポックから経過した秒数を返します。
Unix システムの場合、January 1, 1970, 00:00:00
UTC エポック (時間が始まる時点) です。
import time
seconds = time.time()
print("Seconds since epoch =", seconds)
Python time.ctime()
time.ctime()
関数はエポックから経過した秒数を引数として取り、現地時間を表す文字列を返します。
import time
# seconds passed since epoch
seconds = 1545925769.9618232
local_time = time.ctime(seconds)
print("Local time:", local_time)
プログラムを実行すると、出力は次のようになります:
Local time: Thu Dec 27 15:49:29 2018
Python time.sleep()
sleep()
関数は、指定された秒数の間、現在のスレッドの実行を中断 (遅延) します。
import time
print("This is printed immediately.")
time.sleep(2.4)
print("This is printed after 2.4 seconds.")
詳細については、Python sleep() をご覧ください。
他の時間関連の関数について話す前に、time.struct_time
について調べてみましょう。
time.struct_time クラス
time
のいくつかの関数 gmtime()
などのモジュール 、 asctime()
など time.struct_time
を取る オブジェクトを引数として、またはそれを返します。
time.struct_time
の例を次に示します。 オブジェクト。
time.struct_time(tm_year=2018, tm_mon=12, tm_mday=27, tm_hour=6, tm_min=35, tm_sec=17, tm_wday=3, tm_yday=361, tm_isdst=0)
インデックス | 属性 | 値 |
---|---|---|
0 | tm_year | 0000, ...., 2018, ..., 9999 |
1 | tm_mon | 1, 2, ..., 12 |
2 | tm_mday | 1, 2, ..., 31 |
3 | tm_hour | 0, 1, ..., 23 |
4 | tm_min | 0, 1, ..., 59 |
5 | tm_sec | 0, 1, ..., 61 |
6 | tm_wday | 0、1、...、6;月曜日は 0 |
7 | tm_yday | 1, 2, ..., 366 |
8 | tm_isdst | 0、1、または -1 |
time.struct_time
の値 (要素) オブジェクトは、インデックスと属性の両方を使用してアクセスできます。
Python time.localtime()
localtime()
関数はエポックから経過した秒数を引数として取り、struct_time
を返します 現地時間で .
import time
result = time.localtime(1545925769)
print("result:", result)
print("\nyear:", result.tm_year)
print("tm_hour:", result.tm_hour)
プログラムを実行すると、出力は次のようになります:
result: time.struct_time(tm_year=2018, tm_mon=12, tm_mday=27, tm_hour=15, tm_min=49, tm_sec=29, tm_wday=3, tm_yday=361, tm_isdst=0) year: 2018 tm_hour: 15
引数なしまたは None
の場合 localtime()
に渡されます 、time()
によって返される値
Python time.gmtime()
gmtime()
関数はエポックから経過した秒数を引数として取り、struct_time
を返します。 UTC .
import time
result = time.gmtime(1545925769)
print("result:", result)
print("\nyear:", result.tm_year)
print("tm_hour:", result.tm_hour)
プログラムを実行すると、出力は次のようになります:
result = time.struct_time(tm_year=2018, tm_mon=12, tm_mday=28, tm_hour=8, tm_min=44, tm_sec=4, tm_wday=4, tm_yday=362, tm_isdst=0) year = 2018 tm_hour = 8
引数なしまたは None
の場合 gmtime()
に渡されます 、time()
によって返される値
Python time.mktime()
mktime()
関数は struct_time
かかります (または struct_time
に対応する 9 つの要素を含むタプル ) を引数として受け取り、エポックから経過した秒数を現地時間で返します。基本的には localtime()
の逆関数です .
import time
t = (2018, 12, 28, 8, 44, 4, 4, 362, 0)
local_time = time.mktime(t)
print("Local time:", local_time)
以下の例は、mktime()
の方法を示しています。 と localtime()
import time
seconds = 1545925769
# returns struct_time
t = time.localtime(seconds)
print("t1: ", t)
# returns seconds from struct_time
s = time.mktime(t)
print("\s:", seconds)
プログラムを実行すると、出力は次のようになります:
t1: time.struct_time(tm_year=2018, tm_mon=12, tm_mday=27, tm_hour=15, tm_min=49, tm_sec=29, tm_wday=3, tm_yday=361, tm_isdst=0) s: 1545925769.0
Python time.asctime()
asctime()
関数は struct_time
を取ります (または struct_time
に対応する 9 つの要素を含むタプル ) を引数として受け取り、それを表す文字列を返します。以下に例を示します:
import time
t = (2018, 12, 28, 8, 44, 4, 4, 362, 0)
result = time.asctime(t)
print("Result:", result)
プログラムを実行すると、出力は次のようになります:
Result: Fri Dec 28 08:44:04 2018
Python time.strftime()
strftime()
関数は struct_time
かかります (またはそれに対応するタプル) を引数として使用し、使用される形式コードに基づいてそれを表す文字列を返します。たとえば、
import time
named_tuple = time.localtime() # get struct_time
time_string = time.strftime("%m/%d/%Y, %H:%M:%S", named_tuple)
print(time_string)
プログラムを実行すると、出力は次のようになります:
12/28/2018, 09:47:41
ここでは、%Y
、 %m
、 %d
、 %H
などは形式コードです。
%Y
- 年 [0001,..., 2018, 2019,..., 9999]%m
- 月 [01, 02, ..., 11, 12]%d
- 日 [01, 02, ..., 30, 31]%H
- 時 [00, 01, ..., 22, 23%M
- 分 [00, 01, ..., 58, 59]%S
- 秒 [00, 01, ..., 58, 61]
詳細については、time.strftime() をご覧ください。
Python time.strptime()
strptime()
関数は時間を表す文字列を解析し、struct_time
を返します .
import time
time_string = "21 June, 2018"
result = time.strptime(time_string, "%d %B, %Y")
print(result)
プログラムを実行すると、出力は次のようになります:
time.struct_time(tm_year=2018, tm_mon=6, tm_mday=21, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=172, tm_isdst=-1)
Python