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

📄 ch03_01.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
📖 第 1 页 / 共 2 页
字号:
ROWSPAN="1"COLSPAN="1"><PCLASS="para">day of month</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">1-31</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">&#13;<PRECLASS="programlisting">$month</PRE></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">month of year</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">0-11, 0 == January</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">&#13;<PRECLASS="programlisting">$year</PRE></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">years since 1900</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">1-138 (or more)</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">&#13;<PRECLASS="programlisting">$wday</PRE></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">day of week</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">0-6, 0 == Sunday</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">&#13;<PRECLASS="programlisting">$yday</PRE></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">day of year</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">1-366</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">&#13;<PRECLASS="programlisting">$isdst</PRE></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">0 or 1</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">true if daylight savings is in effect</P></TD></TR></TBODY></TABLE><PCLASS="para">The values for second range from 0-60 to account for leap seconds; you never know when a spare second will leap into existence at the urging of various standards bodies.</P><PCLASS="para">From now on, we'll refer to a list of day, month, year, hour, minute, and seconds as DMYHMS, for no better reason than that writing and reading "distinct day, month, year, hour, minute, and seconds values" is wearisome. The abbreviation is not meant to suggest an order of return values.</P><PCLASS="para"><ACLASS="indexterm"NAME="ch03-idx-1000006221-0"></A>Perl does <EMCLASS="emphasis">not</EM> return a two-digit year value. It returns the year minus 1900, which just happens to be a two-digit number through 1999. Perl doesn't intrinsically have a <ACLASS="indexterm"NAME="ch03-idx-1000007285-0"></A>Year 2000 problem, unless you make one yourself. (Your computer, and Perl, may have a 2038 problem, though, if we're still using 32 bits by that time.) Add 1900 to get the full year value instead of using the construct <CODECLASS="literal">&quot;19$year&quot;</CODE>, or soon your programs will refer to the year <CODECLASS="literal">&quot;19102&quot;</CODE>. We can't pin down the year value's range because it depends on how big an integer your operating system uses for Epoch seconds. Small integers mean a small range; big (64-bit) integers mean a very big range.</P><PCLASS="para">In scalar context, <CODECLASS="literal">localtime</CODE> and <CODECLASS="literal">gmtime</CODE> return the date and time formatted as an ASCII string:</P><PRECLASS="programlisting"><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Fri Apr 11 09:27:08 1997</I></CODE></B></CODE><CODECLASS="literal"></CODE><ACLASS="indexterm"NAME="ch03-idx-1000006223-0"></A></PRE><PCLASS="para">The standard <ACLASS="indexterm"NAME="ch03-idx-1000006231-0"></A><ACLASS="indexterm"NAME="ch03-idx-1000006231-1"></A>Time::tm module provides objects that give you a named interface to these values. The standard <ACLASS="indexterm"NAME="ch03-idx-1000006232-0"></A><ACLASS="indexterm"NAME="ch03-idx-1000006232-1"></A><ACLASS="indexterm"NAME="ch03-idx-1000006232-2"></A><ACLASS="indexterm"NAME="ch03-idx-1000006232-3"></A>Time::localtime and Time::gmtime modules override the list-returning <CODECLASS="literal">localtime</CODE> and <CODECLASS="literal">gmtime</CODE> functions, replacing them with versions that return Time::tm objects. Compare these two pieces of code:</P><PRECLASS="programlisting"># using arraysprint &quot;Today is day &quot;, (localtime)[7], &quot; of the current year.\n&quot;;<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Today is day 117 of the current year.</I></CODE></B></CODE># using Time::tm objectsuse Time::localtime;$tm = localtime;print &quot;Today is day &quot;, $tm-&gt;yday, &quot; of the current year.\n&quot;;<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Today is day 117 of the current year.</I></CODE></B></CODE></PRE><PCLASS="para">To go <EMCLASS="emphasis">from</EM> a list <EMCLASS="emphasis">to</EM> Epoch seconds, use the standard <ACLASS="indexterm"NAME="ch03-idx-1000006233-0"></A><ACLASS="indexterm"NAME="ch03-idx-1000006233-1"></A>Time::Local module. It provides the functions <CODECLASS="literal">timelocal</CODE><ACLASS="indexterm"NAME="ch03-idx-1000006234-0"></A><ACLASS="indexterm"NAME="ch03-idx-1000006234-1"></A> and <CODECLASS="literal">timegm</CODE>, both of which take a nine-element list and return an integer. The list's values have the same meaning and ranges as those returned by <CODECLASS="literal">localtime</CODE> and <CODECLASS="literal">gmtime</CODE>.</P><PCLASS="para">Epoch seconds values are limited by the size of an integer. If you have a 32-bit signed integer holding your Epoch seconds, you can only represent dates (in GMT) from <CODECLASS="literal">Fri</CODE> <CODECLASS="literal">Dec</CODE> <CODECLASS="literal">13</CODE> <CODECLASS="literal">20:45:52</CODE> <CODECLASS="literal">1901</CODE> to <CODECLASS="literal">Tue</CODE> <CODECLASS="literal">Jan</CODE> <CODECLASS="literal">19</CODE> <CODECLASS="literal">03:14:07</CODE> <CODECLASS="literal">2038</CODE> (inclusive). By 2038, it is assumed, computers will change to use larger integers for Epoch seconds. We hope. For operations on dates outside this range, you must use another representation or work from distinct year, month, and day values.</P><PCLASS="para">The <ACLASS="indexterm"NAME="ch03-idx-1000006235-0"></A><ACLASS="indexterm"NAME="ch03-idx-1000006235-1"></A>Date::Calc and Date::Manip modules on CPAN both work from these distinct values, but be warned: years don't necessarily have 1900 subtracted from them the way the year value returned by <CODECLASS="literal">localtime</CODE> does, nor do months and weeks always start at 0. As always, consult the manpage of the appropriate module to make sure you're giving it what it expects and getting back from it what you expect. There's little more embarrassing than realizing you've calculated your company payroll based on a calendar that's 1,900 years in the past.</P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch02_20.htm"TITLE="2.19. Program: Calculating Prime Factors"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 2.19. Program: Calculating Prime Factors"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch03_02.htm"TITLE="3.1. Finding Today's Date"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 3.1. Finding Today's Date"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">2.19. Program: Calculating Prime Factors</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">3.1. Finding Today's Date</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>

⌨️ 快捷键说明

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