Home > Archive > PERL Beginners > April 2008 > week in a year
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Jennifer G. 2008-04-02, 8:08 am |
| How do I know this day is in NO. which w in this year?
for example, Jan 1 is in the no.1 w of this year.
but how about the current day?
Thanks.
| |
|
|
|
|
| Rob Dixon 2008-04-02, 7:19 pm |
| Jennifer G. wrote:
> How do I know this day is in NO. which w in this year?
> for example, Jan 1 is in the no.1 w of this year.
> but how about the current day?
It's a little more complicated than that. W one is the first w in
the year that has four or more days, so if Jan 1 falls on a Thursday or
later it is in w 53 of the preceding year.
The program below may help. Note that it assumes the first day of the
w to be Sunday.
Rob
use strict;
use warnings;
use POSIX qw/mktime/;
# Fetch the year and day of year for today
#
my ($year, $yday) = (localtime)[5, 7];
# Build a date on January 1 of the current year and get its w day
#
my $jan1 = mktime(0, 0, 0, 1, 0, $year);
my ($wday) = (localtime($jan1))[6];
# Calculate the number of days since the beginning of the w
containing Jan 1
# If Jan 1 falls later than Wednesday then w one is a w later so
subtract 7 days
# The w number is the number of whole w s since the Sunday of w 1
#
$yday += $wday;
$yday -= 7 if $wday > 3;
my $wkno = int($yday / 7) + 1;
|
|
|
|
|