⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 module-time.html

📁 一本很好的python的说明书,适合对python感兴趣的人
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>6.9 time -- Time access and conversions</title>
<META NAME="description" CONTENT="6.9 time -- Time access and conversions">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="lib.css" tppabs="http://www.python.org/doc/current/lib/lib.css">
<LINK REL="next" href="module-sched.html" tppabs="http://www.python.org/doc/current/lib/module-sched.html">
<LINK REL="previous" href="module-popen2.html" tppabs="http://www.python.org/doc/current/lib/module-popen2.html">
<LINK REL="up" href="allos.html" tppabs="http://www.python.org/doc/current/lib/allos.html">
<LINK REL="next" href="module-sched.html" tppabs="http://www.python.org/doc/current/lib/module-sched.html">
</head>
<body>
<DIV CLASS="navigation"><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>
<br><hr></DIV>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION008900000000000000000">
6.9 <tt class="module">time</tt> --
         Time access and conversions</A>
</H1>

<P>


<P>
This module provides various time-related functions.
It is always available, but not all functions are available
on all platforms.

<P>
An explanation of some terminology and conventions is in order.

<P>

<UL>
<LI>The <i class="dfn">epoch</i> is the point where the time starts.  On
January 1st of that year, at 0 hours, the ``time since the epoch'' is
zero.  For Unix, the epoch is 1970.  To find out what the epoch is,
look at <code>gmtime(0)</code>.

<P>
</LI>
<LI>The functions in this module do not handle dates and times before the
epoch or far in the future.  The cut-off point in the future is
determined by the C library; for Unix, it is typically in
2038.

<P>
</LI>
<LI><b>Year 2000 (Y2K) issues</b>:  Python
depends on the platform's C library, which generally doesn't have year
2000 issues, since all dates and times are represented internally as
seconds since the epoch.  Functions accepting a time tuple (see below)
generally require a 4-digit year.  For backward compatibility, 2-digit
years are supported if the module variable <code>accept2dyear</code> is a
non-zero integer; this variable is initialized to <code>1</code> unless the
environment variable <a class="envvar" name='l2h-1148'>$PYTHONY2K</a> is set to a non-empty string,
in which case it is initialized to <code>0</code>.  Thus, you can set
<a class="envvar" name='l2h-1149'>$PYTHONY2K</a> to a non-empty string in the environment to require 4-digit
years for all year input.  When 2-digit years are accepted, they are
converted according to the POSIX or X/Open standard: values 69-99
are mapped to 1969-1999, and values 0-68 are mapped to 2000-2068.
Values 100-1899 are always illegal.  Note that this is new as of
Python 1.5.2(a2); earlier versions, up to Python 1.5.1 and 1.5.2a1,
would add 1900 to year values below 1900.

<P>
</LI>
<LI>UTC is Coordinated Universal Time (formerly known as Greenwich Mean
Time, or GMT).  The acronym UTC is not a
mistake but a compromise between English and French.

<P>
</LI>
<LI>DST is Daylight Saving Time, an adjustment
of the timezone by (usually) one hour during part of the year.  DST
rules are magic (determined by local law) and can change from year to
year.  The C library has a table containing the local rules (often it
is read from a system file for flexibility) and is the only source of
True Wisdom in this respect.

<P>
</LI>
<LI>The precision of the various real-time functions may be less than
suggested by the units in which their value or argument is expressed.
E.g. on most Unix systems, the clock ``ticks'' only 50 or 100 times a
second, and on the Mac, times are only accurate to whole seconds.

<P>
</LI>
<LI>On the other hand, the precision of <tt class="function">time()</tt> and
<tt class="function">sleep()</tt> is better than their Unix equivalents: times are
expressed as floating point numbers, <tt class="function">time()</tt> returns the
most accurate time available (using Unix <tt class="cfunction">gettimeofday()</tt>
where available), and <tt class="function">sleep()</tt> will accept a time with a
nonzero fraction (Unix <tt class="cfunction">select()</tt> is used to implement
this, where available).

<P>
</LI>
<LI><P>
The time tuple as returned by <tt class="function">gmtime()</tt>,
<tt class="function">localtime()</tt>, and <tt class="function">strptime()</tt>, and accepted by
<tt class="function">asctime()</tt>, <tt class="function">mktime()</tt> and <tt class="function">strftime()</tt>,
is a tuple of 9 integers:

