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

Hello.
We use localtime to for example:
my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time +);

Can we set for example $yday and get the $wday,$mon,$mday?

Replies are listed 'Best First'.
Re: Localtime...
by kvale (Monsignor) on Sep 01, 2004 at 22:57 UTC
    localtime won't do it, but there is timelocal. From the Time::Local synopsis:
    use Time::Local 'timelocal_nocheck'; # The 365th day of 1999 print scalar localtime timelocal_nocheck 0,0,0,365,0,99;

    -Mark

      Thanks a lot.
Re: Localtime...
by Velaki (Chaplain) on Sep 02, 2004 at 00:12 UTC

    Probably the easiest way to do it is to use Time::Local's timelocal_nocheck function to convert the day number back to seconds, and then use localtime to extract what you want.

    #!/usr/bin/perl use strict; use warnings; use Time::Local qw(timelocal_nocheck); my $yday = 244; # September 1st, 2004 my @a = localtime(timelocal_nocheck(0,0,0,$yday+1,0,4)); # 2004 # remember: watch out for 0. mon == 8 is Sep, not Aug print "$a[6] $a[4] $a[3]\n"; # wday, mon, mday
    Hope that helped,
    -v
    "Perl. There is no substitute."