📄 ch03_07.htm
字号:
<HTML><HEAD><TITLE>Recipe 3.6. Day in a Week/Month/Year or Week Number (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:39Z"><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_06.htm"TITLE="3.5. Difference of Two Dates"><LINKREL="next"HREF="ch03_08.htm"TITLE="3.7. Parsing Dates and Times from Strings"></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_06.htm"TITLE="3.5. Difference of Two Dates"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 3.5. Difference of Two Dates"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_08.htm"TITLE="3.7. Parsing Dates and Times from Strings"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 3.7. Parsing Dates and Times from Strings"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch03-34067">3.6. Day in a Week/Month/Year or Week Number</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch03-pgfId-498">Problem <ACLASS="indexterm"NAME="ch03-idx-1000006316-0"></A></A></H3><PCLASS="para">You have a date, either in Epoch seconds or as distinct year, month, etc. values. You want to find out what week of the year, day of the week, day of the month, or day of the year that the date falls on.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch03-pgfId-504">Solution</A></H3><PCLASS="para">If you have Epoch seconds, the day of the year, day of the month, and day of the week are returned by <CODECLASS="literal">localtime</CODE>. The week of the year is easily calculated from the day of the year (but see discussion below, as standards differ).</P><PRECLASS="programlisting">($MONTHDAY, $WEEKDAY, $YEARDAY) = (localtime $DATE)[3,6,7];$WEEKNUM = int($YEARDAY / 7) + 1;</PRE><PCLASS="para">If you have distinct DMYHMS values, you can either convert them to Epoch seconds values as in <ACLASS="xref"HREF="ch03_04.htm"TITLE="Converting Epoch Seconds to DMYHMS">Recipe 3.3</A> and then use the solution above, or else use the <CODECLASS="literal">Day_of_Week</CODE><ACLASS="indexterm"NAME="ch03-idx-1000006322-0"></A><ACLASS="indexterm"NAME="ch03-idx-1000006322-1"></A><ACLASS="indexterm"NAME="ch03-idx-1000006322-2"></A> , <CODECLASS="literal">Week_Number</CODE>, and <CODECLASS="literal">Day_of_Year</CODE> functions from the CPAN module <ACLASS="indexterm"NAME="ch03-idx-1000006332-0"></A>Date::Calc:</P><PRECLASS="programlisting">use Date::Calc qw(Day_of_Week Week_Number Day_of_Year);# you have $year, $month, and $day# $day is day of month, by definition.$wday = Day_of_Week($year, $month, $day);$wnum = Week_Number($year, $month, $day);$dnum = Day_of_Year($year, $month, $day);</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch03-pgfId-530">Discussion</A></H3><PCLASS="para">The <CODECLASS="literal">Day_of_Week</CODE>, <CODECLASS="literal">Week_Number</CODE>, and <CODECLASS="literal">Day_of_Year</CODE> functions all expect years that haven't had 1900 subtracted from them and months where January is 1, not 0. The return value from <CODECLASS="literal">Day_of_Week</CODE> can be 1 through 7 (corresponding to Monday through Sunday) or 0 in case of an error (an invalid date, for example).</P><PRECLASS="programlisting">use Date::Calc qw(Day_of_Week Week_Number Day_of_Week_to_Text)$year = 1981;$month = 6; # (June)$day = 16;$wday = Day_of_Week($year, $month, $day);print "$month/$day/$year was a ", Day_of_Week_to_Text($wday), "\n";## see comment above$wnum = Week_Number($year, $month, $day);print "in the $wnum week.\n";<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>6/16/1981 was a Tuesday</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>in week number 25.</I></CODE></B></CODE></PRE><PCLASS="para">The governing standards body of particular countries may have rules about when the first week of the year starts. For example, in Norway the first week must have at least 4 days in it (and weeks start on Mondays). If January 1 falls on a week with 3 or fewer days, it is counted as week 52 (or 53) of the previous year. In America, the first Monday of the year is usually the start of the first work-week. Given such rules, you may have to write your own algorithm, or at least look at the <CODECLASS="literal">%G</CODE>, <CODECLASS="literal">%L</CODE>, <CODECLASS="literal">%W</CODE>, and <CODECLASS="literal">%U</CODE> formats to the <CODECLASS="literal">UnixDate</CODE> function in Date::Manip.<ACLASS="indexterm"NAME="ch03-idx-1000006324-0"></A><ACLASS="indexterm"NAME="ch03-idx-1000006324-1"></A><ACLASS="indexterm"NAME="ch03-idx-1000006324-2"></A><ACLASS="indexterm"NAME="ch03-idx-1000006324-3"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch03-pgfId-568">See Also</A></H3><PCLASS="para">The <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 CPAN module Date::Calc<ACLASS="indexterm"NAME="ch03-idx-1000006318-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_06.htm"TITLE="3.5. Difference of Two Dates"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 3.5. Difference of Two Dates"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_08.htm"TITLE="3.7. Parsing Dates and Times from Strings"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 3.7. Parsing Dates and Times from Strings"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">3.5. Difference of Two Dates</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.7. Parsing Dates and Times from Strings</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 + -