What should happen if you run the script on Sat or Sun?
my @Now = localtime(time);
print "Today: $Now[3] - $Now[6]\n";
my $Mon = time - (86400 * ($Now[6] - 1));
my @Monday = localtime($Mon);
print "Monday: $Monday[3] - $Monday[6]\n";
my $Fri = $Mon + (86400 * 4);
my @Friday = localtime($Fri);
print "Friday: $Friday[3] - $Friday[6]\n";
Explanation:
Get the splitted time values for now - returns the day of week as 6 starting with 0 for sunday
Monday is today - (seconds_per_day * (day_of_week - ))
Friday is 5 days after monday
This won't work in the following cases:
On the two change days for daylight saving there is a one hour for each day where the next day is not 86400 seconds away.
There has been a time (and there will be a time) where some days were skipped to match the calendar to the earth/sun relation.
You could advoid this by using localtime to get the current days values and step back (and forward) day-by-day by using timelocal(0,0,0,day,month,year) - 1 or timelocal(59,59,23,day,month,year) + 1 until you reach a monday or friday, but usually the above sample is accurate enough.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.