diff options
Diffstat (limited to 'src/quotes.cc')
-rw-r--r-- | src/quotes.cc | 69 |
1 files changed, 37 insertions, 32 deletions
diff --git a/src/quotes.cc b/src/quotes.cc index 21ed5998..d40106f3 100644 --- a/src/quotes.cc +++ b/src/quotes.cc @@ -2,31 +2,36 @@ namespace ledger { -void quotes_by_script::operator()(commodity_base_t& commodity, - const ptime& moment, - const ptime& date, - const ptime& last, - amount_t& price) +optional<amount_t> +quotes_by_script::operator()(commodity_t& commodity, + const optional<moment_t>& date, + const optional<moment_t>& moment, + const optional<moment_t>& last) { LOGGER("quotes.download"); - DEBUG_("commodity: " << commodity.symbol); - DEBUG_(" now: " << now); - DEBUG_(" moment: " << moment); - DEBUG_(" date: " << date); - DEBUG_(" last: " << last); - - if (SHOW_DEBUG_() && commodity.history) - DEBUG_("last_lookup: " << commodity.history->last_lookup); + IF_DEBUG_() { + DEBUG_("commodity: " << commodity.symbol()); + DEBUG_(" now: " << now); + if (date) + DEBUG_(" date: " << date); + if (moment) + DEBUG_(" moment: " << moment); + if (last) + DEBUG_(" last: " << last); + if (commodity.history()) + DEBUG_("last_lookup: " << commodity.history()->last_lookup); + } DEBUG_("pricing_leeway is " << pricing_leeway); - if ((commodity.history && - (time_now - commodity.history->last_lookup) < pricing_leeway) || - (time_now - last) < pricing_leeway || - (price && moment > date && (moment - date) <= pricing_leeway)) - return; + if ((commodity.history() && + (now - commodity.history()->last_lookup) < pricing_leeway) || + (last && (now - *last) < pricing_leeway) || + (moment && date && *moment > *date && + (*moment - *date) <= pricing_leeway)) + return optional<amount_t>(); - DEBUG_("downloading quote for symbol " << commodity.symbol); + DEBUG_("downloading quote for symbol " << commodity.symbol()); char buf[256]; buf[0] = '\0'; @@ -34,7 +39,7 @@ void quotes_by_script::operator()(commodity_base_t& commodity, bool success = true; if (FILE * fp = popen((string("getquote \"") + - commodity.symbol + "\"").c_str(), "r")) { + commodity.base_symbol() + "\"").c_str(), "r")) { if (feof(fp) || ! fgets(buf, 255, fp)) success = false; if (pclose(fp) != 0) @@ -49,31 +54,31 @@ void quotes_by_script::operator()(commodity_base_t& commodity, DEBUG_("downloaded quote: " << buf); + amount_t price; price.parse(buf); commodity.add_price(now, price); - commodity.history->last_lookup = time_now; + commodity.history()->last_lookup = now; cache_dirty = true; - if (price) { - assert(! price_db.empty()); + assert(! price_db.empty()); #if defined(__GNUG__) && __GNUG__ < 3 - ofstream database(price_db, ios::out | ios::app); + ofstream database(price_db, ios::out | ios::app); #else - ofstream database(price_db, std::ios_base::out | std::ios_base::app); + ofstream database(price_db, std::ios_base::out | std::ios_base::app); #endif #if 0 - // jww (2007-04-18): Need to convert to local time and print - // here, print with UTC timezone specifier - database << "P " << now.to_string("%Y/%m/%d %H:%M:%S") - << " " << commodity.symbol << " " << price << endl; + // jww (2007-04-18): Need to convert to local time and print + // here, print with UTC timezone specifier + database << "P " << now.to_string("%Y/%m/%d %H:%M:%S") + << " " << commodity.symbol << " " << price << endl; #endif - } + return price; } else { throw_(download_error, - "Failed to download price for '" << commodity.symbol << - "' (command: \"getquote " << commodity.symbol << "\")"); + "Failed to download price for '" << commodity.symbol() << + "' (command: \"getquote " << commodity.base_symbol() << "\")"); } } |