Feat: Use datetime for time conversion

drop dependency
Posix::strptime
in favor of
Datetime::Format::Strptime

Signed-off-by: Patrick Menschel <menschel.p@posteo.de>
This commit is contained in:
Patrick Menschel 2021-12-28 09:49:52 +01:00
parent f67726828b
commit 66b3041f5b

View File

@ -4,7 +4,7 @@
# This is my first my perl module and these resources were a good kickstart # This is my first my perl module and these resources were a good kickstart
# https://learn.perl.org/books/beginning-perl/ # https://learn.perl.org/books/beginning-perl/
# https://wiki.volkszaehler.org/hardware/channels/meters/power/eastron_drs155m # https://wiki.volkszaehler.org/hardware/channels/meters/power/eastron_drs155m
# Menschel (C) 2020 # Menschel (C) 2020-2021
package iec1107; # we name our package iec1107 as this is the original protocol name package iec1107; # we name our package iec1107 as this is the original protocol name
@ -15,8 +15,7 @@ use Carp;
use Device::SerialPort; use Device::SerialPort;
#for time conversion #for time conversion
use POSIX::strptime qw( strptime ); use DateTime::Format::Strptime qw( strptime );
use POSIX qw{strftime};
#constants #constants
our $SOH = chr(0x01); our $SOH = chr(0x01);
@ -41,7 +40,7 @@ our %drs110m_values = (
'Reactive Power'=>[ 4, \&_scale_mul_by_10,'VAr'], 'Reactive Power'=>[ 4, \&_scale_mul_by_10,'VAr'],
'Apparent Power'=>[ 5, \&_scale_mul_by_10, 'VA'], 'Apparent Power'=>[ 5, \&_scale_mul_by_10, 'VA'],
'Active Energy' =>[10, \&_scale_1_to_1, 'Wh'], 'Active Energy' =>[10, \&_scale_1_to_1, 'Wh'],
'Time' =>[31, \&_scale_to_time, ''], 'Time' =>[31, \&_scale_raw_time_to_datetime, ''],
'Temperature' =>[32, \&_scale_to_temp, '°C'], 'Temperature' =>[32, \&_scale_to_temp, '°C'],
); );
#actually there are more registers, but who cares about cosphi for example?! #actually there are more registers, but who cares about cosphi for example?!
@ -177,16 +176,11 @@ sub _scale_1_to_1($){
return $val; return $val;
}; };
sub _scale_to_time($){ sub _scale_raw_time_to_datetime($){
my ($str) = @_; my ($str) = @_;
my $fmt = "%y%m%d0%w%H%M%S"; my $fmt = "%y%m%d%w%H%M%S";
my @time = (POSIX::strptime($str,$fmt))[0..7]; my $dt = strptime($fmt, $str);
if (wantarray){ return $dt;
return @time;
}
else{
return strftime("%Y-%m-%d %H:%M:%S",@time);
};
}; };
sub _scale_to_temp($){ sub _scale_to_temp($){