Fixed the use of --effective and --date-format (-y).

This commit is contained in:
John Wiegley 2009-02-09 17:27:54 -04:00
parent cad86ed87f
commit 6ca01af594
8 changed files with 53 additions and 51 deletions

View file

@ -11,7 +11,7 @@ N $
Assets:Bank:Checking $1,000.00
Equity:Opening Balances
2004/05/01 * Investment balance
2004/05/03=2004/05/01 * Investment balance
Assets:Brokerage 50 AAPL @ $30.00
Equity:Opening Balances

View file

@ -420,10 +420,12 @@ void subtotal_xacts::report_subtotal(const char * spec_fmt)
void subtotal_xacts::operator()(xact_t& xact)
{
if (! is_valid(start) || xact.date() < start)
start = xact.date();
if (! is_valid(finish) || xact.date() > finish)
finish = xact.date();
date_t when = xact.date();
if (! is_valid(start) || when < start)
start = when;
if (! is_valid(finish) || when > finish)
finish = when;
account_t * acct = xact.reported_account();
assert(acct);
@ -466,7 +468,7 @@ void interval_xacts::report_subtotal(const date_t& date)
void interval_xacts::operator()(xact_t& xact)
{
const date_t& date(xact.date());
date_t date = xact.date();
if ((is_valid(interval.begin) && date < interval.begin) ||
(is_valid(interval.end) && date >= interval.end))

View file

@ -377,6 +377,14 @@ void global_scope_t::normalize_report_options(const string& verb)
report_t& rep(report());
// jww (2009-02-09): These global are a hack, but hard to avoid
item_t::use_effective_date = rep.HANDLED(effective);
if (rep.HANDLED(date_format_)) {
output_datetime_format = rep.HANDLER(date_format_).str() + " %H:%M:%S";
output_date_format = rep.HANDLER(date_format_).str();
}
// jww (2008-08-14): This code really needs to be rationalized away
// for 3.0.

View file

@ -33,6 +33,8 @@
namespace ledger {
bool item_t::use_effective_date = false;
bool item_t::has_tag(const string& tag) const
{
if (! metadata)
@ -136,12 +138,8 @@ namespace {
}
value_t get_date(item_t& item) {
if (optional<date_t> date = item.date())
return *date;
else
return 0L;
return item.date();
}
value_t get_note(item_t& item) {
return string_value(item.note ? *item.note : empty_string);
}
@ -334,6 +332,13 @@ string item_context(const item_t& item, const string& desc)
assert(len > 0);
assert(len < 2048);
std::ostringstream out;
if (item.pathname == path("/dev/stdin")) {
out << desc << " from standard input:";
return out.str();
}
ifstream in(item.pathname);
in.seekg(item.beg_pos, std::ios::beg);
@ -342,8 +347,6 @@ string item_context(const item_t& item, const string& desc)
assert(static_cast<std::size_t>(in.gcount()) == len);
buf[len] = '\0';
std::ostringstream out;
out << desc << " from \"" << item.pathname.string() << "\"";
if (item.beg_line != item.end_line)

View file

@ -126,8 +126,13 @@ public:
virtual void parse_tags(const char * p, int current_year = -1);
virtual void append_note(const char * p, int current_year = -1);
static bool use_effective_date;
virtual date_t date() const {
assert(_date);
if (use_effective_date)
if (optional<date_t> effective = effective_date())
return *effective;
return *_date;
}
virtual optional<date_t> effective_date() const {

View file

@ -44,7 +44,7 @@ report_t::report_t(session_t& _session) : session(_session)
HANDLER(date_format_).on("%y-%b-%d");
HANDLER(register_format_).on(
"%-.9(display_date) %-.20(payee)"
"%-.9(date) %-.20(payee)"
" %-.23(truncate(account, 23, 2))"
" %!12(print_balance(strip(display_amount), 12, 67))"
" %!12(print_balance(strip(display_total), 12, 80, true))\n%/"
@ -82,7 +82,7 @@ report_t::report_t(session_t& _session) : session(_session)
HANDLER(pricesdb_format_).on("P %[%Y/%m/%d %H:%M:%S] %A %t\n");
HANDLER(csv_format_).on(
"%(quoted(display_date)),"
"%(quoted(date)),"
"%(quoted(payee)),"
"%(quoted(account)),"
"%(quoted(display_amount)),"
@ -151,28 +151,6 @@ value_t report_t::fn_total_expr(call_scope_t& scope)
return HANDLER(total_).expr.calc(scope);
}
value_t report_t::fn_display_date(call_scope_t& args)
{
item_t& item(find_scope<item_t>(args));
// jww (2009-02-06): Should we be calling reported_date here?
date_t when;
if (HANDLED(effective)) {
if (optional<date_t> date = item.effective_date())
when = *date;
else
when = item.date();
} else {
when = item.date();
}
if (HANDLED(date_format_))
return string_value(format_date(when, HANDLER(date_format_).str()));
else
return string_value(format_date(when));
}
value_t report_t::fn_display_amount(call_scope_t& scope)
{
return HANDLER(display_amount_).expr.calc(scope);
@ -570,9 +548,7 @@ expr_t::ptr_op_t report_t::lookup(const string& name)
break;
case 'd':
if (is_eq(p, "display_date"))
return MAKE_FUNCTOR(report_t::fn_display_date);
else if (is_eq(p, "display_amount"))
if (is_eq(p, "display_amount"))
return MAKE_FUNCTOR(report_t::fn_display_amount);
else if (is_eq(p, "display_total"))
return MAKE_FUNCTOR(report_t::fn_display_total);

View file

@ -33,6 +33,10 @@
namespace ledger {
optional<std::string> input_date_format;
std::string output_datetime_format = "%Y-%m-%d %H:%M:%S";
std::string output_date_format = "%Y-%m-%d";
namespace {
const char * formats[] = {
"%y/%m/%d",
@ -53,8 +57,6 @@ namespace {
};
}
optional<string> input_date_format;
namespace {
bool parse_date_mask(const char * date_str, std::tm& result)
{

View file

@ -70,17 +70,17 @@ inline bool is_valid(const date_t& moment) {
#endif
#define CURRENT_DATE() boost::gregorian::day_clock::universal_day()
extern optional<string> input_date_format;
extern optional<std::string> input_date_format;
datetime_t parse_datetime(const char * str, int current_year = -1);
inline datetime_t parse_datetime(const string& str, int current_year = -1) {
inline datetime_t parse_datetime(const std::string& str, int current_year = -1) {
return parse_datetime(str.c_str(), current_year);
}
date_t parse_date(const char * str, int current_year = -1);
inline date_t parse_date(const string& str, int current_year = -1) {
inline date_t parse_date(const std::string& str, int current_year = -1) {
return parse_date(str.c_str(), current_year);
}
@ -94,21 +94,27 @@ inline std::time_t to_time_t(const ptime& t)
return (t-start).total_seconds();
}
inline string format_datetime(const datetime_t& when,
const string format = "%Y-%m-%d %H:%M:%S")
extern std::string output_datetime_format;
inline std::string format_datetime(const datetime_t& when,
const optional<std::string>& format = none)
{
char buf[256];
time_t moment = to_time_t(when);
std::strftime(buf, 255, format.c_str(), std::localtime(&moment));
std::strftime(buf, 255, format ? format->c_str() :
output_datetime_format.c_str(), std::localtime(&moment));
return buf;
}
inline string format_date(const date_t& when,
const string format = "%Y-%m-%d")
extern std::string output_date_format;
inline std::string format_date(const date_t& when,
const optional<std::string>& format = none)
{
char buf[256];
std::tm moment = gregorian::to_tm(when);
std::strftime(buf, 255, format.c_str(), &moment);
std::strftime(buf, 255, format ? format->c_str() :
output_date_format.c_str(), &moment);
return buf;
}