📄 ch03_05.htm
字号:
<HTML><HEAD><TITLE>Recipe 3.4. Adding to or Subtracting from a Date (Perl Cookbook)</TITLE><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:37Z"><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_04.htm"TITLE="3.3. Converting Epoch Seconds to DMYHMS"><LINKREL="next"HREF="ch03_06.htm"TITLE="3.5. Difference of Two Dates"></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_04.htm"TITLE="3.3. Converting Epoch Seconds to DMYHMS"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 3.3. Converting Epoch Seconds to DMYHMS"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_06.htm"TITLE="3.5. Difference of Two Dates"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 3.5. Difference of Two Dates"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch03-chap03_adding_0">3.4. Adding to or Subtracting from a Date</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch03-pgfId-292">Problem <ACLASS="indexterm"NAME="ch03-idx-1000006275-0"></A></A></H3><PCLASS="para">You have a date and time and want to find the date and time of some period in the future or past.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch03-pgfId-298">Solution</A></H3><PCLASS="para">Simply add or subtract Epoch seconds:</P><PRECLASS="programlisting">$when = $now + $difference;$then = $now - $difference;</PRE><PCLASS="para">If you have distinct DMYHMS values, use the CPAN <ACLASS="indexterm"NAME="ch03-idx-1000006276-0"></A><ACLASS="indexterm"NAME="ch03-idx-1000006276-1"></A><ACLASS="indexterm"NAME="ch03-idx-1000006276-2"></A>Date::Calc module. If you're doing arithmetic with days only, use <CODECLASS="literal">Add_Delta_Days</CODE> (<CODECLASS="literal">$offset</CODE> is a positive or negative integral number of days):</P><PRECLASS="programlisting">use Date::Calc qw(Add_Delta_Days);($y2, $m2, $d2) = Add_Delta_Days($y, $m, $d, $offset);</PRE><PCLASS="para">If you are concerned with hours, minutes, and seconds (in other words, times as well as dates), use <CODECLASS="literal">Add_Delta_DHMS</CODE>:</P><PRECLASS="programlisting">use Date::Calc qw(Add_Delta_DHMS);($year2, $month2, $day2, $h2, $m2, $s2) = Add_Delta_DHMS( $year, $month, $day, $hour, $minute, $second, $days_offset, $hour_offset, $minute_offset, $second_offset );</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch03-pgfId-328">Discussion</A></H3><PCLASS="para">Calculating with Epoch seconds is easiest, disregarding the effort to get dates and times into and out of Epoch seconds. This code shows how to calculate an offset (55 days, 2 hours, 17 minutes, and 5 seconds, in this case) from a given base date and time:</P><PRECLASS="programlisting">$birthtime = 96176750; # 18/Jan/1973, 3:45:50 am$interval = 5 + # 5 seconds 17 * 60 + # 17 minutes 2 * 60 * 60 + # 2 hours 55 * 60 * 60 * 24; # and 55 days$then = $birthtime + $interval;print "Then is ", scalar(localtime($then)), "\n";<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Then is Wed Mar 14 06:02:55 1973</I></CODE></B></CODE></PRE><PCLASS="para">We could have used Date::Calc's <CODECLASS="literal">Add_Delta_DHMS</CODE> function and avoided the conversion to and from Epoch seconds:</P><PRECLASS="programlisting">use Date::Calc qw(Add_Delta_DHMS);($year, $month, $day, $hh, $mm, $ss) = Add_Delta_DHMS( 1973, 1, 18, 3, 45, 50, # 18/Jan/1973, 3:45:50 am 55, 2, 17, 5); # 55 days, 2 hrs, 17 min, 5 secprint "To be precise: $hh:$mm:$ss, $month/$day/$year\n";<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>To be precise: 6:2:55, 3/14/1973</I></CODE></B></CODE></PRE><PCLASS="para">As usual, we need to know the range of values the function expects. <CODECLASS="literal">Add_Delta_DHMS</CODE> takes a full year value - that is, one that hasn't had 1900 subtracted from it. The month value for January is 1, not 0. Date::Calc's <CODECLASS="literal">Add_Delta_Days</CODE> function expects the same kind of values:</P><PRECLASS="programlisting">use Date::Calc qw(Add_Delta_Days);($year, $month, $day) = Add_Delta_Days(1973, 1, 18, 55);print "Nat was 55 days old on: $month/$day/$year\n";<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Nat was 55 days old on: 3/14/1973</I></CODE></B></CODE></PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch03-pgfId-1000006293">See Also</A></H3><PCLASS="para"><ACLASS="indexterm"NAME="ch03-idx-1000006294-0"></A><ACLASS="indexterm"NAME="ch03-idx-1000006294-1"></A><ACLASS="indexterm"NAME="ch03-idx-1000006294-2"></A>The documentation for the CPAN module Date::Calc</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_04.htm"TITLE="3.3. Converting Epoch Seconds to DMYHMS"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 3.3. Converting Epoch Seconds to DMYHMS"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_06.htm"TITLE="3.5. Difference of Two Dates"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 3.5. Difference of Two Dates"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">3.3. Converting Epoch Seconds to DMYHMS</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.5. Difference of Two Dates</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 © 2002</a> O'Reilly & 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 + -