dce-reactkit 4.2.3 → 4.2.4-beta-timestamp.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dce-reactkit",
3
- "version": "4.2.3",
3
+ "version": "4.2.4-beta-timestamp.1",
4
4
  "description": "Shared components for Harvard DCE apps",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -36,21 +36,54 @@ 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
- // Build ET ISO string and convert to UTC timestamp
52
- const etISOString = `${year}-${mm}-${dd}T${hh}:${min}:00${etOffset}`;
53
- const timestamp = (new Date(etISOString)).getTime();
45
+ // Use Intl.DateTimeFormat to get the ET offset for the given date
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
+ };
57
+ const parts: Intl.DateTimeFormatPart[] = new Intl.DateTimeFormat('en-US', options).formatToParts(dateForET);
58
+ const etYear: string | undefined = parts.find((p: Intl.DateTimeFormatPart) => {
59
+ return p.type === 'year';
60
+ })?.value;
61
+ const etMonth: string | undefined = parts.find((p: Intl.DateTimeFormatPart) => {
62
+ return p.type === 'month';
63
+ })?.value;
64
+ const etDay: string | undefined = parts.find((p: Intl.DateTimeFormatPart) => {
65
+ return p.type === 'day';
66
+ })?.value;
67
+ const etHour: string | undefined = parts.find((p: Intl.DateTimeFormatPart) => {
68
+ return p.type === 'hour';
69
+ })?.value;
70
+ const etMinute: string | undefined = parts.find((p: Intl.DateTimeFormatPart) => {
71
+ return p.type === 'minute';
72
+ })?.value;
73
+ const etSecond: string | undefined = parts.find((p: Intl.DateTimeFormatPart) => {
74
+ return p.type === 'second';
75
+ })?.value;
76
+
77
+ // Build ET date string and parse as if local, then get UTC timestamp
78
+ if (!etYear || !etMonth || !etDay || !etHour || !etMinute || !etSecond) {
79
+ throw new ErrorWithCode(
80
+ 'Failed to parse date parts from Intl.DateTimeFormat',
81
+ ReactKitErrorCode.ETTimestampInvalid,
82
+ );
83
+ }
84
+ const etDateString: string = `${etYear}-${etMonth}-${etDay}T${etHour}:${etMinute}:${etSecond}`;
85
+ const etDate: Date = new Date(etDateString);
86
+ const timestamp: number = etDate.getTime();
54
87
 
55
88
  // Verify that the timestamp is correct
56
89
  const timeInfoInET = getTimeInfoInET(timestamp);