in reply to About this week Date

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.