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

📄 ch03_03.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 3.2. Converting DMYHMS to Epoch Seconds (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:34Z"><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_02.htm"TITLE="3.1. Finding Today's Date"><LINKREL="next"HREF="ch03_04.htm"TITLE="3.3. Converting Epoch Seconds to DMYHMS"></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_02.htm"TITLE="3.1. Finding Today's Date"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 3.1. Finding Today's 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_04.htm"TITLE="3.3. Converting Epoch Seconds to DMYHMS"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 3.3. Converting Epoch Seconds to DMYHMS"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch03-24880">3.2. Converting DMYHMS to Epoch Seconds</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch03-pgfId-164">Problem <ACLASS="indexterm"NAME="ch03-idx-1000006243-0"></A><ACLASS="indexterm"NAME="ch03-idx-1000006243-1"></A><ACLASS="indexterm"NAME="ch03-idx-1000006243-2"></A></A></H3><PCLASS="para">You want to convert a date, a time, or both with distinct values for day, month, year, etc. to Epoch seconds.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch03-pgfId-170">Solution</A></H3><PCLASS="para">Use the <CODECLASS="literal">timelocal</CODE><ACLASS="indexterm"NAME="ch03-idx-1000006254-0"></A><ACLASS="indexterm"NAME="ch03-idx-1000006254-1"></A><ACLASS="indexterm"NAME="ch03-idx-1000006254-2"></A><ACLASS="indexterm"NAME="ch03-idx-1000006254-3"></A> or <CODECLASS="literal">timegm</CODE> functions in the standard Time::Local module, depending on whether the date and time is in the current time zone or in UTC.</P><PRECLASS="programlisting">use Time::Local;$TIME = timelocal($sec, $min, $hours, $mday, $mon, $year);$TIME = timegm($sec, $min, $hours, $mday, $mon, $year);</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch03-pgfId-184">Discussion</A></H3><PCLASS="para">The built-in function <CODECLASS="literal">localtime</CODE> converts an Epoch seconds value to distinct DMYHMS values; the <CODECLASS="literal">timelocal</CODE> subroutine from the standard Time::Local module converts distinct DMYHMS values to an Epoch seconds value. Here's an example that shows how to find Epoch seconds for a time in the current day. It gets the day, month, and year values from <CODECLASS="literal">localtime</CODE>:</P><PRECLASS="programlisting"># $hours, $minutes, and $seconds represent a time today,# in the current time zoneuse Time::Local;$time = timelocal($seconds, $minutes, $hours, (localtime)[3,4,5]);</PRE><PCLASS="para">If you're passing month and year values to <CODECLASS="literal">timelocal</CODE>, it expects values with the same range as those which <CODECLASS="literal">localtime</CODE> returns. Namely, months start at 0, and years have 1900 subtracted from them.</P><PCLASS="para">The <CODECLASS="literal">timelocal</CODE> function assumes the <ACLASS="indexterm"NAME="ch03-idx-1000006255-0"></A>DMYHMS values represent a time in the current time zone. Time::Local also exports a <CODECLASS="literal">timegm</CODE> subroutine that assumes the DMYHMS values represent a time in the GMT time zone. Unfortunately, there is no convenient way to convert from a time zone other than the current local time zone or GMT. The best you can do is convert to GMT and add or subtract the time zone offset in seconds.</P><PCLASS="para">This code illustrates both the use of <CODECLASS="literal">timegm</CODE> and how to adjust the ranges of months and years:</P><PRECLASS="programlisting"># $day is day in month (1-31)# $month is month in year (1-12)# $year is four-digit year e.g., 1967# $hours, $minutes and $seconds represent UTC time use Time::Local;$time = timegm($seconds, $minutes, $hours, $day, $month-1, $year-1900);</PRE><PCLASS="para">As explained in the introduction, Epoch seconds cannot hold values before <CODECLASS="literal">Fri</CODE> <CODECLASS="literal">Dec</CODE> <CODECLASS="literal">13</CODE> <CODECLASS="literal">20:45:52</CODE> <CODECLASS="literal">1901</CODE> or after <CODECLASS="literal">Tue</CODE> <CODECLASS="literal">Jan</CODE> <CODECLASS="literal">19</CODE> <CODECLASS="literal">03:14:07</CODE> <CODECLASS="literal">2038</CODE>. Don't convert such dates to Epoch seconds &nbsp;-  use a Date:: module from CPAN, and do your calculations with that <CODECLASS="literal"></CODE><ACLASS="indexterm"NAME="ch03-idx-1000006794-0"></A><ACLASS="indexterm"NAME="ch03-idx-1000006794-1"></A><ACLASS="indexterm"NAME="ch03-idx-1000006794-2"></A><ACLASS="indexterm"NAME="ch03-idx-1000006794-3"></A>instead.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch03-pgfId-218">See Also</A></H3><PCLASS="para">The documentation for the standard Time::Local module (also in <ACLASS="olink"HREF="../prog/ch07_01.htm">Chapter 7</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>); convert in the other direction using <ACLASS="xref"HREF="ch03_04.htm"TITLE="Converting Epoch Seconds to DMYHMS">Recipe 3.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_02.htm"TITLE="3.1. Finding Today's Date"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 3.1. Finding Today's 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_04.htm"TITLE="3.3. Converting Epoch Seconds to DMYHMS"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 3.3. Converting Epoch Seconds to DMYHMS"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">3.1. Finding Today's 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.3. Converting Epoch Seconds to DMYHMS</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 + -