Python
 Computer >> コンピューター >  >> プログラミング >> Python

Pythonの時間関数?


Pythonは、「時間」モジュールを使用して、さまざまな方法で時間情報を読み取り、表現し、リセットするためのライブラリを提供します。日付、時刻、日時はPythonのオブジェクトであるため、これらに対して操作を行うときは常に、文字列やタイムスタンプではなくオブジェクトを実際に操作します。

このセクションでは、さまざまな操作を時間どおりに処理できる「時間」モジュールについて説明します。

時間モジュールは、時間が開始するポイントを参照する「EPOCH」規則に従います。 Unixシステムでは、「EPOCH」時間は1970年1月1日午前12時から2038年まで始まりました。

システムのEPOCH時間値を決定するには、以下のコードを入力するだけです-

>>> import time
>>> time.gmtime(0)

出力

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)

Pythonにチェックマークを付けますか?

ティックとは、秒単位で測定される浮動小数点数である時間間隔を指します。夏時間の間に時計が1時間進み、秋に再び戻る夏時間(DST)として時間を取得する場合があります。 。

Pythonタイムモジュールの最も一般的な関数-

1。 time.time()関数

time()は、タイムモジュールのメイン関数です。エポックからの秒数を浮動小数点値として測定します。

構文

time.time()

上記の機能を実証するプログラム:

import time
print("Number of seconds elapsed since the epoch are : ", time.time())

出力

Number of seconds elapsed since the epoch are : 1553262407.0398576

Python時間関数を使用して、2点間の実時間の経過時間を計算できます。

以下は壁時計時間を計算するプログラムです:

import time
start = time.time()
print("Time elapsed on working...")
time.sleep(0.9)
end = time.time()
print("Time consumed in working: ",end - start)

出力

Time elapsed on working...
Time consumed in working: 0.9219651222229004

2。 time.clock()関数

time.clock()関数は、プロセッサ時間を返します。パフォーマンステスト/ベンチマークに使用されます。

構文

time.clock()

clock()関数は、プログラムにかかった適切な時間を返し、対応するものよりも正確です。

区別するために、上記の2つの時間関数(上記で説明)を使用してプログラムを作成しましょう。

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

出力

time()# 1553263728.08, clock()# 0.00
---Sleeping for: 5 sec.
time()# 1553263733.14, clock()# 5.06
---Sleeping for: 4 sec.
time()# 1553263737.25, clock()# 9.17
---Sleeping for: 3 sec.
time()# 1553263740.30, clock()# 12.22
---Sleeping for: 2 sec.
time()# 1553263742.36, clock()# 14.28
---Sleeping for: 1 sec.
time()# 1553263743.42, clock()# 15.34

3。 time.ctime()関数

time.time()関数は、入力として「エポックからの秒数」の時間を取り、現地時間に従って人間が読める形式の文字列値に変換します。引数が渡されない場合は、現在の時刻が返されます。

import time
print('The current local time is :', time.ctime())
newtime = time.time() + 60
print('60 secs from now :', time.ctime(newtime))

出力

The current local time is : Fri Mar 22 19:43:11 2019
60 secs from now : Fri Mar 22 19:44:11 2019

4。 time.sleep()関数

time.sleep()関数は、指定された秒数の間、現在のスレッドの実行を停止します。より正確なスリープ時間を取得するには、浮動小数点値を入力として渡します。

sleep()関数は、ファイルのクローズが完了するのを待つ必要がある場合、またはデータベースのコミットを許可する必要がある場合に使用できます。

import time
# using ctime() to display present time
print ("Time starts from : ",end="")
print (time.ctime())
# using sleep() to suspend execution
print ('Waiting for 5 sec.')
time.sleep(5)
# using ctime() to show present time
print ("Time ends at : ",end="")
print (time.ctime())

出力

Time starts from : Fri Mar 22 20:00:00 2019
Waiting for 5 sec.
Time ends at : Fri Mar 22 20:00:05 2019

5。 time.struct_timeクラス

time.struct_timeは、時間モジュールに存在する唯一のデータ構造です。名前付きタプルインターフェイスがあり、インデックスまたは属性名を介してアクセスできます。

構文

time.struct_time

このクラスは、日付の特定のフィールドにアクセスする必要がある場合に役立ちます。

このクラスは、localtime()、gmtime()などの多くの関数を提供し、struct_timeオブジェクトを返します。

import time
print(' Current local time:', time.ctime())
t = time.localtime()
print('Day of month:', t.tm_mday)
print('Day of week :', t.tm_wday)
print('Day of year :', t.tm_yday)

出力

Current local time: Fri Mar 22 20:10:25 2019
Day of month: 22
Day of week : 4
Day of year : 81

6。 time.strftime()関数

この関数は、2番目の引数でタプルまたはstruct_timeを取り、最初の引数で指定された形式に従って文字列に変換します。

構文

time.strftime()

以下は、time.strftime()関数を実装するプログラムです-

import time
now = time.localtime(time.time())
print("Current date time is: ",time.asctime(now))
print(time.strftime("%y/%m/%d %H:%M", now))
print(time.strftime("%a %b %d", now))
print(time.strftime("%c", now))
print(time.strftime("%I %p", now))
print(time.strftime("%Y-%m-%d %H:%M:%S %Z", now))

出力

Current date time is: Fri Mar 22 20:13:43 2019
19/03/22 20:13
Fri Mar 22
Fri Mar 22 20:13:43 2019
08 PM
2019-03-22 20:13:43 India Standard Time
>

Pythonでタイムゾーンを確認する

タイムゾーン情報を提供する2つの時間プロパティがあります-

1。 time.timezone

ローカル(非DST)タイムゾーンのオフセットをUTC形式で返します。

>>> time.timezone
-19800

2. time.tzname –ローカルの非DSTおよびDSTタイムゾーンを含むタプルを返します。

>>> time.tzname
('India Standard Time', 'India Daylight Time')

  1. Pythonでのベクトル化

    この記事では、Python3.xを使用した実装に関連するベクトル化とさまざまな手法について学習します。またはそれ以前。 ベクトル化とは何ですか? ベクトル化は、ループを使用せずに配列を実装する手法です。代わりに関数を使用すると、コードの実行時間と実行時間を効率的に最小化するのに役立ちます。さまざまな演算が、ベクトルの内積などの配列ではなく、ベクトルに対して実行されています。これは、単一の出力を生成するため、スカラー積とも呼ばれます。外部積は、ベクトルの(長さXの長さ)に等しい次元の二乗行列になります。要素同じインデックスの要素と行列の次元を積む賢明な乗算は変更されません。 内積/内積

  2. Python数学関数

    数学 モジュールは、Pythonの数学関数にアクセスするために使用されます。この関数のすべてのメソッドは、複素数ではなく、整数型または実数型のオブジェクトに使用されます。 このモジュールを使用するには、そのモジュールをコードにインポートする必要があります。 import math いくつかの定数 これらの定数は、計算に含めるために使用されます。 Sr.No。 定数と説明 1 pi 円周率の値を返します:3.141592 2 E 自然ベースの値を返しますe。 eは0.718282 3 タウ タウの値を返します。タウ=6.2