Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Greetings All
What is the best way to convert the Day_of_Year to text.
I dont see anything in the DATE::CALC module.
Any Ideas
Thanks Cal

Replies are listed 'Best First'.
Re: Need Help with DATE::CALC $doy
by blokhead (Monsignor) on Jul 26, 2003 at 07:01 UTC
    Time::Local (which should be in your Perl distribution) has a conversion mode where its date arguments aren't validated to be in the proper range. A nifty side-effect of this is that you can ask it to convert the 176th of January (or 10_000th second of 2003, or 1234th hour of February, etc), and it will Do What You Mean©.
    use Time::Local 'timelocal_nocheck'; my $year = 2003 - 1900; my $dyear = 176; my $epoch = timelocal_nocheck 0,0,0,$dyear,0,$year; ## January is month 0 ^
    Time::Local only converts Y/M/D,H:M:S to epoch seconds, but you can very easily extract out the month and day components from the epoch time with localtime:
    my ($month, $day) = (localtime $epoch)[4,3]; $month++; ## again, months run from 0 to 11
    .. or you could format the date with whatever format you like, using strftime
    use POSIX 'strftime'; print strftime("%x", localtime $epoch), $/;
    For doing simple date calculations like this, using localtime and Time::Local is a lot less overhead than the behemoth Date::Calc.

    UPDATE: If you only care about getting the text output (you don't need the day/month components individually), it looks like strftime also normalizes its input in the same way as timelocal_nocheck:

    $ perl -MPOSIX=strftime -le 'print strftime("%c", 0,0,0,176,0,103)' Thu Jun 25 00:00:00 2003
    Also realized that I needed to use 2003-1900 (not just 2003) as the final argument to timelocal_nocheck, so updated that.

    blokhead

Re: Need Help with DATE::CALC $doy
by cleverett (Friar) on Jul 26, 2003 at 05:57 UTC
    The Day_of_Year Function returns a 3 digit number like 176 that I am trying to convert to a textual date like July 25 ,2003.

    OK, by running the command "perldoc Date::Calc", we see:

    Day_of_Year $doy = Day_of_Year($year,$month,$day);
    so your description of the Day_of_Year function isn't quite spot on. If you indeed are able to get a day of year out of the Date::Calc::Day_of_Year function, then you already know the year, day & month, correct?

    Staying within the Date::Calc module, your best bet is the Month_to_Text function:

    use strict; use Date::Calc qw/Today Month_to_Text/; my ($year, $month, $day) = Today(); # work with today's date print sprintf("%s %d, %d", Month_to_Text($month), $day, $year);
    HTH
Re: Need Help with DATE::CALC $doy
by cleverett (Friar) on Jul 26, 2003 at 05:07 UTC
    Uh, show an example output matched to example input, otherwise we won't know (well I won't anyway ...) what you're talking about ...
    A reply falls below the community's threshold of quality. You may see it by logging in.