From 66b3041f5b0b033d44b6619d7d927bfe3314d961 Mon Sep 17 00:00:00 2001 From: Patrick Menschel Date: Tue, 28 Dec 2021 09:49:52 +0100 Subject: [PATCH] Feat: Use datetime for time conversion drop dependency Posix::strptime in favor of Datetime::Format::Strptime Signed-off-by: Patrick Menschel --- iec1107.pm | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/iec1107.pm b/iec1107.pm index 667c73a..5439bd1 100755 --- a/iec1107.pm +++ b/iec1107.pm @@ -4,7 +4,7 @@ # This is my first my perl module and these resources were a good kickstart # https://learn.perl.org/books/beginning-perl/ # 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 @@ -15,8 +15,7 @@ use Carp; use Device::SerialPort; #for time conversion -use POSIX::strptime qw( strptime ); -use POSIX qw{strftime}; +use DateTime::Format::Strptime qw( strptime ); #constants our $SOH = chr(0x01); @@ -34,15 +33,15 @@ our $ENDCHARACTER = "!"; our %drs110m_values = ( #''=>[
,,''], - 'Voltage' =>[ 0,\&_scale_div_by_10, 'V'], - 'Current' =>[ 1,\&_scale_div_by_10, 'A'], - 'Frequency' =>[ 2,\&_scale_div_by_10, 'Hz'], - 'Active Power' =>[ 3, \&_scale_mul_by_10, 'W'], - 'Reactive Power'=>[ 4, \&_scale_mul_by_10,'VAr'], - 'Apparent Power'=>[ 5, \&_scale_mul_by_10, 'VA'], - 'Active Energy' =>[10, \&_scale_1_to_1, 'Wh'], - 'Time' =>[31, \&_scale_to_time, ''], - 'Temperature' =>[32, \&_scale_to_temp, '°C'], + 'Voltage' =>[ 0, \&_scale_div_by_10, 'V'], + 'Current' =>[ 1, \&_scale_div_by_10, 'A'], + 'Frequency' =>[ 2, \&_scale_div_by_10, 'Hz'], + 'Active Power' =>[ 3, \&_scale_mul_by_10, 'W'], + 'Reactive Power'=>[ 4, \&_scale_mul_by_10,'VAr'], + 'Apparent Power'=>[ 5, \&_scale_mul_by_10, 'VA'], + 'Active Energy' =>[10, \&_scale_1_to_1, 'Wh'], + 'Time' =>[31, \&_scale_raw_time_to_datetime, ''], + 'Temperature' =>[32, \&_scale_to_temp, '°C'], ); #actually there are more registers, but who cares about cosphi for example?! @@ -177,16 +176,11 @@ sub _scale_1_to_1($){ return $val; }; -sub _scale_to_time($){ +sub _scale_raw_time_to_datetime($){ my ($str) = @_; - my $fmt = "%y%m%d0%w%H%M%S"; - my @time = (POSIX::strptime($str,$fmt))[0..7]; - if (wantarray){ - return @time; - } - else{ - return strftime("%Y-%m-%d %H:%M:%S",@time); - }; + my $fmt = "%y%m%d%w%H%M%S"; + my $dt = strptime($fmt, $str); + return $dt; }; sub _scale_to_temp($){