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

Oh do I seek thy wisdom :)

I am looking for assistance on modification of a "Day" to obtain a new "Day". For example, I ask a user on which day a particular "program" can can be launced on and how long to try till it is sucessfully launced. The input I am expecting from the user would be like: Sat/36. Meaning starting Sat at midnight for 36 hours

Thefore, I am trying to have a little script figure out that 36 hour from Sat midnight is Mon at noon.

Thanks in advance

Gary

Replies are listed 'Best First'.
Re: Day/Date Modification
by duckyd (Hermit) on Aug 22, 2007 at 23:28 UTC
    You may also find DateTime useful:
    use DateTime; my $date = DateTime->new( year => 2007, month => 8, day => 25, hour => 23, minute => 59, second => 59, time_zone => 'UTC', ); print $date->ymd, "\t", $date->hms, "\n"; $date->add( hours => 36 ); print $date->ymd, "\t", $date->hms, "\n";
    Outputs:
    2007-08-25 23:59:59 2007-08-27 11:59:59
Re: Day/Date Modification
by thezip (Vicar) on Aug 22, 2007 at 23:00 UTC

    You should be able to accomplish this quite nicely with localtime and Time::Local.

    As for whether the program has successfully run during the specified window, you'll need to implement some sort of queue to keep track of that. As jobs succeed, they should be removed from the queue... If they fail, they get pushed back onto the queue to be run with the other jobs for the next interval.

    Update: BTW, what does Sat/36 actually mean? Is it this Saturday? or the next, etc? If you can change the spec, it's best to specify a date, as in 20070825/36 to eliminate any confusion.


    Where do you want *them* to go today?
Re: Day/Date Modification
by monarch (Priest) on Aug 23, 2007 at 09:54 UTC
    You need to rethink the definition of midnight. Midnight begins a day, not ends a day.

    Another solution is to use Time::ParseDate. Here is some source code that I tested:

    use Time::ParseDate; use strict; my $input = shift || ""; my $day = undef; my $hours = undef; if ( $input =~ m!^(\w{3})/(\d+)! ) { $day = $1; $hours = $2; } else { die( "Error: use an argument of ddd/hh (e.g. Sat/36)" ); } # set up parse string to midnight (beginning) of desired day my $parsestring = "next $day 00:00"; my $epoch = Time::ParseDate::parsedate( $parsestring ); $epoch += ( $hours * 3600 ); print( " Epoch: $epoch\n" ); print( " GMT Time: " . scalar( gmtime( $epoch ) ) . "\n" ); print( "Local Time: " . scalar( localtime( $epoch ) ) . "\n" );

    When executed this outputs the following:

    $ perl -w testparse.pl Sat/36 Epoch: 1188126000 GMT Time: Sun Aug 26 11:00:00 2007 Local Time: Sun Aug 26 12:00:00 2007

    Last note: think carefully whether you are doing times in the local timezone or GMT/UTC. (I was running this from London, UK, in summer time hence local time being one hour ahead of GMT).

Re: Day/Date Modification
by gam3 (Curate) on Aug 23, 2007 at 11:34 UTC
    Here is a solution that uses a different Package, Date::Manip.
    use strict; use Date::Manip; my $arg = shift || ""; my $start; my $end; if (my ($day, $hours) = ($arg =~ m!^(\w{3})/(\d+)!)) { my $err; my $start = ParseDate($day); die "Bad day $day" unless $start; print UnixDate($start, "%u"), "\n"; $end = DateCalc($start,"+ $hours hours", \$err); print UnixDate($end, "%u"), "\n"; } else { die( "Error: use an argument of dow/hours (e.g. Sat/36)" ); }
    -- gam3
    A picture is worth a thousand words, but takes 200K.