Convert Day of Year to Month and Day?
Author Message
Convert Day of Year to Month and Day?

Is there an easy way to convert the day of the year to the month and day -
without using a module like date::calc?  Why?  The overhead involved in
date::calc is more than I can justify for such a 'simple' calculation.

PAW

Sat, 24 Jul 2004 19:19:31 GMT
Convert Day of Year to Month and Day?
[posted & mailed]

On Feb 5, Paul Wasilkoff said:

Quote:
>Is there an easy way to convert the day of the year to the month and day -
>without using a module like date::calc?  Why?  The overhead involved in
>date::calc is more than I can justify for such a 'simple' calculation.

First, determine the first of the year:

use Time::Local;  # standard module, fast and simple
use constant SEC_PER_DAY => 86400;

my \$year = 2002;
my \$yday = 100;
my \$date = timelocal(0,0,12, 1,0,\$year-1900);

That represents 12:00:00 noon on Jan 1st, 2002.  Now we add 86400
seconds/day times (\$yday-1) days.

\$date += SEC_PER_DAY * (\$yday - 1);

Now use localtime() to get the goods:

my (\$day, \$mon) = (localtime \$date)[3,4];
\$mon++;

You're done.

--

RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.

Sat, 24 Jul 2004 19:28:31 GMT
Convert Day of Year to Month and Day?
Thanks Jeff,

This is exactly what I was looking for!

PAW

Sat, 24 Jul 2004 19:47:08 GMT

 Page 1 of 1 [ 3 post ]

Relevant Pages