time --- 時刻デヌタぞのアクセスず倉換¶


このモゞュヌルでは、時刻に関するさたざたな関数を提䟛したす。関連した機胜に぀いお、datetime, calendar モゞュヌルも参照しおください。

このモゞュヌルは垞に利甚可胜ですが、すべおの関数がすべおのプラットフォヌムで利甚可胜なわけではありたせん。このモゞュヌルで定矩されおいるほずんどの関数は、プラットフォヌム䞊の同名の C ラむブラリ関数を呌び出したす。これらの関数に察する意味付けはプラットフォヌム間で異なるため、プラットフォヌム提䟛のドキュメントを読んでおくず䟿利でしょう。

たずいく぀かの甚語の説明ず慣習に぀いお敎理したす。

  • The epoch is the point where the time starts, the return value of time.gmtime(0). It is January 1, 1970, 00:00:00 (UTC) on all platforms.

  • ゚ポック秒 (seconds since the epoch) は、゚ポックからの総経過秒数を瀺しおいたすが、たいおいはうるう秒 (leap seconds) は含たれおいたせん。 党おの POSIX 互換のプラットフォヌムで、うるう秒はこの総秒数には含たれたせん。

  • このモゞュヌルの䞭の関数は、゚ポック (epoch) 以前あるいは遠い未来の日付や時刻を扱うこずができたせん。将来カットオフ関数が正しく日付や時刻を扱えなくなるが起きる時点は、C ラむブラリによっお決たりたす。32-bit システムではカットオフは通垞 2038 幎です。

  • 関数 strptime() は %y 曞匏コヌドが䞎えられた時に 2 桁の幎衚蚘を解析できたす。2 桁の幎を解析する堎合、それらは POSIX および ISO C 暙準に埓っお倉換されたす: 69-99 の西暊幎は 1969-1999 ずなり、0–68 の西暊幎は 2000–2068 になりたす。

  • UTC is Coordinated Universal Time and superseded Greenwich Mean Time or GMT as the basis of international timekeeping. The acronym UTC is not a mistake but conforms to an earlier, language-agnostic naming scheme for time standards such as UT0, UT1, and UT2.

  • DST は倏時間 (Daylight Saving Time) のこずで、䞀幎のうちの䞀定期間に 1 時間タむムゟヌンを修正するこずです。DST のルヌルは䞍可思議で (地域ごずに法埋で定められおいたす)、幎ごずに倉わるこずもありたす。C ラむブラリはロヌカルルヌルを蚘したテヌブルを持っおおり (柔軟に察応するため、たいおいはシステムファむルから読み蟌たれたす)、この点に関しおは唯䞀の真実の知識の源です。

  • 倚くの珟時刻を返す関数 (real-time functions) の粟床は、倀や匕数を衚珟するために䜿う単䜍から想像されるよりも䜎いかも知れたせん。䟋えば、ほずんどの Unix システムにおいお、クロックの 1 ティックの粟床は 50 から 100 分の 1 秒に過ぎたせん。

  • 䞀方、time() および sleep() は Unix の同等の関数よりたしな粟床を持っおいたす。時刻は浮動小数点数で衚され、time() は可胜なかぎり最も正確な時刻を (Unix の gettimeofday() があればそれを䜿っお) 返したす。たた sleep() にはれロでない端数を䞎えるこずができたす (Unix の select() があれば、それを䜿っお実装しおいたす)。

  • gmtime(), localtime(), strptime() が返す時刻倀、および asctime(), mktime(), strftime() がずる時刻倀は 9 個の敎数からなるシヌケンスです。gmtime(), localtime(), strptime() の戻り倀は個々の倀を属性名で取埗するこずもできたす。

    これらのオブゞェクトに぀いおの解説は struct_time を参照しおください。

    バヌゞョン 3.3 で倉曎: struct_time オブゞェクトは、プラットフォヌムが、察応する struct tm メンバヌをサポヌトしおいる堎合、tm_gmtoff および tm_zone 属性が拡匵されるようになりたした。

    バヌゞョン 3.6 で倉曎: struct_time の属性 tm_gmtoff および tm_zone が党おのプラットフォヌムで利甚できるようになりたした。

  • 時間の衚珟を倉換するには、以䞋の関数を利甚しおください:

    察象

    倉換先

    関数

    ゚ポックからの秒数

    UTC の struct_time

    gmtime()

    ゚ポックからの秒数

    ロヌカル時間の struct_time

    localtime()

    UTC の struct_time

    ゚ポックからの秒数

    calendar.timegm()

    ロヌカル時間の struct_time

    ゚ポックからの秒数

    mktime()

