dce-reactkit 4.2.6 → 4.2.8-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/dist/cjs/index.js CHANGED
@@ -16608,6 +16608,60 @@ const getWordCount = (text) => {
16608
16608
  };
16609
16609
 
16610
16610
  // Import shared types
16611
+ /*----------------------------------------*/
16612
+ /* --------------- Helpers -------------- */
16613
+ /*----------------------------------------*/
16614
+ /**
16615
+ * Check if a timestamp is valid
16616
+ * @author Gabe Abrams
16617
+ * @author Gardenia Liu
16618
+ * @param opts object containing all arguments
16619
+ * @param opts.timestamp Timestamp in milliseconds since epoch
16620
+ * @param opts.expectedYear Expected year
16621
+ * @param opts.expectedMonth Expected month
16622
+ * @param opts.expectedDay Expected day
16623
+ * @param opts.expectedHour Expected hour
16624
+ * @param opts.expectedMinute Expected minute
16625
+ * @returns 1 if the timestamp needs to be increased, -1 if it needs to be decreased, 0 if it is valid
16626
+ */
16627
+ const checkTimestamp = (opts) => {
16628
+ const { timestamp, expectedYear, expectedMonth, expectedDay, expectedHour, expectedMinute, } = opts;
16629
+ const timeInfoInET = getTimeInfoInET(timestamp);
16630
+ if (timeInfoInET.year < expectedYear) {
16631
+ return 1;
16632
+ }
16633
+ if (timeInfoInET.year > expectedYear) {
16634
+ return -1;
16635
+ }
16636
+ if (timeInfoInET.month < expectedMonth) {
16637
+ return 1;
16638
+ }
16639
+ if (timeInfoInET.month > expectedMonth) {
16640
+ return -1;
16641
+ }
16642
+ if (timeInfoInET.day < expectedDay) {
16643
+ return 1;
16644
+ }
16645
+ if (timeInfoInET.day > expectedDay) {
16646
+ return -1;
16647
+ }
16648
+ if (timeInfoInET.hour < expectedHour) {
16649
+ return 1;
16650
+ }
16651
+ if (timeInfoInET.hour > expectedHour) {
16652
+ return -1;
16653
+ }
16654
+ if (timeInfoInET.minute < expectedMinute) {
16655
+ return 1;
16656
+ }
16657
+ if (timeInfoInET.minute > expectedMinute) {
16658
+ return -1;
16659
+ }
16660
+ return 0;
16661
+ };
16662
+ /*----------------------------------------*/
16663
+ /* ---------------- Main ---------------- */
16664
+ /*----------------------------------------*/
16611
16665
  /**
16612
16666
  * Get a timestamp (ms since epoch) from time info (year, month, day, hour, minute, etc.) in Eastern Time (ET)
16613
16667
  * @author Gardenia Liu
@@ -16617,7 +16671,7 @@ const getWordCount = (text) => {
16617
16671
  * @param opts.month Month (1-12)
16618
16672
  * @param opts.day Day of the month (1-31)
16619
16673
  * @param opts.hour Hour (0-23)
16620
- * @param opts.minute Minute (0-59))
16674
+ * @param opts.minute Minute (0-59)
16621
16675
  * @returns Timestamp in milliseconds since epoch
16622
16676
  */
16623
16677
  const getTimestampFromTimeInfoInET = (opts) => {
@@ -16635,18 +16689,47 @@ const getTimestampFromTimeInfoInET = (opts) => {
16635
16689
  const min = padZerosLeft(minute, 2);
16636
16690
  // Build ET ISO string and convert to UTC timestamp
16637
16691
  const etISOString = `${year}-${mm}-${dd}T${hh}:${min}:00${etOffset}`;
16638
- const timestamp = (new Date(etISOString)).getTime();
16639
- // Verify that the timestamp is correct
16640
- const timeInfoInET = getTimeInfoInET(timestamp);
16641
- if (timeInfoInET.year !== year
16642
- || timeInfoInET.month !== month
16643
- || timeInfoInET.day !== day
16644
- || timeInfoInET.hour !== hour
16645
- || timeInfoInET.minute !== minute) {
16646
- throw new ErrorWithCode(`Timestamp mismatch: expected ${year}-${mm}-${dd} ${hh}:${min}, got ${timeInfoInET.year}-${padZerosLeft(timeInfoInET.month, 2)}-${padZerosLeft(timeInfoInET.day, 2)} ${padZerosLeft(timeInfoInET.hour, 2)}:${padZerosLeft(timeInfoInET.minute, 2)}`, ReactKitErrorCode$1.ETTimestampInvalid);
16647
- }
16648
- // Valid! Return the timestamp
16649
- return timestamp;
16692
+ const baseTimestamp = (new Date(etISOString)).getTime();
16693
+ let timestamp = baseTimestamp;
16694
+ // Heat seek to get the right timestamp
16695
+ const maxOffset = 24 * 60; // 24 hours in minutes
16696
+ let currentOffset = 0; // minutes
16697
+ const offsetIncrement = 15; // minutes
16698
+ const offsetDirection = checkTimestamp({
16699
+ timestamp,
16700
+ expectedYear: year,
16701
+ expectedMonth: month,
16702
+ expectedDay: day,
16703
+ expectedHour: hour,
16704
+ expectedMinute: minute,
16705
+ });
16706
+ if (offsetDirection === 0) {
16707
+ // Valid! Return the timestamp
16708
+ return timestamp;
16709
+ }
16710
+ // Heat seek
16711
+ while (Math.abs(currentOffset) < maxOffset) {
16712
+ // Update offset
16713
+ currentOffset += (offsetDirection * offsetIncrement);
16714
+ // Update timestamp
16715
+ const offsetMs = currentOffset * 60 * 1000;
16716
+ timestamp = baseTimestamp + offsetMs;
16717
+ // Check timestamp again
16718
+ const newDirection = checkTimestamp({
16719
+ timestamp,
16720
+ expectedYear: year,
16721
+ expectedMonth: month,
16722
+ expectedDay: day,
16723
+ expectedHour: hour,
16724
+ expectedMinute: minute,
16725
+ });
16726
+ if (newDirection === 0) {
16727
+ // Valid! Return the timestamp
16728
+ return timestamp;
16729
+ }
16730
+ // Invalid. Keep looping.
16731
+ }
16732
+ 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);
16650
16733
  };
16651
16734
 
16652
16735
  /*------------------------------------------------------------------------*/