Leser: 2
9 Einträge, 1 Seite |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
sub timespan2seconds {
my $ts = shift;
my $sc = 0;
if ($ts =~ m/(?:(\d+)\s+)?(\d+):(\d+)(?::(\d+))?/) {
$sc += $1 || 0;
$sc *= 24;
$sc += $2;
$sc *= 60;
$sc += $3;
$sc *= 60;
$sc += $4 || 0;
}
else {
my @pt = $ts =~ m/(\d+)([dhms])/g;
my %sf = (
'd' => 24 * 60 * 60,
'h' => 60 * 60,
'm' => 60,
's' => 1
);
$sc += shift(@pt) * $sf{shift(@pt)} while (@pt);
}
return $sc;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
my $x = timespan2seconds('1 20:19:33');
print "$x\n";
my $y = timespan2seconds('20h 33s 1d 19m');
print "$y\n";
$x -= timespan2seconds('2d');
$x -= timespan2seconds('34s');
$x -= timespan2seconds('20:19');
$x += timespan2seconds('1d1s');
print "$x\n";
$y += timespan2seconds('4h');
$y -= timespan2seconds('0:19:33');
$y -= timespan2seconds('2d');
print "$y\n";
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use strict;
use warnings;
my @times = (
'13:45:23',
'14:12:43',
);
my $d = 0; # day
my $h = 0; # hour
my $m = 0; # min
my $s = 0; # sec
foreach my $time (@times) {
my ($hour, $min, $sec) = split /:/, $time;
$s += $hour * 3600 + $min * 60 + $sec;
}
$s >= 86400 and $d = sprintf('%i',$s / 86400) and $s = $s % 86400;
$s >= 3600 and $h = sprintf('%i',$s / 3600) and $s = $s % 3600;
$s >= 60 and $m = sprintf('%i',$s / 60) and $s = $s % 60;
print "${d}d ${h}h ${m}m ${s}s\n";
1
2
3
4
5
sub addTimes {
return(sprintf("%d %d:%d:%d", $d=int(($ts=((@ta=split(':', $_[0]))[0]*3600+$ta[1]*60+$ta[2]+(@tb=split(':', $_[1]))[0]*3600+$tb[1]*60+$tb[2]))/86400), $h=int(($ts=$ts-($d*86400))/3600), $m=int(($ts=$ts-($h*3600))/60),$s=int(($ts=$ts-($m*60))/60)));
}
print addTimes('13:45:23', '14:12:43') . "\n";
1
2
3
4
5
sub addTimes {
return(sprintf("%d %d:%d:%d", $d=int(($ts=((@ta=split(':', $_[0]))[0]*3600+$ta[1]*60+$ta[2]+(@tb=split(':', $_[1]))[0]*3600+$tb[1]*60+$tb[2]))/86400), $h=int(($ts=$ts-($d*86400))/3600), $m=int(($ts=$ts-($h*3600))/60),$s=int(($ts=$ts-($m*60))/60)));
}
print addTimes('13:45:23', '14:12:43') . "\n";
print addTimes('13:45:23', '14:12:43', '15:21:23') . "\n";
9 Einträge, 1 Seite |