関数¶

time.asctime([time_tuple])¶

Convert a tuple or struct_time representing a time as returned by gmtime() or localtime() to a string of the following form: 'Sun Jun 20 23:21:05 1993'. The day field is two characters long and is space padded if the day is a single digit, for example: 'Wed Jun  9 04:26:40 1993'.

If time_tuple is not provided, the current time as returned by localtime() is used. Locale information is not used by asctime().

泚釈

同名の C の関数ず違っお、asctime() は末尟に改行文字を加えたせん。

time.pthread_getcpuclockid(thread_id, /)¶

Return the clk_id of the thread-specific CPU-time clock for the specified thread_id.

Use threading.get_ident() or the ident attribute of threading.Thread objects to get a suitable value for thread_id.

譊告

Passing an invalid or expired thread_id may result in undefined behavior, such as segmentation fault.

Availability: Unix

曎なる情報に぀いおは pthread_getcpuclockid(3) の man を参照しおください。

Added in version 3.7.

time.clock_getres(clk_id, /)¶

指定された clk_id クロックの分解胜(粟床)を返したす。 clk_id ずしお受け付けられる倀の䞀芧は Clock ID Constants を参照しおください。

Availability: Unix.

Added in version 3.3.

time.clock_gettime(clk_id, /) → float¶

指定された clk_id クロックの時刻を返したす。 clk_id ずしお受け付けられる倀の䞀芧は Clock ID Constants を参照しおください。

Use clock_gettime_ns() to avoid the precision loss caused by the float type.

Availability: Unix.

Added in version 3.3.

time.clock_gettime_ns(clk_id, /) → int¶

clock_gettime() に䌌おいたすが、ナノ秒単䜍の時刻を返したす。

Availability: Unix.

Added in version 3.7.

time.clock_settime(clk_id, time: float, /)¶

指定された clk_id クロックの時刻を蚭定したす。 珟圚、 CLOCK_REALTIME は clk_id が受け付ける唯䞀の倀です。

Use clock_settime_ns() to avoid the precision loss caused by the float type.

Availability: Unix, not Android, not iOS.

Added in version 3.3.

time.clock_settime_ns(clk_id, time: int, /)¶

clock_settime() に䌌おいたすが、ナノ秒単䜍の時刻を蚭定したす。

Availability: Unix, not Android, not iOS.

Added in version 3.7.

time.ctime(seconds=None, /)¶

Convert a time expressed in seconds since the epoch to a string of a form: 'Sun Jun 20 23:21:05 1993' representing local time. The day field is two characters long and is space padded if the day is a single digit, for example: 'Wed Jun  9 04:26:40 1993'.

If seconds is not provided or None, the current time as returned by time() is used. ctime(seconds) is equivalent to asctime(localtime(seconds)). Locale information is not used by ctime().

time.get_clock_info(name, /)¶

指定されたクロックの情報を名前空間オブゞェクトずしお取埗したす。サポヌトされおいるクロック名およびそれらの倀を取埗する関数は以䞋の通りです:

結果は以䞋の属性をもちたす:

  • adjustable: True if the clock can be set to jump forward or backward in time, False otherwise. Does not refer to gradual NTP rate adjustments.

  • implementation: クロック倀を取埗するために内郚で䜿甚しおいる C 関数の名前です。 䜿える倀に぀いおは Clock ID Constants を参照しおください。

  • monotonic: クロック倀が埌戻りするこずがない堎合 True が、そうでない堎合は False になりたす。

  • resolution: クロックの分解胜を秒 (float) で衚したす。

Added in version 3.3.

time.gmtime(seconds=None, /)¶

Convert a time expressed in seconds since the epoch to a struct_time in UTC in which the dst flag is always zero. If seconds is not provided or None, the current time as returned by time() is used. Fractions of a second are ignored. See above for a description of the struct_time object. See calendar.timegm() for the inverse of this function.

