Skip to content

Commit

Permalink
fix: update offset error with daylight saving time
Browse files Browse the repository at this point in the history
  • Loading branch information
shaodowm committed Dec 5, 2024
1 parent 4b7ce20 commit 76011ad
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
8 changes: 8 additions & 0 deletions moment-timezone.js
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,15 @@
offset = offset / 60;
}
if (mom.utcOffset !== undefined) {
var _offset = mom._offset;
var z = mom._z;
if (keepTime && typeof keepTime === 'number' && _offset && -offset > _offset) {
var preMom = moment(mom.valueOf() - (-offset - _offset) * 60 * 1000);
var preOffset = mom._z.utcOffset(preMom);
if (offset !== preOffset) {
keepTime = false;
}
}
mom.utcOffset(-offset, keepTime);
mom._z = z;
} else {
Expand Down
20 changes: 20 additions & 0 deletions tests/moment-timezone/manipulate.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,26 @@ exports.manipulate = {
},

add : function (t) {
t.equal(
moment('2024-09-07T00:00:00-04:00').tz('America/Santiago').add(1, 'days').format(),
'2024-09-08T01:00:00-03:00',
"adding 1 day while crossing a DST boundary should not affect time."
);
t.equal(
moment('2024-09-07T04:00:00-04:00').tz('America/Santiago').add(1, 'days').format(),
'2024-09-08T04:00:00-03:00',
"adding 1 day while crossing a DST boundary should not affect time."
);
t.equal(
moment('2024-08-08T00:00:00-04:00').tz('America/Santiago').add(1, 'months').format(),
'2024-09-08T01:00:00-03:00',
"adding 1 day while crossing a DST boundary should not affect time."
);
t.equal(
moment('2024-08-08T04:00:00-04:00').tz('America/Santiago').add(1, 'months').format(),
'2024-09-08T04:00:00-03:00',
"adding 1 day while crossing a DST boundary should not affect time."
);
t.equal(
moment('2012-10-28 00:00:00+01:00').tz('Europe/London').add(1, 'days').format(),
'2012-10-29T00:00:00Z',
Expand Down

0 comments on commit 76011ad

Please sign in to comment.