perlfaq4: How do I find the week-of-the-year/day-of-the-year?

[email protected] (_brian_d_foy)
Newsgroups perl.perlfaq.workers,perl.documentation
Message-ID <260120031221345894%[email protected]>
* re-jiggered the question to get rid of the hyphens

* expanded the answer using POSIX, Time::Local, and Date::Calc


Index: perlfaq.pod
===================================================================
RCS file: /cvs/public/perlfaq/perlfaq.pod,v
retrieving revision 1.17
diff -u -d -r1.17 perlfaq.pod
--- perlfaq.pod 26 Jan 2003 17:45:46 -0000      1.17
+++ perlfaq.pod 26 Jan 2003 18:19:54 -0000
@@ -412,7 +412,7 @@
 
 =item *
 
-How do I find the week-of-the-year/day-of-the-year?
+How do I find the day or week of the year?
 
 =item *
 
Index: perlfaq4.pod
===================================================================
RCS file: /cvs/public/perlfaq/perlfaq4.pod,v
retrieving revision 1.40
diff -u -d -r1.40 perlfaq4.pod
--- perlfaq4.pod        26 Jan 2003 17:43:21 -0000      1.40
+++ perlfaq4.pod        26 Jan 2003 18:19:56 -0000
@@ -353,13 +353,34 @@
 
 =head1 Data: Dates
 
-=head2 How do I find the week-of-the-year/day-of-the-year?
+=head2 How do I find the day or week of the year?
 
-The day of the year is in the array returned by localtime() (see
-L<perlfunc/"localtime">):
+The localtime function returns the day of the week.  Without an
+argument localtime returns the current seconds since the epoch.
 
-    $day_of_year = (localtime(time()))[7];
+    $day_of_year = (localtime)[7];
+    
+The POSIX module can also format a date as the day of the year or
+week of the year.
+
+       use POSIX qw/strftime/;
+       my $day_of_year  = strftime "%j", localtime;
+       my $week_of_year = strftime "%W", localtime;
 
+To get the day of year for any date, use the Time::Local module to get
+a time in epoch seconds for the argument to localtime.
+
+       use POSIX qw/strftime/;
+       use Time::Local;
+       my $week_of_year = strftime "%W", 
+               localtime( timelocal( 0, 0, 0, 18, 11, 1987 ) );
+
+The Date::Calc module provides two functions for to calculate these.
+
+       use Date::Calc;
+       my $day_of_year  = Day_of_Year(  1987, 12, 18 );
+       my $week_of_year = Week_of_Year( 1987, 12, 18 );
+
 =head2 How do I find the current century or millennium?
 
 Use the following simple functions:

-- 
brian d foy, [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.