dce-reactkit 4.2.4-beta-timestamp.2 → 4.2.4-beta.heat-seek.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/dist/cjs/index.js CHANGED
@@ -16385,6 +16385,58 @@ const getWordCount = (text) => {
16385
16385
  };
16386
16386
 
16387
16387
  // Import shared types
16388
+ /*----------------------------------------*/
16389
+ /* --------------- Helpers -------------- */
16390
+ /*----------------------------------------*/
16391
+ /**
16392
+ * Check if a timestamp is valid
16393
+ * @param opts object containing all arguments
16394
+ * @param opts.timestamp Timestamp in milliseconds since epoch
16395
+ * @param opts.expectedYear Expected year
16396
+ * @param opts.expectedMonth Expected month
16397
+ * @param opts.expectedDay Expected day
16398
+ * @param opts.expectedHour Expected hour
16399
+ * @param opts.expectedMinute Expected minute
16400
+ * @returns 1 if the timestamp needs to be increased, -1 if it needs to be decreased, 0 if it is valid
16401
+ */
16402
+ const checkTimestamp = (opts) => {
16403
+ const { timestamp, expectedYear, expectedMonth, expectedDay, expectedHour, expectedMinute, } = opts;
16404
+ const timeInfoInET = getTimeInfoInET(timestamp);
16405
+ if (timeInfoInET.year < expectedYear) {
16406
+ return 1;
16407
+ }
16408
+ if (timeInfoInET.year > expectedYear) {
16409
+ return -1;
16410
+ }
16411
+ if (timeInfoInET.month < expectedMonth) {
16412
+ return 1;
16413
+ }
16414
+ if (timeInfoInET.month > expectedMonth) {
16415
+ return -1;
16416
+ }
16417
+ if (timeInfoInET.day < expectedDay) {
16418
+ return 1;
16419
+ }
16420
+ if (timeInfoInET.day > expectedDay) {
16421
+ return -1;
16422
+ }
16423
+ if (timeInfoInET.hour < expectedHour) {
16424
+ return 1;
16425
+ }
16426
+ if (timeInfoInET.hour > expectedHour) {
16427
+ return -1;
16428
+ }
16429
+ if (timeInfoInET.minute < expectedMinute) {
16430
+ return 1;
16431
+ }
16432
+ if (timeInfoInET.minute > expectedMinute) {
16433
+ return -1;
16434
+ }
16435
+ return 0;
16436
+ };
16437
+ /*----------------------------------------*/
16438
+ /* ---------------- Main ---------------- */
16439
+ /*----------------------------------------*/
16388
16440
  /**
16389
16441
  * Get a timestamp (ms since epoch) from time info (year, month, day, hour, minute, etc.) in Eastern Time (ET)
16390
16442
  * @author Gardenia Liu
@@ -16394,79 +16446,64 @@ const getWordCount = (text) => {
16394
16446
  * @param opts.month Month (1-12)
16395
16447
  * @param opts.day Day of the month (1-31)
16396
16448
  * @param opts.hour Hour (0-23)
16397
- * @param opts.minute Minute (0-59))
16449
+ * @param opts.minute Minute (0-59)
16398
16450
  * @returns Timestamp in milliseconds since epoch
16399
16451
  */
