Please be gentle, this is the first of my code I've posted for public consumption. 8)
{
# 0 1 2 3 4 5 6 7 8
# ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
my $wday = (localtime(time()))[6] + 1;
# how many days into the current week are we?
my $endseconds = time() - ($wday * 24 * 60 * 60);
# subtract that many days from today. this tells us when Saturday (of the previous calendar week) was
my $startseconds = $endseconds - (6 * 24 * 60 * 60);
# six days before that was the Sunday we're interested in
my $year = (localtime($startseconds))[5] + 1900;
my $month = (localtime($startseconds))[4] + 1;
my $day = (localtime($startseconds))[3];
if (length($month) < 2) {$month = "0$month"}
if (length($day) < 2) {$day = "0$day"}
$startdate = "$year$month$day";
$year = (localtime($endseconds))[5] + 1900;
$month = (localtime($endseconds))[4] + 1;
$day = (localtime($endseconds))[3];
if (length($month) < 2) {$month = "0$month"}
if (length($day) < 2) {$day = "0$day"}
$enddate = "$year$month$day";
}
Time::Piece (Score:2)
I suggest looking into the Time::Piece module, to get rid of all those nondescriptive subscripts.
J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
Re:Time::Piece (Score:1)
Typical beginner's error (Score:1)
if (length($month) < 2) {$month = "0$month"}
Horrors. My advice: use sprintf(). In fact, you can combine the sprintf for year, month and day into
$enddate = sprintf "%04d%02d%02d", $y+1900, $m+1, $d;
While we're at it: there's still more you can group, like this getting the year, month and day pout of a localtime() list value at once, ready for the above snippet:
my($d, $m, $y) = (localtime($endseconds))[3, 4, 5]
Re:Typical beginner's error (Score:2)
{sigh} Bad memes.
Re:Typical beginner's error (Score:1)
What can I say? Being new to the language, I didn't remember sprintf when writing the code, opting instead for something easier, simpler, and possibly more legible. 8)
Most excellent! (Score:1)
Argh, this is frustrating. How is a novice Perlie to find out about this sort of thing? I search CPAN aggresively for "calendar week", "previous week", and something else I can't recall right now. I thumbed through Ye Olde Cookbook, the Llama, and The Camel. What else should I have done?
How did you folks find out about Time::Piece initially?
John
Re:Most excellent! (Score:2)
I can't remember how I first found out about Time::Piece. I know that I had a design on my whiteboard for an identical module for the first six months of 2000, and then Matt Sergeant suddenly wrote it. No, he didn't have any contact with my whiteboard, but he did have an identical design from Larry Wall in the early part of that year.
I suggest looking through new modules [cpan.org] each day; it will give you a chance to become familiar with what's out there. You could go read the module list, but it would take fo
J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers