Dual-Life / Time-Piece

Object Oriented time objects
Other
15 stars 33 forks source link

Date math between DST is wrong #46

Open smith153 opened 4 years ago

smith153 commented 4 years ago

This code:

use Time::Piece;
use Time::Seconds;

my $t = localtime(1583731856);

print "Current date: $t with hour offset: " . $t->tzoffset / 60 / 60 . "\n";
my $t2 = $t - ONE_DAY;

print "Minus 24 hours is: $t2 with hour offset: " . $t2->tzoffset / 60 / 60 . "\n";

Outputs this:

Current date: Mon Mar  9 00:30:56 2020 with hour offset: -5
Minus 24 hours is: Sat Mar  7 23:30:56 2020 with hour offset: -6

The correct date should be "Sun Mar 8 00:30:56 2020". The problem is there are only 23 hours in that day and we are subtracting 24.

targzeta commented 1 year ago

I can confirm this issue

use Time::Piece;

my $t1 = localtime(1667080800);
my $t2 = $t1 + Time::Seconds::ONE_DAY;

print "T1: $t1\nT2: $t2\n";

Output:

T1: Sun Oct 30 00:00:00 2022
T2: Sun Oct 30 23:00:00 2022

T2 should be Mon Oct 31 00:00:00 2022.

On the Sun Oct 30 at the 03:00 AM we have moved the clock back of one hour so the day has been 25 hours. How can be fix this issue?

Thank you in advance, Emanuele