parthtamu/rag-code-assistant
0
1<!DOCTYPE html>2 3<html lang="en" data-content_root="../">4 <head>5 <meta charset="utf-8" />6 <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />7<meta property="og:title" content="time — Time access and conversions" />8<meta property="og:type" content="website" />9<meta property="og:url" content="https://docs.python.org/3/library/time.html" />10<meta property="og:site_name" content="Python documentation" />11<meta property="og:description" content="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..." />12<meta property="og:image:width" content="1146" />13<meta property="og:image:height" content="600" />14<meta property="og:image" content="https://docs.python.org/3.15/_images/social_previews/summary_library_time_01a6066d.png" />15<meta property="og:image:alt" content="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..." />16<meta name="description" content="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..." />17<meta name="twitter:card" content="summary_large_image" />18<meta name="theme-color" content="#3776ab">19 20 <title>time — Time access and conversions — Python 3.15.0a6 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">21 22 <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />23 <link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />24 <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />25 <link rel="stylesheet" type="text/css" href="../_static/profiling-sampling-visualization.css?v=0c2600ae" />26 <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />27 28 <script src="../_static/documentation_options.js?v=6b7c9ff5"></script>29 <script src="../_static/doctools.js?v=9bcbadda"></script>30 <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>31 <script src="../_static/profiling-sampling-visualization.js?v=9811ed04"></script>32 33 <script src="../_static/sidebar.js"></script>34 35 <link rel="search" type="application/opensearchdescription+xml"36 title="Search within Python 3.15.0a6 documentation"37 href="../_static/opensearch.xml"/>38 <link rel="author" title="About these documents" href="../about.html" />39 <link rel="index" title="Index" href="../genindex.html" />40 <link rel="search" title="Search" href="../search.html" />41 <link rel="copyright" title="Copyright" href="../copyright.html" />42 <link rel="next" title="logging — Logging facility for Python" href="logging.html" />43 <link rel="prev" title="io — Core tools for working with streams" href="io.html" />44 45 46 <script defer file-types="bz2,epub,zip" data-domain="docs.python.org" src="https://analytics.python.org/js/script.file-downloads.outbound-links.js"></script>47 48 <link rel="canonical" href="https://docs.python.org/3/library/time.html">49 50 51 52 53 <style>54 @media only screen {55 table.full-width-table {56 width: 100%;57 }58 }59 </style>60<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">61 <link rel="shortcut icon" type="image/png" href="../_static/py.svg">62 <script type="text/javascript" src="../_static/copybutton.js"></script>63 <script type="text/javascript" src="../_static/menu.js"></script>64 <script type="text/javascript" src="../_static/search-focus.js"></script>65 <script type="text/javascript" src="../_static/themetoggle.js"></script> 66 <script type="text/javascript" src="../_static/rtd_switcher.js"></script>67 <meta name="readthedocs-addons-api-version" content="1">68 69 </head>70<body>71<div class="mobile-nav">72 <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"73 aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">74 <nav class="nav-content" role="navigation">75 <label for="menuToggler" class="toggler__label">76 <span></span>77 </label>78 <span class="nav-items-wrapper">79 <a href="https://www.python.org/" class="nav-logo">80 <img src="../_static/py.svg" alt="Python logo">81 </a>82 <span class="version_switcher_placeholder"></span>83 <form role="search" class="search" action="../search.html" method="get">84 <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">85 <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>86 </svg>87 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q">88 <input type="submit" value="Go">89 </form>90 </span>91 </nav>92 <div class="menu-wrapper">93 <nav class="menu" role="navigation" aria-label="main navigation">94 <div class="language_switcher_placeholder"></div>95 96<label class="theme-selector-label">97 Theme98 <select class="theme-selector" oninput="activateTheme(this.value)">99 <option value="auto" selected>Auto</option>100 <option value="light">Light</option>101 <option value="dark">Dark</option>102 </select>103</label>104 <div>105 <h3><a href="../contents.html">Table of Contents</a></h3>106 <ul>107<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">time</span></code> — Time access and conversions</a><ul>108<li><a class="reference internal" href="#functions">Functions</a></li>109<li><a class="reference internal" href="#clock-id-constants">Clock ID Constants</a></li>110<li><a class="reference internal" href="#timezone-constants">Timezone Constants</a></li>111</ul>112</li>113</ul>114 115 </div>116 <div>117 <h4>Previous topic</h4>118 <p class="topless"><a href="io.html"119 title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">io</span></code> — Core tools for working with streams</a></p>120 </div>121 <div>122 <h4>Next topic</h4>123 <p class="topless"><a href="logging.html"124 title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging</span></code> — Logging facility for Python</a></p>125 </div>126 <script>127 document.addEventListener('DOMContentLoaded', () => {128 const title = document.querySelector('meta[property="og:title"]').content;129 const elements = document.querySelectorAll('.improvepage');130 const pageurl = window.location.href.split('?')[0];131 elements.forEach(element => {132 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));133 url.searchParams.set('pagetitle', title);134 url.searchParams.set('pageurl', pageurl);135 url.searchParams.set('pagesource', "library/time.rst");136 element.href = url.toString();137 });138 });139 </script>140 <div role="note" aria-label="source link">141 <h3>This page</h3>142 <ul class="this-page-menu">143 <li><a href="../bugs.html">Report a bug</a></li>144 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>145 <li>146 <a href="https://github.com/python/cpython/blob/main/Doc/library/time.rst?plain=1"147 rel="nofollow">Show source148 </a>149 </li>150 151 </ul>152 </div>153 </nav>154 </div>155</div>156 157 158 <div class="related" role="navigation" aria-label="Related">159 <h3>Navigation</h3>160 <ul>161 <li class="right" style="margin-right: 10px">162 <a href="../genindex.html" title="General Index"163 accesskey="I">index</a></li>164 <li class="right" >165 <a href="../py-modindex.html" title="Python Module Index"166 >modules</a> |</li>167 <li class="right" >168 <a href="logging.html" title="logging — Logging facility for Python"169 accesskey="N">next</a> |</li>170 <li class="right" >171 <a href="io.html" title="io — Core tools for working with streams"172 accesskey="P">previous</a> |</li>173 174 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>175 <li><a href="https://www.python.org/">Python</a> »</li>176 <li class="switchers">177 <div class="language_switcher_placeholder"></div>178 <div class="version_switcher_placeholder"></div>179 </li>180 <li>181 182 </li>183 <li id="cpython-language-and-version">184 <a href="../index.html">3.15.0a6 Documentation</a> »185 </li>186 187 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>188 <li class="nav-item nav-item-2"><a href="allos.html" accesskey="U">Generic Operating System Services</a> »</li>189 <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">time</span></code> — Time access and conversions</a></li>190 <li class="right">191 192 193 <div class="inline-search" role="search">194 <form class="inline-search" action="../search.html" method="get">195 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">196 <input type="submit" value="Go">197 </form>198 </div>199 |200 </li>201 <li class="right">202<label class="theme-selector-label">203 Theme204 <select class="theme-selector" oninput="activateTheme(this.value)">205 <option value="auto" selected>Auto</option>206 <option value="light">Light</option>207 <option value="dark">Dark</option>208 </select>209</label> |</li>210 211 </ul>212 </div> 213 214 <div class="document">215 <div class="documentwrapper">216 <div class="bodywrapper">217 <div class="body" role="main">218 219 <section id="module-time">220<span id="time-time-access-and-conversions"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">time</span></code> — Time access and conversions<a class="headerlink" href="#module-time" title="Link to this heading">¶</a></h1>221<hr class="docutils" />222<p>This module provides various time-related functions. For related223functionality, see also the <a class="reference internal" href="datetime.html#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime</span></code></a> and <a class="reference internal" href="calendar.html#module-calendar" title="calendar: Functions for working with calendars, including some emulation of the Unix cal program."><code class="xref py py-mod docutils literal notranslate"><span class="pre">calendar</span></code></a> modules.</p>224<p>Although this module is always available,225not all functions are available on all platforms. Most of the functions226defined in this module call platform C library functions with the same name. It227may sometimes be helpful to consult the platform documentation, because the228semantics of these functions varies among platforms.</p>229<p>An explanation of some terminology and conventions is in order.</p>230<span id="epoch"></span><ul class="simple" id="index-0">231<li><p>The <em class="dfn">epoch</em> is the point where the time starts, the return value of232<code class="docutils literal notranslate"><span class="pre">time.gmtime(0)</span></code>. It is January 1, 1970, 00:00:00 (UTC) on all platforms.</p></li>233</ul>234<ul class="simple" id="index-1">235<li><p>The term <em class="dfn">seconds since the epoch</em> refers to the total number236of elapsed seconds since the epoch, typically excluding237<a class="reference external" href="https://en.wikipedia.org/wiki/Leap_second">leap seconds</a>. Leap seconds are excluded from this total on all238POSIX-compliant platforms.</p></li>239</ul>240<ul class="simple" id="index-2">241<li><p>The functions in this module may not handle dates and times before the <a class="reference internal" href="#epoch">epoch</a> or242far in the future. The cut-off point in the future is determined by the C243library; for 32-bit systems, it is typically in 2038.</p></li>244</ul>245<ul class="simple" id="index-3">246<li><p>Function <a class="reference internal" href="#time.strptime" title="time.strptime"><code class="xref py py-func docutils literal notranslate"><span class="pre">strptime()</span></code></a> can parse 2-digit years when given <code class="docutils literal notranslate"><span class="pre">%y</span></code> format247code. When 2-digit years are parsed, they are converted according to the POSIX248and ISO C standards: values 69–99 are mapped to 1969–1999, and values 0–68249are mapped to 2000–2068.</p></li>250</ul>251<ul class="simple" id="index-4">252<li><p>UTC is <a class="reference external" href="https://en.wikipedia.org/wiki/Coordinated_Universal_Time">Coordinated Universal Time</a> and superseded <a class="reference external" href="https://en.wikipedia.org/wiki/Greenwich_Mean_Time">Greenwich Mean Time</a> or253GMT as the basis of international timekeeping. The acronym UTC is not a254mistake but conforms to an earlier, language-agnostic naming scheme for time255standards such as UT0, UT1, and UT2.</p></li>256</ul>257<ul id="index-5">258<li><p>DST is Daylight Saving Time, an adjustment of the timezone by (usually) one259hour during part of the year. DST rules are magic (determined by local law) and260can change from year to year. The C library has a table containing the local261rules (often it is read from a system file for flexibility) and is the only262source of True Wisdom in this respect.</p></li>263<li><p>The precision of the various real-time functions may be less than suggested by264the units in which their value or argument is expressed. E.g. on most Unix265systems, the clock “ticks” only 50 or 100 times a second.</p></li>266<li><p>On the other hand, the precision of <a class="reference internal" href="#time.time" title="time.time"><code class="xref py py-func docutils literal notranslate"><span class="pre">time()</span></code></a> and <a class="reference internal" href="#time.sleep" title="time.sleep"><code class="xref py py-func docutils literal notranslate"><span class="pre">sleep()</span></code></a> is better267than their Unix equivalents: times are expressed as floating-point numbers,268<code class="xref py py-func docutils literal notranslate"><span class="pre">time()</span></code> returns the most accurate time available (using Unix269<code class="xref c c-func docutils literal notranslate"><span class="pre">gettimeofday()</span></code> where available), and <code class="xref py py-func docutils literal notranslate"><span class="pre">sleep()</span></code> will accept a time270with a nonzero fraction (Unix <code class="xref c c-func docutils literal notranslate"><span class="pre">select()</span></code> is used to implement this, where271available).</p></li>272<li><p>The time value as returned by <a class="reference internal" href="#time.gmtime" title="time.gmtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">gmtime()</span></code></a>, <a class="reference internal" href="#time.localtime" title="time.localtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">localtime()</span></code></a>, and273<a class="reference internal" href="#time.strptime" title="time.strptime"><code class="xref py py-func docutils literal notranslate"><span class="pre">strptime()</span></code></a>, and accepted by <a class="reference internal" href="#time.asctime" title="time.asctime"><code class="xref py py-func docutils literal notranslate"><span class="pre">asctime()</span></code></a>, <a class="reference internal" href="#time.mktime" title="time.mktime"><code class="xref py py-func docutils literal notranslate"><span class="pre">mktime()</span></code></a> and274<a class="reference internal" href="#time.strftime" title="time.strftime"><code class="xref py py-func docutils literal notranslate"><span class="pre">strftime()</span></code></a>, is a sequence of 9 integers. The return values of275<code class="xref py py-func docutils literal notranslate"><span class="pre">gmtime()</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">localtime()</span></code>, and <code class="xref py py-func docutils literal notranslate"><span class="pre">strptime()</span></code> also offer attribute276names for individual fields.</p>277<p>See <a class="reference internal" href="#time.struct_time" title="time.struct_time"><code class="xref py py-class docutils literal notranslate"><span class="pre">struct_time</span></code></a> for a description of these objects.</p>278<div class="versionchanged">279<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 class="xref py py-class docutils literal notranslate"><span class="pre">struct_time</span></code></a> type was extended to provide280the <a class="reference internal" href="#time.struct_time.tm_gmtoff" title="time.struct_time.tm_gmtoff"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tm_gmtoff</span></code></a> and <a class="reference internal" href="#time.struct_time.tm_zone" title="time.struct_time.tm_zone"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tm_zone</span></code></a>281attributes when platform supports corresponding282<code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">tm</span></code> members.</p>283</div>284<div class="versionchanged">285<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 class="xref py py-class docutils literal notranslate"><span class="pre">struct_time</span></code></a> attributes286<a class="reference internal" href="#time.struct_time.tm_gmtoff" title="time.struct_time.tm_gmtoff"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tm_gmtoff</span></code></a> and <a class="reference internal" href="#time.struct_time.tm_zone" title="time.struct_time.tm_zone"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tm_zone</span></code></a>287are now available on all platforms.</p>288</div>289</li>290<li><p>Use the following functions to convert between time representations:</p>291<table class="docutils align-default">292<thead>293<tr class="row-odd"><th class="head"><p>From</p></th>294<th class="head"><p>To</p></th>295<th class="head"><p>Use</p></th>296</tr>297</thead>298<tbody>299<tr class="row-even"><td><p>seconds since the epoch</p></td>300<td><p><a class="reference internal" href="#time.struct_time" title="time.struct_time"><code class="xref py py-class docutils literal notranslate"><span class="pre">struct_time</span></code></a> in301UTC</p></td>302<td><p><a class="reference internal" href="#time.gmtime" title="time.gmtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">gmtime()</span></code></a></p></td>303</tr>304<tr class="row-odd"><td><p>seconds since the epoch</p></td>305<td><p><a class="reference internal" href="#time.struct_time" title="time.struct_time"><code class="xref py py-class docutils literal notranslate"><span class="pre">struct_time</span></code></a> in306local time</p></td>307<td><p><a class="reference internal" href="#time.localtime" title="time.localtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">localtime()</span></code></a></p></td>308</tr>309<tr class="row-even"><td><p><a class="reference internal" href="#time.struct_time" title="time.struct_time"><code class="xref py py-class docutils literal notranslate"><span class="pre">struct_time</span></code></a> in310UTC</p></td>311<td><p>seconds since the epoch</p></td>312<td><p><a class="reference internal" href="calendar.html#calendar.timegm" title="calendar.timegm"><code class="xref py py-func docutils literal notranslate"><span class="pre">calendar.timegm()</span></code></a></p></td>313</tr>314<tr class="row-odd"><td><p><a class="reference internal" href="#time.struct_time" title="time.struct_time"><code class="xref py py-class docutils literal notranslate"><span class="pre">struct_time</span></code></a> in315local time</p></td>316<td><p>seconds since the epoch</p></td>317<td><p><a class="reference internal" href="#time.mktime" title="time.mktime"><code class="xref py py-func docutils literal notranslate"><span class="pre">mktime()</span></code></a></p></td>318</tr>319</tbody>320</table>321</li>322</ul>323<section id="functions">324<span id="time-functions"></span><h2>Functions<a class="headerlink" href="#functions" title="Link to this heading">¶</a></h2>325<dl class="py function">326<dt class="sig sig-object py" id="time.asctime">327<span class="sig-prename descclassname"><span class="pre">time.</span></span><span class="sig-name descname"><span class="pre">asctime</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">t</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#time.asctime" title="Link to this definition">¶</a></dt>328<dd><p>Convert a tuple or <a class="reference internal" href="#time.struct_time" title="time.struct_time"><code class="xref py py-class docutils literal notranslate"><span class="pre">struct_time</span></code></a> representing a time as returned by329<a class="reference internal" href="#time.gmtime" title="time.gmtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">gmtime()</span></code></a> or <a class="reference internal" href="#time.localtime" title="time.localtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">localtime()</span></code></a> to a string of the following330form: <code class="docutils literal notranslate"><span class="pre">'Sun</span> <span class="pre">Jun</span> <span class="pre">20</span> <span class="pre">23:21:05</span> <span class="pre">1993'</span></code>. The day field is two characters long331and is space padded if the day is a single digit,332e.g.: <code class="docutils literal notranslate"><span class="pre">'Wed</span> <span class="pre">Jun</span>  <span class="pre">9</span> <span class="pre">04:26:40</span> <span class="pre">1993'</span></code>.</p>333<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 class="xref py py-func docutils literal notranslate"><span class="pre">localtime()</span></code></a>334is used. Locale information is not used by <code class="xref py py-func docutils literal notranslate"><span class="pre">asctime()</span></code>.</p>335<div class="admonition note">336<p class="admonition-title">Note</p>337<p>Unlike the C function of the same name, <code class="xref py py-func docutils literal notranslate"><span class="pre">asctime()</span></code> does not add a338trailing newline.</p>339</div>340</dd></dl>341 342<dl class="py function">343<dt class="sig sig-object py" id="time.pthread_getcpuclockid">344<span class="sig-prename descclassname"><span class="pre">time.</span></span><span class="sig-name descname"><span class="pre">pthread_getcpuclockid</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">thread_id</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#time.pthread_getcpuclockid" title="Link to this definition">¶</a></dt>345<dd><p>Return the <em>clk_id</em> of the thread-specific CPU-time clock for the specified <em>thread_id</em>.</p>346<p>Use <a class="reference internal" href="threading.html#threading.get_ident" title="threading.get_ident"><code class="xref py py-func docutils literal notranslate"><span class="pre">threading.get_ident()</span></code></a> or the <a class="reference internal" href="threading.html#threading.Thread.ident" title="threading.Thread.ident"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ident</span></code></a>347attribute of <a class="reference internal" href="threading.html#threading.Thread" title="threading.Thread"><code class="xref py py-class docutils literal notranslate"><span class="pre">threading.Thread</span></code></a> objects to get a suitable value348for <em>thread_id</em>.</p>349<div class="admonition warning">350<p class="admonition-title">Warning</p>351<p>Passing an invalid or expired <em>thread_id</em> may result in352undefined behavior, such as segmentation fault.</p>353</div>354<div class="availability docutils container">355<p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix</p>356<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> for357further information.</p>358</div>359<div class="versionadded">360<p><span class="versionmodified added">Added in version 3.7.</span></p>361</div>362</dd></dl>363 364<dl class="py function">365<dt class="sig sig-object py" id="time.clock_getres">366<span class="sig-prename descclassname"><span class="pre">time.</span></span><span class="sig-name descname"><span class="pre">clock_getres</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">clk_id</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#time.clock_getres" title="Link to this definition">¶</a></dt>367<dd><p>Return the resolution (precision) of the specified clock <em>clk_id</em>. Refer to368<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>369<div class="availability docutils container">370<p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>371</div>372<div class="versionadded">373<p><span class="versionmodified added">Added in version 3.3.</span></p>374</div>375</dd></dl>376 377<dl class="py function">378<dt class="sig sig-object py" id="time.clock_gettime">379<span class="sig-prename descclassname"><span class="pre">time.</span></span><span class="sig-name descname"><span class="pre">clock_gettime</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">clk_id</span></span></em><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">→</span> <span class="sig-return-typehint"><a class="reference internal" href="functions.html#float" title="float"><span class="pre">float</span></a></span></span><a class="headerlink" href="#time.clock_gettime" title="Link to this definition">¶</a></dt>380<dd><p>Return the time of the specified clock <em>clk_id</em>. Refer to381<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>382<p>Use <a class="reference internal" href="#time.clock_gettime_ns" title="time.clock_gettime_ns"><code class="xref py py-func docutils literal notranslate"><span class="pre">clock_gettime_ns()</span></code></a> to avoid the precision loss caused by the383<a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a> type.</p>384<div class="availability docutils container">385<p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>386</div>387<div class="versionadded">388<p><span class="versionmodified added">Added in version 3.3.</span></p>389</div>390</dd></dl>391 392<dl class="py function">393<dt class="sig sig-object py" id="time.clock_gettime_ns">394<span class="sig-prename descclassname"><span class="pre">time.</span></span><span class="sig-name descname"><span class="pre">clock_gettime_ns</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">clk_id</span></span></em><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">→</span> <span class="sig-return-typehint"><a class="reference internal" href="functions.html#int" title="int"><span class="pre">int</span></a></span></span><a class="headerlink" href="#time.clock_gettime_ns" title="Link to this definition">¶</a></dt>395<dd><p>Similar to <a class="reference internal" href="#time.clock_gettime" title="time.clock_gettime"><code class="xref py py-func docutils literal notranslate"><span class="pre">clock_gettime()</span></code></a> but return time as nanoseconds.</p>396<div class="availability docutils container">397<p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>398</div>399<div class="versionadded">400<p><span class="versionmodified added">Added in version 3.7.</span></p>401</div>402</dd></dl>403 404<dl class="py function">405<dt class="sig sig-object py" id="time.clock_settime">406<span class="sig-prename descclassname"><span class="pre">time.</span></span><span class="sig-name descname"><span class="pre">clock_settime</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">clk_id</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">time</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#time.clock_settime" title="Link to this definition">¶</a></dt>407<dd><p>Set the time of the specified clock <em>clk_id</em>. Currently,408<a class="reference internal" href="#time.CLOCK_REALTIME" title="time.CLOCK_REALTIME"><code class="xref py py-data docutils literal notranslate"><span class="pre">CLOCK_REALTIME</span></code></a> is the only accepted value for <em>clk_id</em>.</p>409<p>Use <a class="reference internal" href="#time.clock_settime_ns" title="time.clock_settime_ns"><code class="xref py py-func docutils literal notranslate"><span class="pre">clock_settime_ns()</span></code></a> to avoid the precision loss caused by the410<a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a> type.</p>411<div class="availability docutils container">412<p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Android, not iOS.</p>413</div>414<div class="versionadded">415<p><span class="versionmodified added">Added in version 3.3.</span></p>416</div>417<div class="versionchanged">418<p><span class="versionmodified changed">Changed in version 3.15: </span>Accepts any real number as <em>time</em>, not only integer or float.</p>419</div>420</dd></dl>421 422<dl class="py function">423<dt class="sig sig-object py" id="time.clock_settime_ns">424<span class="sig-prename descclassname"><span class="pre">time.</span></span><span class="sig-name descname"><span class="pre">clock_settime_ns</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">clk_id</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">time</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference internal" href="functions.html#int" title="int"><span class="pre">int</span></a></span></em><span class="sig-paren">)</span><a class="headerlink" href="#time.clock_settime_ns" title="Link to this definition">¶</a></dt>425<dd><p>Similar to <a class="reference internal" href="#time.clock_settime" title="time.clock_settime"><code class="xref py py-func docutils literal notranslate"><span class="pre">clock_settime()</span></code></a> but set time with nanoseconds.</p>426<div class="availability docutils container">427<p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Android, not iOS.</p>428</div>429<div class="versionadded">430<p><span class="versionmodified added">Added in version 3.7.</span></p>431</div>432</dd></dl>433 434<dl class="py function">435<dt class="sig sig-object py" id="time.ctime">436<span class="sig-prename descclassname"><span class="pre">time.</span></span><span class="sig-name descname"><span class="pre">ctime</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">secs</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#time.ctime" title="Link to this definition">¶</a></dt>437<dd><p>Convert a time expressed in seconds since the <a class="reference internal" href="#epoch">epoch</a> to a string of a form:438<code class="docutils literal notranslate"><span class="pre">'Sun</span> <span class="pre">Jun</span> <span class="pre">20</span> <span class="pre">23:21:05</span> <span class="pre">1993'</span></code> representing local time. The day field439is two characters long and is space padded if the day is a single digit,440e.g.: <code class="docutils literal notranslate"><span class="pre">'Wed</span> <span class="pre">Jun</span>  <span class="pre">9</span> <span class="pre">04:26:40</span> <span class="pre">1993'</span></code>.</p>441<p>If <em>secs</em> is not provided or <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a>, the current time as442returned by <a class="reference internal" href="#time.time" title="time.time"><code class="xref py py-func docutils literal notranslate"><span class="pre">time()</span></code></a> is used. <code class="docutils literal notranslate"><span class="pre">ctime(secs)</span></code> is equivalent to443<code class="docutils literal notranslate"><span class="pre">asctime(localtime(secs))</span></code>. Locale information is not used by444<code class="xref py py-func docutils literal notranslate"><span class="pre">ctime()</span></code>.</p>445<div class="versionchanged">446<p><span class="versionmodified changed">Changed in version 3.15: </span>Accepts any real number, not only integer or float.</p>447</div>448</dd></dl>449 450<dl class="py function">451<dt class="sig sig-object py" id="time.get_clock_info">452<span class="sig-prename descclassname"><span class="pre">time.</span></span><span class="sig-name descname"><span class="pre">get_clock_info</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#time.get_clock_info" title="Link to this definition">¶</a></dt>453<dd><p>Get information on the specified clock as a namespace object.454Supported clock names and the corresponding functions to read their value455are:</p>456<ul class="simple">457<li><p><code class="docutils literal notranslate"><span class="pre">'monotonic'</span></code>: <a class="reference internal" href="#time.monotonic" title="time.monotonic"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.monotonic()</span></code></a></p></li>458<li><p><code class="docutils literal notranslate"><span class="pre">'perf_counter'</span></code>: <a class="reference internal" href="#time.perf_counter" title="time.perf_counter"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.perf_counter()</span></code></a></p></li>459<li><p><code class="docutils literal notranslate"><span class="pre">'process_time'</span></code>: <a class="reference internal" href="#time.process_time" title="time.process_time"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.process_time()</span></code></a></p></li>460<li><p><code class="docutils literal notranslate"><span class="pre">'thread_time'</span></code>: <a class="reference internal" href="#time.thread_time" title="time.thread_time"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.thread_time()</span></code></a></p></li>461<li><p><code class="docutils literal notranslate"><span class="pre">'time'</span></code>: <a class="reference internal" href="#time.time" title="time.time"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.time()</span></code></a></p></li>462</ul>463<p>The result has the following attributes:</p>464<ul class="simple">465<li><p><em>adjustable</em>: <code class="docutils literal notranslate"><span class="pre">True</span></code> if the clock can be set to jump forward or backward466in time, <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise. Does not refer to gradual NTP rate adjustments.</p></li>467<li><p><em>implementation</em>: The name of the underlying C function used to get468the 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.</p></li>469<li><p><em>monotonic</em>: <code class="docutils literal notranslate"><span class="pre">True</span></code> if the clock cannot go backward,470<code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise</p></li>471<li><p><em>resolution</em>: The resolution of the clock in seconds (<a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a>)</p></li>472</ul>473<div class="versionadded">474<p><span class="versionmodified added">Added in version 3.3.</span></p>475</div>476</dd></dl>477 478<dl class="py function">479<dt class="sig sig-object py" id="time.gmtime">480<span class="sig-prename descclassname"><span class="pre">time.</span></span><span class="sig-name descname"><span class="pre">gmtime</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">secs</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#time.gmtime" title="Link to this definition">¶</a></dt>481<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 class="xref py py-class docutils literal notranslate"><span class="pre">struct_time</span></code></a> in482UTC in which the dst flag is always zero. If <em>secs</em> is not provided or483<a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a>, the current time as returned by <a class="reference internal" href="#time.time" title="time.time"><code class="xref py py-func docutils literal notranslate"><span class="pre">time()</span></code></a> is used. Fractions484of a second are ignored. See above for a description of the485<code class="xref py py-class docutils literal notranslate"><span class="pre">struct_time</span></code> object. See <a class="reference internal" href="calendar.html#calendar.timegm" title="calendar.timegm"><code class="xref py py-func docutils literal notranslate"><span class="pre">calendar.timegm()</span></code></a> for the inverse of this486function.</p>487<div class="versionchanged">488<p><span class="versionmodified changed">Changed in version 3.15: </span>Accepts any real number, not only integer or float.</p>489</div>490</dd></dl>491 492<dl class="py function">493<dt class="sig sig-object py" id="time.localtime">494<span class="sig-prename descclassname"><span class="pre">time.</span></span><span class="sig-name descname"><span class="pre">localtime</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">secs</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#time.localtime" title="Link to this definition">¶</a></dt>495<dd><p>Like <a class="reference internal" href="#time.gmtime" title="time.gmtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">gmtime()</span></code></a> but converts to local time. If <em>secs</em> is not provided or496<a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a>, the current time as returned by <a class="reference internal" href="#time.time" title="time.time"><code class="xref py py-func docutils literal notranslate"><span class="pre">time()</span></code></a> is used. The dst497flag is set to <code class="docutils literal notranslate"><span class="pre">1</span></code> when DST applies to the given time.</p>498<p><code class="xref py py-func docutils literal notranslate"><span class="pre">localtime()</span></code> may raise <a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a>, if the timestamp is499outside the range of values supported by the platform C <code class="xref c c-func docutils literal notranslate"><span class="pre">localtime()</span></code>500or <code class="xref c c-func docutils literal notranslate"><span class="pre">gmtime()</span></code> functions, and <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> on <code class="xref c c-func docutils literal notranslate"><span class="pre">localtime()</span></code> or501<code class="xref c c-func docutils literal notranslate"><span class="pre">gmtime()</span></code> failure. It’s common for this to be restricted to years502between 1970 and 2038.</p>503<div class="versionchanged">504<p><span class="versionmodified changed">Changed in version 3.15: </span>Accepts any real number, not only integer or float.</p>505</div>506</dd></dl>507 508<dl class="py function">509<dt class="sig sig-object py" id="time.mktime">510<span class="sig-prename descclassname"><span class="pre">time.</span></span><span class="sig-name descname"><span class="pre">mktime</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">t</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#time.mktime" title="Link to this definition">¶</a></dt>511<dd><p>This is the inverse function of <a class="reference internal" href="#time.localtime" title="time.localtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">localtime()</span></code></a>. Its argument is the512<a class="reference internal" href="#time.struct_time" title="time.struct_time"><code class="xref py py-class docutils literal notranslate"><span class="pre">struct_time</span></code></a> or full 9-tuple (since the dst flag is needed; use <code class="docutils literal notranslate"><span class="pre">-1</span></code>513as the dst flag if it is unknown) which expresses the time in <em>local</em> time, not514UTC. It returns a floating-point number, for compatibility with <a class="reference internal" href="#time.time" title="time.time"><code class="xref py py-func docutils literal notranslate"><span class="pre">time()</span></code></a>.515If the input value cannot be represented as a valid time, either516<a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a> or <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> will be raised (which depends on517whether the invalid value is caught by Python or the underlying C libraries).518The earliest date for which it can generate a time is platform-dependent.</p>519</dd></dl>520 521<dl class="py function">522<dt class="sig sig-object py" id="time.monotonic">523<span class="sig-prename descclassname"><span class="pre">time.</span></span><span class="sig-name descname"><span class="pre">monotonic</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">→</span> <span class="sig-return-typehint"><a class="reference internal" href="functions.html#float" title="float"><span class="pre">float</span></a></span></span><a class="headerlink" href="#time.monotonic" title="Link to this definition">¶</a></dt>524<dd><p>Return the value (in fractional seconds) of a monotonic clock, i.e. a clock525that cannot go backwards. The clock is not affected by system clock updates.526The reference point of the returned value is undefined, so that only the527difference between the results of two calls is valid.</p>528<p>Clock:</p>529<ul class="simple">530<li><p>On Windows, call <code class="docutils literal notranslate"><span class="pre">QueryPerformanceCounter()</span></code> and531<code class="docutils literal notranslate"><span class="pre">QueryPerformanceFrequency()</span></code>.</p></li>532<li><p>On macOS, call <code class="docutils literal notranslate"><span class="pre">mach_absolute_time()</span></code> and <code class="docutils literal notranslate"><span class="pre">mach_timebase_info()</span></code>.</p></li>533<li><p>On HP-UX, call <code class="docutils literal notranslate"><span class="pre">gethrtime()</span></code>.</p></li>534<li><p>Call <code class="docutils literal notranslate"><span class="pre">clock_gettime(CLOCK_HIGHRES)</span></code> if available.</p></li>535<li><p>Otherwise, call <code class="docutils literal notranslate"><span class="pre">clock_gettime(CLOCK_MONOTONIC)</span></code>.</p></li>536</ul>537<p>Use <a class="reference internal" href="#time.monotonic_ns" title="time.monotonic_ns"><code class="xref py py-func docutils literal notranslate"><span class="pre">monotonic_ns()</span></code></a> to avoid the precision loss caused by the538<a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a> type.</p>539<div class="versionadded">540<p><span class="versionmodified added">Added in version 3.3.</span></p>541</div>542<div class="versionchanged">543<p><span class="versionmodified changed">Changed in version 3.5: </span>The function is now always available and the clock is now the same for544all processes.</p>545</div>546<div class="versionchanged">547<p><span class="versionmodified changed">Changed in version 3.10: </span>On macOS, the clock is now the same for all processes.</p>548</div>549</dd></dl>550 551<dl class="py function">552<dt class="sig sig-object py" id="time.monotonic_ns">553<span class="sig-prename descclassname"><span class="pre">time.</span></span><span class="sig-name descname"><span class="pre">monotonic_ns</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">→</span> <span class="sig-return-typehint"><a class="reference internal" href="functions.html#int" title="int"><span class="pre">int</span></a></span></span><a class="headerlink" href="#time.monotonic_ns" title="Link to this definition">¶</a></dt>554<dd><p>Similar to <a class="reference internal" href="#time.monotonic" title="time.monotonic"><code class="xref py py-func docutils literal notranslate"><span class="pre">monotonic()</span></code></a>, but return time as nanoseconds.</p>555<div class="versionadded">556<p><span class="versionmodified added">Added in version 3.7.</span></p>557</div>558</dd></dl>559 560<dl class="py function">561<dt class="sig sig-object py" id="time.perf_counter">562<span class="sig-prename descclassname"><span class="pre">time.</span></span><span class="sig-name descname"><span class="pre">perf_counter</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">→</span> <span class="sig-return-typehint"><a class="reference internal" href="functions.html#float" title="float"><span class="pre">float</span></a></span></span><a class="headerlink" href="#time.perf_counter" title="Link to this definition">¶</a></dt>563<dd><p id="index-6">Return the value (in fractional seconds) of a performance counter, i.e. a564clock with the highest available resolution to measure a short duration. It565does include time elapsed during sleep. The clock is the same for all566processes. The reference567point of the returned value is undefined, so that only the difference between568the results of two calls is valid.</p>569<div class="impl-detail compound">570<p><strong>CPython implementation detail:</strong> On CPython, use the same clock as <a class="reference internal" href="#time.monotonic" title="time.monotonic"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.monotonic()</span></code></a> and is a571monotonic clock, i.e. a clock that cannot go backwards.</p>572</div>573<p>Use <a class="reference internal" href="#time.perf_counter_ns" title="time.perf_counter_ns"><code class="xref py py-func docutils literal notranslate"><span class="pre">perf_counter_ns()</span></code></a> to avoid the precision loss caused by the574<a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a> type.</p>575<div class="versionadded">576<p><span class="versionmodified added">Added in version 3.3.</span></p>577</div>578<div class="versionchanged">579<p><span class="versionmodified changed">Changed in version 3.10: </span>On Windows, the clock is now the same for all processes.</p>580</div>581<div class="versionchanged">582<p><span class="versionmodified changed">Changed in version 3.13: </span>Use the same clock as <a class="reference internal" href="#time.monotonic" title="time.monotonic"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.monotonic()</span></code></a>.</p>583</div>584</dd></dl>585 586<dl class="py function">587<dt class="sig sig-object py" id="time.perf_counter_ns">588<span class="sig-prename descclassname"><span class="pre">time.</span></span><span class="sig-name descname"><span class="pre">perf_counter_ns</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">→</span> <span class="sig-return-typehint"><a class="reference internal" href="functions.html#int" title="int"><span class="pre">int</span></a></span></span><a class="headerlink" href="#time.perf_counter_ns" title="Link to this definition">¶</a></dt>589<dd><p>Similar to <a class="reference internal" href="#time.perf_counter" title="time.perf_counter"><code class="xref py py-func docutils literal notranslate"><span class="pre">perf_counter()</span></code></a>, but return time as nanoseconds.</p>590<div class="versionadded">591<p><span class="versionmodified added">Added in version 3.7.</span></p>592</div>593</dd></dl>594 595<dl class="py function">596<dt class="sig sig-object py" id="time.process_time">597<span class="sig-prename descclassname"><span class="pre">time.</span></span><span class="sig-name descname"><span class="pre">process_time</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">→</span> <span class="sig-return-typehint"><a class="reference internal" href="functions.html#float" title="float"><span class="pre">float</span></a></span></span><a class="headerlink" href="#time.process_time" title="Link to this definition">¶</a></dt>598<dd><p id="index-7">Return the value (in fractional seconds) of the sum of the system and user599CPU time of the current process. It does not include time elapsed during600sleep. It is process-wide by definition. The reference point of the601returned value is undefined, so that only the difference between the results602of two calls is valid.</p>603<p>Use <a class="reference internal" href="#time.process_time_ns" title="time.process_time_ns"><code class="xref py py-func docutils literal notranslate"><span class="pre">process_time_ns()</span></code></a> to avoid the precision loss caused by the604<a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a> type.</p>605<div class="versionadded">606<p><span class="versionmodified added">Added in version 3.3.</span></p>607</div>608</dd></dl>609 610<dl class="py function">611<dt class="sig sig-object py" id="time.process_time_ns">612<span class="sig-prename descclassname"><span class="pre">time.</span></span><span class="sig-name descname"><span class="pre">process_time_ns</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">→</span> <span class="sig-return-typehint"><a class="reference internal" href="functions.html#int" title="int"><span class="pre">int</span></a></span></span><a class="headerlink" href="#time.process_time_ns" title="Link to this definition">¶</a></dt>613<dd><p>Similar to <a class="reference internal" href="#time.process_time" title="time.process_time"><code class="xref py py-func docutils literal notranslate"><span class="pre">process_time()</span></code></a> but return time as nanoseconds.</p>614<div class="versionadded">615<p><span class="versionmodified added">Added in version 3.7.</span></p>616</div>617</dd></dl>618 619<dl class="py function">620<dt class="sig sig-object py" id="time.sleep">621<span class="sig-prename descclassname"><span class="pre">time.</span></span><span class="sig-name descname"><span class="pre">sleep</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">secs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#time.sleep" title="Link to this definition">¶</a></dt>622<dd><p>Suspend execution of the calling thread for the given number of seconds.623The argument may be a non-integer to indicate a more precise sleep time.</p>624<p>If the sleep is interrupted by a signal and no exception is raised by the625signal handler, the sleep is restarted with a recomputed timeout.</p>626<p>The suspension time may be longer than requested by an arbitrary amount,627because of the scheduling of other activity in the system.</p>628<p class="rubric">Windows implementation</p>629<p>On Windows, if <em>secs</em> is zero, the thread relinquishes the remainder of its630time slice to any other thread that is ready to run. If there are no other631threads ready to run, the function returns immediately, and the thread632continues execution. On Windows 10 and newer the implementation uses633a <a class="reference external" href="https://learn.microsoft.com/windows/win32/api/synchapi/nf-synchapi-createwaitabletimerexw">high-resolution timer</a>634which provides resolution of 100 nanoseconds. If <em>secs</em> is zero, <code class="docutils literal notranslate"><span class="pre">Sleep(0)</span></code> is used.</p>635<p class="rubric">Unix implementation</p>636<ul class="simple">637<li><p>Use <code class="docutils literal notranslate"><span class="pre">clock_nanosleep()</span></code> if available (resolution: 1 nanosecond);</p></li>638<li><p>Or use <code class="docutils literal notranslate"><span class="pre">nanosleep()</span></code> if available (resolution: 1 nanosecond);</p></li>639<li><p>Or use <code class="docutils literal notranslate"><span class="pre">select()</span></code> (resolution: 1 microsecond).</p></li>640</ul>641<div class="admonition note">642<p class="admonition-title">Note</p>643<p>To emulate a “no-op”, use <a class="reference internal" href="../reference/simple_stmts.html#pass"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">pass</span></code></a> instead of <code class="docutils literal notranslate"><span class="pre">time.sleep(0)</span></code>.</p>644<p>To voluntarily relinquish the CPU, specify a real-time <a class="reference internal" href="os.html#os-scheduling-policy"><span class="std std-ref">scheduling645policy</span></a> and use <a class="reference internal" href="os.html#os.sched_yield" title="os.sched_yield"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.sched_yield()</span></code></a> instead.</p>646</div>647<p class="audit-hook" id="audit_event_time_sleep_0">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">time.sleep</span></code> with argument <code class="docutils literal notranslate"><span class="pre">secs</span></code>.</p>648<div class="versionchanged">649<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 interrupted650by a signal, except if the signal handler raises an exception (see651<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>652</div>653<div class="versionchanged">654<p><span class="versionmodified changed">Changed in version 3.11: </span>On Unix, the <code class="docutils literal notranslate"><span class="pre">clock_nanosleep()</span></code> and <code class="docutils literal notranslate"><span class="pre">nanosleep()</span></code> functions are now655used if available. On Windows, a waitable timer is now used.</p>656</div>657<div class="versionchanged">658<p><span class="versionmodified changed">Changed in version 3.13: </span>Raises an auditing event.</p>659</div>660<div class="versionchanged">661<p><span class="versionmodified changed">Changed in version 3.15: </span>Accepts any real number, not only integer or float.</p>662</div>663</dd></dl>664 665<dl class="py function" id="index-9">666<dt class="sig sig-object py" id="time.strftime">667<span class="sig-prename descclassname"><span class="pre">time.</span></span><span class="sig-name descname"><span class="pre">strftime</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">format</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">t</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#time.strftime" title="Link to this definition">¶</a></dt>668<dd><p>Convert a tuple or <a class="reference internal" href="#time.struct_time" title="time.struct_time"><code class="xref py py-class docutils literal notranslate"><span class="pre">struct_time</span></code></a> representing a time as returned by669<a class="reference internal" href="#time.gmtime" title="time.gmtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">gmtime()</span></code></a> or <a class="reference internal" href="#time.localtime" title="time.localtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">localtime()</span></code></a> to a string as specified by the <em>format</em>670argument. If <em>t</em> is not provided, the current time as returned by671<code class="xref py py-func docutils literal notranslate"><span class="pre">localtime()</span></code> is used. <em>format</em> must be a string. <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is672raised if any field in <em>t</em> is outside of the allowed range.</p>673<p>0 is a legal argument for any position in the time tuple; if it is normally674illegal the value is forced to a correct one.</p>675<p>The following directives can be embedded in the <em>format</em> string. They are shown676without the optional field width and precision specification, and are replaced677by the indicated characters in the <code class="xref py py-func docutils literal notranslate"><span class="pre">strftime()</span></code> result:</p>678<table class="docutils align-default">679<thead>680<tr class="row-odd"><th class="head"><p>Directive</p></th>681<th class="head"><p>Meaning</p></th>682<th class="head"><p>Notes</p></th>683</tr>684</thead>685<tbody>686<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%a</span></code></p></td>687<td><p>Locale’s abbreviated weekday name.</p></td>688<td></td>689</tr>690<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%A</span></code></p></td>691<td><p>Locale’s full weekday name.</p></td>692<td></td>693</tr>694<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%b</span></code></p></td>695<td><p>Locale’s abbreviated month name.</p></td>696<td></td>697</tr>698<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%B</span></code></p></td>699<td><p>Locale’s full month name.</p></td>700<td></td>701</tr>702<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%c</span></code></p></td>703<td><p>Locale’s appropriate date and time704representation.</p></td>705<td></td>706</tr>707<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%d</span></code></p></td>708<td><p>Day of the month as a decimal number [01,31].</p></td>709<td></td>710</tr>711<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%f</span></code></p></td>712<td><dl class="simple">713<dt>Microseconds as a decimal number</dt><dd><p>[000000,999999].</p>714</dd>715</dl>716</td>717<td><p>(1)</p></td>718</tr>719<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%H</span></code></p></td>720<td><p>Hour (24-hour clock) as a decimal number721[00,23].</p></td>722<td></td>723</tr>724<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%I</span></code></p></td>725<td><p>Hour (12-hour clock) as a decimal number726[01,12].</p></td>727<td></td>728</tr>729<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%j</span></code></p></td>730<td><p>Day of the year as a decimal number [001,366].</p></td>731<td></td>732</tr>733<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%m</span></code></p></td>734<td><p>Month as a decimal number [01,12].</p></td>735<td></td>736</tr>737<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%M</span></code></p></td>738<td><p>Minute as a decimal number [00,59].</p></td>739<td></td>740</tr>741<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%p</span></code></p></td>742<td><p>Locale’s equivalent of either AM or PM.</p></td>743<td><p>(2)</p></td>744</tr>745<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%S</span></code></p></td>746<td><p>Second as a decimal number [00,61].</p></td>747<td><p>(3)</p></td>748</tr>749<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%U</span></code></p></td>750<td><p>Week number of the year (Sunday as the first751day of the week) as a decimal number [00,53].752All days in a new year preceding the first753Sunday are considered to be in week 0.</p></td>754<td><p>(4)</p></td>755</tr>756<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%u</span></code></p></td>757<td><p>Day of the week (Monday is 1; Sunday is 7)758as a decimal number [1, 7].</p></td>759<td></td>760</tr>761<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%w</span></code></p></td>762<td><p>Weekday as a decimal number [0(Sunday),6].</p></td>763<td></td>764</tr>765<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%W</span></code></p></td>766<td><p>Week number of the year (Monday as the first767day of the week) as a decimal number [00,53].768All days in a new year preceding the first769Monday are considered to be in week 0.</p></td>770<td><p>(4)</p></td>771</tr>772<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%x</span></code></p></td>773<td><p>Locale’s appropriate date representation.</p></td>774<td></td>775</tr>776<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%X</span></code></p></td>777<td><p>Locale’s appropriate time representation.</p></td>778<td></td>779</tr>780<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%y</span></code></p></td>781<td><p>Year without century as a decimal number782[00,99].</p></td>783<td></td>784</tr>785<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%Y</span></code></p></td>786<td><p>Year with century as a decimal number.</p></td>787<td></td>788</tr>789<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%z</span></code></p></td>790<td><p>Time zone offset indicating a positive or791negative time difference from UTC/GMT of the792form +HHMM or -HHMM, where H represents decimal793hour digits and M represents decimal minute794digits [-23:59, +23:59]. <a class="footnote-reference brackets" href="#id4" id="id1" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a></p></td>795<td></td>796</tr>797<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%Z</span></code></p></td>798<td><p>Time zone name (no characters if no time zone799exists). Deprecated. <a class="footnote-reference brackets" href="#id4" id="id2" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a></p></td>800<td></td>801</tr>802<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%G</span></code></p></td>803<td><p>ISO 8601 year (similar to <code class="docutils literal notranslate"><span class="pre">%Y</span></code> but follows804the rules for the ISO 8601 calendar year).805The year starts with the week that contains806the first Thursday of the calendar year.</p></td>807<td></td>808</tr>809<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">%V</span></code></p></td>810<td><p>ISO 8601 week number (as a decimal number811[01,53]). The first week of the year is the812one that contains the first Thursday of the813year. Weeks start on Monday.</p></td>814<td></td>815</tr>816<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">%%</span></code></p></td>817<td><p>A literal <code class="docutils literal notranslate"><span class="pre">'%'</span></code> character.</p></td>818<td></td>819</tr>820</tbody>821</table>822<p>Notes:</p>823<ol class="arabic simple">824<li><p>The <code class="docutils literal notranslate"><span class="pre">%f</span></code> format directive only applies to <a class="reference internal" href="#time.strptime" title="time.strptime"><code class="xref py py-func docutils literal notranslate"><span class="pre">strptime()</span></code></a>,825not to <code class="xref py py-func docutils literal notranslate"><span class="pre">strftime()</span></code>. However, see also <a class="reference internal" href="datetime.html#datetime.datetime.strptime" title="datetime.datetime.strptime"><code class="xref py py-meth docutils literal notranslate"><span class="pre">datetime.datetime.strptime()</span></code></a> and826<a class="reference internal" href="datetime.html#datetime.datetime.strftime" title="datetime.datetime.strftime"><code class="xref py py-meth docutils literal notranslate"><span class="pre">datetime.datetime.strftime()</span></code></a> where the <code class="docutils literal notranslate"><span class="pre">%f</span></code> format directive827<a class="reference internal" href="datetime.html#format-codes"><span class="std std-ref">applies to microseconds</span></a>.</p></li>828<li><p>When used with the <a class="reference internal" href="#time.strptime" title="time.strptime"><code class="xref py py-func docutils literal notranslate"><span class="pre">strptime()</span></code></a> function, the <code class="docutils literal notranslate"><span class="pre">%p</span></code> directive only affects829the output hour field if the <code class="docutils literal notranslate"><span class="pre">%I</span></code> directive is used to parse the hour.</p></li>830</ol>831<ol class="arabic simple" id="leap-second" start="3">832<li><p>The range really is <code class="docutils literal notranslate"><span class="pre">0</span></code> to <code class="docutils literal notranslate"><span class="pre">61</span></code>; value <code class="docutils literal notranslate"><span class="pre">60</span></code> is valid in833timestamps representing <a class="reference external" href="https://en.wikipedia.org/wiki/Leap_second">leap seconds</a> and value <code class="docutils literal notranslate"><span class="pre">61</span></code> is supported834for historical reasons.</p></li>835<li><p>When used with the <a class="reference internal" href="#time.strptime" title="time.strptime"><code class="xref py py-func docutils literal notranslate"><span class="pre">strptime()</span></code></a> function, <code class="docutils literal notranslate"><span class="pre">%U</span></code> and <code class="docutils literal notranslate"><span class="pre">%W</span></code> are only used in836calculations when the day of the week and the year are specified.</p></li>837</ol>838<p>Here is an example, a format for dates compatible with that specified in the839<span class="target" id="index-10"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc5322.html"><strong>RFC 5322</strong></a> Internet email standard. <a class="footnote-reference brackets" href="#id4" id="id3" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a></p>840<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span><span class="w"> </span><span class="nn">time</span><span class="w"> </span><span class="kn">import</span> <span class="n">gmtime</span><span class="p">,</span> <span class="n">strftime</span>841<span class="gp">>>> </span><span class="n">strftime</span><span class="p">(</span><span class="s2">"</span><span class="si">%a</span><span class="s2">, </span><span class="si">%d</span><span class="s2"> %b %Y %H:%M:%S +0000"</span><span class="p">,</span> <span class="n">gmtime</span><span class="p">())</span>842<span class="go">'Thu, 28 Jun 2001 14:17:15 +0000'</span>843</pre></div>844</div>845<p>Additional directives may be supported on certain platforms, but only the846ones listed here have a meaning standardized by ANSI C. To see the full set847of 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>848documentation.</p>849<p>On some platforms, an optional field width and precision specification can850immediately follow the initial <code class="docutils literal notranslate"><span class="pre">'%'</span></code> of a directive in the following order;851this is also not portable. The field width is normally 2 except for <code class="docutils literal notranslate"><span class="pre">%j</span></code> where852it is 3.</p>853</dd></dl>854 855<dl class="py function" id="index-11">856<dt class="sig sig-object py" id="time.strptime">857<span class="sig-prename descclassname"><span class="pre">time.</span></span><span class="sig-name descname"><span class="pre">strptime</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">string</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">format</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#time.strptime" title="Link to this definition">¶</a></dt>858<dd><p>Parse a string representing a time according to a format. The return value859is a <a class="reference internal" href="#time.struct_time" title="time.struct_time"><code class="xref py py-class docutils literal notranslate"><span class="pre">struct_time</span></code></a> as returned by <a class="reference internal" href="#time.gmtime" title="time.gmtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">gmtime()</span></code></a> or860<a class="reference internal" href="#time.localtime" title="time.localtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">localtime()</span></code></a>.</p>861<p>The <em>format</em> parameter uses the same directives as those used by862<a class="reference internal" href="#time.strftime" title="time.strftime"><code class="xref py py-func docutils literal notranslate"><span class="pre">strftime()</span></code></a>; it defaults to <code class="docutils literal notranslate"><span class="pre">"%a</span> <span class="pre">%b</span> <span class="pre">%d</span> <span class="pre">%H:%M:%S</span> <span class="pre">%Y"</span></code> which matches the863formatting returned by <a class="reference internal" href="#time.ctime" title="time.ctime"><code class="xref py py-func docutils literal notranslate"><span class="pre">ctime()</span></code></a>. If <em>string</em> cannot be parsed according864to <em>format</em>, or if it has excess data after parsing, <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is865raised. The default values used to fill in any missing data when more866accurate values cannot be inferred are <code class="docutils literal notranslate"><span class="pre">(1900,</span> <span class="pre">1,</span> <span class="pre">1,</span> <span class="pre">0,</span> <span class="pre">0,</span> <span class="pre">0,</span> <span class="pre">0,</span> <span class="pre">1,</span> <span class="pre">-1)</span></code>.867Both <em>string</em> and <em>format</em> must be strings.</p>868<p>For example:</p>869<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span><span class="w"> </span><span class="nn">time</span>870<span class="gp">>>> </span><span class="n">time</span><span class="o">.</span><span class="n">strptime</span><span class="p">(</span><span class="s2">"30 Nov 00"</span><span class="p">,</span> <span class="s2">"</span><span class="si">%d</span><span class="s2"> %b %y"</span><span class="p">)</span>871<span class="go">time.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0,</span>872<span class="go"> tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1)</span>873</pre></div>874</div>875<p>Support for the <code class="docutils literal notranslate"><span class="pre">%Z</span></code> directive is based on the values contained in <code class="docutils literal notranslate"><span class="pre">tzname</span></code>876and whether <code class="docutils literal notranslate"><span class="pre">daylight</span></code> is true. Because of this, it is platform-specific877except for recognizing UTC and GMT which are always known (and are considered to878be non-daylight savings timezones).</p>879<p>Only the directives specified in the documentation are supported. Because880<code class="docutils literal notranslate"><span class="pre">strftime()</span></code> is implemented per platform it can sometimes offer more881directives than those listed. But <code class="docutils literal notranslate"><span class="pre">strptime()</span></code> is independent of any platform882and thus does not necessarily support all directives available that are not883documented as supported.</p>884</dd></dl>885 886<dl class="py class">887<dt class="sig sig-object py" id="time.struct_time">888<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">time.</span></span><span class="sig-name descname"><span class="pre">struct_time</span></span><a class="headerlink" href="#time.struct_time" title="Link to this definition">¶</a></dt>889<dd><p>The type of the time value sequence returned by <a class="reference internal" href="#time.gmtime" title="time.gmtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">gmtime()</span></code></a>,890<a class="reference internal" href="#time.localtime" title="time.localtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">localtime()</span></code></a>, and <a class="reference internal" href="#time.strptime" title="time.strptime"><code class="xref py py-func docutils literal notranslate"><span class="pre">strptime()</span></code></a>. It is an object with a <a class="reference internal" href="../glossary.html#term-named-tuple"><span class="xref std std-term">named891tuple</span></a> interface: values can be accessed by index and by attribute name. The892following values are present:</p>893<table class="docutils align-default">894<tbody>895<tr class="row-odd"><td><p>Index</p></td>896<td><p>Attribute</p></td>897<td><p>Values</p></td>898</tr>899<tr class="row-even"><td><p>0</p></td>900<td><dl class="py attribute">901<dt class="sig sig-object py" id="time.struct_time.tm_year">902<span class="sig-name descname"><span class="pre">tm_year</span></span><a class="headerlink" href="#time.struct_time.tm_year" title="Link to this definition">¶</a></dt>903<dd></dd></dl>904 905</td>906<td><p>(for example, 1993)</p></td>907</tr>908<tr class="row-odd"><td><p>1</p></td>909<td><dl class="py attribute">910<dt class="sig sig-object py" id="time.struct_time.tm_mon">911<span class="sig-name descname"><span class="pre">tm_mon</span></span><a class="headerlink" href="#time.struct_time.tm_mon" title="Link to this definition">¶</a></dt>912<dd></dd></dl>913 914</td>915<td><p>range [1, 12]</p></td>916</tr>917<tr class="row-even"><td><p>2</p></td>918<td><dl class="py attribute">919<dt class="sig sig-object py" id="time.struct_time.tm_mday">920<span class="sig-name descname"><span class="pre">tm_mday</span></span><a class="headerlink" href="#time.struct_time.tm_mday" title="Link to this definition">¶</a></dt>921<dd></dd></dl>922 923</td>924<td><p>range [1, 31]</p></td>925</tr>926<tr class="row-odd"><td><p>3</p></td>927<td><dl class="py attribute">928<dt class="sig sig-object py" id="time.struct_time.tm_hour">929<span class="sig-name descname"><span class="pre">tm_hour</span></span><a class="headerlink" href="#time.struct_time.tm_hour" title="Link to this definition">¶</a></dt>930<dd></dd></dl>931 932</td>933<td><p>range [0, 23]</p></td>934</tr>935<tr class="row-even"><td><p>4</p></td>936<td><dl class="py attribute">937<dt class="sig sig-object py" id="time.struct_time.tm_min">938<span class="sig-name descname"><span class="pre">tm_min</span></span><a class="headerlink" href="#time.struct_time.tm_min" title="Link to this definition">¶</a></dt>939<dd></dd></dl>940 941</td>942<td><p>range [0, 59]</p></td>943</tr>944<tr class="row-odd"><td><p>5</p></td>945<td><dl class="py attribute">946<dt class="sig sig-object py" id="time.struct_time.tm_sec">947<span class="sig-name descname"><span class="pre">tm_sec</span></span><a class="headerlink" href="#time.struct_time.tm_sec" title="Link to this definition">¶</a></dt>948<dd></dd></dl>949 950</td>951<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 class="xref py py-func docutils literal notranslate"><span class="pre">strftime()</span></code></a></p></td>952</tr>953<tr class="row-even"><td><p>6</p></td>954<td><dl class="py attribute">955<dt class="sig sig-object py" id="time.struct_time.tm_wday">956<span class="sig-name descname"><span class="pre">tm_wday</span></span><a class="headerlink" href="#time.struct_time.tm_wday" title="Link to this definition">¶</a></dt>957<dd></dd></dl>958 959</td>960<td><p>range [0, 6]; Monday is 0</p></td>961</tr>962<tr class="row-odd"><td><p>7</p></td>963<td><dl class="py attribute">964<dt class="sig sig-object py" id="time.struct_time.tm_yday">965<span class="sig-name descname"><span class="pre">tm_yday</span></span><a class="headerlink" href="#time.struct_time.tm_yday" title="Link to this definition">¶</a></dt>966<dd></dd></dl>967 968</td>969<td><p>range [1, 366]</p></td>970</tr>971<tr class="row-even"><td><p>8</p></td>972<td><dl class="py attribute">973<dt class="sig sig-object py" id="time.struct_time.tm_isdst">974<span class="sig-name descname"><span class="pre">tm_isdst</span></span><a class="headerlink" href="#time.struct_time.tm_isdst" title="Link to this definition">¶</a></dt>975<dd></dd></dl>976 977</td>978<td><p>0, 1 or -1; see below</p></td>979</tr>980<tr class="row-odd"><td><p>N/A</p></td>981<td><dl class="py attribute">982<dt class="sig sig-object py" id="time.struct_time.tm_zone">983<span class="sig-name descname"><span class="pre">tm_zone</span></span><a class="headerlink" href="#time.struct_time.tm_zone" title="Link to this definition">¶</a></dt>984<dd></dd></dl>985 986</td>987<td><p>abbreviation of timezone name</p></td>988</tr>989<tr class="row-even"><td><p>N/A</p></td>990<td><dl class="py attribute">991<dt class="sig sig-object py" id="time.struct_time.tm_gmtoff">992<span class="sig-name descname"><span class="pre">tm_gmtoff</span></span><a class="headerlink" href="#time.struct_time.tm_gmtoff" title="Link to this definition">¶</a></dt>993<dd></dd></dl>994 995</td>996<td><p>offset east of UTC in seconds</p></td>997</tr>998</tbody>999</table>1000<p>Note that unlike the C structure, the month value is a range of [1, 12], not1001[0, 11].</p>1002<p>In calls to <a class="reference internal" href="#time.mktime" title="time.mktime"><code class="xref py py-func docutils literal notranslate"><span class="pre">mktime()</span></code></a>, <a class="reference internal" href="#time.struct_time.tm_isdst" title="time.struct_time.tm_isdst"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tm_isdst</span></code></a> may be set to 1 when daylight1003savings time is in effect, and 0 when it is not. A value of -1 indicates that1004this is not known, and will usually result in the correct state being filled in.</p>1005<p>When a tuple with an incorrect length is passed to a function expecting a1006<code class="xref py py-class docutils literal notranslate"><span class="pre">struct_time</span></code>, or having elements of the wrong type, a1007<a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised.</p>1008</dd></dl>1009 1010<dl class="py function">1011<dt class="sig sig-object py" id="time.time">1012<span class="sig-prename descclassname"><span class="pre">time.</span></span><span class="sig-name descname"><span class="pre">time</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">→</span> <span class="sig-return-typehint"><a class="reference internal" href="functions.html#float" title="float"><span class="pre">float</span></a></span></span><a class="headerlink" href="#time.time" title="Link to this definition">¶</a></dt>1013<dd><p>Return the time in seconds since the <a class="reference internal" href="#epoch">epoch</a> as a floating-point1014number. The handling of <a class="reference external" href="https://en.wikipedia.org/wiki/Leap_second">leap seconds</a> is platform dependent.1015On Windows and most Unix systems, the leap seconds are not counted towards1016the 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">Unix1017time</a>.</p>1018<p>Note that even though the time is always returned as a floating-point1019number, not all systems provide time with a better precision than 1 second.1020While this function normally returns non-decreasing values, it can return a1021lower value than a previous call if the system clock has been set back1022between the two calls.</p>1023<p>The number returned by <code class="xref py py-func docutils literal notranslate"><span class="pre">time()</span></code> may be converted into a more common1024time format (i.e. year, month, day, hour, etc…) in UTC by passing it to1025<a class="reference internal" href="#time.gmtime" title="time.gmtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">gmtime()</span></code></a> function or in local time by passing it to the1026<a class="reference internal" href="#time.localtime" title="time.localtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">localtime()</span></code></a> function. In both cases a1027<a class="reference internal" href="#time.struct_time" title="time.struct_time"><code class="xref py py-class docutils literal notranslate"><span class="pre">struct_time</span></code></a> object is returned, from which the components1028of the calendar date may be accessed as attributes.</p>1029<p>Clock:</p>1030<ul class="simple">1031<li><p>On Windows, call <code class="docutils literal notranslate"><span class="pre">GetSystemTimePreciseAsFileTime()</span></code>.</p></li>1032<li><p>Call <code class="docutils literal notranslate"><span class="pre">clock_gettime(CLOCK_REALTIME)</span></code> if available.</p></li>1033<li><p>Otherwise, call <code class="docutils literal notranslate"><span class="pre">gettimeofday()</span></code>.</p></li>1034</ul>1035<p>Use <a class="reference internal" href="#time.time_ns" title="time.time_ns"><code class="xref py py-func docutils literal notranslate"><span class="pre">time_ns()</span></code></a> to avoid the precision loss caused by the <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a>1036type.</p>1037</dd></dl>1038 1039<div class="versionchanged">1040<p><span class="versionmodified changed">Changed in version 3.13: </span>On Windows, calls <code class="docutils literal notranslate"><span class="pre">GetSystemTimePreciseAsFileTime()</span></code> instead of1041<code class="docutils literal notranslate"><span class="pre">GetSystemTimeAsFileTime()</span></code>.</p>1042</div>1043<dl class="py function">1044<dt class="sig sig-object py" id="time.time_ns">1045<span class="sig-prename descclassname"><span class="pre">time.</span></span><span class="sig-name descname"><span class="pre">time_ns</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">→</span> <span class="sig-return-typehint"><a class="reference internal" href="functions.html#int" title="int"><span class="pre">int</span></a></span></span><a class="headerlink" href="#time.time_ns" title="Link to this definition">¶</a></dt>1046<dd><p>Similar to <a class="reference internal" href="#time.time" title="time.time"><code class="xref py py-func docutils literal notranslate"><span class="pre">time()</span></code></a> but returns time as an integer number of1047nanoseconds since the <a class="reference internal" href="#epoch">epoch</a>.</p>1048<div class="versionadded">1049<p><span class="versionmodified added">Added in version 3.7.</span></p>1050</div>1051</dd></dl>1052 1053<dl class="py function">1054<dt class="sig sig-object py" id="time.thread_time">1055<span class="sig-prename descclassname"><span class="pre">time.</span></span><span class="sig-name descname"><span class="pre">thread_time</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">→</span> <span class="sig-return-typehint"><a class="reference internal" href="functions.html#float" title="float"><span class="pre">float</span></a></span></span><a class="headerlink" href="#time.thread_time" title="Link to this definition">¶</a></dt>1056<dd><p id="index-12">Return the value (in fractional seconds) of the sum of the system and user1057CPU time of the current thread. It does not include time elapsed during1058sleep. It is thread-specific by definition. The reference point of the1059returned value is undefined, so that only the difference between the results1060of two calls in the same thread is valid.</p>1061<p>Use <a class="reference internal" href="#time.thread_time_ns" title="time.thread_time_ns"><code class="xref py py-func docutils literal notranslate"><span class="pre">thread_time_ns()</span></code></a> to avoid the precision loss caused by the1062<a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a> type.</p>1063<div class="availability docutils container">1064<p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux, Unix, Windows.</p>1065<p>Unix systems supporting <code class="docutils literal notranslate"><span class="pre">CLOCK_THREAD_CPUTIME_ID</span></code>.</p>1066</div>1067<div class="versionadded">1068<p><span class="versionmodified added">Added in version 3.7.</span></p>1069</div>1070</dd></dl>1071 1072<dl class="py function">1073<dt class="sig sig-object py" id="time.thread_time_ns">1074<span class="sig-prename descclassname"><span class="pre">time.</span></span><span class="sig-name descname"><span class="pre">thread_time_ns</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">→</span> <span class="sig-return-typehint"><a class="reference internal" href="functions.html#int" title="int"><span class="pre">int</span></a></span></span><a class="headerlink" href="#time.thread_time_ns" title="Link to this definition">¶</a></dt>1075<dd><p>Similar to <a class="reference internal" href="#time.thread_time" title="time.thread_time"><code class="xref py py-func docutils literal notranslate"><span class="pre">thread_time()</span></code></a> but return time as nanoseconds.</p>1076<div class="versionadded">1077<p><span class="versionmodified added">Added in version 3.7.</span></p>1078</div>1079</dd></dl>1080 1081<dl class="py function">1082<dt class="sig sig-object py" id="time.tzset">1083<span class="sig-prename descclassname"><span class="pre">time.</span></span><span class="sig-name descname"><span class="pre">tzset</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#time.tzset" title="Link to this definition">¶</a></dt>1084<dd><p>Reset the time conversion rules used by the library routines. The environment1085variable <span class="target" id="index-13"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">TZ</span></code> specifies how this is done. It will also set the variables1086<code class="docutils literal notranslate"><span class="pre">tzname</span></code> (from the <span class="target" id="index-14"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">TZ</span></code> environment variable), <code class="docutils literal notranslate"><span class="pre">timezone</span></code> (non-DST1087seconds West of UTC), <code class="docutils literal notranslate"><span class="pre">altzone</span></code> (DST seconds west of UTC) and <code class="docutils literal notranslate"><span class="pre">daylight</span></code>1088(to 0 if this timezone does not have any daylight saving time rules, or to1089nonzero if there is a time, past, present or future when daylight saving time1090applies).</p>1091<div class="availability docutils container">1092<p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p>1093</div>1094<div class="admonition note">1095<p class="admonition-title">Note</p>1096<p>Although in many cases, changing the <span class="target" id="index-15"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">TZ</span></code> environment variable may1097affect the output of functions like <a class="reference internal" href="#time.localtime" title="time.localtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">localtime()</span></code></a> without calling1098<code class="xref py py-func docutils literal notranslate"><span class="pre">tzset()</span></code>, this behavior should not be relied on.</p>1099<p>The <span class="target" id="index-16"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">TZ</span></code> environment variable should contain no whitespace.</p>1100</div>1101<p>The standard format of the <span class="target" id="index-17"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">TZ</span></code> environment variable is (whitespace1102added for clarity):</p>1103<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">std</span> <span class="n">offset</span> <span class="p">[</span><span class="n">dst</span> <span class="p">[</span><span class="n">offset</span> <span class="p">[,</span><span class="n">start</span><span class="p">[</span><span class="o">/</span><span class="n">time</span><span class="p">],</span> <span class="n">end</span><span class="p">[</span><span class="o">/</span><span class="n">time</span><span class="p">]]]]</span>1104</pre></div>1105</div>1106<p>Where the components are:</p>1107<dl>1108<dt><code class="docutils literal notranslate"><span class="pre">std</span></code> and <code class="docutils literal notranslate"><span class="pre">dst</span></code></dt><dd><p>Three or more alphanumerics giving the timezone abbreviations. These will be1109propagated into time.tzname</p>1110</dd>1111<dt><code class="docutils literal notranslate"><span class="pre">offset</span></code></dt><dd><p>The offset has the form: <code class="docutils literal notranslate"><span class="pre">±</span> <span class="pre">hh[:mm[:ss]]</span></code>. This indicates the value1112added the local time to arrive at UTC. If preceded by a ‘-’, the timezone1113is east of the Prime Meridian; otherwise, it is west. If no offset follows1114dst, summer time is assumed to be one hour ahead of standard time.</p>1115</dd>1116<dt><code class="docutils literal notranslate"><span class="pre">start[/time],</span> <span class="pre">end[/time]</span></code></dt><dd><p>Indicates when to change to and back from DST. The format of the1117start and end dates are one of the following:</p>1118<dl class="simple">1119<dt><code class="samp docutils literal notranslate"><span class="pre">J</span><em><span class="pre">n</span></em></code></dt><dd><p>The Julian day <em>n</em> (1 <= <em>n</em> <= 365). Leap days are not counted, so in1120all years February 28 is day 59 and March 1 is day 60.</p>1121</dd>1122<dt><code class="samp docutils literal notranslate"><em><span class="pre">n</span></em></code></dt><dd><p>The zero-based Julian day (0 <= <em>n</em> <= 365). Leap days are counted, and1123it is possible to refer to February 29.</p>1124</dd>1125<dt><code class="samp docutils literal notranslate"><span class="pre">M</span><em><span class="pre">m</span></em><span class="pre">.</span><em><span class="pre">n</span></em><span class="pre">.</span><em><span class="pre">d</span></em></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 (11126<= <em>n</em> <= 5, 1 <= <em>m</em> <= 12, where week 5 means “the last <em>d</em> day in1127month <em>m</em>” which may occur in either the fourth or the fifth1128week). Week 1 is the first week in which the <em>d</em>’th day occurs. Day1129zero is a Sunday.</p>1130</dd>1131</dl>1132<p><code class="docutils literal notranslate"><span class="pre">time</span></code> has the same format as <code class="docutils literal notranslate"><span class="pre">offset</span></code> except that no leading sign1133(‘-’ or ‘+’) is allowed. The default, if time is not given, is 02:00:00.</p>1134</dd>1135</dl>1136<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s1">'TZ'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'EST+05EDT,M4.1.0,M10.5.0'</span>1137<span class="gp">>>> </span><span class="n">time</span><span class="o">.</span><span class="n">tzset</span><span class="p">()</span>1138<span class="gp">>>> </span><span class="n">time</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s1">'</span><span class="si">%X</span><span class="s1"> </span><span class="si">%x</span><span class="s1"> %Z'</span><span class="p">)</span>1139<span class="go">'02:07:36 05/08/03 EDT'</span>1140<span class="gp">>>> </span><span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s1">'TZ'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'AEST-10AEDT-11,M10.5.0,M3.5.0'</span>1141<span class="gp">>>> </span><span class="n">time</span><span class="o">.</span><span class="n">tzset</span><span class="p">()</span>1142<span class="gp">>>> </span><span class="n">time</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s1">'</span><span class="si">%X</span><span class="s1"> </span><span class="si">%x</span><span class="s1"> %Z'</span><span class="p">)</span>1143<span class="go">'16:08:12 05/08/03 AEST'</span>1144</pre></div>1145</div>1146<p>On many Unix systems (including *BSD, Linux, Solaris, and Darwin), it is more1147convenient 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 to1148specify the timezone rules. To do this, set the <span class="target" id="index-18"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">TZ</span></code> environment1149variable to the path of the required timezone datafile, relative to the root of1150the systems ‘zoneinfo’ timezone database, usually located at1151<code class="file docutils literal notranslate"><span class="pre">/usr/share/zoneinfo</span></code>. For example, <code class="docutils literal notranslate"><span class="pre">'US/Eastern'</span></code>,1152<code class="docutils literal notranslate"><span class="pre">'Australia/Melbourne'</span></code>, <code class="docutils literal notranslate"><span class="pre">'Egypt'</span></code> or <code class="docutils literal notranslate"><span class="pre">'Europe/Amsterdam'</span></code>.</p>1153<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s1">'TZ'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'US/Eastern'</span>1154<span class="gp">>>> </span><span class="n">time</span><span class="o">.</span><span class="n">tzset</span><span class="p">()</span>1155<span class="gp">>>> </span><span class="n">time</span><span class="o">.</span><span class="n">tzname</span>1156<span class="go">('EST', 'EDT')</span>1157<span class="gp">>>> </span><span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s1">'TZ'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'Egypt'</span>1158<span class="gp">>>> </span><span class="n">time</span><span class="o">.</span><span class="n">tzset</span><span class="p">()</span>1159<span class="gp">>>> </span><span class="n">time</span><span class="o">.</span><span class="n">tzname</span>1160<span class="go">('EET', 'EEST')</span>1161</pre></div>1162</div>1163</dd></dl>1164 1165</section>1166<section id="clock-id-constants">1167<span id="time-clock-id-constants"></span><h2>Clock ID Constants<a class="headerlink" href="#clock-id-constants" title="Link to this heading">¶</a></h2>1168<p>These constants are used as parameters for <a class="reference internal" href="#time.clock_getres" title="time.clock_getres"><code class="xref py py-func docutils literal notranslate"><span class="pre">clock_getres()</span></code></a> and1169<a class="reference internal" href="#time.clock_gettime" title="time.clock_gettime"><code class="xref py py-func docutils literal notranslate"><span class="pre">clock_gettime()</span></code></a>.</p>1170<dl class="py data">1171<dt class="sig sig-object py" id="time.CLOCK_BOOTTIME">1172<span class="sig-prename descclassname"><span class="pre">time.</span></span><span class="sig-name descname"><span class="pre">CLOCK_BOOTTIME</span></span><a class="headerlink" href="#time.CLOCK_BOOTTIME" title="Link to this definition">¶</a></dt>1173<dd><p>Identical to <a class="reference internal" href="#time.CLOCK_MONOTONIC" title="time.CLOCK_MONOTONIC"><code class="xref py py-data docutils literal notranslate"><span class="pre">CLOCK_MONOTONIC</span></code></a>, except it also includes any time that1174the system is suspended.</p>1175<p>This allows applications to get a suspend-aware monotonic clock without1176having to deal with the complications of <a class="reference internal" href="#time.CLOCK_REALTIME" title="time.CLOCK_REALTIME"><code class="xref py py-data docutils literal notranslate"><span class="pre">CLOCK_REALTIME</span></code></a>, which may1177have discontinuities if the time is changed using <code class="docutils literal notranslate"><span class="pre">settimeofday()</span></code> or1178similar.</p>1179<div class="availability docutils container">1180<p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux >= 2.6.39.</p>1181</div>1182<div class="versionadded">1183<p><span class="versionmodified added">Added in version 3.7.</span></p>1184</div>1185</dd></dl>1186 1187<dl class="py data">1188<dt class="sig sig-object py" id="time.CLOCK_HIGHRES">1189<span class="sig-prename descclassname"><span class="pre">time.</span></span><span class="sig-name descname"><span class="pre">CLOCK_HIGHRES</span></span><a class="headerlink" href="#time.CLOCK_HIGHRES" title="Link to this definition">¶</a></dt>1190<dd><p>The Solaris OS has a <code class="docutils literal notranslate"><span class="pre">CLOCK_HIGHRES</span></code> timer that attempts to use an optimal1191hardware source, and may give close to nanosecond resolution.1192<code class="docutils literal notranslate"><span class="pre">CLOCK_HIGHRES</span></code> is the nonadjustable, high-resolution clock.</p>1193<div class="availability docutils container">1194<p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Solaris.</p>1195</div>1196<div class="versionadded">1197<p><span class="versionmodified added">Added in version 3.3.</span></p>1198</div>1199</dd></dl>1200 