-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timezone.ts
309 lines (292 loc) · 7.77 KB
/
Timezone.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import { DatePartResponse } from './types'
import { padNumber } from './utils'
/**
* The base class for timezones.
*
* Two timezones are provided by default: {@link tzLocal} and {@link tzUtc}.
* There is also a class {@link IANATimezone} which can be used with the time
* zone database maintained by IANA.
* All timezone sensitive operations take a timezone as an optional parameter
* which defaults to tzLocal.
*
* There is a tutorial [here](../../pages/guide/timezones.html).
*
* The timezone object provides accessors for the common properties of a date
* such as {@link Timezone.year}. Here is an example of using the year.
*
* ```js
* import { tzLocal, tzUtc } from '@jetblack/date'
*
* const utcMillennium = new Date("2000-01-01T00:00:00Z")
*
* // The UTC year will always be 2000
* console.log(tzUtc.year(utcMillennium) === 2000)
*
* // The year in the local timezone will depend where you are.
* // If the timezone was New York ...
* console.log(tzLocal.year(utcMillennium) === 1999)
* ```
*
* There are two complimentary methods used for date construction:
* {@link Timezone.makeDate | makeDate} and {@link Timezone.dateParts | dateParts}.
* These are used by the library functions to efficiently deconstruct and
* construct dates for performing calculations.
*
* There are two functions specific to timezones:
* {@link Timezone.isDaylightSavings | isDaylightSavings} and {@link Timezone.toISOString | toISOString}.
* The JavaScript built in [Date.toISOString](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString)
* is only aware of the UTC timezone. This method provides support for any
* defined timezone.
*
* @category Timezone
*/
export abstract class Timezone {
/** @ignore */
#name: string
/**
* Construct a new timezone.
*
* @param name The timezone name.
*/
constructor(name: string) {
this.#name = name
}
/**
* Get the name of the timezone.
*/
get name(): string {
return this.#name
}
/**
* Create a date from its component parts.
*
* @param year The year.
* @param monthIndex The month index where January is 0.
* @param day The day of the month.
* @param hours The hour of the day.
* @param minutes The minute of the day.
* @param seconds The second of the day.
* @param milliseconds The millisecond of the day.
* @returns A new date built from the parts.
*/
abstract makeDate(
year: number,
monthIndex: number,
day?: number,
hours?: number,
minutes?: number,
seconds?: number,
milliseconds?: number
): Date
/**
* Extract the date parts.
*
* If more than one date part is required this is the preferred way to get
* them, as the timezone calculation is only performed once.
*
* ```js
* const {
* year,
* monthIndex,
* day,
* weekday,
* hours,
* minutes,
* seconds,
* milliseconds
* } = tz.dateParts(date)
* ```
*
* @param date The date.
* @param request The request.
* @returns The date parts.
*/
abstract dateParts(date: Date): DatePartResponse
/**
* The signed offset in minutes from UTC for the given date.
*
* @param date The date.
*/
abstract offset(date: Date): number
/**
* The year for the date.
*
* ```js
* import { tzUtc } from '@jetblack/date'
*
* const date = tzUtc.makeDate(2000, 0, 1)
* console.log(tzUtc.year(date))
* // returns 2000
* ```
*
* @param date The date.
* @returns The year.
*/
abstract year(date: Date): number
/**
* The month index for the given date where 0 is January.
*
* ```js
* import { tzUtc } from '@jetblack/date'
*
* const date = tzUtc.makeDate(2000, 0, 1)
* console.log(tzUtc.monthIndex(date))
* // returns 0
* ```
*
* @param date The date.
* @returns The month index of the date where 0 is January.
*/
abstract monthIndex(date: Date): number
/**
* The day of the week for the given date where 0 is Sunday.
*
* ```js
* import { tzUtc } from '@jetblack/date'
*
* const date = tzUtc.makeDate(2000, 0, 1)
* console.log(tzUtc.weekday(date))
* // returns 6
* ```
*
* @param date The date.
* @returns The day of the week where 0 is Sunday.
*/
abstract weekday(date: Date): number
/**
* The day of the month for the given date.
*
* ```js
* import { tzUtc } from '@jetblack/date'
*
* const date = tzUtc.makeDate(2000, 0, 1)
* console.log(tzUtc.day(date))
* // returns 1
* ```
*
* @param date The date.
* @returns The day of the month.
*/
abstract day(date: Date): number
/**
* The hour of the day for the given date.
*
* ```js
* import { tzUtc } from '@jetblack/date'
*
* const date = tzUtc.makeDate(2000, 0, 1, 12, 15, 30, 123)
* console.log(tzUtc.hours(date))
* // returns 12
* ```
*
* @param date The date.
* @returns The hour of the day.
*/
abstract hours(date: Date): number
/**
* The minute of the day for the given date.
*
* ```js
* import { tzUtc } from '@jetblack/date'
*
* const date = tzUtc.makeDate(2000, 0, 1, 12, 15, 30, 123)
* console.log(tzUtc.minutes(date))
* // returns 15
* ```
*
* @param date The date.
* @returns The minute of the day.
*/
abstract minutes(date: Date): number
/**
* The second of the day for a given date.
*
* ```js
* import { tzUtc } from '@jetblack/date'
*
* const date = tzUtc.makeDate(2000, 0, 1, 12, 15, 30, 123)
* console.log(tzUtc.seconds(date))
* // returns 30
* ```
*
* @param date The date.
* @returns The second of the day.
*/
abstract seconds(date: Date): number
/**
* The millisecond of the day for a given date.
*
* ```js
* import { tzUtc } from '@jetblack/date'
*
* const date = tzUtc.makeDate(2000, 0, 1, 12, 15, 30, 123)
* console.log(tzUtc.milliseconds(date))
* // returns 123
* ```
*
* @param date The date.
* @returns The milliseconds of the day.
*/
abstract milliseconds(date: Date): number
/**
* Find if the date was subject to daylight savings time.
*
* @param date The date.
* @returns True if the date was subject to daylight savings time.
*/
abstract isDaylightSavings(date: Date): boolean
/**
* Makes a new date by taking the date parts using this timezone and making the
* date with the supplied time zone.
*
* @param date A date
* @param tz A timezone
* @returns A new date using the date parts from this timezone, constructed with the supplied timezone.
*/
as(date: Date, tz: Timezone): Date {
const { year, monthIndex, day, hours, minutes, seconds, milliseconds } =
this.dateParts(date)
return tz.makeDate(
year,
monthIndex,
day,
hours,
minutes,
seconds,
milliseconds
)
}
/**
* The ISO 8601 date string representation for a given date.
*
* @param date The date.
* @returns The ISO date string.
*/
toISOString(date: Date) {
const { year, monthIndex, day, hours, minutes, seconds, milliseconds } =
this.dateParts(date)
const offset = this.offset(date)
const offsetSign = Math.sign(offset)
const offsetHours = offsetSign * Math.trunc(offset / 60)
const offsetMinutes = offsetSign * (offset % 60)
const datePart =
padNumber(year, 4) +
'-' +
padNumber(monthIndex + 1, 2) +
'-' +
padNumber(day, 2)
const timePart =
padNumber(hours, 2) +
':' +
padNumber(minutes, 2) +
':' +
padNumber(seconds, 2) +
(milliseconds !== 0 ? '.' + padNumber(milliseconds, 3) : '')
const offsetPart =
(offsetSign === -1 ? '-' : '+') +
padNumber(offsetHours, 2) +
':' +
padNumber(offsetMinutes, 2)
return datePart + 'T' + timePart + offsetPart
}
}