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ãšããã¯ããã®ç§æ°
ããŒã«ã«æéã®
struct_timeUTC ã®
struct_timeãšããã¯ããã®ç§æ°
ããŒã«ã«æéã®
struct_timeãšããã¯ããã®ç§æ°
颿°Â¶
- time.asctime([time_tuple])¶
Convert a tuple or
struct_timerepresenting a time as returned bygmtime()orlocaltime()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 byasctime().泚é
ååã® 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 theidentattribute ofthreading.Threadobjects 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 thefloattype.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 thefloattype.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 bytime()is used.ctime(seconds)is equivalent toasctime(localtime(seconds)). Locale information is not used byctime().
- time.get_clock_info(name, /)¶
æå®ãããã¯ããã¯ã®æ å ±ãåå空éãªããžã§ã¯ããšããŠååŸããŸãããµããŒããããŠããã¯ããã¯åããã³ãããã®å€ãååŸãã颿°ã¯ä»¥äžã®éãã§ã:
'monotonic':time.monotonic()'perf_counter':time.perf_counter()'process_time':time.process_time()'thread_time':time.thread_time()'time':time.time()
çµæã¯ä»¥äžã®å±æ§ããã¡ãŸã:
adjustable:
Trueif the clock can be set to jump forward or backward in time,Falseotherwise. 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_timein UTC in which the dst flag is always zero. If seconds is not provided orNone, the current time as returned bytime()is used. Fractions of a second are ignored. See above for a description of thestruct_timeobject. Seecalendar.timegm()for the inverse of this function.
- time.localtime(seconds=None, /)¶
Like
gmtime()but converts to local time. If seconds is not provided orNone, the current time as returned bytime()is used. The dst flag is set to1when DST applies to the given time.localtime()may raiseOverflowError, if the timestamp is outside the range of values supported by the platform Clocaltime()orgmtime()functions, andOSErroronlocaltime()orgmtime()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()andQueryPerformanceFrequency().On macOS, call
mach_absolute_time()andmach_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 thefloattype.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 thefloattype.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 thefloattype.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
passinstead oftime.sleep(0).To voluntarily relinquish the CPU, specify a real-time scheduling policy and use
os.sched_yield()instead.Raises an auditing event
time.sleepwith argumentseconds.ããŒãžã§ã³ 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()andnanosleep()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_timerepresenting a time as returned bygmtime()orlocaltime()to a string as specified by the format argument. If time_tuple is not provided, the current time as returned bylocaltime()is used. format must be a string.ValueErroris 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)
%uDay 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]
%GISO 8601 year (similar to
%Ybut follows the rules for the ISO 8601 calendar year). The year starts with the week that contains the first Thursday of the calendar year.%VISO 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.
%%æå
'%'ã衚ããŸããæ³šé:
The
%fformat directive only applies tostrptime(), not tostrftime(). However, see alsodatetime.datetime.strptime()anddatetime.datetime.strftime()where the%fformat directive applies to microseconds.strptime()颿°ã§äœ¿ãå Žåã%pãã£ã¬ã¯ãã£ããåºåçµæã®æå»ãã£ãŒã«ãã«åœ±é¿ãåãŒãã®ã¯ãæå»ãè§£éããããã«%Iã䜿ã£ããšãã®ã¿ã§ãã
å€ã®å¹ ã¯å®éã«
0ãã61ã§ã;60㯠ãããç§<leap seconds> ã衚ãã61ã¯æŽå²ççç±ã«ãããµããŒããããŠããŸãã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 thefloattype.
ããŒãžã§ã³ 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 thefloattype.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
TZspecifies how this is done. It will also set the variablestzname(from theTZenvironment variable),timezone(non-DST seconds West of UTC),altzone(DST seconds west of UTC) anddaylight(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.dm æã®é± 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 usingsettimeofday()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¶
-
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()ã®éãè¡ã颿°ã§ãã
èæ³š