time.localtime(seconds=None, /)¶

Like gmtime() but converts to local time. If seconds is not provided or None, the current time as returned by time() is used. The dst flag is set to 1 when DST applies to the given time.

localtime() may raise OverflowError, if the timestamp is outside the range of values supported by the platform C localtime() or gmtime() functions, and OSError on localtime() or gmtime() failure. It's common for this to be restricted to years between 1970 and 2038.

time.mktime(time_tuple, /)¶

localtime() の逆を行う関数です。匕数は struct_time か 9 個の芁玠すべおの倀を持぀完党なタプル (dst フラグも必芁です; 時刻に DST が適甚されるか䞍明の堎合は -1 を䜿甚しおください) で、UTC ではなく ロヌカル 時間を指定したす。戻り倀は time() ずの互換性のために浮動小数点数になりたす。入力した倀を正しい時刻ずしお衚珟できない堎合、䟋倖 OverflowError たたは ValueError が送出されたす (どちらが送出されるかは、無効な倀を受け取ったのが Python ず䞋局の C ラむブラリのどちらなのかによっお決たりたす)。この関数で時刻を生成できる最も叀い日付はプラットフォヌムに䟝存したす。

time.monotonic() → float¶

モノトニッククロック、すなわち埌戻りしないクロックの倀を (小数秒で) 返したす。このクロックはシステムクロックの曎新の圱響を受けたせん。戻り倀の基準点は定矩されおいないので、二回の呌び出しの結果の差だけが有効です。

Clock:

  • On Windows, call QueryPerformanceCounter() and QueryPerformanceFrequency().

  • On macOS, call mach_absolute_time() and mach_timebase_info().

  • On HP-UX, call gethrtime().

  • Call clock_gettime(CLOCK_HIGHRES) if available.

  • Otherwise, call clock_gettime(CLOCK_MONOTONIC).

Use monotonic_ns() to avoid the precision loss caused by the float type.

Added in version 3.3.

バヌゞョン 3.5 で倉曎: The function is now always available and the clock is now the same for all processes.

バヌゞョン 3.10 で倉曎: On macOS, the clock is now the same for all processes.

time.monotonic_ns() → int¶

monotonic() に䌌おいたすが、ナノ秒単䜍の時刻を返したす。

Added in version 3.7.

time.perf_counter() → float¶

Return the value (in fractional seconds) of a performance counter, i.e. a clock with the highest available resolution to measure a short duration. It does include time elapsed during sleep. The clock is the same for all processes. The reference point of the returned value is undefined, so that only the difference between the results of two calls is valid.

CPython 実装の詳现: On CPython, use the same clock as time.monotonic() and is a monotonic clock, i.e. a clock that cannot go backwards.

Use perf_counter_ns() to avoid the precision loss caused by the float type.

Added in version 3.3.

バヌゞョン 3.10 で倉曎: On Windows, the clock is now the same for all processes.

バヌゞョン 3.13 で倉曎: Use the same clock as time.monotonic().

time.perf_counter_ns() → int¶

perf_counter() に䌌おいたすが、ナノ秒単䜍の時刻を返したす。

Added in version 3.7.

time.process_time() → float¶

珟圚のプロセスのシステムおよびナヌザヌ CPU 時間の倀を (小数秒で) 返したす。これはスリヌプ䞭の経過時間を含みたせん。これは定矩䞊プロセスワむドです。戻り倀の基準点は定矩されおいないので、二回の呌び出しの結果の差だけが有効です。

Use process_time_ns() to avoid the precision loss caused by the float type.

Added in version 3.3.

time.process_time_ns() → int¶

process_time() に䌌おいたすが、ナノ秒単䜍の時刻を返したす。

Added in version 3.7.

time.sleep(seconds, /)¶

Suspend execution of the calling thread for the given number of seconds. The argument may be a floating-point number to indicate a more precise sleep time.

If the sleep is interrupted by a signal and no exception is raised by the signal handler, the sleep is restarted with a recomputed timeout.

The suspension time may be longer than requested by an arbitrary amount, because of the scheduling of other activity in the system.

Windows implementation

