@discomedia/utils 1.0.16 → 1.0.17

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/index.mjs CHANGED
@@ -386,16 +386,24 @@ class MarketTimeCalculator {
386
386
  /**
387
387
  * Get the last full trading date
388
388
  */
389
- getLastFullTradingDate(currentDate = new Date()) {
389
+ getLastFullTradingDate(currentDate = new Date(), extendedHours = true) {
390
390
  const nowET = toZonedTime(currentDate, this.timezone);
391
- // If today is a market day and we're after extended hours close, return today
392
391
  if (this.calendar.isMarketDay(nowET)) {
393
392
  const timeInMinutes = nowET.getHours() * 60 + nowET.getMinutes();
394
- let extendedEndMinutes = MARKET_CONFIG.TIMES.EXTENDED_END.hour * 60 + MARKET_CONFIG.TIMES.EXTENDED_END.minute;
395
- if (this.calendar.isEarlyCloseDay(nowET)) {
396
- extendedEndMinutes = MARKET_CONFIG.TIMES.EARLY_EXTENDED_END.hour * 60 + MARKET_CONFIG.TIMES.EARLY_EXTENDED_END.minute;
393
+ let endMinutes;
394
+ if (extendedHours) {
395
+ endMinutes = MARKET_CONFIG.TIMES.EXTENDED_END.hour * 60 + MARKET_CONFIG.TIMES.EXTENDED_END.minute;
396
+ if (this.calendar.isEarlyCloseDay(nowET)) {
397
+ endMinutes = MARKET_CONFIG.TIMES.EARLY_EXTENDED_END.hour * 60 + MARKET_CONFIG.TIMES.EARLY_EXTENDED_END.minute;
398
+ }
399
+ }
400
+ else {
401
+ endMinutes = MARKET_CONFIG.TIMES.MARKET_CLOSE.hour * 60 + MARKET_CONFIG.TIMES.MARKET_CLOSE.minute;
402
+ if (this.calendar.isEarlyCloseDay(nowET)) {
403
+ endMinutes = MARKET_CONFIG.TIMES.EARLY_CLOSE.hour * 60 + MARKET_CONFIG.TIMES.EARLY_CLOSE.minute;
404
+ }
397
405
  }
398
- if (timeInMinutes >= extendedEndMinutes) {
406
+ if (timeInMinutes >= endMinutes) {
399
407
  return fromZonedTime(set(nowET, { hours: 0, minutes: 0, seconds: 0, milliseconds: 0 }), this.timezone);
400
408
  }
401
409
  }
@@ -766,6 +774,12 @@ function getMarketStatus(options = {}) {
766
774
  function isMarketDay(date) {
767
775
  return marketCalendar.isMarketDay(date);
768
776
  }
777
+ /**
778
+ * Check if a date is within market hours
779
+ */
780
+ function isWithinMarketHours(date, intradayReporting = 'market_hours') {
781
+ return marketTimeCalculator.isWithinMarketHours(date, intradayReporting);
782
+ }
769
783
  /**
770
784
  * Function to find complete trading date periods, starting at the beginning of one trading date and ending at the last.
771
785
  * By default, it gets the last trading date, returning the beginning and end. But we can also a) define the end date
@@ -778,9 +792,16 @@ function isMarketDay(date) {
778
792
  function getTradingStartAndEndDates(options = {}) {
779
793
  const { endDate = new Date(), days = 1 } = options;
780
794
  // Get the last full trading date for the end date
781
- const endTradingDate = getLastFullTradingDate(endDate).date;
782
- // Get the market close time for the end date (4:00 PM ET or 1:00 PM on short days)
783
- const endMarketClose = getMarketOpenClose({ date: endTradingDate }).close;
795
+ let endTradingDate;
796
+ // If within market hours, on a trading day, use the current date as end
797
+ if (isWithinMarketHours(endDate, 'market_hours')) {
798
+ endTradingDate = endDate;
799
+ }
800
+ else {
801
+ // If after market hours, use the last full trading date, which should be the previous trading day, or after extended hours, the same trading day
802
+ const lastFullTradingDate = marketTimeCalculator.getLastFullTradingDate(endDate, false);
803
+ endTradingDate = getMarketOpenClose({ date: lastFullTradingDate }).close;
804
+ }
784
805
  let startDate;
785
806
  if (days <= 1) {
786
807
  // For 1 day, start is market open of the same trading day as end
@@ -800,7 +821,7 @@ function getTradingStartAndEndDates(options = {}) {
800
821
  // Get the market open time for the start date
801
822
  startDate = getMarketOpenClose({ date: currentDate }).open;
802
823
  }
803
- return { startDate, endDate: endMarketClose };
824
+ return { startDate, endDate: endTradingDate };
804
825
  }
805
826
  // Export the MARKET_TIMES constant for backward compatibility
806
827
  const MARKET_TIMES = {