#! /usr/bin/perl
## Include the required modules
use File::Basename;
use File::Copy;
use Getopt::Long;
use Cwd;
use Time::localtime;
use Date::Calc qw(Week_Number);
#Package sort_sub
$debug = 0;
if ( GetOptions("-f=s" => \$file,
"-d=i" => \$debug, ) ) {
#print "hello\n";
}
else {
print "Error! Bad command line arguments\n";
print "work_week.pl -f <file_name> <-d 1>\n";
print "options: -d is debug mode\n";
die "Aborting script - fix command line arguments!\n";
}
#declare hash for counting work-weeks and storing year
# hash key is work-week
# hash value is count
%weeks;
# Open input files for reading
open( FILE, "< $file" )
or die "ERROR: Couldn't open $file for reading: $!\n";
# Read input file
while (<FILE>) {
chomp;
$LINE = $_;
# parse the date
# Assumes the format is: 05/09/2005 11:59 AM 720 perwin
($mm, $dd, $yyyy) = ($LINE =~ /^(\d+)\/(\d+)\/(\d+)/);
if ($debug==2) {
print "month = $mm, date = $dd, year = $yyyy\n";
}
# using Date:: Calc module
$wnum = Week_Number($yyyy, $mm, $dd);
$wnum = $wnum . " " . $yyyy;
if ($debug) {
print " workweek = $wnum using Date module\n";
}
if ( exists($weeks{$wnum}) ) {
# this week and year already encountered - increment count
$weeks{$wnum}++;
if ($debug) { print "$weeks{$wnum}\n"; }
}
else {
# this is the first instance of this ww and year - insert into hash
# set count to 1
$weeks{$wnum} = 1;
if ($debug) { print "set count to $weeks{$wnum} for Week: $wnum\n"; }
}
}
#Now sort and print
print "Week Numer\t\tNo. of Users\n";
print "======================================\n";
foreach $key ( sort { $a <=> $b } keys %weeks ) {
print "Work Week $key \t\t $weeks{$key}\n";
}