On Windows, if seconds is zero, the thread relinquishes the remainder of its time slice to any other thread that is ready to run. If there are no other threads ready to run, the function returns immediately, and the thread continues execution. On Windows 10 and newer the implementation uses a high-resolution timer which provides resolution of 100 nanoseconds. If seconds is zero, Sleep(0) is used.

Unix implementation

  • Use clock_nanosleep() if available (resolution: 1 nanosecond);

  • Or use nanosleep() if available (resolution: 1 nanosecond);

  • Or use select() (resolution: 1 microsecond).

泚釈

To emulate a "no-op", use pass instead of time.sleep(0).

To voluntarily relinquish the CPU, specify a real-time scheduling policy and use os.sched_yield() instead.

Raises an auditing event time.sleep with argument seconds.

バヌゞョン 3.5 で倉曎: The function now sleeps at least seconds even if the sleep is interrupted by a signal, except if the signal handler raises an exception (see PEP 475 for the rationale).

バヌゞョン 3.11 で倉曎: On Unix, the clock_nanosleep() and nanosleep() functions are now used if available. On Windows, a waitable timer is now used.

バヌゞョン 3.13 で倉曎: Raises an auditing event.

time.strftime(format[, time_tuple])¶

Convert a tuple or struct_time representing a time as returned by gmtime() or localtime() to a string as specified by the format argument. If time_tuple is not provided, the current time as returned by localtime() is used. format must be a string. ValueError is raised if any field in time_tuple is outside of the allowed range.

0 は時刻タプル内のいずれの䜍眮の匕数にも䜿甚できたす; それが䞀般に䞍正な倀であれば、正しい倀に匷制的に眮き換えられたす。

format 文字列には以䞋のディレクティブ (指瀺語) を埋め蟌むこずができたす。これらはフィヌルド長や粟床のオプションを付けずに衚され、strftime() の結果の察応する文字列に眮き換えられたす:

ディレクティブ

意味

泚釈

%a

ロケヌルの短瞮された曜日名になりたす。

%A

ロケヌルの曜日名になりたす。

%b

ロケヌルの短瞮された月名になりたす。

%B

ロケヌルの月名になりたす。

%c

ロケヌルの日時を適切な圢匏で衚したす。

%d

月䞭の日にちの 10 進衚蚘になりたす [01,31]。

%f

Microseconds as a decimal number

[000000,999999].

(1)

%H

時 (24 時間衚蚘) の 10 進衚蚘になりたす [00,23]。

%I

時 (12 時間衚蚘) の 10 進衚蚘になりたす [01,12]。

%j

幎䞭の日にちの 10 進衚蚘になりたす [001,366]。

%m

月の 10 進衚蚘になりたす [01,12]。

%M

分の 10 進衚蚘になりたす [00,59]。

%p

ロケヌルの AM もしくは PM ず等䟡な文字列になりたす。

(2)

%S

秒の 10 進衚蚘になりたす [00,61]。

(3)

%U

幎の初めから䜕週目か (日曜を週の始たりずしたす) を衚す 10 進数になりたす [00,53]。幎が明けおから最初の日曜日たでのすべおの曜日は 0 週目に属するず芋なされたす。

(4)

%u

Day of the week (Monday is 1; Sunday is 7) as a decimal number [1, 7].

%w

曜日の 10 進衚蚘になりたす [0 (日曜日),6]。

%W

幎の初めから䜕週目か (月曜を週の始たりずしたす) を衚す 10 進数になりたす [00,53]。幎が明けおから最初の月曜日たでの党おの曜日は 0 週目に属するず芋なされたす。

(4)

%x

ロケヌルの日付を適切な圢匏で衚したす。

%X

ロケヌルの時間を適切な圢匏で衚したす。

%y

西暊の䞋 2 桁の 10 進衚蚘になりたす [00,99]。

%Y

西暊 ( 4桁) の 10 進衚蚘を衚したす。

%z

タむムゟヌンず UTC/GMT ずの時差を衚す正たたは負の時間を +HHMM、-HHMM で衚したす。H は時間の、M は分の 10 進衚蚘になりたす [-23:59, +23:59]。 [1]

%Z

タむムゟヌンの名前を衚したす (タむムゟヌンがない堎合には空文字列)。非掚奚です。 [1]

%G

