"John W. Krahn" wrote:
>
> Dan wrote:
> >
> > I have a string, which is to represent a length of time, say 3d4h45m12s
> > which can be variable, so you may end up with just 3m, or 5h2m, etc. What I
> > need to do, is to take this string, and split it up into $days, $hours,
> > $minutes etc, and then create that amount of time in seconds, which will
> > then be added to the current timestamp to create an expiry time.
>
> $ perl -e'
> my @x = qw/3d4h45m12s 3m, or 5h2m, /;
> for ( @x ) {
> my ($sec) = /(\d+)s/;
> my ($min) = /(\d+)m/;
> $sec += $min * 60;
> my ($hr) = /(\d+)h/;
> $sec += $hr * 3600;
> my ($day) = /(\d+)d/;
> $sec += $day * 86400;
>
> print "$sec $_\n";
> }
> '
> 276312 3d4h45m12s
> 180 3m,
> 0 or
> 18120 5h2m,
Another way to do it. :-)
$ perl -e'
my %mult = ( s => 1, m => 60, h => 3600, d => 86400 );
my @x = qw/3d4h45m12s 3m, or 5h2m, /;
for ( @x ) {
my %time = reverse /(\d+)([smhd])/g;
my $sec = 0;
$sec += $time{$_} * $mult{$_} for keys %time;
print "$sec $_\n";
}
'
276312 3d4h45m12s
180 3m,
0 or
18120 5h2m,
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]