Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

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

Report this thread to moderator Post Follow-up to this message
Old Post
Jennifer G.
04-02-08 01:08 PM


Re: week in a year
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/
>
>
>


Report this thread to moderator Post Follow-up to this message
Old Post
Rob Coops
04-02-08 01:08 PM


Re: week in a year
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

Report this thread to moderator Post Follow-up to this message
Old Post
John W. Krahn
04-03-08 12:18 AM


Re: week in a year
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;

Report this thread to moderator Post Follow-up to this message
Old Post
Rob Dixon
04-03-08 12:19 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 11:05 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.