mirror of
https://github.com/fhem/fhem-mirror.git
synced 2025-04-24 15:19:21 +00:00
UConv.pm: add ::duration()
git-svn-id: https://svn.fhem.de/fhem/trunk@17589 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
parent
0ad25b338a
commit
575023a6ed
@ -8,6 +8,7 @@ use Scalar::Util qw(looks_like_number);
|
||||
use POSIX qw(strftime);
|
||||
use Data::Dumper;
|
||||
|
||||
|
||||
####################
|
||||
# Translations
|
||||
|
||||
@ -728,6 +729,30 @@ sub distance($$$$;$$) {
|
||||
( $unit eq "nmi" ? km2nmi($km) : ( $unit ? km2mi($km) : $km ) ), $rnd );
|
||||
}
|
||||
|
||||
sub duration ($$;$) {
|
||||
my ( $datetimeNow, $datetimeOld, $format ) = @_;
|
||||
|
||||
if ( $datetimeNow eq "" || $datetimeOld eq "" ) {
|
||||
$datetimeNow = "1970-01-01 00:00:00";
|
||||
$datetimeOld = "1970-01-01 00:00:00";
|
||||
}
|
||||
|
||||
my $timestampNow = main::time_str2num($datetimeNow);
|
||||
my $timestampOld = main::time_str2num($datetimeOld);
|
||||
my $timeDiff = $timestampNow - $timestampOld;
|
||||
|
||||
# return seconds
|
||||
return _round( $timeDiff, 0 )
|
||||
if ( defined($format) && $format eq "sec" );
|
||||
|
||||
# return minutes
|
||||
return _round( $timeDiff / 60, 0 )
|
||||
if ( defined($format) && $format eq "min" );
|
||||
|
||||
# return human readable format
|
||||
return s2hms( _round( $timeDiff, 0 ) );
|
||||
}
|
||||
|
||||
#################################
|
||||
### Textual unit conversions
|
||||
###
|
||||
|
Loading…
x
Reference in New Issue
Block a user