Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Time::Local Problem

by Anonymous Monk
on Oct 17, 2002 at 09:13 UTC ( [id://205964]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Time::Local Problem
by physgreg (Scribe) on Oct 17, 2002 at 10:22 UTC
    I think the cause of your problem is trying to calculate the date at midnight, which will get confused by daylight saving time. PHP has the same problem. I got round it by using 12 noon as my base time.

    Update: I tested this, and it works if you simply substitute 12 for 0 in every hour field.

Re: Time::Local Problem
by Tanalis (Curate) on Oct 17, 2002 at 10:11 UTC
    You could probably use the Date::Calc module to do this job for you ..

    So far as I can tell, all you'd need to do would be calculate the first Saturday of a year (which is relatively simple - Date::Calc will tell you what day a date falls on, then it's simply arithmetic) then repeatedly Add_Delta_Days until the year-end.

    Might be more complicated than that .. just a quick thought.

    Hope this helps
    --Foxcub.

Re: Time::Local Problem
by joe++ (Friar) on Oct 17, 2002 at 10:12 UTC
    Why not take the CPAN route and use module Date::Calc? Reinventing this wheel doesn't look very attractive to me...

    Just $0.02...

    --
    Cheers, Joe

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://205964]
Approved by mce
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-19 05:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found