in reply to Re^3: Date/Time Manipulation
in thread Date/Time Manipulation
Be aware that you might not be such a happy camper if the times fall within the hour after midnight! That code matches and captures, in the special variable $1, the two digits following a space, i.e. the hour value. It then executes (e modifier) the sprintf command in the substitution to replace the hour with a decremented value ($1 - 1). The q{ %2.2i} is the format specifier; q{ %02d} would also work. This method will break down if the hour is zero.
The simple way.
$ perl -Mstrict -Mwarnings -E ' my $dateStr = q{2016-05-16 00:51:07}; $dateStr =~ s{ (\d\d)}{ sprintf q{ %2.2i}, $1 - 1 }e; say $dateStr;' 2016-05-16 -01:51:07 $
A little more robust.
$ perl -MTime::Piece -MTime::Seconds -Mstrict -Mwarnings -E ' my $dateStr = q{2016-05-16 00:51:07}; my $dateFmt = q{%Y-%m-%d %H:%M:%S}; my $tp = Time::Piece->strptime( $dateStr, $dateFmt ); $tp -= ONE_HOUR; say $tp->strftime( $dateFmt );' 2016-05-15 23:51:07 $
I hope this is of interest.
Cheers,
JohnGG
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Date/Time Manipulation
by hippo (Archbishop) on May 25, 2016 at 15:27 UTC |