#ifndef __MarketHours_h_ #define __MarketHours_h_ #include /* We don't do a great job handling market hours. The start and end time seem * to be spread throughout the Delphi and PHP code, hardcoded everywhere. This * is at attempt at at least identifying all of the cases where we need to know * the market hours. */ class MarketHours { // This class is just a trick to allow people to read values as an inline // reference to a simple variable, but to have no ability to change them. private: static int _open; static int _close; static int _minutesOfTrading; public: static int open() { return _open; } static int close() { return _close; } static int minutesOfTrading() { return _minutesOfTrading; } static void init(); }; // These are quoted in seconds #define MARKET_HOURS_MINUTE 60 #define MARKET_HOURS_HOUR (60 * MARKET_HOURS_MINUTE) #define MARKET_HOURS_OPEN (MarketHours::open()) #define MARKET_HOURS_CLOSE (MarketHours::close()) #define MARKET_HOURS_PERIOD (MARKET_HOURS_MINUTE * 15) #define MINUTES_OF_TRADING (MarketHours::minutesOfTrading()) inline int secondOfTheDay(time_t time) { struct tm brokenDown; localtime_r(&time, &brokenDown); return brokenDown.tm_sec + brokenDown.tm_min * MARKET_HOURS_MINUTE + brokenDown.tm_hour * MARKET_HOURS_HOUR; } inline time_t midnight(time_t time) { // This is imperfect. Around daylight savings time it will not be right. // As long as we are just trying to reset once a day, this should be // sufficient. return time - secondOfTheDay(time); } // This will go forward or backward to the nearest midnight. inline time_t roundToMidnight(time_t time) { return midnight(time + 12 * MARKET_HOURS_HOUR); } inline time_t nyToLocal(time_t time) { return time - 3 * MARKET_HOURS_HOUR; } inline time_t localToNy(time_t time) { return time + 3 * MARKET_HOURS_HOUR; } #define MARKET_HOURS_DAY (MARKET_HOURS_HOUR * 24) #endif