time — Time access and conversions¶
This module provides various time-related functions. For related
functionality, see also the datetime and calendar modules.
Although this module is always available, not all functions are available on all platforms. Most of the functions defined in this module call platform C library functions with the same name. It may sometimes be helpful to consult the platform documentation, because the semantics of these functions varies among platforms.
An explanation of some terminology and conventions is in order.
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.
The term seconds since the epoch refers to the total number of elapsed seconds since the epoch, typically excluding leap seconds. Leap seconds are excluded from this total on all POSIX-compliant platforms.
The functions in this module may not handle dates and times before the epoch or far in the future. The cut-off point in the future is determined by the C library; for 32-bit systems, it is typically in 2038.
Function
strptime()can parse 2-digit years when given%yformat code. When 2-digit years are parsed, they are converted according to the POSIX and ISO C standards: values 69–99 are mapped to 1969–1999, and values 0–68 are mapped to 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 is Daylight Saving Time, an adjustment of the timezone by (usually) one hour during part of the year. DST rules are magic (determined by local law) and can change from year to year. The C library has a table containing the local rules (often it is read from a system file for flexibility) and is the only source of True Wisdom in this respect.
The precision of the various real-time functions may be less than suggested by the units in which their value or argument is expressed. E.g. on most Unix systems, the clock “ticks” only 50 or 100 times a second.
On the other hand, the precision of
time()andsleep()is better than their Unix equivalents: times are expressed as floating-point numbers,time()returns the most accurate time available (using Unixgettimeofday()where available), andsleep()will accept a time with a nonzero fraction (Unixselect()is used to implement this, where available).The time value as returned by
gmtime(),localtime(), andstrptime(), and accepted byasctime(),mktime()andstrftime(), is a sequence of 9 integers. The return values ofgmtime(),localtime(), andstrptime()also offer attribute names for individual fields.See
struct_timefor a description of these objects.Changed in version 3.3: The
struct_timetype was extended to provide thetm_gmtoffandtm_zoneattributes when platform supports correspondingstruct tmmembers.Changed in version 3.6: The
struct_timeattributestm_gmtoffandtm_zoneare now available on all platforms.Use the following functions to convert between time representations:
From
To
Use
seconds since the epoch
struct_timein UTCseconds since the epoch
struct_timein local timestruct_timein UTCseconds since the epoch
struct_timein local timeseconds since the epoch
Functions¶
- 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().Note
Unlike the C function of the same name,
asctime()does not add a trailing newline.
- 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.Warning
Passing an invalid or expired thread_id may result in undefined behavior, such as segmentation fault.
Availability: Unix
See the man page for pthread_getcpuclockid(3) for further information.
Added in version 3.7.
- time.clock_getres(clk_id, /)¶
Return the resolution (precision) of the specified clock clk_id. Refer to Clock ID Constants for a list of accepted values for clk_id.
Availability: Unix.
Added in version 3.3.
- time.clock_gettime(clk_id, /) float¶
Return the time of the specified clock clk_id. Refer to Clock ID Constants for a list of accepted values for clk_id.
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¶
Similar to
clock_gettime()but return time as nanoseconds.Availability: Unix.
Added in version 3.7.
- time.clock_settime(clk_id, time, /)¶
Set the time of the specified clock clk_id. Currently,
CLOCK_REALTIMEis the only accepted value for 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.
Changed in version 3.15: Accepts any real number as time, not only integer or float.
- time.clock_settime_ns(clk_id, time: int, /)¶
Similar to
clock_settime()but set time with nanoseconds.Availability: Unix, not Android, not iOS.
Added in version 3.7.
- time.ctime(seconds=None,