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

📄 ch03_06.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 3.5. Difference of Two Dates (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:30:38Z"><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="ch03_01.htm"TITLE="3. Dates and Times"><LINKREL="prev"HREF="ch03_05.htm"TITLE="3.4. Adding to or Subtracting from a Date"><LINKREL="next"HREF="ch03_07.htm"TITLE="3.6. Day in a Week/Month/Year or Week Number"></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="ch03_05.htm"TITLE="3.4. Adding to or Subtracting from a Date"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 3.4. Adding to or Subtracting from a Date"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch03_01.htm"TITLE="3. Dates and Times"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch03_07.htm"TITLE="3.6. Day in a Week/Month/Year or Week Number"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 3.6. Day in a Week/Month/Year or Week Number"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch03-chap03_difference_0">3.5. Difference of Two Dates</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch03-pgfId-384">Problem</A></H3><PCLASS="para">You need to find the number of days between two dates or times.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch03-pgfId-390">Solution</A></H3><PCLASS="para">If your dates are in Epoch seconds, and fall in the range <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), simply subtract one from the other and convert the seconds to days.</P><PRECLASS="programlisting">$seconds = $recent - $earlier;</PRE><PCLASS="para">If you have distinct DMYMHS values, or are worried about the range limitations of Epoch seconds, use the <ACLASS="indexterm"NAME="ch03-idx-1000006308-0"></A>Date::Calc module from CPAN. It can calculate the difference between dates: <CODECLASS="literal"></CODE><ACLASS="indexterm"NAME="ch03-idx-1000006312-0"></A><ACLASS="indexterm"NAME="ch03-idx-1000006312-1"></A></P><PRECLASS="programlisting">use Date::Calc qw(Delta_Days);$days = Delta_Days( $year1, $month1, $day1, $year2, $month2, $day2);</PRE><PCLASS="para">It also calculates the difference between dates and times:</P><PRECLASS="programlisting">use Date::Calc qw(Delta_DHMS);($days, $hours, $minutes, $seconds) =  Delta_DHMS( $year1, $month1, $day1, $hour1, $minute1, $seconds1,  # earlier              $year2, $month2, $day2, $hour2, $minute2, $seconds2); # later</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch03-pgfId-418">Discussion</A></H3><PCLASS="para">One problem with Epoch seconds is how to convert the large integers back to forms that people can read. The following example shows one way of converting an Epoch seconds value back to its component numbers of weeks, days, hours, minutes, and seconds:</P><PRECLASS="programlisting">$bree = 361535725;          # 16 Jun 1981, 4:35:25$nat  =  96201950;          # 18 Jan 1973, 3:45:50$difference = $bree - $nat;print &quot;There were $difference seconds between Nat and Bree\n&quot;;<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>There were 265333775 seconds between Nat and Bree</I></CODE></B></CODE>$seconds    =  $difference % 60;$difference = ($difference - $seconds) / 60;$minutes    =  $difference % 60;$difference = ($difference - $minutes) / 60;$hours      =  $difference % 24;$difference = ($difference - $hours)   / 24;$days       =  $difference % 7;$weeks      = ($difference - $days)    /  7;print &quot;($weeks weeks, $days days, $hours:$minutes:$seconds)\n&quot;;<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>(438 weeks, 4 days, 23:49:35)</I></CODE></B></CODE></PRE><PCLASS="para">Date::Calc's functions can ease these calculations. <CODECLASS="literal"></CODE><ACLASS="indexterm"NAME="ch03-idx-1000006309-0"></A>The <CODECLASS="literal">Delta_Days</CODE> function returns the number of days between two dates. It takes the two dates as a list: year, month, day. The dates are given chronologically &nbsp;-  earliest first.</P><PRECLASS="programlisting">use Date::Calc qw(Delta_Days);@bree = (1981, 6, 16);      # 16 Jun 1981@nat  = (1973, 1, 18);      # 18 Jan 1973$difference = Delta_Days(@nat, @bree);print &quot;There were $difference days between Nat and Bree\n&quot;;<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>There were 3071 days between Nat and Bree</I></CODE></B></CODE></PRE><PCLASS="para"><CODECLASS="literal"></CODE><ACLASS="indexterm"NAME="ch03-idx-1000006310-0"></A>The <CODECLASS="literal">Delta_DHMS</CODE> function returns a four-element list corresponding to the number of days, hours, minutes, and seconds between the two dates you give it.</P><PRECLASS="programlisting">use Date::Calc qw(Delta_DHMS);@bree = (1981, 6, 16, 4, 35, 25);   # 16 Jun 1981, 4:35:25@nat  = (1973, 1, 18, 3, 45, 50);   # 18 Jan 1973, 3:45:50@diff = Delta_DHMS(@nat, @bree);print &quot;Bree came $diff[0] days, $diff[1]:$diff[2]:$diff[3] after Nat\n&quot;;<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Bree came 3071 days, 0:49:35 after Nat</I></CODE></B></CODE></PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch03-pgfId-488">See Also</A></H3><PCLASS="para">The documentation for the CPAN module Date::Calc <ACLASS="indexterm"NAME="ch03-idx-1000006304-0"></A></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="ch03_05.htm"TITLE="3.4. Adding to or Subtracting from a Date"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 3.4. Adding to or Subtracting from a Date"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_07.htm"TITLE="3.6. Day in a Week/Month/Year or Week Number"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 3.6. Day in a Week/Month/Year or Week Number"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">3.4. Adding to or Subtracting from a Date</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.6. Day in a Week/Month/Year or Week Number</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 + -