From ab433d28e08bbe23bbc75d333ed63343356e043a Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Tue, 27 Oct 2009 21:32:55 -0400 Subject: Support a --now option, for testing purposes This sets Ledger's notion of the "current time" to the given date. This makes it possible to have stable output from budgeting and forecasting reports, for the sake of baseline tests. --- src/report.cc | 1 + src/report.h | 10 ++++++++++ src/times.cc | 2 ++ src/times.h | 11 ++++++++--- 4 files changed, 21 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/report.cc b/src/report.cc index 7f31b615..87ebb71d 100644 --- a/src/report.cc +++ b/src/report.cc @@ -592,6 +592,7 @@ option_t * report_t::lookup_option(const char * p) OPT_CH(collapse); else OPT(no_color); else OPT(no_total); + else OPT(now_); break; case 'o': OPT(only_); diff --git a/src/report.h b/src/report.h index e665ac2d..40097c8d 100644 --- a/src/report.h +++ b/src/report.h @@ -250,6 +250,7 @@ public: HANDLER(market).report(out); HANDLER(monthly).report(out); HANDLER(no_total).report(out); + HANDLER(now_).report(out); HANDLER(only_).report(out); HANDLER(output_).report(out); HANDLER(pager_).report(out); @@ -602,6 +603,15 @@ public: OPTION(report_t, no_total); + OPTION_(report_t, now_, DO_(args) { + date_interval_t interval(args[1].to_string()); + if (! interval.start) + throw_(std::invalid_argument, + _("Could not determine beginning of period '%1'") + << args[1].to_string()); + ledger::epoch = datetime_t(*interval.start); + }); + OPTION__ (report_t, only_, CTOR(report_t, only_) {} diff --git a/src/times.cc b/src/times.cc index facdc4f6..7b6eb6e8 100644 --- a/src/times.cc +++ b/src/times.cc @@ -35,6 +35,8 @@ namespace ledger { +optional epoch; + date_time::weekdays start_of_week = gregorian::Sunday; //#define USE_BOOST_FACETS 1 diff --git a/src/times.h b/src/times.h index c77cde1d..035d86cd 100644 --- a/src/times.h +++ b/src/times.h @@ -66,12 +66,17 @@ inline bool is_valid(const date_t& moment) { return ! moment.is_not_a_date(); } +extern optional epoch; + #ifdef BOOST_DATE_TIME_HAS_HIGH_PRECISION_CLOCK -#define CURRENT_TIME() boost::posix_time::microsec_clock::universal_time() +#define CURRENT_TIME() \ + (epoch ? *epoch : boost::posix_time::microsec_clock::universal_time()) #else -#define CURRENT_TIME() boost::posix_time::second_clock::universal_time() +#define CURRENT_TIME() \ + (epoch ? *epoch : boost::posix_time::second_clock::universal_time()) #endif -#define CURRENT_DATE() boost::gregorian::day_clock::universal_day() +#define CURRENT_DATE() \ + (epoch ? epoch->date() : boost::gregorian::day_clock::universal_day()) extern date_time::weekdays start_of_week; -- cgit v1.2.3