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