📄 module-time.html
字号:
<P>
<dl><dt><b><a name='l2h-1137'><tt class='function'>localtime</tt></a></b> (<var>secs</var>)
<dd>
Like <tt class="function">gmtime()</tt> but converts to local time. The dst flag is
set to <code>1</code> when DST applies to the given time.
</dl>
<P>
<dl><dt><b><a name='l2h-1138'><tt class='function'>mktime</tt></a></b> (<var>tuple</var>)
<dd>
This is the inverse function of <tt class="function">localtime()</tt>. Its argument
is the full 9-tuple (since the dst flag is needed; use <code>-1</code> as
the dst flag if it is unknown) which expresses the time in
<i>local</i> time, not UTC. It returns a floating point number, for
compatibility with <tt class="function">time()</tt>. If the input value cannot be
represented as a valid time, <tt class="exception">OverflowError</tt> is raised.
</dl>
<P>
<dl><dt><b><a name='l2h-1139'><tt class='function'>sleep</tt></a></b> (<var>secs</var>)
<dd>
Suspend execution for the given number of seconds. The argument may
be a floating point number to indicate a more precise sleep time.
The actual suspension time may be less than that requested because any
caught signal will terminate the <tt class="function">sleep()</tt> following
execution of that signal's catching routine. Also, the suspension
time may be longer than requested by an arbitrary amount because of
the scheduling of other activity in the system.
</dl>
<P>
<dl><dt><b><a name='l2h-1140'><tt class='function'>strftime</tt></a></b> (<var>format, tuple</var>)
<dd>
Convert a tuple representing a time as returned by <tt class="function">gmtime()</tt>
or <tt class="function">localtime()</tt> to a string as specified by the <var>format</var>
argument. <var>format</var> must be a string.
<P>
The following directives can be embedded in the <var>format</var> string.
They are shown without the optional field width and precision
specification, and are replaced by the indicated characters in the
<tt class="function">strftime()</tt> result:
<P>
<table border align="center" style="border-collapse: collapse">
<thead>
<tr>
<th align="center"><b>Directive</b> </th>
<th align="left"><b>Meaning</b> </th>
<th align="center"><b>Notes</b> </th>
</thead>
<tbody valign='baseline'>
<tr><td align="center" valign="baseline"><code>%a</code></td>
<td align="left">Locale's abbreviated weekday name.</td>
<td align="center"> </td>
<tr><td align="center" valign="baseline"><code>%A</code></td>
<td align="left">Locale's full weekday name.</td>
<td align="center"> </td>
<tr><td align="center" valign="baseline"><code>%b</code></td>
<td align="left">Locale's abbreviated month name.</td>
<td align="center"> </td>
<tr><td align="center" valign="baseline"><code>%B</code></td>
<td align="left">Locale's full month name.</td>
<td align="center"> </td>
<tr><td align="center" valign="baseline"><code>%c</code></td>
<td align="left">Locale's appropriate date and time representation.</td>
<td align="center"> </td>
<tr><td align="center" valign="baseline"><code>%d</code></td>
<td align="left">Day of the month as a decimal number [01,31].</td>
<td align="center"> </td>
<tr><td align="center" valign="baseline"><code>%H</code></td>
<td align="left">Hour (24-hour clock) as a decimal number [00,23].</td>
<td align="center"> </td>
<tr><td align="center" valign="baseline"><code>%I</code></td>
<td align="left">Hour (12-hour clock) as a decimal number [01,12].</td>
<td align="center"> </td>
<tr><td align="center" valign="baseline"><code>%j</code></td>
<td align="left">Day of the year as a decimal number [001,366].</td>
<td align="center"> </td>
<tr><td align="center" valign="baseline"><code>%m</code></td>
<td align="left">Month as a decimal number [01,12].</td>
<td align="center"> </td>
<tr><td align="center" valign="baseline"><code>%M</code></td>
<td align="left">Minute as a decimal number [00,59].</td>
<td align="center"> </td>
<tr><td align="center" valign="baseline"><code>%p</code></td>
<td align="left">Locale's equivalent of either AM or PM.</td>
<td align="center"> </td>
<tr><td align="center" valign="baseline"><code>%S</code></td>
<td align="left">Second as a decimal number [00,61].</td>
<td align="center">(1)</td>
<tr><td align="center" valign="baseline"><code>%U</code></td>
<td align="left">Week number of the year (Sunday as the first day of the
week) as a decimal number [00,53]. All days in a new year
preceding the first Sunday are considered to be in week 0.</td>
<td align="center"> </td>
<tr><td align="center" valign="baseline"><code>%w</code></td>
<td align="left">Weekday as a decimal number [0(Sunday),6].</td>
<td align="center"> </td>
<tr><td align="center" valign="baseline"><code>%W</code></td>
<td align="left">Week number of the year (Monday as the first day of the
week) as a decimal number [00,53]. All days in a new year
preceding the first Sunday are considered to be in week 0.</td>
<td align="center"> </td>
<tr><td align="center" valign="baseline"><code>%x</code></td>
<td align="left">Locale's appropriate date representation.</td>
<td align="center"> </td>
<tr><td align="center" valign="baseline"><code>%X</code></td>
<td align="left">Locale's appropriate time representation.</td>
<td align="center"> </td>
<tr><td align="center" valign="baseline"><code>%y</code></td>
<td align="left">Year without century as a decimal number [00,99].</td>
<td align="center"> </td>
<tr><td align="center" valign="baseline"><code>%Y</code></td>
<td align="left">Year with century as a decimal number.</td>
<td align="center"> </td>
<tr><td align="center" valign="baseline"><code>%Z</code></td>
<td align="left">Time zone name (or by no characters if no time zone exists).</td>
<td align="center"> </td>
<tr><td align="center" valign="baseline"><code>%%</code></td>
<td align="left">A literal "<tt class="character">%</tt>" character.</td>
<td align="center"> </td></tbody>
</table>
<P>
Notes:
<P>
<DL>
<DT><STRONG>(1)</STRONG></DT>
<DD>The range really is <code>0</code> to <code>61</code>; this accounts for leap
seconds and the (very rare) double leap seconds.
</DD>
</DL>
<P>
Additional directives may be supported on certain platforms, but
only the ones listed here have a meaning standardized by ANSI C.
<P>
On some platforms, an optional field width and precision
specification can immediately follow the initial "<tt class="character">%</tt>" of a
directive in the following order; this is also not portable.
The field width is normally 2 except for <code>%j</code> where it is 3.
</dl>
<P>
<dl><dt><b><a name='l2h-1141'><tt class='function'>strptime</tt></a></b> (<var>string</var><big>[</big><var>, format</var><big>]</big>)
<dd>
Parse a string representing a time according to a format. The return
value is a tuple as returned by <tt class="function">gmtime()</tt> or
<tt class="function">localtime()</tt>. The <var>format</var> parameter uses the same
directives as those used by <tt class="function">strftime()</tt>; it defaults to
<code>"%a %b %d %H:%M:%S %Y"</code> which matches the formatting
returned by <tt class="function">ctime()</tt>. The same platform caveats apply; see
the local Unix documentation for restrictions or additional
supported directives. If <var>string</var> cannot be parsed according to
<var>format</var>, <tt class="exception">ValueError</tt> is raised. Values which are not
provided as part of the input string are filled in with default
values; the specific values are platform-dependent as the XPG standard
does not provide sufficient information to constrain the result.
<P>
<b>Note:</b> This function relies entirely on the underlying
platform's C library for the date parsing, and some of these libraries
are buggy. There's nothing to be done about this short of a new,
portable implementation of <tt class="cfunction">strptime()</tt>.
<P>
Availability: Most modern Unix systems.
</dl>
<P>
<dl><dt><b><a name='l2h-1142'><tt class='function'>time</tt></a></b> ()
<dd>
Return the time as a floating point number expressed in seconds since
the epoch, in UTC. Note that even though the time is always returned
as a floating point number, not all systems provide time with a better
precision than 1 second.
</dl>
<P>
<dl><dt><b><a name='l2h-1143'><tt>timezone</tt></a></b>
<dd>
The offset of the local (non-DST) timezone, in seconds west of UTC
(i.e. negative in most of Western Europe, positive in the US, zero in
the UK).
</dl>
<P>
<dl><dt><b><a name='l2h-1144'><tt>tzname</tt></a></b>
<dd>
A tuple of two strings: the first is the name of the local non-DST
timezone, the second is the name of the local DST timezone. If no DST
timezone is defined, the second string should not be used.
</dl>
<P>
<div class='seealso'>
<p class='heading'><b>See Also:</b></p>
<dl compact class="seemodule">
<dt>Module <b><tt class='module'><a href="module-locale.html" tppabs="http://www.python.org/doc/current/lib/module-locale.html">locale</a></tt>:</b>
<dd>Internationalization services. The locale
settings can affect the return values for some of
the functions in the <tt class="module">time</tt> module.
</dl>
</div>
<DIV CLASS="navigation"><p><hr><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="popen3-objects.html" tppabs="http://www.python.org/doc/current/lib/popen3-objects.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="allos.html" tppabs="http://www.python.org/doc/current/lib/allos.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="module-sched.html" tppabs="http://www.python.org/doc/current/lib/module-sched.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="popen3-objects.html" tppabs="http://www.python.org/doc/current/lib/popen3-objects.html">6.8.1 Popen3 and Popen4</A>
<b class="navlabel">Up:</b> <a class="sectref" href="allos.html" tppabs="http://www.python.org/doc/current/lib/allos.html">6. Generic Operating System</A>
<b class="navlabel">Next:</b> <a class="sectref" href="module-sched.html" tppabs="http://www.python.org/doc/current/lib/module-sched.html">6.10 sched </A>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>See <i><a href="about.html" tppabs="http://www.python.org/doc/current/lib/about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -