Python 3 timeライブラリ関数
4408 ワード
詳細timeライブラリ関数リファレンスhttps://docs.python.org/3/library/time.html?highlight=strftime#time
形式:
一、時間取得:
1.time():現在のコンピュータの内部時間を取得し、浮動小数点数を返す
2.ctime():現在時刻を読みやすく取得し、文字列を返す
3.gmtime():現在の時間を取得し、コンピュータが処理できる時間フォーマットに戻り、UTCの時間計
4.localtime():コンピュータの現在時間を現地時間で取得
二、時間フォーマット:
1.strftime(tpl,ts):tplはフォーマットテンプレート文字列であり、出力効果を定義し、tsは時間変数である
Directive
Meaning
Notes
Locale’s abbreviated weekday name.
Locale’s full weekday name.
Locale’s abbreviated month name.
Locale’s full month name.
Locale’s appropriate date and time representation.
Day of the month as a decimal number [01,31].
Hour (24-hour clock) as a decimal number [00,23].
Hour (12-hour clock) as a decimal number [01,12].
Day of the year as a decimal number [001,366].
Month as a decimal number [01,12].
Minute as a decimal number [00,59].
Locale’s equivalent of either AM or PM.
(1)
Second as a decimal number [00,61].
(2)
Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.
(3)
Weekday as a decimal number [0(Sunday),6].
Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.
(3)
Locale’s appropriate date representation.
Locale’s appropriate time representation.
Year without century as a decimal number [00,99].
Year with century as a decimal number.
Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59].
Time zone name (no characters if no time zone exists).
A literal
2.strptime(str,tpl):strは文字列形式の時間値であり、tplはフォーマットテンプレート文字列であり、出力効果を定義する
三、プログラムタイミング:
1.sleep(s):スリープ時間、秒単位、浮動小数点数、すなわちs秒待ち
2.perf_counter():1つのCPUレベルの正確な時間カウントを返し、単位は秒で、連続的に差を求めることでプログラム実行時間を得ることができる.
形式:
import time
time.() # ()
一、時間取得:
1.time():現在のコンピュータの内部時間を取得し、浮動小数点数を返す
>>> time.time()
1524146321.7763612 # 。。。
2.ctime():現在時刻を読みやすく取得し、文字列を返す
>>> time.ctime()
'Thu Apr 19 21:59:34 2018' # 2018-4-19 21:59:34
3.gmtime():現在の時間を取得し、コンピュータが処理できる時間フォーマットに戻り、UTCの時間計
>>> time.gmtime()
time.struct_time(tm_year=2018, tm_mon=4, tm_mday=19, tm_hour=14, tm_min=1, tm_sec=8, tm_wday=3, tm_yday=109, tm_isdst=0) # , 。。。
4.localtime():コンピュータの現在時間を現地時間で取得
>>> time.localtime()
time.struct_time(tm_year=2018, tm_mon=4, tm_mday=20, tm_hour=10, tm_min=12, tm_sec=38, tm_wday=4, tm_yday=110, tm_isdst=0)
二、時間フォーマット:
1.strftime(tpl,ts):tplはフォーマットテンプレート文字列であり、出力効果を定義し、tsは時間変数である
Directive
Meaning
Notes
%a
Locale’s abbreviated weekday name.
%A
Locale’s full weekday name.
%b
Locale’s abbreviated month name.
%B
Locale’s full month name.
%c
Locale’s appropriate date and time representation.
%d
Day of the month as a decimal number [01,31].
%H
Hour (24-hour clock) as a decimal number [00,23].
%I
Hour (12-hour clock) as a decimal number [01,12].
%j
Day of the year as a decimal number [001,366].
%m
Month as a decimal number [01,12].
%M
Minute as a decimal number [00,59].
%p
Locale’s equivalent of either AM or PM.
(1)
%S
Second as a decimal number [00,61].
(2)
%U
Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.
(3)
%w
Weekday as a decimal number [0(Sunday),6].
%W
Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.
(3)
%x
Locale’s appropriate date representation.
%X
Locale’s appropriate time representation.
%y
Year without century as a decimal number [00,99].
%Y
Year with century as a decimal number.
%z
Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59].
%Z
Time zone name (no characters if no time zone exists).
%%
A literal
'%'
character. >>> time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
'2018-04-20 10:34:57'
2.strptime(str,tpl):strは文字列形式の時間値であり、tplはフォーマットテンプレート文字列であり、出力効果を定義する
>>> time.strptime('2018-4-19 22:14:15','%Y-%m-%d %H:%M:%S')
time.struct_time(tm_year=2018, tm_mon=4, tm_mday=19, tm_hour=22, tm_min=14, tm_sec=15, tm_wday=3, tm_yday=109, tm_isdst=-1)
三、プログラムタイミング:
1.sleep(s):スリープ時間、秒単位、浮動小数点数、すなわちs秒待ち
2.perf_counter():1つのCPUレベルの正確な時間カウントを返し、単位は秒で、連続的に差を求めることでプログラム実行時間を得ることができる.
>>> start=time.perf_counter()
>>> end=time.perf_counter()
>>> end-start
10.702251718395534