16400
16452
  const getTimestampFromTimeInfoInET = (opts) => {
16401
- var _a, _b, _c, _d, _e, _f, _g;
16402
16453
  // Destructure opts
16403
16454
  const { year, month, day, hour, minute, } = opts;
16455
+ // Determine if the date is in DST in Eastern Time
16456
+ const tempDate = new Date(year, month - 1, day);
16457
+ const janOffset = new Date(year, 0, 1).getTimezoneOffset();
16458
+ const isDST = tempDate.getTimezoneOffset() < janOffset;
16459
+ const etOffset = isDST ? '-04:00' : '-05:00';
16404
16460
  // Format with leading zeroes
16405
16461
  const mm = padZerosLeft(month, 2);
16406
16462
  const dd = padZerosLeft(day, 2);
16407
16463
  const hh = padZerosLeft(hour, 2);
16408
16464
  const min = padZerosLeft(minute, 2);
16409
- // Use Intl.DateTimeFormat to get the ET offset and ET components
16410
- const dateForET = new Date(Date.UTC(year, month - 1, day, hour, minute, 0));
16411
- const options = {
16412
- timeZone: 'America/New_York',
16413
- year: 'numeric',
16414
- month: '2-digit',
16415
- day: '2-digit',
16416
- hour: '2-digit',
16417
- minute: '2-digit',
16418
- second: '2-digit',
16419
- hour12: false,
16420
- timeZoneName: 'shortOffset',
16421
- };
16422
- const parts = new Intl.DateTimeFormat('en-US', options).formatToParts(dateForET);
16423
- const etYear = (_a = parts.find((p) => {
16424
- return p.type === 'year';
16425
- })) === null || _a === void 0 ? void 0 : _a.value;
16426
- const etMonth = (_b = parts.find((p) => {
16427
- return p.type === 'month';
16428
- })) === null || _b === void 0 ? void 0 : _b.value;
16429
- const etDay = (_c = parts.find((p) => {
16430
- return p.type === 'day';
16431
- })) === null || _c === void 0 ? void 0 : _c.value;
16432
- const etHour = (_d = parts.find((p) => {
16433
- return p.type === 'hour';
16434
- })) === null || _d === void 0 ? void 0 : _d.value;
16435
- const etMinute = (_e = parts.find((p) => {
16436
- return p.type === 'minute';
16437
- })) === null || _e === void 0 ? void 0 : _e.value;
16438
- const etSecond = (_f = parts.find((p) => {
16439
- return p.type === 'second';
16440
- })) === null || _f === void 0 ? void 0 : _f.value;
16441
- const tzName = (_g = parts.find((p) => {
16442
- return p.type === 'timeZoneName';
16443
- })) === null || _g === void 0 ? void 0 : _g.value;
16444
- // Validate we got everything we need
16445
- if (!etYear || !etMonth || !etDay || !etHour || !etMinute || !etSecond || !tzName) {
16446
- throw new ErrorWithCode('Failed to parse date parts from Intl.DateTimeFormat', ReactKitErrorCode$1.ETTimestampInvalid);
16447
- }
16448
- // Extract numeric offset from "GMT-4" or "UTC-5"
16449
- const offsetNum = Number(tzName.replace(/^(GMT|UTC)/, ''));
16450
- const sign = offsetNum >= 0 ? '+' : '-';
16451
- const offset = `${sign}${padZerosLeft(Math.abs(offsetNum), 2)}:00`;
16452
- // Build ISO string with offset
16453
- const etDateString = `${etYear}-${etMonth}-${etDay}T${etHour}:${etMinute}:${etSecond}${offset}`;
16454
- // Parse into ET timestamp
16455
- const etDate = new Date(etDateString);
16456
- const timestamp = etDate.getTime();
16457
- // Verify that the timestamp is correct
16458
- const timeInfoInET = getTimeInfoInET(timestamp);
16459
- if (timeInfoInET.year !== year
16460
- || timeInfoInET.month !== month
16461
- || timeInfoInET.day !== day
16462
- || timeInfoInET.hour !== hour
16463
- || timeInfoInET.minute !== minute) {
16464
- throw new ErrorWithCode(`Timestamp mismatch: expected ${year}-${mm}-${dd} ${hh}:${min}, `
16465
- + `got ${timeInfoInET.year}-${padZerosLeft(timeInfoInET.month, 2)}-${padZerosLeft(timeInfoInET.day, 2)} `
16466
- + `${padZerosLeft(timeInfoInET.hour, 2)}:${padZerosLeft(timeInfoInET.minute, 2)}`, ReactKitErrorCode$1.ETTimestampInvalid);
16467
- }
16468
- // Valid! Return the timestamp
16469
- return timestamp;
16465
+ // Build ET ISO string and convert to UTC timestamp
16466
+ const etISOString = `${year}-${mm}-${dd}T${hh}:${min}:00${etOffset}`;
16467
+ let timestamp = (new Date(etISOString)).getTime();
16468
+ // Heat seek to get the right timestamp
16469
+ const maxOffset = 24 * 60; // 24 hours in minutes
16470
+ let currentOffset = 0; // minutes
16471
+ const offsetIncrement = 15; // minutes
16472
+ const offsetDirection = checkTimestamp({
16473
+ timestamp,
16474
+ expectedYear: year,
16475
+ expectedMonth: month,
16476
+ expectedDay: day,
16477
+ expectedHour: hour,
16478
+ expectedMinute: minute,
16479
+ });
16480
+ if (offsetDirection === 0) {
16481
+ // Valid! Return the timestamp
16482
+ return timestamp;
16483
+ }
16484
+ // Heat seek
16485
+ while (Math.abs(currentOffset) < maxOffset) {
16486
+ // Update offset
16487
+ currentOffset += (offsetDirection * offsetIncrement);
16488
+ // Update timestamp
16489
+ const offsetMs = currentOffset * 60 * 1000;
16490
+ timestamp += offsetMs;
16491
+ // Check timestamp again
16492
+ const newDirection = checkTimestamp({
16493
+ timestamp,
16494
+ expectedYear: year,
16495
+ expectedMonth: month,
16496
+ expectedDay: day,
16497
+ expectedHour: hour,
16498
+ expectedMinute: minute,
16499
+ });
16500
+ if (newDirection === 0) {
16501
+ // Valid! Return the timestamp
16502
+ return timestamp;
16503
+ }
16504
+ // Invalid. Keep looping.
16505
+ }
16506
+ throw new ErrorWithCode(`Timestamp mismatch: expected ${year}-${mm}-${dd} ${hh}:${min}, seeked to offset ${currentOffset} minutes but could not find a valid timestamp.`, ReactKitErrorCode$1.ETTimestampInvalid);
16470
16507
  };
16471
16508
 
16472
16509
  /*------------------------------------------------------------------------*/