From f9822e7c022366de22db1f58755f5d7445ac04df Mon Sep 17 00:00:00 2001 From: pzien Date: Mon, 18 Jul 2022 09:41:00 +0200 Subject: [PATCH] refactor: Replace moment-timezone with luxon --- package.json | 2 +- src/CalDate.js | 43 +++++++++++++++++++++++++++++++------------ test/CalDate.mocha.js | 18 ++++++++++++++++++ 3 files changed, 50 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index bae270b..3d0d285 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,7 @@ "colors": true }, "dependencies": { - "moment-timezone": "^0.5.34" + "luxon": "^3.0.1" }, "devDependencies": { "c8": "^7.11.3", diff --git a/src/CalDate.js b/src/CalDate.js index 96b3095..6869dd5 100644 --- a/src/CalDate.js +++ b/src/CalDate.js @@ -1,5 +1,4 @@ - -import moment from 'moment-timezone' +import { DateTime } from 'luxon' import { toYear, toNumber, isDate, pad0 } from './utils.js' const PROPS = ['year', 'month', 'day', 'hour', 'minute', 'second'] @@ -182,7 +181,22 @@ export class CalDate { */ toTimezone (timezone) { if (timezone) { - return new Date(moment.tz(this.toString(), timezone).format()) + return DateTime.fromObject( + { + day: this.day, + month: this.month, + year: this.year, + hour: this.hour, + minute: this.minute, + second: this.second, + millisecond: 0 + }, + { + zone: timezone + } + ) + .toUTC() + .toJSDate() } else { return this.toDate() } @@ -196,13 +210,13 @@ export class CalDate { */ fromTimezone (dateUTC, timezone) { if (timezone) { - const m = moment.tz(dateUTC, timezone) - this.year = m.year() - this.month = m.month() + 1 - this.day = m.date() - this.hour = m.hours() - this.minute = m.minutes() - this.second = m.seconds() + const m = DateTime.fromJSDate(dateUTC, { zone: timezone }) + this.year = m.year + this.month = m.month + this.day = m.day + this.hour = m.hour + this.minute = m.minute + this.second = m.second } else { this.set(dateUTC) } @@ -215,8 +229,13 @@ export class CalDate { */ toDate () { return new Date( - this.year, this.month - 1, this.day, - this.hour, this.minute, this.second, 0 + this.year, + this.month - 1, + this.day, + this.hour, + this.minute, + this.second, + 0 ) } diff --git a/test/CalDate.mocha.js b/test/CalDate.mocha.js index b32d4fa..e2da0a1 100644 --- a/test/CalDate.mocha.js +++ b/test/CalDate.mocha.js @@ -96,6 +96,24 @@ describe('#CalDate', function () { assert.strictEqual(res, '2000-01-01T05:00:00.000Z') }) + it('can move date by timezone Tokyo (march 2015)', function () { + const caldate = new CalDate(new Date('2015-03-21 00:00:00')) + const res = caldate.toTimezone('Asia/Tokyo').toISOString() + assert.strictEqual(res, '2015-03-20T15:00:00.000Z') + }) + + it('can move date by timezone Tokyo (sepetember 2015)', function () { + const caldate = new CalDate(new Date('2015-09-23 00:00:00')) + const res = caldate.toTimezone('Asia/Tokyo').toISOString() + assert.strictEqual(res, '2015-09-22T15:00:00.000Z') + }) + + it('can move date by timezone Tokyo (sepetember 2021)', function () { + const caldate = new CalDate(new Date('2021-09-23 00:00:00')) + const res = caldate.toTimezone('Asia/Tokyo').toISOString() + assert.strictEqual(res, '2021-09-22T15:00:00.000Z') + }) + it('can return date in current timezone', function () { const caldate = new CalDate({ year: 2000, month: 1, day: 1 }) const exp = new Date('2000-01-01 00:00:00')