http://qs1969.pair.com?node_id=705315
Category: Miscellaneous
Author/Contact Info Sander Venema <sander.venema@gmail.com>
Description: This is a small Perl script to calculate the number of the week from any given date.

This is a small Perl script to calculate the number of the week from any given date, (given in the ISO 8601 format), or uses today's date if no date is specified. It was written just to scratch an itch I had in the past, when I frequently needed to know the number of the week for planning purposes.

See the POD for more information about how it works. This is the first time I share my code here on PerlMonks, and any comments, suggestions or improvements are greatly appreciated.

#!/usr/bin/perl -w
#
# calcweek.pl - Calculate the week number from any given date, 
# or the default, if no date is given.
#
# Sander Venema <sander.venema@gmail.com> version 20080819

use strict;
use Getopt::Long;
use Pod::Usage;
use Data::Dumper;
use Date::ISO;
use POSIX qw(strftime);

# set some defaults
my $verbose    = 0;
my $date      = strftime("%Y%m%d", localtime);

# fetch options
Getopt::Long::Configure(qw(no_ignore_case bundling));
GetOptions(
    'h|help'        => sub { pod2usage({-verbose => 1, -input => \*DAT
+A}); exit; },
    'H|man'            => sub { pod2usage({-verbose => 2, -input => \*
+DATA}); exit; },
    'v|verbose'        => \$verbose,
    'd|date=i'        => \$date,
) or pod2usage(2);

# convert date string to Date::ISO object and calculate the
# year, week and number of the day (starting with Monday)
my $iso                      = Date::ISO->new(iso => $date);
my ($year, $week, $week_day) = ($iso->year, $iso->iso_week, $iso->iso_
+week_day);
    
# print everything, depending on level of $verbose
print "$year-W$week-$week_day\n" if $verbose;
print "$week\n"                  if !$verbose;

__DATA__
=head1 NAME

calcweek.pl - Calculate the week number from any given date

=head1 SYNOPSIS

calcweek.pl [-h] [-H] [-v] [-d yyyymmdd]

=head1 DESCRIPTION

This is a small Perl script to calculate the number of the week from a
+ny given date,
(given in the ISO 8601 format), or uses today's date if no date is spe
+cified. It was 
written just to scratch an itch I had in the past, when I frequently n
+eeded to know
the number of the week for planning purposes.

The program returns the number of the week it is, and if verbose is tu
+rned on, it 
will print the year, weeknumber, and day of the week in ISO 8601 forma
+t (YYYY-Www-D).
When verbose is turned off, it will only output the number of the week
+.

The following options are available:

    -h    --help            Display this help
    -H    --man            Display detailed help (manpage)
    -v    --verbose        Turn more verbose messages on
    -d    --date <yyyymmdd>    Set the date to calculate (default: tod
+ay)
    
=head1 DEPENDENCIES

Date::ISO, POSIX, Getopt::Long, Pod::Usage, Data::Dumper

=head1 SEE ALSO

For more information about ISO 8601:
http://www.iso.org/iso/date_and_time_format

=head1 BUGS

Lots. Probably. Any help on how to improve it is appreciated. :-)

=head1 AUTHOR

Sander Venema