"John W. Krahn" <jwkr...@shaw.ca> writes:

[...]

> The main problem is that you are not testing edge cases like:
>
> ev   101201 3
>
> Or:
>
> ev   100101 3
>
> If you did you would get an error message like:
>
> Month '12' out of range 0..11 at Why_is_the_answer_right.pl line 74


Thanks for the help cleaning the script up.

I may have gotten it a little less incorrect with those tips.

Input data:

ev   100409 3
  send state tax payment by 15th
ev 100604
  Newsguy account expires 100607
ev 100421 4
  my appt is today
ev 100131 3
  my appt is today
ev 101230 2
  my appt is today
-------        ---------       ---=---       ---------      -------- 
#!/usr/local/bin/perl

use strict;
use warnings;
use Time::Local;

## values below hardwired so we can use Time::Local gracefully
## and we don't need any for our current job

## Changed to decimal but left them early since this will be read
## by cron only once per day...  12 would mean a late notification.
my $sec  = 3;
my $min  = 3;
my $hour = 3;

while(<>){
  if( my ($year,$mon,$mday,$predays) = $_ =~ 
                m/^ev\s+(\d\d)(\d\d)(\d\d)\s+(\d)\s*$/){
     my ($oyear,$omon,$omday) = ($year,$mon,$mday);
     #You need to adjust the month and year correctly:
       
     ## Correct the mnth by -1 since we fed a 1..12 base mnth and timelocal
     ## uses 0..11
     ## incoming is 2 dig yr, but here we need 4 for localtime to work right
     ## We'll convert back to 2 later
     my $time = timelocal( $sec, $min, $hour, $mday, $mon - 1,"20$year" - 1900 
);
       
     ## calc based on 86400 second in 24 hr
     my $offset = ($time - ($predays * 86400)); 

     ## ($preday * 86400) seconds less than target date gives
     ## the offset in seconds that we will compare to determine
     ## if its time to send mail 
     ## We turn the offset time back into YYMMDD for comparison
##                  #  0    1    2     3     4    5     6     7     8
#                   ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
#                      localtime(time);
     ($year,$mon,$mday) = (localtime($offset))[5,4,3];
     $year += 1900;  
     $mon  += 1;

     my $PaddedDateStr = sprintf "%02d%02d%02d", ($year % 100),$mon,$mday;
     my $incoming_date = $oyear . $omon . $omday;
     my $str = "\n       Let's see how it works:
  -------        ----=----       -------      
     Incoming year mon day  =  $incoming_date
     predays ($predays) * 86400 
     gives us this 
        offset $offset 
     Which leaves us with   =  $PaddedDateStr
  -------        ----=----       -------
";
    print $str;
  }
}  
print "\n";

-------        ---------       ---=---       ---------      -------- 
output
myscript file:

       Let's see how it works:
  -------        ----=----       -------      
     Incoming year mon day  =  100409
     predays (3) * 86400 
     gives us this 
        offset 1270540983 
     Which leaves us with   =  100406
  -------        ----=----       -------

       Let's see how it works:
  -------        ----=----       -------      
     Incoming year mon day  =  100421
     predays (4) * 86400 
     gives us this 
        offset 1271491383 
     Which leaves us with   =  100417
  -------        ----=----       -------

       Let's see how it works:
  -------        ----=----       -------      
     Incoming year mon day  =  100131
     predays (3) * 86400 
     gives us this 
        offset 1264669383 
     Which leaves us with   =  100128
  -------        ----=----       -------

       Let's see how it works:
  -------        ----=----       -------      
     Incoming year mon day  =  101230
     predays (2) * 86400 
     gives us this 
        offset 1293526983 
     Which leaves us with   =  101228
  -------        ----=----       -------



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to