Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | 2x 39x 39x 39x 39x 39x 39x 39x 2x 37x 27x 21x 1x 11x 3x 3x 8x 8x 11x 7x 7x 11x 9x 4x 8x 3x 5x 5x 5x 3x 2x 2x 2x 2x 2x 2x 2x 2x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 13x 13x 13x 13x 117x 13x 13x 13x 13x 13x 13x 5x 5x 5x 5x 5x 5x 5x 5x 27x 3x 24x 24x 2x 22x 22x 1x 2x | /**
* marketClock.js
*
* Manages market hours for equity, forex and crypto.
* Fires events when market opens and closes.
* Developer passes their own schedule — library enforces it.
*
* Features:
* - Timezone aware (uses Intl API — no external deps)
* - Supports always-open markets (crypto)
* - Fires onOpen / onClose callbacks
* - Checks market status on every tick
* - No broken UTC offset math from old code
*/
"use strict";
const MARKET_TYPES = {
CRYPTO: "crypto",
FOREX: "forex",
EQUITY: "equity",
};
class MarketClock {
/**
* @param {Object} config
* @param {string} config.type - 'crypto' | 'forex' | 'equity'
* @param {Object} config.marketHours - Required for equity/forex
* @param {string} config.marketHours.open - e.g. '09:30'
* @param {string} config.marketHours.close - e.g. '16:00'
* @param {string} config.marketHours.timezone - e.g. 'America/New_York'
* @param {number[]} config.marketHours.days - Days open (0=Sun, 6=Sat)
* default: [1,2,3,4,5] weekdays
* @param {Function} config.onOpen - Callback when market opens
* @param {Function} config.onClose - Callback when market closes
*/
constructor(config = {}) {
this.type = config.type ?? MARKET_TYPES.CRYPTO;
this.marketHours = config.marketHours ?? null;
this.onOpen = config.onOpen ?? null;
this.onClose = config.onClose ?? null;
// Internal state
this._isOpen = false;
this._checkTimer = null;
// Validate
if (this.type !== MARKET_TYPES.CRYPTO && !this.marketHours) {
throw new Error(
`[MarketClock] marketHours is required for type "${this.type}". ` +
`Only crypto markets are always open.`,
);
}
if (this.marketHours) {
this._validateMarketHours(this.marketHours);
// Default to weekdays if days not specified
if (!this.marketHours.days) {
this.marketHours.days = [1, 2, 3, 4, 5];
}
}
}
/**
* Start the market clock
* Begins checking market status every minute
*/
start() {
// Crypto is always open
if (this.type === MARKET_TYPES.CRYPTO) {
this._isOpen = true;
return;
}
// Check immediately
this._checkStatus();
// Then check every minute
this._checkTimer = setInterval(() => {
this._checkStatus();
}, 60 * 1000);
}
/**
* Stop the market clock
*/
stop() {
if (this._checkTimer) {
clearInterval(this._checkTimer);
this._checkTimer = null;
}
this._isOpen = false;
}
/**
* Is the market currently open?
* @returns {boolean}
*/
isOpen() {
// Crypto never closes
if (this.type === MARKET_TYPES.CRYPTO) return true;
return this._isOpen;
}
/**
* Get current market status info
* @returns {Object}
*/
getStatus() {
if (this.type === MARKET_TYPES.CRYPTO) {
return {
isOpen: true,
type: this.type,
reason: "Crypto markets never close",
openTime: null,
closeTime: null,
};
}
const now = this._getCurrentTime();
const { open, close, timezone } = this.marketHours;
return {
isOpen: this._isOpen,
type: this.type,
timezone,
openTime: open,
closeTime: close,
localTime: now.timeStr,
localDay: now.day,
reason: this._getStatusReason(now),
};
}
/**
* Get time until next open (ms)
* Returns 0 if already open
*/
msUntilOpen() {
if (this.isOpen()) return 0;
// Simplified — returns ms until next open time today or tomorrow
const now = new Date();
const { open, timezone } = this.marketHours;
const [h, m] = open.split(":").map(Number);
// Get today's open time in market timezone
const openDate = new Date(
now.toLocaleString("en-US", { timeZone: timezone }),
);
openDate.setHours(h, m, 0, 0);
let diff = openDate.getTime() - now.getTime();
Eif (diff < 0) diff += 24 * 60 * 60 * 1000; // next day
return diff;
}
// ── Private helpers ──────────────────────── //
/**
* Check if market should be open right now
* Uses Intl API for correct timezone handling — no manual offset math
*/
_checkStatus() {
const now = this._getCurrentTime();
const wasOpen = this._isOpen;
const shouldBeOpen = this._shouldBeOpen(now);
this._isOpen = shouldBeOpen;
// Fire callbacks on state change only
Iif (!wasOpen && shouldBeOpen) {
if (this.onOpen) {
this.onOpen({
type: this.type,
time: now.timeStr,
timezone: this.marketHours.timezone,
timestamp: Date.now(),
});
}
} else Iif (wasOpen && !shouldBeOpen) {
if (this.onClose) {
this.onClose({
type: this.type,
time: now.timeStr,
timezone: this.marketHours.timezone,
timestamp: Date.now(),
});
}
}
}
/**
* Determine if market should be open based on current time
* @param {Object} now - from _getCurrentTime()
*/
_shouldBeOpen(now) {
const { days } = this.marketHours;
// Check if today is a trading day
Iif (!days.includes(now.day)) return false;
// Check time window
const { open, close } = this.marketHours;
const [openH, openM] = open.split(":").map(Number);
const [closeH, closeM] = close.split(":").map(Number);
const openMins = openH * 60 + openM;
const closeMins = closeH * 60 + closeM;
const nowMins = now.hours * 60 + now.minutes;
return nowMins >= openMins && nowMins < closeMins;
}
/**
* Get current time in the market's timezone
* Uses Intl API — correct for ALL timezones, no offset math
* @returns {{ hours, minutes, day, timeStr }}
*/
_getCurrentTime() {
const timezone = this.marketHours?.timezone ?? "UTC";
const now = new Date();
// Format in target timezone
const formatter = new Intl.DateTimeFormat("en-US", {
timeZone: timezone,
hour: "numeric",
minute: "numeric",
weekday: "short",
hour12: false,
});
const parts = formatter.formatToParts(now);
const get = (type) => parts.find((p) => p.type === type)?.value;
const hours = parseInt(get("hour"), 10);
const minutes = parseInt(get("minute"), 10);
const weekday = get("weekday");
// Convert weekday string to number (0=Sun, 6=Sat)
const dayMap = { Sun: 0, Mon: 1, Tue: 2, Wed: 3, Thu: 4, Fri: 5, Sat: 6 };
const day = dayMap[weekday] ?? 0;
return {
hours,
minutes,
day,
timeStr: `${String(hours).padStart(2, "0")}:${String(minutes).padStart(2, "0")}`,
};
}
/**
* Human readable reason for market status
*/
_getStatusReason(now) {
const { days, open, close } = this.marketHours;
Iif (!days.includes(now.day)) {
return `Market closed — weekend or non-trading day`;
}
const [openH, openM] = open.split(":").map(Number);
const [closeH, closeM] = close.split(":").map(Number);
const openMins = openH * 60 + openM;
const closeMins = closeH * 60 + closeM;
const nowMins = now.hours * 60 + now.minutes;
Eif (nowMins < openMins) return `Market opens at ${open}`;
if (nowMins >= closeMins) return `Market closed at ${close}`;
return `Market is open`;
}
/**
* Validate marketHours config
*/
_validateMarketHours(hours) {
if (!hours.open || !hours.close || !hours.timezone) {
throw new Error(
"[MarketClock] marketHours must have: open, close, timezone. " +
'Example: { open: "09:30", close: "16:00", timezone: "America/New_York" }',
);
}
// Validate time format HH:MM
const timeRegex = /^\d{2}:\d{2}$/;
if (!timeRegex.test(hours.open) || !timeRegex.test(hours.close)) {
throw new Error(
"[MarketClock] open and close must be in HH:MM format. " +
'Example: "09:30", "16:00"',
);
}
// Validate timezone using Intl
try {
Intl.DateTimeFormat("en-US", { timeZone: hours.timezone });
} catch {
throw new Error(
`[MarketClock] Invalid timezone "${hours.timezone}". ` +
`Use IANA timezone names like "America/New_York", "Europe/London", "Asia/Kolkata"`,
);
}
}
}
module.exports = { MarketClock, MARKET_TYPES };
|