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  
 
 [ 3 post ] 

 Relevant Pages 

1. from day of year to day, month, year

2. Converting yy.ddd to month-day-year

3. Converting Month Day Year to time_t.

4. Calculating weekday given year, month and day

5. Extract the day, month, year of a string

6. Date/Time modules - any without default months/days?

7. Finding first day of a month

8. Calandaring in perl - Day of month from date?

9. Day of the month redirection - revised

10. Day of the month redirection

11. parsedate for 1st day of the next month

12. month/day algorithm

 

 
Powered by phpBB® Forum Software