Bart Lateur wrote: > On Wed, 5 Dec 2001 15:46:26 -0800, [EMAIL PROTECTED] wrote: > >> I'm at a bit of a loss. I need to be able to determine what day of the >> week a given date is (that is, December 5, 2001 is day 4 or some such). I >> would think this is trivial, but can't seem to find the technique for doing >> it. Any suggestions would be appreciated. > > One of the fields in localtime() in list context has it. So has > gmtime(). It's... (peeking at the docs for "localtime"...) number 6. > > print +(localtime)[6]
This more generic approach works on a month, date, and year, rather than a number of seconds: sub dow { my($month, $day, $year) = @_; # 1 <= $month <= 12 my @ary = (($year % 4 or $year % 400 and not $year % 100) ? (3, 0) : (4, 1), 0, 4, 2, 6, 4, 1, 5, 3, 0, 5); my $a = $year - 1600; my($b, $c) = ($a % 12, int($a/100)); return((2+$b-$c+$day+int($a/12)+int($b/4)+int($c/4)-$ary[$month-1])%7); } dow() returns the index of the day of week, where 0 = Sunday, 6 = Saturday. IIRC, this only works for dates after sometime in September, 1752. Regards, David > That prints a 4, but it's already past midnight. FYI sunday is day zero. > > That is for today. If you want it for any date, the standard module > Time::Local, with functions timelocal() and timegm(), the inverse of > localtime() and gmtime(), can be used to turn a date back into the > seconds-since-the-epoch number that you need to apply the above function > to.