Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use warnings;
- use strict;
- # it's the same as POSIX: "date +%V"
- # Week of the Year
- # INPUT: firstDayOfTheWeek = 0:sun, 1:mon , undef:sun
- sub yweek(;$) {
- my $fdw = shift // 0;
- # current date => nth day of the year
- # 0 1 2 3 4 5 6 [7]0 8
- my (undef,undef,undef,undef,undef,undef, undef, $yday, undef) = localtime(time);
- # (1st day of the year) was (which day of the week)?
- # 0 1 2 3 4 5 [6] 7 8
- my (undef,undef,undef,undef,undef,undef, $first_yday, undef, undef) = localtime(time - 24*60*60*$yday);
- # if year starts w/ sunday and 1st day of the week is not sunday;
- my $addFirstSunDelta = ($first_yday == 0 && $fdw != 0) ? 7 : 0;
- my $weekNum = int(($yday + $first_yday - $fdw + $addFirstSunDelta) / 7)+1;
- return $weekNum;
- }
- # testing:
- print yweek() ."\n";
- print yweek(0) ."\n";
- print yweek(1) ."\n";
- # special case: when a year starts with Sunday and the 1st day of the week is Monday;
Advertisement
Add Comment
Please, Sign In to add comment