@yangsaiyam/helper 1.5.0 → 1.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -1
- package/src/time/index.test.ts +37 -3
- package/src/time/index.ts +33 -20
package/package.json
CHANGED
package/src/time/index.test.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
|
+
convertTimestampToTimezone,
|
|
4
5
|
TIMEZONE,
|
|
6
|
+
TIMEZONE_CURRENT_UTC_OFFSET,
|
|
5
7
|
TIMEZONE_TO_UTC,
|
|
6
8
|
normalizeTimezone,
|
|
7
9
|
timezoneLabelKey,
|
|
@@ -21,7 +23,14 @@ assert.equal(timezoneToUtc("UTC"), "UTC+00:00");
|
|
|
21
23
|
assert.equal(timezoneToUtc("Etc/UTC"), "UTC+00:00");
|
|
22
24
|
assert.equal(timezoneToUtc("Asia/Kuala_Lumpur"), "UTC+08:00");
|
|
23
25
|
assert.equal(timezoneToUtc("kuala lumpur"), "UTC+08:00");
|
|
24
|
-
assert.equal(
|
|
26
|
+
assert.equal(
|
|
27
|
+
timezoneToUtc("America/New_York", Date.UTC(2024, 0, 1, 12, 0, 0)),
|
|
28
|
+
"UTC-05:00",
|
|
29
|
+
);
|
|
30
|
+
assert.equal(
|
|
31
|
+
timezoneToUtc("America/New_York", Date.UTC(2024, 6, 1, 12, 0, 0)),
|
|
32
|
+
"UTC-04:00",
|
|
33
|
+
);
|
|
25
34
|
assert.equal(timezoneToUtc("unknown"), undefined);
|
|
26
35
|
|
|
27
36
|
assert.equal(timezoneLabelKey("UTC"), "time.timezone.utc");
|
|
@@ -38,5 +47,30 @@ assert.deepEqual(TIMEZONE[0], {
|
|
|
38
47
|
});
|
|
39
48
|
assert.equal(TIMEZONE[2]?.value, "Asia/Singapore");
|
|
40
49
|
|
|
41
|
-
assert.equal(
|
|
42
|
-
assert.equal(
|
|
50
|
+
assert.equal(TIMEZONE_CURRENT_UTC_OFFSET.UTC, "UTC+00:00");
|
|
51
|
+
assert.equal(TIMEZONE_CURRENT_UTC_OFFSET["Asia/Kuala_Lumpur"], "UTC+08:00");
|
|
52
|
+
assert.equal(TIMEZONE_TO_UTC, TIMEZONE_CURRENT_UTC_OFFSET);
|
|
53
|
+
|
|
54
|
+
const utcNineAm = Date.UTC(2024, 0, 1, 9, 0, 0);
|
|
55
|
+
const klNineAm = Date.UTC(2024, 0, 1, 1, 0, 0);
|
|
56
|
+
|
|
57
|
+
assert.equal(
|
|
58
|
+
convertTimestampToTimezone(utcNineAm, "UTC", "Asia/Kuala_Lumpur"),
|
|
59
|
+
Date.UTC(2024, 0, 1, 1, 0, 0),
|
|
60
|
+
);
|
|
61
|
+
assert.equal(
|
|
62
|
+
convertTimestampToTimezone(
|
|
63
|
+
klNineAm,
|
|
64
|
+
"Asia/Kuala_Lumpur",
|
|
65
|
+
"America/New_York",
|
|
66
|
+
),
|
|
67
|
+
Date.UTC(2024, 0, 1, 14, 0, 0),
|
|
68
|
+
);
|
|
69
|
+
assert.equal(
|
|
70
|
+
convertTimestampToTimezone(utcNineAm, "unknown", "Asia/Kuala_Lumpur"),
|
|
71
|
+
undefined,
|
|
72
|
+
);
|
|
73
|
+
assert.equal(
|
|
74
|
+
convertTimestampToTimezone(utcNineAm, "UTC", "unknown"),
|
|
75
|
+
undefined,
|
|
76
|
+
);
|
package/src/time/index.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
|
+
import dayjs from "dayjs";
|
|
2
|
+
import timezone from "dayjs/plugin/timezone.js";
|
|
3
|
+
import utc from "dayjs/plugin/utc.js";
|
|
4
|
+
|
|
1
5
|
import type { MappingOption } from "../shared/types";
|
|
2
6
|
|
|
7
|
+
dayjs.extend(utc);
|
|
8
|
+
dayjs.extend(timezone);
|
|
9
|
+
|
|
3
10
|
export const TIMEZONE = [
|
|
4
11
|
{ key: 1, value: "UTC", i18nKey: "time.timezone.utc" },
|
|
5
12
|
{
|
|
@@ -82,25 +89,6 @@ export const TIMEZONE = [
|
|
|
82
89
|
export type CanonicalTimezone = (typeof TIMEZONE)[number]["value"];
|
|
83
90
|
export type TimezoneInput = CanonicalTimezone | string;
|
|
84
91
|
|
|
85
|
-
export const TIMEZONE_TO_UTC: Record<CanonicalTimezone, string> = {
|
|
86
|
-
UTC: "UTC+00:00",
|
|
87
|
-
"Asia/Kuala_Lumpur": "UTC+08:00",
|
|
88
|
-
"Asia/Singapore": "UTC+08:00",
|
|
89
|
-
"Asia/Bangkok": "UTC+07:00",
|
|
90
|
-
"Asia/Jakarta": "UTC+07:00",
|
|
91
|
-
"Asia/Manila": "UTC+08:00",
|
|
92
|
-
"Asia/Hong_Kong": "UTC+08:00",
|
|
93
|
-
"Asia/Shanghai": "UTC+08:00",
|
|
94
|
-
"Asia/Tokyo": "UTC+09:00",
|
|
95
|
-
"Australia/Sydney": "UTC+10:00",
|
|
96
|
-
"Europe/London": "UTC+00:00",
|
|
97
|
-
"Europe/Berlin": "UTC+01:00",
|
|
98
|
-
"America/New_York": "UTC-05:00",
|
|
99
|
-
"America/Chicago": "UTC-06:00",
|
|
100
|
-
"America/Denver": "UTC-07:00",
|
|
101
|
-
"America/Los_Angeles": "UTC-08:00",
|
|
102
|
-
};
|
|
103
|
-
|
|
104
92
|
const TIMEZONE_ALIAS_TO_CANONICAL: Record<string, CanonicalTimezone> = {
|
|
105
93
|
utc: "UTC",
|
|
106
94
|
"etc/utc": "UTC",
|
|
@@ -164,6 +152,7 @@ export function normalizeTimezone(
|
|
|
164
152
|
|
|
165
153
|
export function timezoneToUtc(
|
|
166
154
|
timezone: TimezoneInput | undefined,
|
|
155
|
+
timestamp = Date.now(),
|
|
167
156
|
): string | undefined {
|
|
168
157
|
const normalizedTimezone = normalizeTimezone(timezone);
|
|
169
158
|
|
|
@@ -171,9 +160,15 @@ export function timezoneToUtc(
|
|
|
171
160
|
return undefined;
|
|
172
161
|
}
|
|
173
162
|
|
|
174
|
-
return
|
|
163
|
+
return `UTC${dayjs(timestamp).tz(normalizedTimezone).format("Z")}`;
|
|
175
164
|
}
|
|
176
165
|
|
|
166
|
+
export const TIMEZONE_CURRENT_UTC_OFFSET: Record<CanonicalTimezone, string> = Object.fromEntries(
|
|
167
|
+
TIMEZONE.map(({ value }) => [value, timezoneToUtc(value)]),
|
|
168
|
+
) as Record<CanonicalTimezone, string>;
|
|
169
|
+
|
|
170
|
+
export const TIMEZONE_TO_UTC = TIMEZONE_CURRENT_UTC_OFFSET;
|
|
171
|
+
|
|
177
172
|
export function timezoneLabelKey(timezone: TimezoneInput | undefined): string {
|
|
178
173
|
const normalizedTimezone = normalizeTimezone(timezone);
|
|
179
174
|
|
|
@@ -183,3 +178,21 @@ export function timezoneLabelKey(timezone: TimezoneInput | undefined): string {
|
|
|
183
178
|
|
|
184
179
|
return TIMEZONE_I18N_KEY[normalizedTimezone];
|
|
185
180
|
}
|
|
181
|
+
|
|
182
|
+
export function convertTimestampToTimezone(
|
|
183
|
+
timestamp: number,
|
|
184
|
+
sourceTimezone: TimezoneInput | undefined,
|
|
185
|
+
targetTimezone: TimezoneInput | undefined,
|
|
186
|
+
): number | undefined {
|
|
187
|
+
const normalizedSourceTimezone = normalizeTimezone(sourceTimezone);
|
|
188
|
+
const normalizedTargetTimezone = normalizeTimezone(targetTimezone);
|
|
189
|
+
|
|
190
|
+
if (!normalizedSourceTimezone || !normalizedTargetTimezone) {
|
|
191
|
+
return undefined;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const sourceDateTime = dayjs(timestamp).tz(normalizedSourceTimezone);
|
|
195
|
+
const preservedWallClock = sourceDateTime.format("YYYY-MM-DDTHH:mm:ss.SSS");
|
|
196
|
+
|
|
197
|
+
return dayjs.tz(preservedWallClock, normalizedTargetTimezone).valueOf();
|
|
198
|
+
}
|