<P>
<table border align="center" style="border-collapse: collapse">
  <thead>
    <tr>
      <th align="right"><b>Index</b>&nbsp;</th>
      <th align="left"><b>Field</b>&nbsp;</th>
      <th align="left"><b>Values</b>&nbsp;</th>
    </thead>
  <tbody valign='baseline'>
    <tr><td align="right" valign="baseline"><code>0</code>&nbsp;</td>
        <td align="left">year</td>
        <td align="left">(e.g. 1993)</td>
    <tr><td align="right" valign="baseline"><code>1</code>&nbsp;</td>
        <td align="left">month</td>
        <td align="left">range [1,12]</td>
    <tr><td align="right" valign="baseline"><code>2</code>&nbsp;</td>
        <td align="left">day</td>
        <td align="left">range [1,31]</td>
    <tr><td align="right" valign="baseline"><code>3</code>&nbsp;</td>
        <td align="left">hour</td>
        <td align="left">range [0,23]</td>
    <tr><td align="right" valign="baseline"><code>4</code>&nbsp;</td>
        <td align="left">minute</td>
        <td align="left">range [0,59]</td>
    <tr><td align="right" valign="baseline"><code>5</code>&nbsp;</td>
        <td align="left">second</td>
        <td align="left">range [0,61]; see <b>(1)</b> in <tt class="function">strftime()</tt> description</td>
    <tr><td align="right" valign="baseline"><code>6</code>&nbsp;</td>
        <td align="left">weekday</td>
        <td align="left">range [0,6], Monday is 0</td>
    <tr><td align="right" valign="baseline"><code>7</code>&nbsp;</td>
        <td align="left">Julian day</td>
        <td align="left">range [1,366]</td>
    <tr><td align="right" valign="baseline"><code>8</code>&nbsp;</td>
        <td align="left">daylight savings flag</td>
        <td align="left">0, 1 or -1; see below</td></tbody>
</table>

<P>
Note that unlike the C structure, the month value is a
range of 1-12, not 0-11.  A year value will be handled as described
under ``Year 2000 (Y2K) issues'' above.  A <code>-1</code> argument as
daylight savings flag, passed to <tt class="function">mktime()</tt> will usually
result in the correct daylight savings state to be filled in.

<P>
</LI>
</UL>

<P>
The module defines the following functions and data items:

<P>
<dl><dt><b><a name='l2h-1130'><tt>accept2dyear</tt></a></b>
<dd>
Boolean value indicating whether two-digit year values will be
accepted.  This is true by default, but will be set to false if the
environment variable <a class="envvar" name='l2h-1154'>$PYTHONY2K</a> has been set to a non-empty
string.  It may also be modified at run time.
</dl>

<P>
<dl><dt><b><a name='l2h-1131'><tt>altzone</tt></a></b>
<dd>
The offset of the local DST timezone, in seconds west of UTC, if one
is defined.  This is negative if the local DST timezone is east of UTC
(as in Western Europe, including the UK).  Only use this if
<code>daylight</code> is nonzero.
</dl>

<P>
<dl><dt><b><a name='l2h-1132'><tt class='function'>asctime</tt></a></b> (<var>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 24-character string of the following form:
<code>'Sun Jun 20 23:21:05 1993'</code>.  Note: unlike the C function of
the same name, there is no trailing newline.
</dl>

<P>
<dl><dt><b><a name='l2h-1133'><tt class='function'>clock</tt></a></b> ()
<dd>
Return the current CPU time as a floating point number expressed in
seconds.  The precision, and in fact the very definition of the meaning
of ``CPU time'', depends on that of the C function
of the same name, but in any case, this is the function to use for
benchmarking Python or timing algorithms.
</dl>

<P>
<dl><dt><b><a name='l2h-1134'><tt class='function'>ctime</tt></a></b> (<var>secs</var>)
<dd>
Convert a time expressed in seconds since the epoch to a string
representing local time.  <code>ctime(<var>secs</var>)</code> is equivalent to
<code>asctime(localtime(<var>secs</var>))</code>.
</dl>

<P>
<dl><dt><b><a name='l2h-1135'><tt>daylight</tt></a></b>
<dd>
Nonzero if a DST timezone is defined.
</dl>

<P>
<dl><dt><b><a name='l2h-1136'><tt class='function'>gmtime</tt></a></b> (<var>secs</var>)
<dd>
Convert a time expressed in seconds since the epoch to a time tuple
in UTC in which the dst flag is always zero.  Fractions of a second are
ignored.  See above for a description of the tuple lay-out.
</dl>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -