From 76011ad14e69a155fb8630088ea8a949407a1c77 Mon Sep 17 00:00:00 2001 From: shaodowm Date: Thu, 5 Dec 2024 19:36:47 +0800 Subject: [PATCH] fix: update offset error with daylight saving time --- moment-timezone.js | 8 ++++++++ tests/moment-timezone/manipulate.js | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/moment-timezone.js b/moment-timezone.js index faaff9b1..841497c9 100644 --- a/moment-timezone.js +++ b/moment-timezone.js @@ -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 { diff --git a/tests/moment-timezone/manipulate.js b/tests/moment-timezone/manipulate.js index a448a95b..7868c52d 100644 --- a/tests/moment-timezone/manipulate.js +++ b/tests/moment-timezone/manipulate.js @@ -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',