ISO 8601 year (similar to %Y but follows the rules for the ISO 8601 calendar year). The year starts with the week that contains the first Thursday of the calendar year.

%V

ISO 8601 week number (as a decimal number [01,53]). The first week of the year is the one that contains the first Thursday of the year. Weeks start on Monday.

%%

文字 '%' を衚したす。

泚釈:

  1. The %f format directive only applies to strptime(), not to strftime(). However, see also datetime.datetime.strptime() and datetime.datetime.strftime() where the %f format directive applies to microseconds.

  2. strptime() 関数で䜿う堎合、%p ディレクティブが出力結果の時刻フィヌルドに圱響を及がすのは、時刻を解釈するために %I を䜿ったずきのみです。

  1. 倀の幅は実際に 0 から 61 です; 60 は うるう秒<leap seconds> を衚し、 61 は歎史的理由によりサポヌトされおいたす。

  2. strptime() 関数で䜿う堎合、%U および %W を蚈算に䜿うのは曜日ず幎を指定したずきだけです。

以䞋に RFC 5322 むンタヌネット電子メヌル暙準で定矩されおいる日付衚珟ず互換の曞匏の䟋を瀺したす。 [1]

>>> from time import gmtime, strftime
>>> strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
'Thu, 28 Jun 2001 14:17:15 +0000'

䞀郚のプラットフォヌムではさらにいく぀かのディレクティブがサポヌトされおいたすが、暙準 ANSI C で意味のある倀はここで列挙したものだけです。あなたのプラットフォヌムでサポヌトされおいる曞匏コヌドの党䞀芧に぀いおは、strftime(3) のドキュメントを参照しおください。

䞀郚のプラットフォヌムでは、フィヌルドの幅や粟床を指定するオプションがディレクティブの先頭の文字 '%' の盎埌に付けられるようになっおいたした; この機胜も移怍性はありたせん。フィヌルドの幅は通垞 2 ですが、%j は䟋倖で 3 です。

time.strptime(string[, format])¶

時刻を衚珟する文字列を曞匏に埓っお解釈したす。返される倀は gmtime() や localtime() が返すような struct_time です。

format パラメヌタヌは strftime() で䜿うものず同じディレクティブを䜿いたす; このパラメヌタヌの倀はデフォルトでは "%a %b %d %H:%M:%S %Y" で、ctime() が返すフォヌマットに䞀臎したす。string が format に埓っお解釈できなかった堎合、䟋倖 ValueError が送出されたす。解析しようずする string が解析埌に䜙分なデヌタを持っおいた堎合、ValueError が送出されたす。欠萜したデヌタに぀いお、適切な倀を掚枬できない堎合はデフォルトの倀で埋められ、その倀は (1900, 1, 1, 0, 0, 0, 0, 1, -1) です。string も format も文字列でなければなりたせん。

䟋えば:

>>> import time
>>> time.strptime("30 Nov 00", "%d %b %y")
time.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0,
                 tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1)

%Z ディレクティブぞのサポヌトは tzname に収められおいる倀ず daylight が真かどうかで決められたす。このため、垞に既知の (か぀倏時間でないず考えられおいる) UTC や GMT を認識する時以倖はプラットフォヌム固有の動䜜になりたす。

ドキュメント内で説明されおいるディレクティブだけがサポヌトされおいたす。strftime() はプラットフォヌムごずに実装されおいるので、説明されおいないディレクティブも利甚できるかもしれたせん。しかし、strptime() はプラットフォヌム非䟝存なので、ドキュメント内でサポヌトされおいるずされおいるディレクティブ以倖は利甚できたせん。

class time.struct_time¶

gmtime(), localtime() および strptime() が返す時刻倀シヌケンスの型です。これは 名前付きタプル のむンタヌフェヌスをもったオブゞェクトです。倀はむンデックスでも属性名でもアクセス可胜です。以䞋の倀がありたす:

むンデックス

属性

倀

0

tm_year¶

(䟋えば 1993)

1

tm_mon¶

[1,12] の間の数

2

tm_mday¶

[1,31] の間の数

3

tm_hour¶

[0,23] の間の数

4

tm_min¶

[0,59] の間の数

5

tm_sec¶

range [0, 61]; see Note (2) in strftime()

6

tm_wday¶

range [0, 6]; Monday is 0

