summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/amount.cc17
-rw-r--r--src/commodity.cc56
-rw-r--r--src/commodity.h5
-rw-r--r--src/global.cc8
-rw-r--r--src/pool.cc2
-rw-r--r--src/pool.h7
-rw-r--r--src/report.cc1
7 files changed, 63 insertions, 33 deletions
diff --git a/src/amount.cc b/src/amount.cc
index c9b02c65..0b798686 100644
--- a/src/amount.cc
+++ b/src/amount.cc
@@ -556,13 +556,22 @@ amount_t::value(const bool primary_only,
annotation().has_flags(ANNOTATION_PRICE_FIXATED)) {
return (*annotation().price * number()).rounded();
}
- else if (optional<price_point_t> point =
- commodity().find_price(in_terms_of, moment)) {
- return (point->price * number()).rounded();
+ else {
+ optional<price_point_t> point =
+ commodity().find_price(in_terms_of, moment);
+
+ // Whether a price was found or not, check whether we should attempt
+ // to download a price from the Internet. This is done if (a) no
+ // price was found, or (b) the price is "stale" according to the
+ // setting of --price-exp.
+ point = commodity().check_for_updated_price(point, moment, in_terms_of);
+ if (point)
+ return (point->price * number()).rounded();
}
}
} else {
- throw_(amount_error, _("Cannot determine value of an uninitialized amount"));
+ throw_(amount_error,
+ _("Cannot determine value of an uninitialized amount"));
}
return none;
}
diff --git a/src/commodity.cc b/src/commodity.cc
index db7fabd6..900fe07d 100644
--- a/src/commodity.cc
+++ b/src/commodity.cc
@@ -347,26 +347,6 @@ optional<price_point_t>
DEBUG("commodity.download",
"found price " << best.price << " from " << best.when);
#endif
-#if 0
- DEBUG("commodity.download", "leeway = " << download_leeway);
- datetime_t::sec_type seconds_diff;
- if (moment) {
- seconds_diff = (*moment - best.when).total_seconds();
- DEBUG("commodity.download", "moment = " << *moment);
- DEBUG("commodity.download", "slip.moment = " << seconds_diff);
- } else {
- seconds_diff = (CURRENT_TIME() - best.when).total_seconds();
- DEBUG("commodity.download", "slip.now = " << seconds_diff);
- }
-
- if (download_quotes && ! source.has_flags(COMMODITY_NOMARKET) &&
- seconds_diff > download_leeway) {
- DEBUG("commodity.download",
- "attempting to download a more current quote...");
- if (optional<price_point_t> quote = source.download_quote(commodity))
- return quote;
- }
-#endif
return best;
}
return none;
@@ -398,6 +378,42 @@ optional<commodity_t::base_t::history_t&>
return none;
}
+optional<price_point_t>
+commodity_t::check_for_updated_price(const optional<price_point_t>& point,
+ const optional<datetime_t>& moment,
+ const optional<commodity_t&>& in_terms_of)
+{
+ if (parent().get_quotes && ! has_flags(COMMODITY_NOMARKET)) {
+ bool exceeds_leeway = true;
+
+ if (point) {
+ time_duration_t::sec_type seconds_diff;
+ if (moment) {
+ seconds_diff = (*moment - point->when).total_seconds();
+ DEBUG("commodity.download", "moment = " << *moment);
+ DEBUG("commodity.download", "slip.moment = " << seconds_diff);
+ } else {
+ seconds_diff = (CURRENT_TIME() - point->when).total_seconds();
+ DEBUG("commodity.download", "slip.now = " << seconds_diff);
+ }
+
+ DEBUG("commodity.download", "leeway = " << parent().quote_leeway);
+ if (seconds_diff < parent().quote_leeway)
+ exceeds_leeway = false;
+ }
+
+ if (exceeds_leeway) {
+ DEBUG("commodity.download",
+ "attempting to download a more current quote...");
+ if (optional<price_point_t> quote =
+ parent().get_commodity_quote(*this, in_terms_of)) {
+ return quote;
+ }
+ }
+ }
+ return point;
+}
+
commodity_t::operator bool() const
{
return this != parent().null_commodity;
diff --git a/src/commodity.h b/src/commodity.h
index 4bd5ee82..5d73f4e8 100644
--- a/src/commodity.h
+++ b/src/commodity.h
@@ -309,6 +309,11 @@ public:
return none;
}
+ optional<price_point_t>
+ check_for_updated_price(const optional<price_point_t>& point,
+ const optional<datetime_t>& moment,
+ const optional<commodity_t&>& in_terms_of);
+
// Methods related to parsing, reading, writing, etc., the commodity
// itself.
diff --git a/src/global.cc b/src/global.cc
index 26ee9ad7..084b92da 100644
--- a/src/global.cc
+++ b/src/global.cc
@@ -417,12 +417,12 @@ void global_scope_t::normalize_report_options(const string& verb)
report_t& rep(report());
// jww (2009-02-09): These globals are a hack, but hard to avoid.
- item_t::use_effective_date = rep.HANDLED(effective);
- rep.session.commodity_pool->keep_base = rep.HANDLED(base);
- rep.session.commodity_pool->download_quotes = rep.session.HANDLED(download);
+ item_t::use_effective_date = rep.HANDLED(effective);
+ rep.session.commodity_pool->keep_base = rep.HANDLED(base);
+ rep.session.commodity_pool->get_quotes = rep.session.HANDLED(download);
if (rep.session.HANDLED(price_exp_))
- rep.session.commodity_pool->download_leeway =
+ rep.session.commodity_pool->quote_leeway =
rep.session.HANDLER(price_exp_).value.as_long();
if (rep.session.HANDLED(price_db_))
diff --git a/src/pool.cc b/src/pool.cc
index 1b7a0f80..48dd7f84 100644
--- a/src/pool.cc
+++ b/src/pool.cc
@@ -40,7 +40,7 @@ namespace ledger {
commodity_pool_t::commodity_pool_t()
: default_commodity(NULL), keep_base(false),
- download_leeway(86400), download_quotes(false)
+ quote_leeway(86400), get_quotes(false)
{
TRACE_CTOR(commodity_pool_t, "");
null_commodity = create("");
diff --git a/src/pool.h b/src/pool.h
index 17781bf1..cec2f05e 100644
--- a/src/pool.h
+++ b/src/pool.h
@@ -83,12 +83,13 @@ public:
bool keep_base; // --base
optional<path> price_db; // --price-db=
- long download_leeway; // --leeway=
- bool download_quotes; // --download
+ long quote_leeway; // --leeway=
+ bool get_quotes; // --download
public:
function<optional<price_point_t>
- (const optional<commodity_t&>& commodity)> get_commodity_quote;
+ (const commodity_t& commodity,
+ const optional<commodity_t&>& in_terms_of)> get_commodity_quote;
explicit commodity_pool_t();
diff --git a/src/report.cc b/src/report.cc
index d3c936f1..731de501 100644
--- a/src/report.cc
+++ b/src/report.cc
@@ -133,7 +133,6 @@ value_t report_t::fn_market(call_scope_t& scope)
optional<datetime_t> moment = (args.has(1) ?
args.get<datetime_t>(1) :
optional<datetime_t>());
-
if (args.has(2))
result = args.value_at(0).exchange_commodities(args.get<string>(2),
/* add_prices= */ false,