diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/python~3.12/library%2Ftime.html | |
new repository
Diffstat (limited to 'devdocs/python~3.12/library%2Ftime.html')
| -rw-r--r-- | devdocs/python~3.12/library%2Ftime.html | 278 |
1 files changed, 278 insertions, 0 deletions
diff --git a/devdocs/python~3.12/library%2Ftime.html b/devdocs/python~3.12/library%2Ftime.html new file mode 100644 index 00000000..ca3cc0c3 --- /dev/null +++ b/devdocs/python~3.12/library%2Ftime.html @@ -0,0 +1,278 @@ + <span id="time-time-access-and-conversions"></span><h1>time — Time access and conversions</h1> <p>This module provides various time-related functions. For related functionality, see also the <a class="reference internal" href="datetime#module-datetime" title="datetime: Basic date and time types."><code>datetime</code></a> and <a class="reference internal" href="calendar#module-calendar" title="calendar: Functions for working with calendars, including some emulation of the Unix cal program."><code>calendar</code></a> modules.</p> <p>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.</p> <p>An explanation of some terminology and conventions is in order.</p> <span class="target" id="epoch"></span><ul class="simple" id="index-0"> <li>The <em class="dfn">epoch</em> is the point where the time starts, the return value of <code>time.gmtime(0)</code>. It is January 1, 1970, 00:00:00 (UTC) on all platforms.</li> </ul> <ul class="simple" id="index-1"> <li>The term <em class="dfn">seconds since the epoch</em> refers to the total number of elapsed seconds since the epoch, typically excluding <a class="reference external" href="https://en.wikipedia.org/wiki/Leap_second">leap seconds</a>. Leap seconds are excluded from this total on all POSIX-compliant platforms.</li> </ul> <ul class="simple" id="index-2"> <li>The functions in this module may not handle dates and times before the <a class="reference internal" href="#epoch">epoch</a> 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.</li> </ul> <ul class="simple" id="index-3"> <li>Function <a class="reference internal" href="#time.strptime" title="time.strptime"><code>strptime()</code></a> can parse 2-digit years when given <code>%y</code> format 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.</li> </ul> <ul class="simple" id="index-4"> <li>UTC is Coordinated Universal Time (formerly known as Greenwich Mean Time, or GMT). The acronym UTC is not a mistake but a compromise between English and French.</li> </ul> <ul id="index-5"> <li>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.</li> <li>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.</li> <li>On the other hand, the precision of <a class="reference internal" href="#time.time" title="time.time"><code>time()</code></a> and <a class="reference internal" href="#time.sleep" title="time.sleep"><code>sleep()</code></a> is better than their Unix equivalents: times are expressed as floating point numbers, <a class="reference internal" href="#time.time" title="time.time"><code>time()</code></a> returns the most accurate time available (using Unix <code>gettimeofday()</code> where available), and <a class="reference internal" href="#time.sleep" title="time.sleep"><code>sleep()</code></a> will accept a time with a nonzero fraction (Unix <code>select()</code> is used to implement this, where available).</li> <li> +<p>The time value as returned by <a class="reference internal" href="#time.gmtime" title="time.gmtime"><code>gmtime()</code></a>, <a class="reference internal" href="#time.localtime" title="time.localtime"><code>localtime()</code></a>, and <a class="reference internal" href="#time.strptime" title="time.strptime"><code>strptime()</code></a>, and accepted by <a class="reference internal" href="#time.asctime" title="time.asctime"><code>asctime()</code></a>, <a class="reference internal" href="#time.mktime" title="time.mktime"><code>mktime()</code></a> and <a class="reference internal" href="#time.strftime" title="time.strftime"><code>strftime()</code></a>, is a sequence of 9 integers. The return values of <a class="reference internal" href="#time.gmtime" title="time.gmtime"><code>gmtime()</code></a>, <a class="reference internal" href="#time.localtime" title="time.localtime"><code>localtime()</code></a>, and <a class="reference internal" href="#time.strptime" title="time.strptime"><code>strptime()</code></a> also offer attribute names for individual fields.</p> <p>See <a class="reference internal" href="#time.struct_time" title="time.struct_time"><code>struct_time</code></a> for a description of these objects.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span>The <a class="reference internal" href="#time.struct_time" title="time.struct_time"><code>struct_time</code></a> type was extended to provide the <a class="reference internal" href="#time.struct_time.tm_gmtoff" title="time.struct_time.tm_gmtoff"><code>tm_gmtoff</code></a> and <a class="reference internal" href="#time.struct_time.tm_zone" title="time.struct_time.tm_zone"><code>tm_zone</code></a> attributes when platform supports corresponding <code>struct tm</code> members.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>The <a class="reference internal" href="#time.struct_time" title="time.struct_time"><code>struct_time</code></a> attributes <a class="reference internal" href="#time.struct_time.tm_gmtoff" title="time.struct_time.tm_gmtoff"><code>tm_gmtoff</code></a> and <a class="reference internal" href="#time.struct_time.tm_zone" title="time.struct_time.tm_zone"><code>tm_zone</code></a> are now available on all platforms.</p> </div> </li> <li> +<p>Use the following functions to convert between time representations:</p> <table class="docutils align-default"> <thead> <tr> +<th class="head"><p>From</p></th> <th class="head"><p>To</p></th> <th class="head"><p>Use</p></th> </tr> </thead> <tr> +<td><p>seconds since the epoch</p></td> <td><p><a class="reference internal" href="#time.struct_time" title="time.struct_time"><code>struct_time</code></a> in UTC</p></td> <td><p><a class="reference internal" href="#time.gmtime" title="time.gmtime"><code>gmtime()</code></a></p></td> </tr> <tr> +<td><p>seconds since the epoch</p></td> <td><p><a class="reference internal" href="#time.struct_time" title="time.struct_time"><code>struct_time</code></a> in local time</p></td> <td><p><a class="reference internal" href="#time.localtime" title="time.localtime"><code>localtime()</code></a></p></td> </tr> <tr> +<td><p><a class="reference internal" href="#time.struct_time" title="time.struct_time"><code>struct_time</code></a> in UTC</p></td> <td><p>seconds since the epoch</p></td> <td><p><a class="reference internal" href="calendar#calendar.timegm" title="calendar.timegm"><code>calendar.timegm()</code></a></p></td> </tr> <tr> +<td><p><a class="reference internal" href="#time.struct_time" title="time.struct_time"><code>struct_time</code></a> in local time</p></td> <td><p>seconds since the epoch</p></td> <td><p><a class="reference internal" href="#time.mktime" title="time.mktime"><code>mktime()</code></a></p></td> </tr> </table> </li> </ul> <section id="functions"> <span id="time-functions"></span><h2>Functions</h2> <dl class="py function"> <dt class="sig sig-object py" id="time.asctime"> +<code>time.asctime([t])</code> </dt> <dd> +<p>Convert a tuple or <a class="reference internal" href="#time.struct_time" title="time.struct_time"><code>struct_time</code></a> representing a time as returned by <a class="reference internal" href="#time.gmtime" title="time.gmtime"><code>gmtime()</code></a> or <a class="reference internal" href="#time.localtime" title="time.localtime"><code>localtime()</code></a> to a string of the following form: <code>'Sun Jun 20 23:21:05 1993'</code>. The day field is two characters long and is space padded if the day is a single digit, e.g.: <code>'Wed Jun 9 04:26:40 1993'</code>.</p> <p>If <em>t</em> is not provided, the current time as returned by <a class="reference internal" href="#time.localtime" title="time.localtime"><code>localtime()</code></a> is used. Locale information is not used by <a class="reference internal" href="#time.asctime" title="time.asctime"><code>asctime()</code></a>.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>Unlike the C function of the same name, <a class="reference internal" href="#time.asctime" title="time.asctime"><code>asctime()</code></a> does not add a trailing newline.</p> </div> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="time.pthread_getcpuclockid"> +<code>time.pthread_getcpuclockid(thread_id)</code> </dt> <dd> +<p>Return the <em>clk_id</em> of the thread-specific CPU-time clock for the specified <em>thread_id</em>.</p> <p>Use <a class="reference internal" href="threading#threading.get_ident" title="threading.get_ident"><code>threading.get_ident()</code></a> or the <a class="reference internal" href="threading#threading.Thread.ident" title="threading.Thread.ident"><code>ident</code></a> attribute of <a class="reference internal" href="threading#threading.Thread" title="threading.Thread"><code>threading.Thread</code></a> objects to get a suitable value for <em>thread_id</em>.</p> <div class="admonition warning"> <p class="admonition-title">Warning</p> <p>Passing an invalid or expired <em>thread_id</em> may result in undefined behavior, such as segmentation fault.</p> </div> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix</p> <p>See the man page for <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/pthread_getcpuclockid(3)">pthread_getcpuclockid(3)</a></em> for further information.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="time.clock_getres"> +<code>time.clock_getres(clk_id)</code> </dt> <dd> +<p>Return the resolution (precision) of the specified clock <em>clk_id</em>. Refer to <a class="reference internal" href="#time-clock-id-constants"><span class="std std-ref">Clock ID Constants</span></a> for a list of accepted values for <em>clk_id</em>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="time.clock_gettime"> +<code>time.clock_gettime(clk_id) → float</code> </dt> <dd> +<p>Return the time of the specified clock <em>clk_id</em>. Refer to <a class="reference internal" href="#time-clock-id-constants"><span class="std std-ref">Clock ID Constants</span></a> for a list of accepted values for <em>clk_id</em>.</p> <p>Use <a class="reference internal" href="#time.clock_gettime_ns" title="time.clock_gettime_ns"><code>clock_gettime_ns()</code></a> to avoid the precision loss caused by the <a class="reference internal" href="functions#float" title="float"><code>float</code></a> type.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="time.clock_gettime_ns"> +<code>time.clock_gettime_ns(clk_id) → int</code> </dt> <dd> +<p>Similar to <a class="reference internal" href="#time.clock_gettime" title="time.clock_gettime"><code>clock_gettime()</code></a> but return time as nanoseconds.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="time.clock_settime"> +<code>time.clock_settime(clk_id, time: float)</code> </dt> <dd> +<p>Set the time of the specified clock <em>clk_id</em>. Currently, <a class="reference internal" href="#time.CLOCK_REALTIME" title="time.CLOCK_REALTIME"><code>CLOCK_REALTIME</code></a> is the only accepted value for <em>clk_id</em>.</p> <p>Use <a class="reference internal" href="#time.clock_settime_ns" title="time.clock_settime_ns"><code>clock_settime_ns()</code></a> to avoid the precision loss caused by the <a class="reference internal" href="functions#float" title="float"><code>float</code></a> type.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="time.clock_settime_ns"> +<code>time.clock_settime_ns(clk_id, time: int)</code> </dt> <dd> +<p>Similar to <a class="reference internal" href="#time.clock_settime" title="time.clock_settime"><code>clock_settime()</code></a> but set time with nanoseconds.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="time.ctime"> +<code>time.ctime([secs])</code> </dt> <dd> +<p>Convert a time expressed in seconds since the <a class="reference internal" href="#epoch">epoch</a> to a string of a form: <code>'Sun Jun 20 23:21:05 1993'</code> representing local time. The day field is two characters long and is space padded if the day is a single digit, e.g.: <code>'Wed Jun 9 04:26:40 1993'</code>.</p> <p>If <em>secs</em> is not provided or <a class="reference internal" href="constants#None" title="None"><code>None</code></a>, the current time as returned by <a class="reference internal" href="#time.time" title="time.time"><code>time()</code></a> is used. <code>ctime(secs)</code> is equivalent to <code>asctime(localtime(secs))</code>. Locale information is not used by <a class="reference internal" href="#time.ctime" title="time.ctime"><code>ctime()</code></a>.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="time.get_clock_info"> +<code>time.get_clock_info(name)</code> </dt> <dd> +<p>Get information on the specified clock as a namespace object. Supported clock names and the corresponding functions to read their value are:</p> <ul class="simple"> <li> +<code>'monotonic'</code>: <a class="reference internal" href="#time.monotonic" title="time.monotonic"><code>time.monotonic()</code></a> +</li> <li> +<code>'perf_counter'</code>: <a class="reference internal" href="#time.perf_counter" title="time.perf_counter"><code>time.perf_counter()</code></a> +</li> <li> +<code>'process_time'</code>: <a class="reference internal" href="#time.process_time" title="time.process_time"><code>time.process_time()</code></a> +</li> <li> +<code>'thread_time'</code>: <a class="reference internal" href="#time.thread_time" title="time.thread_time"><code>time.thread_time()</code></a> +</li> <li> +<code>'time'</code>: <a class="reference internal" href="#time.time" title="time.time"><code>time.time()</code></a> +</li> </ul> <p>The result has the following attributes:</p> <ul class="simple"> <li> +<em>adjustable</em>: <code>True</code> if the clock can be changed automatically (e.g. by a NTP daemon) or manually by the system administrator, <code>False</code> otherwise</li> <li> +<em>implementation</em>: The name of the underlying C function used to get the clock value. Refer to <a class="reference internal" href="#time-clock-id-constants"><span class="std std-ref">Clock ID Constants</span></a> for possible values.</li> <li> +<em>monotonic</em>: <code>True</code> if the clock cannot go backward, <code>False</code> otherwise</li> <li> +<em>resolution</em>: The resolution of the clock in seconds (<a class="reference internal" href="functions#float" title="float"><code>float</code></a>)</li> </ul> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="time.gmtime"> +<code>time.gmtime([secs])</code> </dt> <dd> +<p>Convert a time expressed in seconds since the <a class="reference internal" href="#epoch">epoch</a> to a <a class="reference internal" href="#time.struct_time" title="time.struct_time"><code>struct_time</code></a> in UTC in which the dst flag is always zero. If <em>secs</em> is not provided or <a class="reference internal" href="constants#None" title="None"><code>None</code></a>, the current time as returned by <a class="reference internal" href="#time.time" title="time.time"><code>time()</code></a> is used. Fractions of a second are ignored. See above for a description of the <a class="reference internal" href="#time.struct_time" title="time.struct_time"><code>struct_time</code></a> object. See <a class="reference internal" href="calendar#calendar.timegm" title="calendar.timegm"><code>calendar.timegm()</code></a> for the inverse of this function.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="time.localtime"> +<code>time.localtime([secs])</code> </dt> <dd> +<p>Like <a class="reference internal" href="#time.gmtime" title="time.gmtime"><code>gmtime()</code></a> but converts to local time. If <em>secs</em> is not provided or <a class="reference internal" href="constants#None" title="None"><code>None</code></a>, the current time as returned by <a class="reference internal" href="#time.time" title="time.time"><code>time()</code></a> is used. The dst flag is set to <code>1</code> when DST applies to the given time.</p> <p><a class="reference internal" href="#time.localtime" title="time.localtime"><code>localtime()</code></a> may raise <a class="reference internal" href="exceptions#OverflowError" title="OverflowError"><code>OverflowError</code></a>, if the timestamp is outside the range of values supported by the platform C <code>localtime()</code> or <code>gmtime()</code> functions, and <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> on <code>localtime()</code> or <code>gmtime()</code> failure. It’s common for this to be restricted to years between 1970 and 2038.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="time.mktime"> +<code>time.mktime(t)</code> </dt> <dd> +<p>This is the inverse function of <a class="reference internal" href="#time.localtime" title="time.localtime"><code>localtime()</code></a>. Its argument is the <a class="reference internal" href="#time.struct_time" title="time.struct_time"><code>struct_time</code></a> or full 9-tuple (since the dst flag is needed; use <code>-1</code> as the dst flag if it is unknown) which expresses the time in <em>local</em> time, not UTC. It returns a floating point number, for compatibility with <a class="reference internal" href="#time.time" title="time.time"><code>time()</code></a>. If the input value cannot be represented as a valid time, either <a class="reference internal" href="exceptions#OverflowError" title="OverflowError"><code>OverflowError</code></a> or <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a> will be raised (which depends on whether the invalid value is caught by Python or the underlying C libraries). The earliest date for which it can generate a time is platform-dependent.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="time.monotonic"> +<code>time.monotonic() → float</code> </dt> <dd> +<p>Return the value (in fractional seconds) of a monotonic clock, i.e. a clock that cannot go backwards. The clock is not affected by system clock updates. The reference point of the returned value is undefined, so that only the difference between the results of two calls is valid.</p> <p>Use <a class="reference internal" href="#time.monotonic_ns" title="time.monotonic_ns"><code>monotonic_ns()</code></a> to avoid the precision loss caused by the <a class="reference internal" href="functions#float" title="float"><code>float</code></a> type.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.5: </span>The function is now always available and always system-wide.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.10: </span>On macOS, the function is now system-wide.</p> </div> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="time.monotonic_ns"> +<code>time.monotonic_ns() → int</code> </dt> <dd> +<p>Similar to <a class="reference internal" href="#time.monotonic" title="time.monotonic"><code>monotonic()</code></a>, but return time as nanoseconds.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="time.perf_counter"> +<code>time.perf_counter() → float</code> </dt> <dd> +<p id="index-6">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 and is system-wide. The reference point of the returned value is undefined, so that only the difference between the results of two calls is valid.</p> <p>Use <a class="reference internal" href="#time.perf_counter_ns" title="time.perf_counter_ns"><code>perf_counter_ns()</code></a> to avoid the precision loss caused by the <a class="reference internal" href="functions#float" title="float"><code>float</code></a> type.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.10: </span>On Windows, the function is now system-wide.</p> </div> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="time.perf_counter_ns"> +<code>time.perf_counter_ns() → int</code> </dt> <dd> +<p>Similar to <a class="reference internal" href="#time.perf_counter" title="time.perf_counter"><code>perf_counter()</code></a>, but return time as nanoseconds.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="time.process_time"> +<code>time.process_time() → float</code> </dt> <dd> +<p id="index-7">Return the value (in fractional seconds) of the sum of the system and user CPU time of the current process. It does not include time elapsed during sleep. It is process-wide by definition. The reference point of the returned value is undefined, so that only the difference between the results of two calls is valid.</p> <p>Use <a class="reference internal" href="#time.process_time_ns" title="time.process_time_ns"><code>process_time_ns()</code></a> to avoid the precision loss caused by the <a class="reference internal" href="functions#float" title="float"><code>float</code></a> type.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="time.process_time_ns"> +<code>time.process_time_ns() → int</code> </dt> <dd> +<p>Similar to <a class="reference internal" href="#time.process_time" title="time.process_time"><code>process_time()</code></a> but return time as nanoseconds.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="time.sleep"> +<code>time.sleep(secs)</code> </dt> <dd> +<p>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.</p> <p>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.</p> <p>The suspension time may be longer than requested by an arbitrary amount, because of the scheduling of other activity in the system.</p> <p>On Windows, if <em>secs</em> 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 8.1 and newer the implementation uses a <a class="reference external" href="https://docs.microsoft.com/en-us/windows-hardware/drivers/kernel/high-resolution-timers">high-resolution timer</a> which provides resolution of 100 nanoseconds. If <em>secs</em> is zero, <code>Sleep(0)</code> is used.</p> <p>Unix implementation:</p> <ul class="simple"> <li>Use <code>clock_nanosleep()</code> if available (resolution: 1 nanosecond);</li> <li>Or use <code>nanosleep()</code> if available (resolution: 1 nanosecond);</li> <li>Or use <code>select()</code> (resolution: 1 microsecond).</li> </ul> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.11: </span>On Unix, the <code>clock_nanosleep()</code> and <code>nanosleep()</code> functions are now used if available. On Windows, a waitable timer is now used.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.5: </span>The function now sleeps at least <em>secs</em> even if the sleep is interrupted by a signal, except if the signal handler raises an exception (see <span class="target" id="index-8"></span><a class="pep reference external" href="https://peps.python.org/pep-0475/"><strong>PEP 475</strong></a> for the rationale).</p> </div> </dd> +</dl> <span class="target" id="index-9"></span><dl class="py function"> <dt class="sig sig-object py" id="time.strftime"> +<code>time.strftime(format[, t])</code> </dt> <dd> +<p>Convert a tuple or <a class="reference internal" href="#time.struct_time" title="time.struct_time"><code>struct_time</code></a> representing a time as returned by <a class="reference internal" href="#time.gmtime" title="time.gmtime"><code>gmtime()</code></a> or <a class="reference internal" href="#time.localtime" title="time.localtime"><code>localtime()</code></a> to a string as specified by the <em>format</em> argument. If <em>t</em> is not provided, the current time as returned by <a class="reference internal" href="#time.localtime" title="time.localtime"><code>localtime()</code></a> is used. <em>format</em> must be a string. <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a> is raised if any field in <em>t</em> is outside of the allowed range.</p> <p>0 is a legal argument for any position in the time tuple; if it is normally illegal the value is forced to a correct one.</p> <p>The following directives can be embedded in the <em>format</em> string. They are shown without the optional field width and precision specification, and are replaced by the indicated characters in the <a class="reference internal" href="#time.strftime" title="time.strftime"><code>strftime()</code></a> result:</p> <table class="docutils align-default"> <thead> <tr> +<th class="head"><p>Directive</p></th> <th class="head"><p>Meaning</p></th> <th class="head"><p>Notes</p></th> </tr> </thead> <tr> +<td><p><code>%a</code></p></td> <td><p>Locale’s abbreviated weekday name.</p></td> <td></td> </tr> <tr> +<td><p><code>%A</code></p></td> <td><p>Locale’s full weekday name.</p></td> <td></td> </tr> <tr> +<td><p><code>%b</code></p></td> <td><p>Locale’s abbreviated month name.</p></td> <td></td> </tr> <tr> +<td><p><code>%B</code></p></td> <td><p>Locale’s full month name.</p></td> <td></td> </tr> <tr> +<td><p><code>%c</code></p></td> <td><p>Locale’s appropriate date and time representation.</p></td> <td></td> </tr> <tr> +<td><p><code>%d</code></p></td> <td><p>Day of the month as a decimal number [01,31].</p></td> <td></td> </tr> <tr> +<td><p><code>%H</code></p></td> <td><p>Hour (24-hour clock) as a decimal number [00,23].</p></td> <td></td> </tr> <tr> +<td><p><code>%I</code></p></td> <td><p>Hour (12-hour clock) as a decimal number [01,12].</p></td> <td></td> </tr> <tr> +<td><p><code>%j</code></p></td> <td><p>Day of the year as a decimal number [001,366].</p></td> <td></td> </tr> <tr> +<td><p><code>%m</code></p></td> <td><p>Month as a decimal number [01,12].</p></td> <td></td> </tr> <tr> +<td><p><code>%M</code></p></td> <td><p>Minute as a decimal number [00,59].</p></td> <td></td> </tr> <tr> +<td><p><code>%p</code></p></td> <td><p>Locale’s equivalent of either AM or PM.</p></td> <td><p>(1)</p></td> </tr> <tr> +<td><p><code>%S</code></p></td> <td><p>Second as a decimal number [00,61].</p></td> <td><p>(2)</p></td> </tr> <tr> +<td><p><code>%U</code></p></td> <td><p>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.</p></td> <td><p>(3)</p></td> </tr> <tr> +<td><p><code>%w</code></p></td> <td><p>Weekday as a decimal number [0(Sunday),6].</p></td> <td></td> </tr> <tr> +<td><p><code>%W</code></p></td> <td><p>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.</p></td> <td><p>(3)</p></td> </tr> <tr> +<td><p><code>%x</code></p></td> <td><p>Locale’s appropriate date representation.</p></td> <td></td> </tr> <tr> +<td><p><code>%X</code></p></td> <td><p>Locale’s appropriate time representation.</p></td> <td></td> </tr> <tr> +<td><p><code>%y</code></p></td> <td><p>Year without century as a decimal number [00,99].</p></td> <td></td> </tr> <tr> +<td><p><code>%Y</code></p></td> <td><p>Year with century as a decimal number.</p></td> <td></td> </tr> <tr> +<td><p><code>%z</code></p></td> <td><p>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]. <a class="footnote-reference brackets" href="#id4" id="id1">1</a></p></td> <td></td> </tr> <tr> +<td><p><code>%Z</code></p></td> <td><p>Time zone name (no characters if no time zone exists). Deprecated. <a class="footnote-reference brackets" href="#id4" id="id2">1</a></p></td> <td></td> </tr> <tr> +<td><p><code>%%</code></p></td> <td><p>A literal <code>'%'</code> character.</p></td> <td></td> </tr> </table> <p>Notes:</p> <ol class="arabic simple"> <li>When used with the <a class="reference internal" href="#time.strptime" title="time.strptime"><code>strptime()</code></a> function, the <code>%p</code> directive only affects the output hour field if the <code>%I</code> directive is used to parse the hour.</li> </ol> <ol class="arabic simple" id="leap-second" start="2"> <li>The range really is <code>0</code> to <code>61</code>; value <code>60</code> is valid in timestamps representing <a class="reference external" href="https://en.wikipedia.org/wiki/Leap_second">leap seconds</a> and value <code>61</code> is supported for historical reasons.</li> <li>When used with the <a class="reference internal" href="#time.strptime" title="time.strptime"><code>strptime()</code></a> function, <code>%U</code> and <code>%W</code> are only used in calculations when the day of the week and the year are specified.</li> </ol> <p>Here is an example, a format for dates compatible with that specified in the <span class="target" id="index-10"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2822.html"><strong>RFC 2822</strong></a> Internet email standard. <a class="footnote-reference brackets" href="#id4" id="id3">1</a></p> <pre data-language="python">>>> from time import gmtime, strftime +>>> strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime()) +'Thu, 28 Jun 2001 14:17:15 +0000' +</pre> <p>Additional directives may be supported on certain platforms, but only the ones listed here have a meaning standardized by ANSI C. To see the full set of format codes supported on your platform, consult the <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/strftime(3)">strftime(3)</a></em> documentation.</p> <p>On some platforms, an optional field width and precision specification can immediately follow the initial <code>'%'</code> of a directive in the following order; this is also not portable. The field width is normally 2 except for <code>%j</code> where it is 3.</p> </dd> +</dl> <span class="target" id="index-11"></span><dl class="py function"> <dt class="sig sig-object py" id="time.strptime"> +<code>time.strptime(string[, format])</code> </dt> <dd> +<p>Parse a string representing a time according to a format. The return value is a <a class="reference internal" href="#time.struct_time" title="time.struct_time"><code>struct_time</code></a> as returned by <a class="reference internal" href="#time.gmtime" title="time.gmtime"><code>gmtime()</code></a> or <a class="reference internal" href="#time.localtime" title="time.localtime"><code>localtime()</code></a>.</p> <p>The <em>format</em> parameter uses the same directives as those used by <a class="reference internal" href="#time.strftime" title="time.strftime"><code>strftime()</code></a>; it defaults to <code>"%a %b %d %H:%M:%S %Y"</code> which matches the formatting returned by <a class="reference internal" href="#time.ctime" title="time.ctime"><code>ctime()</code></a>. If <em>string</em> cannot be parsed according to <em>format</em>, or if it has excess data after parsing, <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a> is raised. The default values used to fill in any missing data when more accurate values cannot be inferred are <code>(1900, 1, 1, 0, 0, 0, 0, 1, -1)</code>. Both <em>string</em> and <em>format</em> must be strings.</p> <p>For example:</p> <pre data-language="python">>>> 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) +</pre> <p>Support for the <code>%Z</code> directive is based on the values contained in <code>tzname</code> and whether <code>daylight</code> is true. Because of this, it is platform-specific except for recognizing UTC and GMT which are always known (and are considered to be non-daylight savings timezones).</p> <p>Only the directives specified in the documentation are supported. Because <code>strftime()</code> is implemented per platform it can sometimes offer more directives than those listed. But <code>strptime()</code> is independent of any platform and thus does not necessarily support all directives available that are not documented as supported.</p> </dd> +</dl> <dl class="py class"> <dt class="sig sig-object py" id="time.struct_time"> +<code>class time.struct_time</code> </dt> <dd> +<p>The type of the time value sequence returned by <a class="reference internal" href="#time.gmtime" title="time.gmtime"><code>gmtime()</code></a>, <a class="reference internal" href="#time.localtime" title="time.localtime"><code>localtime()</code></a>, and <a class="reference internal" href="#time.strptime" title="time.strptime"><code>strptime()</code></a>. It is an object with a <a class="reference internal" href="../glossary#term-named-tuple"><span class="xref std std-term">named tuple</span></a> interface: values can be accessed by index and by attribute name. The following values are present:</p> <table class="docutils align-default"> <tr> +<td><p>Index</p></td> <td><p>Attribute</p></td> <td><p>Values</p></td> </tr> <tr> +<td><p>0</p></td> <td> +<dl class="py attribute"> <dt class="sig sig-object py" id="time.struct_time.tm_year"> +<code>tm_year</code> </dt> <dd></dd> +</dl> </td> <td><p>(for example, 1993)</p></td> </tr> <tr> +<td><p>1</p></td> <td> +<dl class="py attribute"> <dt class="sig sig-object py" id="time.struct_time.tm_mon"> +<code>tm_mon</code> </dt> <dd></dd> +</dl> </td> <td><p>range [1, 12]</p></td> </tr> <tr> +<td><p>2</p></td> <td> +<dl class="py attribute"> <dt class="sig sig-object py" id="time.struct_time.tm_day"> +<code>tm_day</code> </dt> <dd></dd> +</dl> </td> <td><p>range [1, 31]</p></td> </tr> <tr> +<td><p>3</p></td> <td> +<dl class="py attribute"> <dt class="sig sig-object py" id="time.struct_time.tm_hour"> +<code>tm_hour</code> </dt> <dd></dd> +</dl> </td> <td><p>range [0, 23]</p></td> </tr> <tr> +<td><p>4</p></td> <td> +<dl class="py attribute"> <dt class="sig sig-object py" id="time.struct_time.tm_min"> +<code>tm_min</code> </dt> <dd></dd> +</dl> </td> <td><p>range [0, 59]</p></td> </tr> <tr> +<td><p>5</p></td> <td> +<dl class="py attribute"> <dt class="sig sig-object py" id="time.struct_time.tm_sec"> +<code>tm_sec</code> </dt> <dd></dd> +</dl> </td> <td><p>range [0, 61]; see <a class="reference internal" href="#leap-second"><span class="std std-ref">Note (2)</span></a> in <a class="reference internal" href="#time.strftime" title="time.strftime"><code>strftime()</code></a></p></td> </tr> <tr> +<td><p>6</p></td> <td> +<dl class="py attribute"> <dt class="sig sig-object py" id="time.struct_time.tm_wday"> +<code>tm_wday</code> </dt> <dd></dd> +</dl> </td> <td><p>range [0, 6]; Monday is 0</p></td> </tr> <tr> +<td><p>7</p></td> <td> +<dl class="py attribute"> <dt class="sig sig-object py" id="time.struct_time.tm_yday"> +<code>tm_yday</code> </dt> <dd></dd> +</dl> </td> <td><p>range [1, 366]</p></td> </tr> <tr> +<td><p>8</p></td> <td> +<dl class="py attribute"> <dt class="sig sig-object py" id="time.struct_time.tm_isdst"> +<code>tm_isdst</code> </dt> <dd></dd> +</dl> </td> <td><p>0, 1 or -1; see below</p></td> </tr> <tr> +<td><p>N/A</p></td> <td> +<dl class="py attribute"> <dt class="sig sig-object py" id="time.struct_time.tm_zone"> +<code>tm_zone</code> </dt> <dd></dd> +</dl> </td> <td><p>abbreviation of timezone name</p></td> </tr> <tr> +<td><p>N/A</p></td> <td> +<dl class="py attribute"> <dt class="sig sig-object py" id="time.struct_time.tm_gmtoff"> +<code>tm_gmtoff</code> </dt> <dd></dd> +</dl> </td> <td><p>offset east of UTC in seconds</p></td> </tr> </table> <p>Note that unlike the C structure, the month value is a range of [1, 12], not [0, 11].</p> <p>In calls to <a class="reference internal" href="#time.mktime" title="time.mktime"><code>mktime()</code></a>, <a class="reference internal" href="#time.struct_time.tm_isdst" title="time.struct_time.tm_isdst"><code>tm_isdst</code></a> may be set to 1 when daylight savings time is in effect, and 0 when it is not. A value of -1 indicates that this is not known, and will usually result in the correct state being filled in.</p> <p>When a tuple with an incorrect length is passed to a function expecting a <a class="reference internal" href="#time.struct_time" title="time.struct_time"><code>struct_time</code></a>, or having elements of the wrong type, a <a class="reference internal" href="exceptions#TypeError" title="TypeError"><code>TypeError</code></a> is raised.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="time.time"> +<code>time.time() → float</code> </dt> <dd> +<p>Return the time in seconds since the <a class="reference internal" href="#epoch">epoch</a> as a floating point number. The handling of <a class="reference external" href="https://en.wikipedia.org/wiki/Leap_second">leap seconds</a> is platform dependent. On Windows and most Unix systems, the leap seconds are not counted towards the time in seconds since the <a class="reference internal" href="#epoch">epoch</a>. This is commonly referred to as <a class="reference external" href="https://en.wikipedia.org/wiki/Unix_time">Unix time</a>.</p> <p>Note that even though the time is always returned as a floating point number, not all systems provide time with a better precision than 1 second. While this function normally returns non-decreasing values, it can return a lower value than a previous call if the system clock has been set back between the two calls.</p> <p>The number returned by <a class="reference internal" href="#time.time" title="time.time"><code>time()</code></a> may be converted into a more common time format (i.e. year, month, day, hour, etc…) in UTC by passing it to <a class="reference internal" href="#time.gmtime" title="time.gmtime"><code>gmtime()</code></a> function or in local time by passing it to the <a class="reference internal" href="#time.localtime" title="time.localtime"><code>localtime()</code></a> function. In both cases a <a class="reference internal" href="#time.struct_time" title="time.struct_time"><code>struct_time</code></a> object is returned, from which the components of the calendar date may be accessed as attributes.</p> <p>Use <a class="reference internal" href="#time.time_ns" title="time.time_ns"><code>time_ns()</code></a> to avoid the precision loss caused by the <a class="reference internal" href="functions#float" title="float"><code>float</code></a> type.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="time.time_ns"> +<code>time.time_ns() → int</code> </dt> <dd> +<p>Similar to <a class="reference internal" href="#time.time" title="time.time"><code>time()</code></a> but returns time as an integer number of nanoseconds since the <a class="reference internal" href="#epoch">epoch</a>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="time.thread_time"> +<code>time.thread_time() → float</code> </dt> <dd> +<p id="index-12">Return the value (in fractional seconds) of the sum of the system and user CPU time of the current thread. It does not include time elapsed during sleep. It is thread-specific by definition. The reference point of the returned value is undefined, so that only the difference between the results of two calls in the same thread is valid.</p> <p>Use <a class="reference internal" href="#time.thread_time_ns" title="time.thread_time_ns"><code>thread_time_ns()</code></a> to avoid the precision loss caused by the <a class="reference internal" href="functions#float" title="float"><code>float</code></a> type.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux, Unix, Windows.</p> <p>Unix systems supporting <code>CLOCK_THREAD_CPUTIME_ID</code>.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="time.thread_time_ns"> +<code>time.thread_time_ns() → int</code> </dt> <dd> +<p>Similar to <a class="reference internal" href="#time.thread_time" title="time.thread_time"><code>thread_time()</code></a> but return time as nanoseconds.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="time.tzset"> +<code>time.tzset()</code> </dt> <dd> +<p>Reset the time conversion rules used by the library routines. The environment variable <span class="target" id="index-13"></span><code>TZ</code> specifies how this is done. It will also set the variables <code>tzname</code> (from the <span class="target" id="index-14"></span><code>TZ</code> environment variable), <code>timezone</code> (non-DST seconds West of UTC), <code>altzone</code> (DST seconds west of UTC) and <code>daylight</code> (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).</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> <div class="admonition note"> <p class="admonition-title">Note</p> <p>Although in many cases, changing the <span class="target" id="index-15"></span><code>TZ</code> environment variable may affect the output of functions like <a class="reference internal" href="#time.localtime" title="time.localtime"><code>localtime()</code></a> without calling <a class="reference internal" href="#time.tzset" title="time.tzset"><code>tzset()</code></a>, this behavior should not be relied on.</p> <p>The <span class="target" id="index-16"></span><code>TZ</code> environment variable should contain no whitespace.</p> </div> <p>The standard format of the <span class="target" id="index-17"></span><code>TZ</code> environment variable is (whitespace added for clarity):</p> <pre data-language="python">std offset [dst [offset [,start[/time], end[/time]]]] +</pre> <p>Where the components are:</p> <dl> <dt> +<code>std and dst</code> </dt> +<dd> +<p>Three or more alphanumerics giving the timezone abbreviations. These will be propagated into time.tzname</p> </dd> <dt> +<code>offset</code> </dt> +<dd> +<p>The offset has the form: <code>± hh[:mm[:ss]]</code>. This indicates the value added the local time to arrive at UTC. If preceded by a ‘-’, the timezone is east of the Prime Meridian; otherwise, it is west. If no offset follows dst, summer time is assumed to be one hour ahead of standard time.</p> </dd> <dt> +<code>start[/time], end[/time]</code> </dt> +<dd> +<p>Indicates when to change to and back from DST. The format of the start and end dates are one of the following:</p> <dl class="simple"> <dt> +<code>Jn</code> </dt> +<dd> +<p>The Julian day <em>n</em> (1 <= <em>n</em> <= 365). Leap days are not counted, so in all years February 28 is day 59 and March 1 is day 60.</p> </dd> <dt> +<code>n</code> </dt> +<dd> +<p>The zero-based Julian day (0 <= <em>n</em> <= 365). Leap days are counted, and it is possible to refer to February 29.</p> </dd> <dt> +<code>Mm.n.d</code> </dt> +<dd> +<p>The <em>d</em>’th day (0 <= <em>d</em> <= 6) of week <em>n</em> of month <em>m</em> of the year (1 <= <em>n</em> <= 5, 1 <= <em>m</em> <= 12, where week 5 means “the last <em>d</em> day in month <em>m</em>” which may occur in either the fourth or the fifth week). Week 1 is the first week in which the <em>d</em>’th day occurs. Day zero is a Sunday.</p> </dd> </dl> <p><code>time</code> has the same format as <code>offset</code> except that no leading sign (‘-’ or ‘+’) is allowed. The default, if time is not given, is 02:00:00.</p> </dd> </dl> <pre data-language="python">>>> 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' +</pre> <p>On many Unix systems (including *BSD, Linux, Solaris, and Darwin), it is more convenient to use the system’s zoneinfo (<em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/tzfile(5)">tzfile(5)</a></em>) database to specify the timezone rules. To do this, set the <span class="target" id="index-18"></span><code>TZ</code> environment variable to the path of the required timezone datafile, relative to the root of the systems ‘zoneinfo’ timezone database, usually located at <code>/usr/share/zoneinfo</code>. For example, <code>'US/Eastern'</code>, <code>'Australia/Melbourne'</code>, <code>'Egypt'</code> or <code>'Europe/Amsterdam'</code>.</p> <pre data-language="python">>>> os.environ['TZ'] = 'US/Eastern' +>>> time.tzset() +>>> time.tzname +('EST', 'EDT') +>>> os.environ['TZ'] = 'Egypt' +>>> time.tzset() +>>> time.tzname +('EET', 'EEST') +</pre> </dd> +</dl> </section> <section id="clock-id-constants"> <span id="time-clock-id-constants"></span><h2>Clock ID Constants</h2> <p>These constants are used as parameters for <a class="reference internal" href="#time.clock_getres" title="time.clock_getres"><code>clock_getres()</code></a> and <a class="reference internal" href="#time.clock_gettime" title="time.clock_gettime"><code>clock_gettime()</code></a>.</p> <dl class="py data"> <dt class="sig sig-object py" id="time.CLOCK_BOOTTIME"> +<code>time.CLOCK_BOOTTIME</code> </dt> <dd> +<p>Identical to <a class="reference internal" href="#time.CLOCK_MONOTONIC" title="time.CLOCK_MONOTONIC"><code>CLOCK_MONOTONIC</code></a>, except it also includes any time that the system is suspended.</p> <p>This allows applications to get a suspend-aware monotonic clock without having to deal with the complications of <a class="reference internal" href="#time.CLOCK_REALTIME" title="time.CLOCK_REALTIME"><code>CLOCK_REALTIME</code></a>, which may have discontinuities if the time is changed using <code>settimeofday()</code> or similar.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux >= 2.6.39.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd> +</dl> <dl class="py data"> <dt class="sig sig-object py" id="time.CLOCK_HIGHRES"> +<code>time.CLOCK_HIGHRES</code> </dt> <dd> +<p>The Solaris OS has a <code>CLOCK_HIGHRES</code> timer that attempts to use an optimal hardware source, and may give close to nanosecond resolution. <code>CLOCK_HIGHRES</code> is the nonadjustable, high-resolution clock.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Solaris.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd> +</dl> <dl class="py data"> <dt class="sig sig-object py" id="time.CLOCK_MONOTONIC"> +<code>time.CLOCK_MONOTONIC</code> </dt> <dd> +<p>Clock that cannot be set and represents monotonic time since some unspecified starting point.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd> +</dl> <dl class="py data"> <dt class="sig sig-object py" id="time.CLOCK_MONOTONIC_RAW"> +<code>time.CLOCK_MONOTONIC_RAW</code> </dt> <dd> +<p>Similar to <a class="reference internal" href="#time.CLOCK_MONOTONIC" title="time.CLOCK_MONOTONIC"><code>CLOCK_MONOTONIC</code></a>, but provides access to a raw hardware-based time that is not subject to NTP adjustments.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux >= 2.6.28, macOS >= 10.12.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd> +</dl> <dl class="py data"> <dt class="sig sig-object py" id="time.CLOCK_PROCESS_CPUTIME_ID"> +<code>time.CLOCK_PROCESS_CPUTIME_ID</code> </dt> <dd> +<p>High-resolution per-process timer from the CPU.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd> +</dl> <dl class="py data"> <dt class="sig sig-object py" id="time.CLOCK_PROF"> +<code>time.CLOCK_PROF</code> </dt> <dd> +<p>High-resolution per-process timer from the CPU.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: FreeBSD, NetBSD >= 7, OpenBSD.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd> +</dl> <dl class="py data"> <dt class="sig sig-object py" id="time.CLOCK_TAI"> +<code>time.CLOCK_TAI</code> </dt> <dd> +<p><a class="reference external" href="https://www.nist.gov/pml/time-and-frequency-division/nist-time-frequently-asked-questions-faq#tai">International Atomic Time</a></p> <p>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.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.9.</span></p> </div> </dd> +</dl> <dl class="py data"> <dt class="sig sig-object py" id="time.CLOCK_THREAD_CPUTIME_ID"> +<code>time.CLOCK_THREAD_CPUTIME_ID</code> </dt> <dd> +<p>Thread-specific CPU-time clock.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd> +</dl> <dl class="py data"> <dt class="sig sig-object py" id="time.CLOCK_UPTIME"> +<code>time.CLOCK_UPTIME</code> </dt> <dd> +<p>Time whose absolute value is the time the system has been running and not suspended, providing accurate uptime measurement, both absolute and interval.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: FreeBSD, OpenBSD >= 5.5.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd> +</dl> <dl class="py data"> <dt class="sig sig-object py" id="time.CLOCK_UPTIME_RAW"> +<code>time.CLOCK_UPTIME_RAW</code> </dt> <dd> +<p>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.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: macOS >= 10.12.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.8.</span></p> </div> </dd> +</dl> <p>The following constant is the only parameter that can be sent to <a class="reference internal" href="#time.clock_settime" title="time.clock_settime"><code>clock_settime()</code></a>.</p> <dl class="py data"> <dt class="sig sig-object py" id="time.CLOCK_REALTIME"> +<code>time.CLOCK_REALTIME</code> </dt> <dd> +<p>System-wide real-time clock. Setting this clock requires appropriate privileges.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd> +</dl> </section> <section id="timezone-constants"> <span id="time-timezone-constants"></span><h2>Timezone Constants</h2> <dl class="py data"> <dt class="sig sig-object py" id="time.altzone"> +<code>time.altzone</code> </dt> <dd> +<p>The offset of the local DST timezone, in seconds west of UTC, if one is defined. This is negative if the local DST timezone is east of UTC (as in Western Europe, including the UK). Only use this if <code>daylight</code> is nonzero. See note below.</p> </dd> +</dl> <dl class="py data"> <dt class="sig sig-object py" id="time.daylight"> +<code>time.daylight</code> </dt> <dd> +<p>Nonzero if a DST timezone is defined. See note below.</p> </dd> +</dl> <dl class="py data"> <dt class="sig sig-object py" id="time.timezone"> +<code>time.timezone</code> </dt> <dd> +<p>The offset of the local (non-DST) timezone, in seconds west of UTC (negative in most of Western Europe, positive in the US, zero in the UK). See note below.</p> </dd> +</dl> <dl class="py data"> <dt class="sig sig-object py" id="time.tzname"> +<code>time.tzname</code> </dt> <dd> +<p>A tuple of two strings: the first is the name of the local non-DST timezone, the second is the name of the local DST timezone. If no DST timezone is defined, the second string should not be used. See note below.</p> </dd> +</dl> <div class="admonition note"> <p class="admonition-title">Note</p> <p>For the above Timezone constants (<a class="reference internal" href="#time.altzone" title="time.altzone"><code>altzone</code></a>, <a class="reference internal" href="#time.daylight" title="time.daylight"><code>daylight</code></a>, <a class="reference internal" href="#time.timezone" title="time.timezone"><code>timezone</code></a>, and <a class="reference internal" href="#time.tzname" title="time.tzname"><code>tzname</code></a>), the value is determined by the timezone rules in effect at module load time or the last time <a class="reference internal" href="#time.tzset" title="time.tzset"><code>tzset()</code></a> is called and may be incorrect for times in the past. It is recommended to use the <a class="reference internal" href="#time.struct_time.tm_gmtoff" title="time.struct_time.tm_gmtoff"><code>tm_gmtoff</code></a> and <a class="reference internal" href="#time.struct_time.tm_zone" title="time.struct_time.tm_zone"><code>tm_zone</code></a> results from <a class="reference internal" href="#time.localtime" title="time.localtime"><code>localtime()</code></a> to obtain timezone information.</p> </div> <div class="admonition seealso"> <p class="admonition-title">See also</p> <dl class="simple"> <dt> +<code>Module</code> <a class="reference internal" href="datetime#module-datetime" title="datetime: Basic date and time types."><code>datetime</code></a> +</dt> +<dd> +<p>More object-oriented interface to dates and times.</p> </dd> <dt> +<code>Module</code> <a class="reference internal" href="locale#module-locale" title="locale: Internationalization services."><code>locale</code></a> +</dt> +<dd> +<p>Internationalization services. The locale setting affects the interpretation of many format specifiers in <a class="reference internal" href="#time.strftime" title="time.strftime"><code>strftime()</code></a> and <a class="reference internal" href="#time.strptime" title="time.strptime"><code>strptime()</code></a>.</p> </dd> <dt> +<code>Module</code> <a class="reference internal" href="calendar#module-calendar" title="calendar: Functions for working with calendars, including some emulation of the Unix cal program."><code>calendar</code></a> +</dt> +<dd> +<p>General calendar-related functions. <a class="reference internal" href="calendar#calendar.timegm" title="calendar.timegm"><code>timegm()</code></a> is the inverse of <a class="reference internal" href="#time.gmtime" title="time.gmtime"><code>gmtime()</code></a> from this module.</p> </dd> </dl> </div> <h4 class="rubric">Footnotes</h4> <dl class="footnote brackets"> <dt class="label" id="id4"> +<code>1(1,2,3)</code> </dt> <dd> +<p>The use of <code>%Z</code> is now deprecated, but the <code>%z</code> escape that expands to the preferred hour/minute offset is not supported by all ANSI C libraries. Also, a strict reading of the original 1982 <span class="target" id="index-19"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc822.html"><strong>RFC 822</strong></a> standard calls for a two-digit year (<code>%y</code> rather than <code>%Y</code>), but practice moved to 4-digit years long before the year 2000. After that, <span class="target" id="index-20"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc822.html"><strong>RFC 822</strong></a> became obsolete and the 4-digit year has been first recommended by <span class="target" id="index-21"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc1123.html"><strong>RFC 1123</strong></a> and then mandated by <span class="target" id="index-22"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2822.html"><strong>RFC 2822</strong></a>.</p> </dd> </dl> </section> <div class="_attribution"> + <p class="_attribution-p"> + © 2001–2023 Python Software Foundation<br>Licensed under the PSF License.<br> + <a href="https://docs.python.org/3.12/library/time.html" class="_attribution-link">https://docs.python.org/3.12/library/time.html</a> + </p> +</div> |
