📄 ch03_01.htm
字号:
<HTML><HEAD><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:30:29Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="index.htm"TITLE="Perl Cookbook"><LINKREL="prev"HREF="ch02_20.htm"TITLE="2.19. Program: Calculating Prime Factors"><LINKREL="next"HREF="ch03_02.htm"TITLE="3.1. Finding Today's Date"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><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"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"></FONT></B></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></TABLE></DIV><DIVCLASS="chapter"><H1CLASS="chapter"><ACLASS="title"NAME="ch03-16597">3. Dates and Times</A></H1><DIVCLASS="htmltoc"><P><B>Contents:</B><BR><ACLASS="sect1"HREF="#ch03-15450"TITLE="3.0. Introduction">Introduction</A><BR><ACLASS="sect1"HREF="ch03_02.htm"TITLE="3.1. Finding Today's Date">Finding Today's Date</A><BR><ACLASS="sect1"HREF="ch03_03.htm"TITLE="3.2. Converting DMYHMS to Epoch Seconds">Converting DMYHMS to Epoch Seconds</A><BR><ACLASS="sect1"HREF="ch03_04.htm"TITLE="3.3. Converting Epoch Seconds to DMYHMS">Converting Epoch Seconds to DMYHMS</A><BR><ACLASS="sect1"HREF="ch03_05.htm"TITLE="3.4. Adding to or Subtracting from a Date">Adding to or Subtracting from a Date</A><BR><ACLASS="sect1"HREF="ch03_06.htm"TITLE="3.5. Difference of Two Dates">Difference of Two Dates</A><BR><ACLASS="sect1"HREF="ch03_07.htm"TITLE="3.6. Day in a Week/Month/Year or Week Number">Day in a Week/Month/Year or Week Number</A><BR><ACLASS="sect1"HREF="ch03_08.htm"TITLE="3.7. Parsing Dates and Times from Strings">Parsing Dates and Times from Strings</A><BR><ACLASS="sect1"HREF="ch03_09.htm"TITLE="3.8. Printing a Date">Printing a Date</A><BR><ACLASS="sect1"HREF="ch03_10.htm"TITLE="3.9. High-Resolution Timers">High-Resolution Timers</A><BR><ACLASS="sect1"HREF="ch03_11.htm"TITLE="3.10. Short Sleeps">Short Sleeps</A><BR><ACLASS="sect1"HREF="ch03_12.htm"TITLE="3.11. Program: hopdelta">Program: hopdelta</A></P><P></P></DIV><DIVCLASS="epigraph"ALIGN="right"><PCLASS="para"ALIGN="right"><I>It is inappropriate to require that a time represented as seconds since the Epoch precisely represent the number of seconds between the referenced time and the Epoch.</I></P><PCLASS="attribution"ALIGN="right">- IEEE Std 1003.1b-1993 (POSIX) Section B.2.2.2 </P></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch03-15450">3.0. Introduction</A></H2><PCLASS="para"><ACLASS="indexterm"NAME="ch03-idx-1000006215-0"></A><ACLASS="indexterm"NAME="ch03-idx-1000006215-1"></A>Times and dates are important things to be able to manipulate. "How many users logged in last month?", "How many seconds should I sleep, if I want to wake up at midday?", and "Has this user's password expired yet?" are all common questions whose answers involve surprisingly non-obvious manipulations.</P><PCLASS="para">Perl represents points in time as intervals, measuring seconds past a point in time called <ICLASS="firstterm">the Epoch</I>. On Unix and many other systems, the Epoch was 00:00 Jan 1, 1970, Greenwich Mean Time (GMT).[<ACLASS="footnote"HREF="#ch03-pgfId-1000000809">1</A>] On a Mac, all dates and times are expressed in the local time zone. The <CODECLASS="literal">gmtime</CODE> function returns the correct GMT time, based on your Mac's time zone offset. Bear this in mind when considering the recipes in this chapter. The Macintosh's Epoch seconds value ranges from 00:00 Jan 1, 1904 to 06:28:15 Feb 6, 2040.</P><BLOCKQUOTECLASS="footnote"><DIVCLASS="footnote"><PCLASS="para"><ACLASS="footnote"NAME="ch03-pgfId-1000000809">[1]</A> These days GMT is increasingly referred to as UTC (Universal Coordinated Time).</P></DIV></BLOCKQUOTE><PCLASS="para">When we talk about dates and times, we often interchange two different concepts: points in time (dates and times) and intervals between points in time (weeks, months, days, etc.). Epoch <ACLASS="indexterm"NAME="ch03-idx-1000006220-0"></A><ACLASS="indexterm"NAME="ch03-idx-1000006220-1"></A><ACLASS="indexterm"NAME="ch03-idx-1000006220-2"></A><ACLASS="indexterm"NAME="ch03-idx-1000006220-3"></A><ACLASS="indexterm"NAME="ch03-idx-1000006220-4"></A><ACLASS="indexterm"NAME="ch03-idx-1000006220-5"></A><ACLASS="indexterm"NAME="ch03-idx-1000006220-6"></A><ACLASS="indexterm"NAME="ch03-idx-1000006220-7"></A>seconds represent intervals and points in the same units, so you can do basic arithmetic on them.</P><PCLASS="para">However, people are not used to working with Epoch seconds. We are more used to dealing with individual year, month, day, hour, minute, and second values. Furthermore, the month can be represented by its full name or its abbreviation. The day can precede or follow the month. Because of the difficulty of performing calculations with a variety of formats, we typically convert human-supplied strings or lists to Epoch seconds, calculate, and then convert back to strings or lists for output.</P><PCLASS="para">For convenience in calculation, Epoch seconds are always calculated in GMT. When converting to or from distinct values, we must always consider whether the time represented is GMT or local. Use different conversion functions depending on whether you need to convert from GMT to local time or vice versa.</P><PCLASS="para">Perl's <CODECLASS="literal">time</CODE><ACLASS="indexterm"NAME="ch03-idx-1000006218-0"></A> function returns the number of seconds that have passed since the Epoch - more or less.[<ACLASS="footnote"HREF="#ch03-pgfId-1000000823">2</A>] To convert Epoch seconds into distinct values for days, months, years, hours, minutes, and seconds, use the <CODECLASS="literal">localtime</CODE><ACLASS="indexterm"NAME="ch03-idx-1000006219-0"></A><ACLASS="indexterm"NAME="ch03-idx-1000006219-1"></A> and <CODECLASS="literal">gmtime</CODE> functions. In list context, these functions return a nine-element list with the following elements:</P><BLOCKQUOTECLASS="footnote"><DIVCLASS="footnote"><PCLASS="para"><ACLASS="footnote"NAME="ch03-pgfId-1000000823">[2]</A> Well, less actually. To be precise, 21 seconds less as of this writing. POSIX requires that <CODECLASS="literal">time</CODE> not include leap seconds, a peculiar practice of adjusting the world's clock by a second here and there to account for the slowing down of the Earth's rotation due to tidal angular-momentum dissipation. See the <EMCLASS="emphasis">sci.astro</EM> FAQ, section 3, in<ACLASS="systemitem.url"HREF=" http://sciastro.astronomy.net/sci.astro.3.FAQ"> http://sciastro.astronomy.net/sci.astro.3.FAQ</A>.</P></DIV></BLOCKQUOTE><TABLECLASS="informaltable"BORDER="1"CELLPADDING="3"><THEADCLASS="thead"><TRCLASS="row"VALIGN="TOP"><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1"><PCLASS="para">Variable</P></TH><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1"><PCLASS="para">Values</P></TH><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1"><PCLASS="para">Range</P></TH></TR></THEAD><TBODYCLASS="tbody"><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"> <PRECLASS="programlisting">$sec</PRE></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">seconds</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">0-60</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"> <PRECLASS="programlisting">$min</PRE></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">minutes</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">0-59</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"> <PRECLASS="programlisting">$hours</PRE></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">hours</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">0-23</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"> <PRECLASS="programlisting">$mday</PRE></TD><TDCLASS="entry"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -