This bash
script has:
future="${1:-Dec 08 2017 22:00:00}"
t1=$(date -j -f "%b %d %Y %H:%M:%S" "$future" +%s)
t0=$(date +%s)
echo "Current: $(date)"
echo "Future : $future"
echo "Diff : $(( $t1 - $t0 )) secs"
He prints:
Current: pi 8. december 2017 21:25:25 CET
Future : Dec 08 2017 22:00:00
Diff : 2075 secs
The result (diff) is correct.
Now try to do the same with perl:
use strict;
use warnings;
use feature 'say';
use Time::Piece;
my $format = '%b %d %Y %H:%M:%S';
my $future = shift // 'Dec 08 2017 22:00:00';
say "Future: $future";
say "localtime: ", scalar localtime();
say "gmtime : ", scalar gmtime();
my $tf = Time::Piece->strptime($future, $format);
say 'localtime-diff : ', $tf-localtime();
say 'gmtime-diff : ', $tf-gmtime();
he prints
Future: Dec 08 2017 22:00:00
localtime: Fri Dec 8 21:27:45 2017 #correct
gmtime : Fri Dec 8 20:27:45 2017 #correct
localtime-diff : 5535 #incorrect (expecting 3600 secs less)
gmtime-diff : 5535 #ok
What's wrong? So why does it print the same diff for localtime
and gmtime
, but scalar localtime
also scalar gmtime
print different (and correct) lines?
EDIT: So, the main question: how to get the same result as in bash using perl?
source
share