Skip to content

Commit 7410a3e

Browse files
committed
refactor: Replace moment-timezone with luxon
1 parent e04dfea commit 7410a3e

3 files changed

Lines changed: 50 additions & 13 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"colors": true
5555
},
5656
"dependencies": {
57-
"moment-timezone": "^0.5.34"
57+
"luxon": "^3.0.1"
5858
},
5959
"devDependencies": {
6060
"c8": "^7.11.3",

src/CalDate.js

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
2-
import moment from 'moment-timezone'
1+
import { DateTime } from 'luxon'
32
import { toYear, toNumber, isDate, pad0 } from './utils.js'
43

54
const PROPS = ['year', 'month', 'day', 'hour', 'minute', 'second']
@@ -182,7 +181,22 @@ export class CalDate {
182181
*/
183182
toTimezone (timezone) {
184183
if (timezone) {
185-
return new Date(moment.tz(this.toString(), timezone).format())
184+
return DateTime.fromObject(
185+
{
186+
day: this.day,
187+
month: this.month,
188+
year: this.year,
189+
hour: this.hour,
190+
minute: this.minute,
191+
second: this.second,
192+
millisecond: 0,
193+
},
194+
{
195+
zone: timezone,
196+
}
197+
)
198+
.toUTC()
199+
.toJSDate()
186200
} else {
187201
return this.toDate()
188202
}
@@ -196,13 +210,13 @@ export class CalDate {
196210
*/
197211
fromTimezone (dateUTC, timezone) {
198212
if (timezone) {
199-
const m = moment.tz(dateUTC, timezone)
200-
this.year = m.year()
201-
this.month = m.month() + 1
202-
this.day = m.date()
203-
this.hour = m.hours()
204-
this.minute = m.minutes()
205-
this.second = m.seconds()
213+
const m = DateTime.fromJSDate(dateUTC, { zone: timezone })
214+
this.year = m.year
215+
this.month = m.month
216+
this.day = m.day
217+
this.hour = m.hour
218+
this.minute = m.minute
219+
this.second = m.second
206220
} else {
207221
this.set(dateUTC)
208222
}
@@ -215,8 +229,13 @@ export class CalDate {
215229
*/
216230
toDate () {
217231
return new Date(
218-
this.year, this.month - 1, this.day,
219-
this.hour, this.minute, this.second, 0
232+
this.year,
233+
this.month - 1,
234+
this.day,
235+
this.hour,
236+
this.minute,
237+
this.second,
238+
0
220239
)
221240
}
222241

test/CalDate.mocha.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,24 @@ describe('#CalDate', function () {
9696
assert.strictEqual(res, '2000-01-01T05:00:00.000Z')
9797
})
9898

99+
it('can move date by timezone Tokyo (march 2015)', function () {
100+
const caldate = new CalDate(new Date('2015-03-21 00:00:00'))
101+
const res = caldate.toTimezone('Asia/Tokyo').toISOString()
102+
assert.strictEqual(res, '2015-03-20T15:00:00.000Z')
103+
})
104+
105+
it('can move date by timezone Tokyo (sepetember 2015)', function () {
106+
const caldate = new CalDate(new Date('2015-09-23 00:00:00'))
107+
const res = caldate.toTimezone('Asia/Tokyo').toISOString()
108+
assert.strictEqual(res, '2015-09-22T15:00:00.000Z')
109+
})
110+
111+
it('can move date by timezone Tokyo (sepetember 2021)', function () {
112+
const caldate = new CalDate(new Date('2021-09-23 00:00:00'))
113+
const res = caldate.toTimezone('Asia/Tokyo').toISOString()
114+
assert.strictEqual(res, '2021-09-22T15:00:00.000Z')
115+
})
116+
99117
it('can return date in current timezone', function () {
100118
const caldate = new CalDate({ year: 2000, month: 1, day: 1 })
101119
const exp = new Date('2000-01-01 00:00:00')

0 commit comments

Comments
 (0)