I needed to find the start and end dates for the previous calendar week, and couldn't find anything pre-rolled, so here's what I came up with, which populates $startdate and $enddate. It'll be posted on perlmonks, but I wanted to show it here too. Does anyone have anything better, or any suggestions on how I can improve this?
Please be gentle, this is the first of my code I've posted for public consumption. 8)
I suggest looking into the Time::Piece module, to get rid of all those nondescriptive subscripts.
Re:Time::Piece
gav on 2002-09-30T19:46:10
Something like this?:) use Time::Piece;
use Time::Seconds;
my $time = localtime();
my $end = $time - ONE_DAY * $time->wday;
my $start = $end - ONE_DAY * 6;
my ($startdate, $enddate) = map $_->strftime('%Y%m%d'), $start, $end;
print "Start: $startdate, End: $enddate\n";
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
merlyn on 2002-10-01T01:37:57
Matt's Script Archive code did it that way, so a lot of junior perlhackerwannabe's also adopted that style.{sigh} Bad memes.
Re:Typical beginner's error
jbodoni on 2002-10-01T05:16:52
What does it say about me if I didn't adopt that style courtesy of Matt's Script Archive?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)
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!
jdavidb on 2002-10-14T03:56:21
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 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 forever! If you look at the new modules every day, though, you will get to know each one as it is updated. (And there are very few good modules that are not periodically updated. Some, but very few.)
Oh, and don't worry about feeling like a novice. It will pass. And along the way you will have a blast learning things!