dce-reactkit 4.2.3 → 4.2.4-beta-timestamp.2
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/dist/cjs/index.js +52 -9
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.js +52 -9
- package/dist/esm/index.js.map +1 -1
- package/package.json +1 -1
- package/src/helpers/getTimestampFromTimeInfoInET.ts +58 -10
package/package.json
CHANGED
|
@@ -36,21 +36,67 @@ const getTimestampFromTimeInfoInET = (
|
|
|
36
36
|
minute,
|
|
37
37
|
} = opts;
|
|
38
38
|
|
|
39
|
-
// Determine if the date is in DST in Eastern Time
|
|
40
|
-
const tempDate = new Date(year, month - 1, day);
|
|
41
|
-
const janOffset = new Date(year, 0, 1).getTimezoneOffset();
|
|
42
|
-
const isDST = tempDate.getTimezoneOffset() < janOffset;
|
|
43
|
-
const etOffset = isDST ? '-04:00' : '-05:00';
|
|
44
|
-
|
|
45
39
|
// Format with leading zeroes
|
|
46
40
|
const mm = padZerosLeft(month, 2);
|
|
47
41
|
const dd = padZerosLeft(day, 2);
|
|
48
42
|
const hh = padZerosLeft(hour, 2);
|
|
49
43
|
const min = padZerosLeft(minute, 2);
|
|
50
44
|
|
|
51
|
-
//
|
|
52
|
-
const
|
|
53
|
-
const
|
|
45
|
+
// Use Intl.DateTimeFormat to get the ET offset and ET components
|
|
46
|
+
const dateForET = new Date(Date.UTC(year, month - 1, day, hour, minute, 0));
|
|
47
|
+
const options: Intl.DateTimeFormatOptions = {
|
|
48
|
+
timeZone: 'America/New_York',
|
|
49
|
+
year: 'numeric',
|
|
50
|
+
month: '2-digit',
|
|
51
|
+
day: '2-digit',
|
|
52
|
+
hour: '2-digit',
|
|
53
|
+
minute: '2-digit',
|
|
54
|
+
second: '2-digit',
|
|
55
|
+
hour12: false,
|
|
56
|
+
timeZoneName: 'shortOffset',
|
|
57
|
+
};
|
|
58
|
+
const parts: Intl.DateTimeFormatPart[] = new Intl.DateTimeFormat('en-US', options).formatToParts(dateForET);
|
|
59
|
+
const etYear: string | undefined = parts.find((p: Intl.DateTimeFormatPart) => {
|
|
60
|
+
return p.type === 'year';
|
|
61
|
+
})?.value;
|
|
62
|
+
const etMonth: string | undefined = parts.find((p: Intl.DateTimeFormatPart) => {
|
|
63
|
+
return p.type === 'month';
|
|
64
|
+
})?.value;
|
|
65
|
+
const etDay: string | undefined = parts.find((p: Intl.DateTimeFormatPart) => {
|
|
66
|
+
return p.type === 'day';
|
|
67
|
+
})?.value;
|
|
68
|
+
const etHour: string | undefined = parts.find((p: Intl.DateTimeFormatPart) => {
|
|
69
|
+
return p.type === 'hour';
|
|
70
|
+
})?.value;
|
|
71
|
+
const etMinute: string | undefined = parts.find((p: Intl.DateTimeFormatPart) => {
|
|
72
|
+
return p.type === 'minute';
|
|
73
|
+
})?.value;
|
|
74
|
+
const etSecond: string | undefined = parts.find((p: Intl.DateTimeFormatPart) => {
|
|
75
|
+
return p.type === 'second';
|
|
76
|
+
})?.value;
|
|
77
|
+
const tzName: string | undefined = parts.find((p: Intl.DateTimeFormatPart) => {
|
|
78
|
+
return p.type === 'timeZoneName';
|
|
79
|
+
})?.value;
|
|
80
|
+
|
|
81
|
+
// Validate we got everything we need
|
|
82
|
+
if (!etYear || !etMonth || !etDay || !etHour || !etMinute || !etSecond || !tzName) {
|
|
83
|
+
throw new ErrorWithCode(
|
|
84
|
+
'Failed to parse date parts from Intl.DateTimeFormat',
|
|
85
|
+
ReactKitErrorCode.ETTimestampInvalid,
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Extract numeric offset from "GMT-4" or "UTC-5"
|
|
90
|
+
const offsetNum = Number(tzName.replace(/^(GMT|UTC)/, ''));
|
|
91
|
+
const sign = offsetNum >= 0 ? '+' : '-';
|
|
92
|
+
const offset = `${sign}${padZerosLeft(Math.abs(offsetNum), 2)}:00`;
|
|
93
|
+
|
|
94
|
+
// Build ISO string with offset
|
|
95
|
+
const etDateString = `${etYear}-${etMonth}-${etDay}T${etHour}:${etMinute}:${etSecond}${offset}`;
|
|
96
|
+
|
|
97
|
+
// Parse into ET timestamp
|
|
98
|
+
const etDate = new Date(etDateString);
|
|
99
|
+
const timestamp = etDate.getTime();
|
|
54
100
|
|
|
55
101
|
// Verify that the timestamp is correct
|
|
56
102
|
const timeInfoInET = getTimeInfoInET(timestamp);
|
|
@@ -62,7 +108,9 @@ const getTimestampFromTimeInfoInET = (
|
|
|
62
108
|
|| timeInfoInET.minute !== minute
|
|
63
109
|
) {
|
|
64
110
|
throw new ErrorWithCode(
|
|
65
|
-
`Timestamp mismatch: expected ${year}-${mm}-${dd} ${hh}:${min},
|
|
111
|
+
`Timestamp mismatch: expected ${year}-${mm}-${dd} ${hh}:${min}, `
|
|
112
|
+
+ `got ${timeInfoInET.year}-${padZerosLeft(timeInfoInET.month, 2)}-${padZerosLeft(timeInfoInET.day, 2)} `
|
|
113
|
+
+ `${padZerosLeft(timeInfoInET.hour, 2)}:${padZerosLeft(timeInfoInET.minute, 2)}`,
|
|
66
114
|
ReactKitErrorCode.ETTimestampInvalid,
|
|
67
115
|
);
|
|
68
116
|
}
|