in reply to Convert weekday to numerical date
G'day james28909,
You can do this sort of thing fairly easily with Time::Piece and Time::Seconds.
There were some points I wasn't too sure about from your spec:
Here's code, showing the basic techniques. Bear in mind my stated assumptions. Note that the BEGIN block is just to get a point of reference before Time::Piece reimplements localtime. Also, in case the dates and times look a bit screwy, my timezone is currently UTC+10:00.
#!/usr/bin/env perl -l use strict; use warnings; BEGIN { print scalar localtime } use Time::Piece; use Time::Seconds; my $t = localtime; my @weekdays = ($t->mday); my %lastweek = ($t->day => $t->mday); $t -= ONE_DAY; push @weekdays, $t->mday; $lastweek{$t->day} = $t->mday; for (1 .. 5) { $t -= ONE_DAY; $lastweek{$t->day} = $t->mday; } push @weekdays, @lastweek{qw{Sun Mon Tue Wed Thu Fri Sat}}; print "@weekdays";
Output:
Sat Apr 29 16:20:18 2017 29 28 23 24 25 26 27 28 29
Had you also wanted the second Sunday, the output would have been:
29 28 23 24 25 26 27 28 29 16
If you had wanted the eight days, with two Sundays, to be sequential, the output would have been:
29 28 16 17 18 19 20 21 22 23
I'm guessing you can probably work it out from here; however, if you need further help, please provide expected output.
— Ken
|
|---|