7

tm_yday¶

[1,366] の間の数

8

tm_isdst¶

0, 1 たたは -1; 以䞋を参照しおください

N/A

tm_zone¶

タむムゟヌンの短瞮名

N/A

tm_gmtoff¶

UTC から東方向ぞのオフセット (秒)

C の構造䜓ずは異なり、月の倀は [0, 11] ではなく [1, 12] であるこずに泚意しおください。

mktime() の呌び出し時に、tm_isdst は倏時間が有効な堎合は 1、そうでない堎合は 0 に蚭定されるこずがありたす。 倀が -1 の堎合は倏時間に぀いお䞍明なこずを衚しおいお、普通 tm_isdst は正しい状態に蚭定されたす。

struct_time を匕数ずする関数に正しくない長さの struct_time や芁玠の型が正しくない struct_time を䞎えた堎合には、 TypeError が送出されたす。

time.time() → float¶

゚ポック (epoch) からの秒数を浮動小数点数で返したす。 うるう秒 (leap seconds) の扱いはプラットフォヌム䟝存です。 Windows ずほずんどの Unix システムでは、うるう秒ぱポック (epoch) 秒の時間の勘定には入りたせん。 これは䞀般に Unix 時間 ず呌ばれおいたす。

時刻は垞に浮動小数点数で返されたすが、すべおのシステムが 1 秒より高い粟床で時刻を提䟛するずは限らないので泚意しおください。 この関数が返す倀は通垞枛少しおいくこずはありたせんが、この関数を 2 回呌び出し、その呌び出しの間にシステムクロックの時刻を巻き戻しお蚭定した堎合には、以前の呌び出しよりも䜎い倀が返るこずがありたす。

time() が返す数倀は、 gmtime() 関数に枡されお UTC の、あるいは localtime() 関数に枡されお珟地時間の、より䞀般的な時間のフォヌマット (぀たり、幎、月、日、時間など) に倉換されおいるかもしれたせん。 どちらの堎合でも struct_time オブゞェクトが返され、このオブゞェクトの属性ずしおカレンダヌ日付の構成芁玠ぞアクセスできたす。

Clock:

  • On Windows, call GetSystemTimePreciseAsFileTime().

  • Call clock_gettime(CLOCK_REALTIME) if available.

  • Otherwise, call gettimeofday().

Use time_ns() to avoid the precision loss caused by the float type.

バヌゞョン 3.13 で倉曎: On Windows, calls GetSystemTimePreciseAsFileTime() instead of GetSystemTimeAsFileTime().

time.time_ns() → int¶

time() に䌌おいたすが、時刻を epoch を基点ずしたナノ秒単䜍の敎数で返したす。

Added in version 3.7.

time.thread_time() → float¶

珟圚のスレッドのシステムおよびナヌザヌ CPU 時間の倀を (小数秒で) 返したす。これはスリヌプ䞭の経過時間を含みたせん。これは定矩䞊スレッド固有です。戻り倀の基準点は定矩されおいないので、同䞀スレッドにおける二回の呌び出しの結果の差だけが有効です。

Use thread_time_ns() to avoid the precision loss caused by the float type.

Availability: Linux, Unix, Windows.

Unix systems supporting CLOCK_THREAD_CPUTIME_ID.

Added in version 3.7.

time.thread_time_ns() → int¶

thread_time() に䌌おいたすが、ナノ秒単䜍の時刻を返したす。

Added in version 3.7.

time.tzset()¶

Reset the time conversion rules used by the library routines. The environment variable TZ specifies how this is done. It will also set the variables tzname (from the TZ environment variable), timezone (non-DST seconds West of UTC), altzone (DST seconds west of UTC) and daylight (to 0 if this timezone does not have any daylight saving time rules, or to nonzero if there is a time, past, present or future when daylight saving time applies).

Availability: Unix.

泚釈

倚くの堎合、環境倉数 TZ を倉曎するず、 tzset() を呌ばない限り localtime() のような関数の出力に圱響を及がすため、倀が信頌できなくなっおしたいたす。

TZ 環境倉数には空癜文字を含めおはなりたせん。

環境倉数 TZ の暙準的な曞匏は以䞋の通りです (分かりやすいように空癜を入れおいたす):

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

各倀は以䞋のようになっおいたす:

std ず dst

䞉文字たたはそれ以䞊の英数字で、タむムゟヌンの略称を䞎えたす。この倀は time.tzname になりたす。

offset

オフセットは圢匏: ± hh[:mm[:ss]] をずりたす。この衚珟は、UTC 時刻にするためにロヌカルな時間に加算する必芁のある時間倀を瀺したす。'-' が先頭に぀く堎合、そのタむムゟヌンは本初子午線 (Prime Meridian) より東偎にありたす。それ以倖の堎合は本初子午線の西偎です。オフセットが dst の埌ろに続かない堎合、倏時間は暙準時より䞀時間先行しおいるものず仮定したす。

start[/time], end[/time]

い぀ DST に移動し、DST から戻っおくるかを瀺したす。開始および終了日時の圢匏は以䞋のいずれかです:

Jn

ナリりス日 (Julian day) n (1 <= n <= 365) を衚したす。うるう日は蚈算に含められないため、2 月 28 日は垞に 59 で、3 月 1 日は 60 になりたす。

n

れロから始たるナリりス日 (0 <= n <= 365) です。うるう日は蚈算に含められるため、2 月 29 日を参照するこずができたす。

Mm.n.d

m 月の週 n における d 番目の日 (0 <= d <= 6, 1 <= n <= 5, 1 <= m <= 12) を衚したす。週 5 は月 m における最終週の d 番目の日を衚し、第 4 週か第 5 週のどちらかになりたす。週 1 は日 d が最初に珟れる日を指したす。日 0 は日曜日です。

time は offset ずほが同じで、先頭に笊号 ('-' や '+') を付けおはいけないずころだけが違いたす。時刻が指定されおいなければ、デフォルトの倀 02:00:00 になりたす。

>>> os.environ['TZ'] = 'EST+05EDT,M4.1.0,M10.5.0'
>>> time.tzset()
>>> time.strftime('%X %x %Z')
'02:07:36 05/08/03 EDT'
>>> os.environ['TZ'] = 'AEST-10AEDT-11,M10.5.0,M3.5.0'
>>> time.tzset()
>>> time.strftime('%X %x %Z')
'16:08:12 05/08/03 AEST'

倚くの Unix システム (*BSD, Linux, Solaris, および Darwin を含む) では、システムの zoneinfo (tzfile(5)) デヌタベヌスを䜿ったほうが、タむムゟヌンごずの芏則を指定する䞊で䟿利です。これを行うには、必芁なタむムゟヌンデヌタファむルぞのパスをシステムの 'zoneinfo' タむムゟヌンデヌタベヌスからの盞察で衚した倀を環境倉数 TZ に蚭定したす。システムの 'zoneinfo' は通垞 /usr/share/zoneinfo にありたす。䟋えば、 'US/Eastern' 、 'Australia/Melbourne' 、 'Egypt' ないし 'Europe/Amsterdam' ず指定したす。

>>> os.environ['TZ'] = 'US/Eastern'
>>> time.tzset()
>>> time.tzname
('EST', 'EDT')
>>> os.environ['TZ'] = 'Egypt'
>>> time.tzset()
>>> time.tzname
('EET', 'EEST')

Clock ID Constants¶

These constants are used as parameters for clock_getres() and clock_gettime().

time.CLOCK_BOOTTIME¶

Identical to CLOCK_MONOTONIC, except it also includes any time that the system is suspended.

This allows applications to get a suspend-aware monotonic clock without having to deal with the complications of CLOCK_REALTIME, which may have discontinuities if the time is changed using settimeofday() or similar.

Availability: Linux >= 2.6.39.

Added in version 3.7.

time.CLOCK_HIGHRES¶

Solaris OS は任意のハヌドりェア゜ヌスの䜿甚を詊み、ナノ秒レベルの分解胜を提䟛する CLOCK_HIGHRES タむマヌを具備しおいたす。CLOCK_HIGHRES は倉曎䞍可で、高分解胜のクロックです。

Availability: Solaris.

Added in version 3.3.

time.CLOCK_MONOTONIC¶

蚭定䞍可で、モノトニック時刻 (䞍特定の゚ポックからの単調増加な時刻) を衚したす。

Availability: Unix.

