2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-03-03 16:56:54 +00:00

98_HTTPMOD: extended utils

git-svn-id: https://svn.fhem.de/fhem/trunk@26498 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
StefanStrobel 2022-10-07 17:28:04 +00:00
parent b02e92a3c1
commit 3c88564939

View File

@ -30,6 +30,7 @@ use warnings;
use GPUtils qw(:all);
use Time::HiRes qw(gettimeofday);
use POSIX;
use Encode qw(decode encode);
use Scalar::Util qw(looks_like_number);
use DevIo;
@ -52,6 +53,9 @@ our @EXPORT_OK = qw(UpdateTimer FhemCaller
BodyDecode
IsOpen
FmtTimeMs
FmtDate
DateDiff
date_str2num
ReadableArray
Statistics Profiler
);
@ -332,7 +336,12 @@ sub FhemCaller {
#########################################
# Try to convert a value with a map
# called from Set and FormatReading
# todo: also pass map as named parameter
# map example: 0:mittig, 1:über, 2:unterhalb
# todo: potential extension: 0:mittig, 1:über, 2:unterhalb, *:undefined ??
# or for slave mode and the reverse map: 0:*??
# or better pass new named parameters mapDefault / rmapDefault from new attrs?
sub MapConvert {
my $hash = shift;
my $oRef = shift; # hash ref for passing options and variables for use in expressions
@ -344,7 +353,7 @@ sub MapConvert {
my $inVal = $oRef->{'val'}; # input value
my $name = $hash->{NAME};
return $inVal if (!$map); # don't change anyting if map is empty
return $inVal if (!$map); # don't change anything if map is empty
$map =~ s/\s+/ /g; # substitute all \t \n etc. by one space only
if ($reverse) {
@ -365,8 +374,12 @@ sub MapConvert {
else {
Log3 $name, 3, "$name: MapConvert called from " . FhemCaller() . " did not find $val ($inVal) in" .
($reverse ? " reversed" : "") . " map $map";
return if ($UndefIfNoMatch);
return $inVal;
if (defined($oRef->{'default'})) {
Log3 $name, 3, "$name: MapConvert returns defined default value $oRef->{'default'}";
return $oRef->{'default'};
}
return if ($UndefIfNoMatch); # no match -> return undef because of $UndefIfNoMatch
return $inVal; # no match -> return original value
}
}
@ -805,6 +818,37 @@ sub FmtTimeMs {
}
####################################################
# format time as date string only
sub FmtDate {
my @t = localtime(shift);
return sprintf("%04d-%02d-%02d", $t[5]+1900, $t[4]+1, $t[3]);
}
##################################################################
# get number of days between first and seccond (later) date string
sub DateDiff {
my $d1 = shift; # earlier date
my $d2 = shift // FmtDate(gettimeofday()); # later date
my $d1d = (split (/ /, $d1))[0]; # split time and date part of string
my $d2d = (split (/ /, $d2))[0]; # split time and date part of string
# subtract lastdate as number from todays date as number and divide
my $days = (date_str2num($d2d) - date_str2num($d1d)) / 86400;
return $days;
}
####################################################
# convert date to num
sub date_str2num($) {
my ($str) = @_;
my @a;
@a = split("[T: -]", $str);
return mktime(0,0,0,$a[2],$a[1]-1,$a[0]-1900,0,0,-1);
}
#########################################################
sub ReadableArray {
my $val = shift;