📄 ch03_08.htm
字号:
<HTML><HEAD><TITLE>Recipe 3.7. Parsing Dates and Times from Strings (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:41Z"><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_07.htm"TITLE="3.6. Day in a Week/Month/Year or Week Number"><LINKREL="next"HREF="ch03_09.htm"TITLE="3.8. Printing 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_07.htm"TITLE="3.6. Day in a Week/Month/Year or Week Number"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 3.6. Day in a Week/Month/Year or Week Number"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_09.htm"TITLE="3.8. Printing a Date"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 3.8. Printing a Date"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch03-30416">3.7. Parsing Dates and Times from Strings</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch03-pgfId-578">Problem <ACLASS="indexterm"NAME="ch03-idx-1000006333-0"></A><ACLASS="indexterm"NAME="ch03-idx-1000006333-1"></A><ACLASS="indexterm"NAME="ch03-idx-1000006333-2"></A></A></H3><PCLASS="para">You read in a date or time specification in an arbitrary format but need to convert that string into distinct year, month, etc. values.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch03-pgfId-584">Solution</A></H3><PCLASS="para">If your date is already numeric, or in a rigid and easily parsed format, use a regular expression (and possibly a hash mapping month names to numbers) to extract individual day, month, and year values, and then use the standard Time::Local module's <CODECLASS="literal">timelocal</CODE> and <CODECLASS="literal">timegm</CODE> functions to turn that into an Epoch seconds value.</P><PRECLASS="programlisting">use Time::Local;# $date is "1998-06-03" (YYYY-MM-DD form).($yyyy, $mm, $dd) = ($date =~ /(\d+)-(\d+)-(\d+)/;# calculate epoch seconds at midnight on that day in this timezone$epoch_seconds = timelocal(0, 0, 0, $dd, $mm, $yyyy);</PRE><PCLASS="para">For a more flexible solution, use the <CODECLASS="literal">ParseDate</CODE><ACLASS="indexterm"NAME="ch03-idx-1000006585-0"></A><ACLASS="indexterm"NAME="ch03-idx-1000006585-1"></A> function provided by the CPAN module Date::Manip, and then use <CODECLASS="literal">UnixDate</CODE><ACLASS="indexterm"NAME="ch03-idx-1000006587-0"></A> to extract the individual values.</P><PRECLASS="programlisting">use Date::Manip qw(ParseDate UnixDate);$date = ParseDate($STRING);if (!$date) { # bad date} else { @VALUES = UnixDate($date, @FORMATS);}</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch03-pgfId-604">Discussion</A></H3><PCLASS="para">The flexible <CODECLASS="literal">ParseDate</CODE> function accepts many formats. It even converts strings like "today", "2 weeks ago Friday", and "2nd Sunday in 1996", and understands the date and time format used in mail and news headers. It returns the decoded date in its own format: a string of the form "YYYYMMDDHH:MM:SS". You can compare two such strings to compare the dates they represent, but arithmetic is difficult. For this reason, we use the <CODECLASS="literal">UnixDate</CODE> function to extract the year, month, and day values in a preferred format.</P><PCLASS="para"><CODECLASS="literal">UnixDate</CODE> takes a date in the string form returned by <CODECLASS="literal">ParseDate</CODE> and a list of formats. It applies each format to the string and returns the result. A format is a string describing one or more elements of the date and time and the way that the elements are to be formatted. For example, <CODECLASS="literal">%Y</CODE> is the format for the year in four-digit form. Here's an example:</P><PRECLASS="programlisting">use Date::Manip qw(ParseDate UnixDate);while (<>) { $date = ParseDate($_); if (!$date) { warn "Bad date string: $_\n"; next; } else { ($year, $month, $day) = UnixDate($date, "%Y", "%m", "%d"); print "Date was $month/$day/$year\n"; }}</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch03-pgfId-636">See Also</A></H3><PCLASS="para">The documentation for the CPAN module Date::Manip; we use this in <ACLASS="xref"HREF="ch03_12.htm"TITLE="Program: hopdelta">Recipe 3.11</A> <ACLASS="indexterm"NAME="ch03-idx-1000006335-0"></A><ACLASS="indexterm"NAME="ch03-idx-1000006335-1"></A><ACLASS="indexterm"NAME="ch03-idx-1000006335-2"></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_07.htm"TITLE="3.6. Day in a Week/Month/Year or Week Number"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 3.6. Day in a Week/Month/Year or Week Number"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_09.htm"TITLE="3.8. Printing a Date"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 3.8. Printing a Date"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">3.6. Day in a Week/Month/Year or Week Number</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.8. Printing 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 © 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 + -