Hi i write this little script to compute every saturday of a year but
between november 1th and March 12th the saturdays becomes Friday.
May be you can help me or give me more informations.
I sent you a part of my script.
Thanks in advance for your help
Yves
Time_calc.pl
#!/usr/bin/perl
require "./time_calc.pm";
$day = qx{date +%d}; # Get the system days
$month = qx{date +%m}; # Get the system month
$year = qx{date +%Y}; # Get the system year
$dw = qx{date +%u}; # Get the system day of the week
$cmp_date = ""; # Computed date
$ref_date = ""; # Reference date for the comparaison
chomp($day);
chomp($month);
chomp($year);
chomp($dw);
my @month_txt = ("","Jan","Feb","Mar","April","May","Jun","Jul","Aug",
+"Sep","Oct","Nov","Dec"); # Array with Text month
my @days = ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
+ # Array witg Text days
$sec_date = &date_to_sec("$day/$month/$year"); # convert curren
+t date in seconds
$sec_date += ((6 - $dw) * 86400); # Add the good number of
+seconds to reach the next saturday
@date_spl = split(/\//, &sec_to_date($sec_date - 86400)); # Create
+an array with the next saturday converted in real date
$i = 0;
$ref_date = "$date_spl[0]/$date_spl[1]/"; # Construct a dat
+e with one more year for referance
$ref_date .= $date_spl[2] + 1; #
#####################################################
# This loop increase the current date converted in seconds
# of 7 * 86400 seconds (1 weeks) to get the next saturday.
# And print it...
#####################################################
print "Ln nb\t: dw\tday\tmonth\tyear\tdate in seconds\n";
print "-----------------------------------------------------------\n";
while($cmp_date ne $ref_date && $i <= 60){
$cmp_date = &sec_to_date($sec_date); # convert computed
+ date from seconds to real date
@date_split = split(/\//, $cmp_date); # split computed
+and converted date to be formated
if($date_split[1] < 10){ # format the print out of
+ the date
$date_split[1] = "0" .$date_split[1]; #
} #
print "$i\t: $days[$date_split[0]]\t$date_split[1]\t$month_txt
+[$date_split[2]]\t$date_split[3]\t$sec_date\n";
$sec_date += ( 7 * 86400); # increase the date in s
+econds of 7 days
$i++;
}
############################################################
Time_calc.pm
# Perl Sub
#
# File : time_calc.pm
#
######################################################################
+#####
use Time::Local;
# Compute the difference between two dates.
##########################################################
sub date_diff
{
my $from_date = shift;
my $to_date = shift;
my ($mday,$month,$year);
($mday,$month,$year) = split /\//, $from_date;
$month--; # month : 1-12 -> 0-11
my $from_lgtime = timelocal(0,0,0,$mday,$month,$year);
($mday,$month,$year) = split /\//, $to_date;
$month--; # month : 1-12 -> 0-11
my $to_lgtime = timelocal(0,0,0,$mday,$month,$year);
my $date_diff = $to_lgtime - $from_lgtime; # date diff in
+ seconds
use integer; # integer division
$date_diff /= (24 * 60 * 60); # date diff in
+ days
no integer; # end of integer divis
+ion
return($date_diff);
}
# --------------------------------------------------------------------
+-------- #
# check_date_exist
# --------------------------------------------------------------------
+-------- #
# IN date (dd/mm/[yy]yy
# OUT 0|1 (1 : date exist | 0 : date does not exist)
#
# How it works : entry date is converted as a lgtime (with timelocal)
+and the
# the result is converted back to a date.
# If those 2 dates are the same => the date is OK,
# if not, this date does not exist.
#
# Example : 04/31/00 will be (after convertion) 05/01/00
# because 04/31/00 does not exist
sub check_date_exist
{
my $date = shift;
my ($mday,$month,$year) = split /\//, $date;
$month--; # Month [1-12] -> [0-11]
my $hour = 0;
my $min = 0;
my $sec = 0;
my $lgtime = timelocal($sec,$min,$hour,$mday,$month,$year);
my ($new_sec,$new_min,$new_hour,$new_mday,$new_month,$new_year
+,$new_wday,$new_yday,$new_isdst) = localtime($lgtime);
my $date_exist_flag = ! (($new_month == $month) && ($new_mday
+== $mday));
return $date_exist_flag;
}
######################################
# Convert date in seconds
######################################
sub date_to_sec {
my $date = shift;
my ($mday,$month,$year) = split /\//, $date;
$month--; # Month [1-12] -> [0-11]
my $hour = 0;
my $min = 0;
my $sec = 0;
my $lgtime = timelocal($sec,$min,$hour,$mday,$month,$year);
return $lgtime;
}
######################################
# Convert seconds in date
######################################
sub sec_to_date {
my $lgtime = shift;
my ($new_sec,$new_min,$new_hour,$new_mday,$new_month,$new_year,$ne
+w_wday,$new_yday,$new_isdst) = localtime($lgtime);
$new_year = ($new_year - 100) + 2000;
$new_month += 1;
return "$new_wday/$new_mday/$new_month/$new_year";
}
1;
########################################################
20021017 Edit by Corion: Moved from Discussion to Seekers of Perl Wisdom