local.html

来自「perl教程」· HTML 代码 · 共 200 行

HTML
200
字号
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>Time::Local - efficiently compute time from local and GMT time</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>

<body>

<script>writelinks('__top__',2);</script>
<h1><a>Time::Local - efficiently compute time from local and GMT time</a></h1>
<p><a name="__index__"></a></p>

<!-- INDEX BEGIN -->

<ul>

	<li><a href="#name">NAME</a></li>
	<li><a href="#synopsis">SYNOPSIS</a></li>
	<li><a href="#description">DESCRIPTION</a></li>
	<ul>

		<li><a href="#ambiguous_local_times__dst_">Ambiguous Local Times (DST)</a></li>
		<li><a href="#nonexistent_local_times__dst_">Non-Existent Local Times (DST)</a></li>
		<li><a href="#negative_epoch_values">Negative Epoch Values</a></li>
	</ul>

	<li><a href="#implementation">IMPLEMENTATION</a></li>
	<li><a href="#bugs">BUGS</a></li>
	<li><a href="#support">SUPPORT</a></li>
	<li><a href="#author">AUTHOR</a></li>
</ul>
<!-- INDEX END -->

<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>Time::Local - efficiently compute time from local and GMT time</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
    <span class="variable">$time</span> <span class="operator">=</span> <span class="variable">timelocal</span><span class="operator">(</span><span class="variable">$sec</span><span class="operator">,</span><span class="variable">$min</span><span class="operator">,</span><span class="variable">$hour</span><span class="operator">,</span><span class="variable">$mday</span><span class="operator">,</span><span class="variable">$mon</span><span class="operator">,</span><span class="variable">$year</span><span class="operator">);</span>
    <span class="variable">$time</span> <span class="operator">=</span> <span class="variable">timegm</span><span class="operator">(</span><span class="variable">$sec</span><span class="operator">,</span><span class="variable">$min</span><span class="operator">,</span><span class="variable">$hour</span><span class="operator">,</span><span class="variable">$mday</span><span class="operator">,</span><span class="variable">$mon</span><span class="operator">,</span><span class="variable">$year</span><span class="operator">);</span>
</pre>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>These routines are the inverse of built-in perl functions <a href="../../lib/Pod/perlfunc.html#item_localtime"><code>localtime()</code></a>
and gmtime().  They accept a date as a six-element array, and return
the corresponding <a href="../../lib/Pod/perlfunc.html#item_time"><code>time(2)</code></a> value in seconds since the system epoch
(Midnight, January 1, 1970 GMT on Unix, for example).  This value can
be positive or negative, though POSIX only requires support for
positive values, so dates before the system's epoch may not work on
all operating systems.</p>
<p>It is worth drawing particular attention to the expected ranges for
the values provided.  The value for the day of the month is the actual day
(ie 1..31), while the month is the number of months since January (0..11).
This is consistent with the values returned from <a href="../../lib/Pod/perlfunc.html#item_localtime"><code>localtime()</code></a> and gmtime().</p>
<p>The <code>timelocal()</code> and <code>timegm()</code> functions perform range checking on the
input $sec, $min, $hour, $mday, and $mon values by default.  If you'd
rather they didn't, you can explicitly import the <code>timelocal_nocheck()</code>
and <code>timegm_nocheck()</code> functions.</p>
<pre>
        <span class="keyword">use</span> <span class="variable">Time::Local</span> <span class="string">'timelocal_nocheck'</span><span class="operator">;</span>
</pre>
<pre>
        <span class="operator">{</span>
            <span class="comment"># The 365th day of 1999</span>
            <span class="keyword">print</span> <span class="keyword">scalar</span> <span class="keyword">localtime</span> <span class="variable">timelocal_nocheck</span> <span class="number">0</span><span class="operator">,</span><span class="number">0</span><span class="operator">,</span><span class="number">0</span><span class="operator">,</span><span class="number">365</span><span class="operator">,</span><span class="number">0</span><span class="operator">,</span><span class="number">99</span><span class="operator">;</span>
</pre>
<pre>
            <span class="comment"># The twenty thousandth day since 1970</span>
            <span class="keyword">print</span> <span class="keyword">scalar</span> <span class="keyword">localtime</span> <span class="variable">timelocal_nocheck</span> <span class="number">0</span><span class="operator">,</span><span class="number">0</span><span class="operator">,</span><span class="number">0</span><span class="operator">,</span><span class="number">20000</span><span class="operator">,</span><span class="number">0</span><span class="operator">,</span><span class="number">70</span><span class="operator">;</span>
</pre>
<pre>
            <span class="comment"># And even the 10,000,000th second since 1999!</span>
            <span class="keyword">print</span> <span class="keyword">scalar</span> <span class="keyword">localtime</span> <span class="variable">timelocal_nocheck</span> <span class="number">10000000</span><span class="operator">,</span><span class="number">0</span><span class="operator">,</span><span class="number">0</span><span class="operator">,</span><span class="number">1</span><span class="operator">,</span><span class="number">0</span><span class="operator">,</span><span class="number">99</span><span class="operator">;</span>
                    <span class="operator">}</span>
</pre>
<p>Your mileage may vary when trying these with minutes and hours,
and it doesn't work at all for months.</p>
<p>Strictly speaking, the year should also be specified in a form consistent
with localtime(), i.e. the offset from 1900.
In order to make the interpretation of the year easier for humans,
however, who are more accustomed to seeing years as two-digit or four-digit
values, the following conventions are followed:</p>
<ul>
<li>
<p>Years greater than 999 are interpreted as being the actual year,
rather than the offset from 1900.  Thus, 1964 would indicate the year
Martin Luther King won the Nobel prize, not the year 3864.</p>
</li>
<li>
<p>Years in the range 100..999 are interpreted as offset from 1900, 
so that 112 indicates 2012.  This rule also applies to years less than zero
(but see note below regarding date range).</p>
</li>
<li>
<p>Years in the range 0..99 are interpreted as shorthand for years in the
rolling &quot;current century,&quot; defined as 50 years on either side of the current
year.  Thus, today, in 1999, 0 would refer to 2000, and 45 to 2045,
but 55 would refer to 1955.  Twenty years from now, 55 would instead refer
to 2055.  This is messy, but matches the way people currently think about
two digit dates.  Whenever possible, use an absolute four digit year instead.</p>
</li>
</ul>
<p>The scheme above allows interpretation of a wide range of dates, particularly
if 4-digit years are used.</p>
<p>Please note, however, that the range of dates that can be actually be handled
depends on the size of an integer (time_t) on a given platform.  
Currently, this is 32 bits for most systems, yielding an approximate range 
from Dec 1901 to Jan 2038.</p>
<p>Both <code>timelocal()</code> and <code>timegm()</code> croak if given dates outside the supported
range.</p>
<p>
</p>
<h2><a name="ambiguous_local_times__dst_">Ambiguous Local Times (DST)</a></h2>
<p>Because of DST changes, there are many time zones where the same local
time occurs for two different GMT times on the same day.  For example,
in the &quot;Europe/Paris&quot; time zone, the local time of 2001-10-28 02:30:00
can represent either 2001-10-28 00:30:00 GMT, <strong>or</strong> 2001-10-28
01:30:00 GMT.</p>
<p>When given an ambiguous local time, the <code>timelocal()</code> function should
always return the epoch for the <em>earlier</em> of the two possible GMT
times.</p>
<p>
</p>
<h2><a name="nonexistent_local_times__dst_">Non-Existent Local Times (DST)</a></h2>
<p>When a DST change causes a locale clock to skip one hour forward,
there will be an hour's worth of local times that don't exist.  Again,
for the &quot;Europe/Paris&quot; time zone, the local clock jumped from
2001-03-25 01:59:59 to 2001-03-25 03:00:00.</p>
<p>If the <code>timelocal()</code> function is given a non-existent local time, it
will simply return an epoch value for the time one hour later.</p>
<p>
</p>
<h2><a name="negative_epoch_values">Negative Epoch Values</a></h2>
<p>Negative epoch (time_t) values are not officially supported by the
POSIX standards, so this module's tests do not test them.  On some
systems, they are known not to work.  These include MacOS (pre-OSX)
and Win32.</p>
<p>On systems which do support negative epoch values, this module should
be able to cope with dates before the start of the epoch, down the
minimum value of time_t for the system.</p>
<p>
</p>
<hr />
<h1><a name="implementation">IMPLEMENTATION</a></h1>
<p>These routines are quite efficient and yet are always guaranteed to agree
with <a href="../../lib/Pod/perlfunc.html#item_localtime"><code>localtime()</code></a> and gmtime().  We manage this by caching the start times
of any months we've seen before.  If we know the start time of the month,
we can always calculate any time within the month.  The start times
are calculated using a mathematical formula. Unlike other algorithms
that do multiple calls to gmtime().</p>
<p><code>timelocal()</code> is implemented using the same cache.  We just assume that we're
translating a GMT time, and then fudge it when we're done for the timezone
and daylight savings arguments.  Note that the timezone is evaluated for
each date because countries occasionally change their official timezones.
Assuming that <a href="../../lib/Pod/perlfunc.html#item_localtime"><code>localtime()</code></a> corrects for these changes, this routine will
also be correct.</p>
<p>
</p>
<hr />
<h1><a name="bugs">BUGS</a></h1>
<p>The whole scheme for interpreting two-digit years can be considered a bug.</p>
<p>
</p>
<hr />
<h1><a name="support">SUPPORT</a></h1>
<p>Support for this module is provided via the <a href="mailto:datetime@perl.org">datetime@perl.org</a>
email list.  See <a href="http://lists.perl.org/">http://lists.perl.org/</a> for more details.</p>
<p>Please submit bugs using the RT system at rt.cpan.org, or as a last
resort, to the <a href="mailto:datetime@perl.org">datetime@perl.org</a> list.</p>
<p>
</p>
<hr />
<h1><a name="author">AUTHOR</a></h1>
<p>This module is based on a Perl 4 library, timelocal.pl, that was
included with Perl 4.036, and was most likely written by Tom
Christiansen.</p>
<p>The current version was written by Graham Barr.</p>
<p>It is now being maintained separately from the Perl core by Dave
Rolsky, &lt;<a href="mailto:autarch@urth.org">autarch@urth.org</a>&gt;.</p>

</body>

</html>

⌨️ 快捷键说明

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