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