From 018eafb77030dc75d5db21c1bd40719a18a14939 Mon Sep 17 00:00:00 2001 From: Guillem Jover Date: Thu, 17 Oct 2019 23:49:39 +0200 Subject: [PATCH] TT#69200 Use Time::Piece instead of ad-hoc code to print dates and times This is a core module, which requires no additional dependencies. Change-Id: I0830f022788a5e7b69b0afa462654fabf277e254 --- lib/NGCP/BulkProcessor/Utils.pm | 76 +++++++++++++-------------------- t/Utils.t | 23 +++++++++- 2 files changed, 51 insertions(+), 48 deletions(-) diff --git a/lib/NGCP/BulkProcessor/Utils.pm b/lib/NGCP/BulkProcessor/Utils.pm index 1bc79b6..2c79282 100644 --- a/lib/NGCP/BulkProcessor/Utils.pm +++ b/lib/NGCP/BulkProcessor/Utils.pm @@ -24,6 +24,7 @@ use Net::Domain qw(hostname hostfqdn hostdomain); use Cwd qw(abs_path); #use File::Basename qw(fileparse); +use Time::Piece; use Time::Seconds; use Date::Manip qw(Date_Init ParseDate UnixDate); #Date_Init('Language=English','DateFormat=non-US'); @@ -167,17 +168,40 @@ sub check_bool { } sub timestampdigits { + return localtime(shift)->strftime('%Y%m%d%H%M%S'); +} - my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); - return sprintf "%4d%02d%02d%02d%02d%02d",$year+1900,$mon+1,$mday,$hour,$min,$sec; +sub datestampdigits { + return localtime(shift)->strftime('%Y%m%d'); +} +sub timestamp { + return localtime(shift)->strftime('%Y-%m-%d %H:%M:%S'); } -sub datestampdigits { +# Compat alias +sub timestamp_fromepochsecs { + return timestamp(shift); +} + +sub datestamp { + return localtime(shift)->strftime('%Y-%m-%d'); +} + +sub get_year { + return localtime(shift)->strftime('%Y'); +} + +sub get_year_month { + my $t = localtime(shift); - my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); - return sprintf "%4d%02d%02d",$year+1900,$mon+1,$mday; + return ($t->strftime('%Y'), $t->strftime('%m')); +} + +sub get_year_month_day { + my $t = localtime(shift); + return ($t->strftime('%Y'), $t->strftime('%m'), $t->strftime('%d')); } sub parse_datetime { @@ -397,48 +421,6 @@ sub urldecode { return $urltodecode; } -sub timestamp { - - my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); - return sprintf "%4d-%02d-%02d %02d:%02d:%02d",$year+1900,$mon+1,$mday,$hour,$min,$sec; - -} - -sub timestamp_fromepochsecs { - - my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(shift); - return sprintf "%4d-%02d-%02d %02d:%02d:%02d",$year+1900,$mon+1,$mday,$hour,$min,$sec; - -} - -sub datestamp { - - my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); - return sprintf "%4d-%02d-%02d",$year+1900,$mon+1,$mday; - -} - -sub get_year { - - my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); - return (sprintf "%4d",$year+1900); - -} - -sub get_year_month { - - my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); - return ((sprintf "%4d",$year+1900),(sprintf "%02d",$mon+1)); - -} - -sub get_year_month_day { - - my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); - return ((sprintf "%4d",$year+1900),(sprintf "%02d",$mon+1),(sprintf "%02d",$mday)); - -} - sub zerofill { my ($integer,$digits) = @_; diff --git a/t/Utils.t b/t/Utils.t index b7d796b..bfcfa03 100644 --- a/t/Utils.t +++ b/t/Utils.t @@ -16,13 +16,22 @@ use strict; use warnings; -use Test::More tests => 10; +use Test::More tests => 18; +use Time::Local; require_ok('NGCP::BulkProcessor::Utils'); NGCP::BulkProcessor::Utils->import(qw( zerofill secs_to_years + timestampdigits + datestampdigits + timestamp + timestamp_fromepochsecs + datestamp + get_year + get_year_month + get_year_month_day )); # zerofill() @@ -37,3 +46,15 @@ is(secs_to_years(3661), '1 hour, 1 minute, 1 second'); is(secs_to_years(7322), '2 hours, 2 minutes, 2 seconds'); is(secs_to_years(86461), '1 day, 0 hours, 1 minute, 1 second'); is(secs_to_years(691261), '8 days, 0 hours, 1 minute, 1 second'); + +# time functions +my $time = timelocal(58, 59, 23, 2, 10, 2042); + +is(timestampdigits($time), '20421102235958'); +is(datestampdigits($time), '20421102'); +is(timestamp($time), '2042-11-02 23:59:58'); +is(timestamp_fromepochsecs($time), '2042-11-02 23:59:58'); +is(datestamp($time), '2042-11-02'); +is(get_year($time), '2042'); +is_deeply([ get_year_month($time) ], [ '2042', '11' ]); +is_deeply([ get_year_month_day($time) ], [ '2042', '11', '02' ]);