From 84a366ef6435608a13a5fb31106b99d510fe3f2d Mon Sep 17 00:00:00 2001 From: Andinus Date: Sat, 6 Jun 2020 15:39:44 +0530 Subject: Remove DateTime, use Time::Moment instead After removing all DateTime code it took ~0.98s to run, before that it was ~1.2s. And after removing 'use DateTime' it now takes ~0.30s to run, all of this is without File::Fetch part, it loaded the data from disk instead of fetching. --- ara.pl | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/ara.pl b/ara.pl index 29a828a..9ec1f4d 100755 --- a/ara.pl +++ b/ara.pl @@ -4,7 +4,6 @@ use strict; use warnings; use feature 'say'; -use DateTime qw( ); use Path::Tiny; use Time::Moment; use JSON::MaybeXS qw( decode_json ); @@ -84,22 +83,25 @@ my $state_notes = Text::ASCIITable->new( { drawRowLine => 1 } ); $state_notes->setCols( 'State', 'Notes' ); $state_notes->setColWidth( 'Notes', 84 ); -my $today = DateTime->now( time_zone => 'Asia/Kolkata' ); +my $today = Time::Moment + ->now_utc + ->plus_hours(5) + ->plus_minutes(30); # Current time in 'Asia/Kolkata' TimeZone. # Add first 37 entries to $rows. foreach my $i (0...37) { my $update_info; - my $lastupdatedtime = $statewise->[$i]{'lastupdatedtime'}; + my $lastupdatedtime = $statewise->[$i]{lastupdatedtime}; my $last_update_dmy = substr( $lastupdatedtime, 0, 10 ); # Add $update_info. - if ( $last_update_dmy eq $today->dmy('/') ) { + if ( $last_update_dmy eq $today->strftime( "%d/%m/%Y" ) ) { $update_info = "Today"; } elsif ( $last_update_dmy eq - $today->clone->subtract( days => 1 )->dmy('/') ) { + $today->minus_days(1)->strftime( "%d/%m/%Y" ) ) { $update_info = "Yesterday"; } elsif ( $last_update_dmy eq - $today->clone->add( days => 1 )->dmy('/') ) { + $today->plus_days(1)->strftime( "%d/%m/%Y" ) ) { $update_info = "Tomorrow"; # Hopefully we don't see this. } else { $update_info = @@ -108,11 +110,11 @@ foreach my $i (0...37) { substr( $lastupdatedtime, 0, 2 ); } - my $state = $statewise->[$i]{'state'}; + my $state = $statewise->[$i]{state}; $state = "India" if $state eq "Total"; - $state = $statewise->[$i]{'statecode'} if + $state = $statewise->[$i]{statecode} if length($state) > 16; my $confirmed = "$statewise->[$i]{confirmed}"; @@ -129,7 +131,7 @@ foreach my $i (0...37) { push @$covid_19_data, [ $state, $confirmed, - $statewise->[$i]{'active'}, + $statewise->[$i]{active}, $recovered, $deaths, $update_info, -- cgit 1.4.1-2-gfad0