Wednesday, December 3, 2008

Perl: Converting between day of year (DOY) dates and normal YYMMDD dates

Perl has limited support for converting between dates, and to convert between dates that include "day of year" values, you will need an external Perl module. 
 
Use the module: Date::Calc
 
 To convert a date like YY/DOY to YY/MM/DD format, try: 
  • Use  Add_Delta_Days function in Date::Calc
  • Extract by: 
 my ($new_year, $new_month, $new_day) = Add_Delta_Days($old_year, 1, 1, $old_doy - 1);
 
          Where: 
$new_year is the year value in YY/MM/DD
$new_month is the month value in YY/MM/DD
$new_day is the day value in YY/MM/DD
$old_year is the year value in YY/DOY
$old_doy is the day of year value in YY/DOY
We set the second and third arguments of Add_Delta_Days to "1" because we want to add the number of days starting from January 1st of the given year 



To convert a date like YY/MM/DD to YY/DOY, try:
  • Use  Day_of_Year function in Date::Calc
  • Calculate the day of the year by:
  my $doy = Day_of_Year($year, $month, $day);
 
 

NOTE: Don't forget to include the Date::Calc package by stating at the top of your file:
use Date::Calc qw(Add_Delta_Days Day_of_Year);
 


For more information, see:

No comments: