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

📄 ch03_04.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 3.3. Converting Epoch Seconds to DMYHMS (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:36Z"><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_03.htm"TITLE="3.2. Converting DMYHMS to Epoch Seconds"><LINKREL="next"HREF="ch03_05.htm"TITLE="3.4. Adding to or Subtracting from a 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="ch03_03.htm"TITLE="3.2. Converting DMYHMS to Epoch Seconds"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 3.2. Converting DMYHMS to Epoch Seconds"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_05.htm"TITLE="3.4. Adding to or Subtracting from a Date"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 3.4. Adding to or Subtracting from a Date"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch03-41138">3.3. Converting Epoch Seconds to DMYHMS</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch03-pgfId-228">Problem</A></H3><PCLASS="para">You have a date and time in Epoch seconds, and you want to calculate individual DMYHMS values from it.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch03-pgfId-234">Solution</A></H3><PCLASS="para">Use the <CODECLASS="literal">localtime</CODE><ACLASS="indexterm"NAME="ch03-idx-1000006268-0"></A> or <CODECLASS="literal">gmtime</CODE> functions, depending on whether you want the date and time in GMT or your local time zone.</P><PRECLASS="programlisting">($seconds, $minutes, $hours, $day_of_month, $month, $year,    $wday, $yday, $isdst) = localtime($time);</PRE><PCLASS="para">The standard <ACLASS="indexterm"NAME="ch03-idx-1000006269-0"></A><ACLASS="indexterm"NAME="ch03-idx-1000006269-1"></A><ACLASS="indexterm"NAME="ch03-idx-1000006269-2"></A><ACLASS="indexterm"NAME="ch03-idx-1000006269-3"></A>Time::timelocal and Time::gmtime modules override the <CODECLASS="literal">localtime</CODE> and <CODECLASS="literal">gmtime</CODE> functions to provide named access to the individual values.</P><PRECLASS="programlisting">use Time::localtime;        # or Time::gmtime$tm = localtime($TIME);     # or gmtime($TIME)$seconds = $tm-&gt;sec;# ...</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch03-pgfId-254">Discussion</A></H3><PCLASS="para">The <CODECLASS="literal">localtime</CODE> and <CODECLASS="literal">gmtime</CODE> functions return strange year and month values;  the year has 1900 subtracted from it, and 0 is the month value for January. Be sure to correct the base values for year and month, as this example does:</P><PRECLASS="programlisting">($seconds, $minutes, $hours, $day_of_month, $month, $year,    $wday, $yday, $isdst) = localtime($time);printf(&quot;Dateline: %02d:%02d:%02d-%04d/%02d/%02d\n&quot;,    $hours, $minutes, $seconds, $year+1900, $month+1,    $day_of_month);</PRE><PCLASS="para">We could have used the Time::localtime module to avoid the temporary variables:</P><PRECLASS="programlisting">use Time::localtime;$tm = localtime($time);printf(&quot;Dateline: %02d:%02d:%02d-%04d/%02d/%02d\n&quot;,    $tm-&gt;hour, $tm-&gt;min, $tm-&gt;sec, $tm-&gt;year+1900,    $tm-&gt;mon+1, $tm-&gt;mday);</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch03-pgfId-282">See Also</A></H3><PCLASS="para">The <ACLASS="indexterm"NAME="ch03-idx-1000006271-0"></A><ACLASS="indexterm"NAME="ch03-idx-1000006271-1"></A><ACLASS="indexterm"NAME="ch03-idx-1000006271-2"></A><ACLASS="indexterm"NAME="ch03-idx-1000006271-3"></A><CODECLASS="literal">localtime</CODE> function in <EMCLASS="emphasis">perlfunc</EM> (1) and <ACLASS="olink"HREF="../prog/ch03_01.htm">Chapter 3</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>; the documentation for the standard Time::localtime and Time::gmtime modules; convert in the other direction using <ACLASS="xref"HREF="ch03_03.htm"TITLE="Converting DMYHMS to Epoch Seconds">Recipe 3.2</A> <ACLASS="indexterm"NAME="ch03-idx-1000006264-0"></A><ACLASS="indexterm"NAME="ch03-idx-1000006264-1"></A><ACLASS="indexterm"NAME="ch03-idx-1000006264-2"></A><ACLASS="indexterm"NAME="ch03-idx-1000006264-3"></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_03.htm"TITLE="3.2. Converting DMYHMS to Epoch Seconds"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 3.2. Converting DMYHMS to Epoch Seconds"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_05.htm"TITLE="3.4. Adding to or Subtracting from a Date"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 3.4. Adding to or Subtracting from a Date"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">3.2. Converting DMYHMS to Epoch Seconds</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.4. Adding to or Subtracting from a 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 + -