For Programmers: Free Programming Magazines  


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]

 

Author week in a year
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 Coops

2008-04-02, 8:08 am

If you are calculating it your self and assuming you are intrested in the
ISO defenition of the w number: http://www.proesite.com/timex/wkcalc.htm

Of course there are modules on CPAN that do the same but then again, if all
you want to know is the number of the w it might be simpler to calculate
it rather then adding a module requirement to your perl code. (depends a bit
on how portable your code needs to be)

Regards,

Rob

On Wed, Apr 2, 2008 at 2:00 PM, Jennifer G. <practicalperl@gmail.com> 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?
> Thanks.
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>


John W. Krahn

2008-04-02, 7:18 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 depends on how you define "w". The simple answer is:

$ perl -le'print int( ( localtime )[ 7 ] / 7 )'
13


Perhaps have a look at the Date::Calc and Date::Manip modules (or many
of the other Date related modules on CPAN.)


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
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 wday
#
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 ws since the Sunday of w 1
#
$yday += $wday;
$yday -= 7 if $wday > 3;
my $wkno = int($yday / 7) + 1;
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com