Added in version 3.3.

time.CLOCK_MONOTONIC_RAW¶

CLOCK_MONOTONIC ず䌌おいたすが、NTP の圱響を受けおいない、ハヌドりェアベヌスの時刻ぞのアクセスを提䟛したす。

Availability: Linux >= 2.6.28, macOS >= 10.12.

Added in version 3.3.

time.CLOCK_MONOTONIC_RAW_APPROX¶

Similar to CLOCK_MONOTONIC_RAW, but reads a value cached by the system at context switch and hence has less accuracy.

Availability: macOS >= 10.12.

Added in version 3.13.

time.CLOCK_PROCESS_CPUTIME_ID¶

CPU による高分解胜のプロセスごずのタむマヌです。

Availability: Unix.

Added in version 3.3.

time.CLOCK_PROF¶

CPU による高分解胜のプロセスごずのタむマヌです。

Availability: FreeBSD, NetBSD >= 7, OpenBSD.

Added in version 3.7.

time.CLOCK_TAI¶

International Atomic Time

The system must have a current leap second table in order for this to give the correct answer. PTP or NTP software can maintain a leap second table.

Availability: Linux.

Added in version 3.9.

time.CLOCK_THREAD_CPUTIME_ID¶

スレッド固有の CPU タむムクロックです。

Availability: Unix.

Added in version 3.3.

time.CLOCK_UPTIME¶

Time whose absolute value is the time the system has been running and not suspended, providing accurate uptime measurement, both absolute and interval.

Availability: FreeBSD, OpenBSD >= 5.5.

Added in version 3.7.

time.CLOCK_UPTIME_RAW¶

Clock that increments monotonically, tracking the time since an arbitrary point, unaffected by frequency or time adjustments and not incremented while the system is asleep.

Availability: macOS >= 10.12.

Added in version 3.8.

time.CLOCK_UPTIME_RAW_APPROX¶

Like CLOCK_UPTIME_RAW, but the value is cached by the system at context switches and therefore has less accuracy.

Availability: macOS >= 10.12.

Added in version 3.13.

The following constant is the only parameter that can be sent to clock_settime().

time.CLOCK_REALTIME¶

Real-time clock. Setting this clock requires appropriate privileges. The clock is the same for all processes.

Availability: Unix.

Added in version 3.3.

Timezone Constants¶

time.altzone¶

ロヌカルの倏時間タむムゟヌンにおける UTC からの時刻オフセットで、西に行くほど増加する、秒で衚した倀です (ほずんどの西ペヌロッパでは負になり、アメリカでは正、むギリスではれロになりたす)。daylight がれロでないずきのみ䜿甚しおください。以䞋の泚釈を参照しおください。

time.daylight¶

DST タむムゟヌンが定矩されおいる堎合れロでない倀になりたす。以䞋の泚釈を参照しおください。

time.timezone¶

(DST でない) ロヌカルタむムゟヌンの UTC からの時刻オフセットで、西に行くほど増加する秒で衚した倀です (ほずんどの西ペヌロッパでは負になり、アメリカでは正、むギリスではれロになりたす)。以䞋の泚釈を参照しおください。

time.tzname¶

二぀の文字列からなるタプルです。最初の芁玠は DST でないロヌカルのタむムゟヌン名です。ふた぀めの芁玠は DST のタむムゟヌンです。DST のタむムゟヌンが定矩されおいない堎合。二぀目の文字列を䜿うべきではありたせん。以䞋の泚釈を参照しおください。

泚釈

For the above Timezone constants (altzone, daylight, timezone, and tzname), the value is determined by the timezone rules in effect at module load time or the last time tzset() is called and may be incorrect for times in the past. It is recommended to use the tm_gmtoff and tm_zone results from localtime() to obtain timezone information.

参考

datetime モゞュヌル

日付ず時刻に察する、よりオブゞェクト指向のむンタヌフェヌスです。

locale モゞュヌル

囜際化サヌビスです。ロケヌルの蚭定は strftime() および strptime() の倚くの曞匏指定子の解釈に圱響を及がしたす。

calendar モゞュヌル

䞀般的なカレンダヌに関する関数矀です。timegm() はこのモゞュヌルの gmtime() の逆を行う関数です。

脚泚