#include "../../shared/MarketHours.h" #include "AlertBase.h" // DurationString is a common way to convert the // time between two times into English, so all alerts // will use similar words. The assumption is that // ToTime > FromTime. If FromTime >= ToTime, the // wording will suggest a very short time, assuming // we are looking at roundoff error. std::string Alert::durationString(time_t fromTime, time_t toTime) { const int duration = toTime - fromTime; std::string result; if (duration >= 25 * MARKET_HOURS_HOUR) { // At least 25 hours. Report days and hours. const int totalHours = (duration + MARKET_HOURS_HOUR/2) / MARKET_HOURS_HOUR; const int days = totalHours / 24; const int hours = totalHours % 24; if (days == 1) { result = "1 day"; } else { result = ntoa(days) + " days"; } if (hours > 1) { result += ' '; result += ntoa(hours); result += " hours"; } else if (hours == 1) { result += " 1 hour"; } } else if (duration >= 90 * MARKET_HOURS_MINUTE) { const int totalMinutes = (duration + MARKET_HOURS_MINUTE/2) / MARKET_HOURS_MINUTE; const int hours = totalMinutes / 60; const int minutes = totalMinutes % 60; if (hours == 1) { result = "1 hour"; } else { result = ntoa(hours) + " hours"; } if (minutes > 1) { result += ' '; result += ntoa(minutes); result += " minutes"; } else if (minutes == 1) { result += " 1 minute"; } } else if (duration >= 90) { // At least 90 seconds. Reports minutes and seconds. const int minutes = duration / 60; const int seconds = duration % 60; if (minutes == 1) { result = "1 minute"; } else { result = ntoa(minutes) + " minutes"; } if (seconds > 1) { result += ' '; result += ntoa(seconds); result += " seconds"; } else if (seconds == 1) { result += " 1 second"; } } else if (duration > 1) { result = ntoa(duration) + " seconds"; } else if (duration == 1) { result = "1 second"; } else { result = "less than one second"; } return result; } std::string Alert::formatPrice(double price, bool alwaysShowSign) { if (alwaysShowSign && (price >= 0.0)) return '+' + priceToString(price); else return priceToString(price); } void Alert::getInteger(bool &valid, Integer &value) const { valid = true; value = _count; } void Alert::getDouble(bool &valid, double &value) const { valid = _qualityValid; value = _quality; } void Alert::getString(bool &valid, std::string &value) const { valid = true; value = _msg; } Alert::Alert() : _quality(0.0), _count(0), _qualityValid(false) { } std::string Alert::BLANK = ""; void Alert::report(std::string const &msg, std::string const &altMsg) { _msg = msg; _altMsg = altMsg; _qualityValid = false; _count++; notifyListeners(); } void Alert::report(std::string const &msg, std::string const &altMsg, double quality) { _msg = msg; _altMsg = altMsg; _qualityValid = true; _quality = quality; _count++; notifyListeners(); }