Forum Moderators: coopster & phranque

Message Too Old, No Replies

How to get dates for the week

         

sumeet ruiwale

5:34 pm on Jul 8, 2005 (gmt 0)

10+ Year Member



Hi,
finally this script i was working on is running. now i need help in showing the dates of the week. like the output is week 24 number of users = 24. Now i need to display the date of the week 24... like 15th may to 21st may . Can anyone help me with that.

heres the code i have written.

#!/usr/local/bin/perl

use File::Basename;
use File::Copy;
use Getopt::Long;
use Cwd;
use Time::localtime;

use Date::Calc qw(Week_Number);
#Package sort_sub
$debug = 2;

#declare hash for counting work-weeks and storing year

%weeks;

# Open input files for reading
$file = "pautesting.txt";
open( FILE, "<$file")
or die "ERROR: Couldn't open $file for reading: $!\n";

# Read input file
while (<FILE>) {

chomp;
$LINE = $_;

next if /^(\s)*$/;

# parse the date

($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

$weeks{$wnum} = 1;
if ($debug) { print "set count to $weeks{$wnum} for Week: $wnum\n"; }
}
}

#Now sort and print
print "Dates\t\Week Number\t\tNo. of Users\n";
print "======================================\n";
foreach $key ( sort sub_sort (keys %weeks)) {
print "week is $key, $weeks{$key}\n";
}

sub sub_sort {

($wk1, $y1) = split /\s+/, $a;
($wk2, $y2) = split /\s+/, $b;

# sort by year and then by week
($y1 <=> $y2 ¦¦ $wk1 <=> $wk2) ¦¦ $a <=> $b;
}

WWMike

2:18 am on Jul 18, 2005 (gmt 0)

10+ Year Member



Here ya go:

#!/usr/local/bin/perl

print "Content-type: text/html\n\n";
print &weekdates(46);
exit;

sub weekdates {

# get requested week

( $week ) = @_;

# store name of each month

@month = (January, February, March, April, May, June, July, August, September, October, November, December);

# store number of days in each month

@daysinmonth = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

# store current timer tick

$time = time;

# get current year

( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime($time);

# convert system year to 4-digit format

$year += 1900;

# adjust for leap year if evenly divisible by 4 but not evenly divisible by both 100 and 400

if ( ($year/4) eq int ($year/4) ) {
$daysinmonth[1]++;
if ( ($year/100) eq int ($year/100) && ($year/400) ne int ($year/400) ) {
$daysinmonth[1]--;
}
}

# calculate timer tick for same time on the first day of the current year

$first = $time - $yday * 86400;

# get the day of the week for the first day of the current year

( $sec2, $min2, $hour2, $mday2, $mon2, $year2, $wday2, $yday2, $isdst2 ) = localtime($first);

# set the starting element

$n = $wday2;

# create flat calendar

$m = 0;
for ($m; $m<12; $m++) {
$d = 1;
for ($d; $d<=$daysinmonth[$m]; $d++) {
$cal[$n] = "$m-$d";
$n++;
}
}

# calculate start and end dates

($startmonth, $startday) = split (/-/, $cal[($week * 7 - 7)]);
($endmonth, $endday) = split (/-/, $cal[($week * 7 - 1)]);
return "Week $week of $year is $month[$startmonth] $startday thru $month[$endmonth] $endday";

}

sumeet ruiwale

5:59 pm on Jul 19, 2005 (gmt 0)

10+ Year Member



thanks mate.. keep up the gr8 work