Revised how Ledger handles the "current year"

Now when the Y directive sets the current year for a region, it affects
everything, as if the clock really were set back to that year.
This commit is contained in:
John Wiegley 2010-06-14 03:18:32 -04:00
parent e2fa8c6145
commit dcffd218a1
16 changed files with 126 additions and 152 deletions

View file

@ -49,6 +49,8 @@ global_scope_t::global_scope_t(char ** envp)
{
TRACE_CTOR(global_scope_t, "");
epoch = CURRENT_TIME();
#if defined(HAVE_BOOST_PYTHON)
if (! python_session.get()) {
python_session.reset(new ledger::python_interpreter_t);

View file

@ -134,10 +134,9 @@ item_t::set_tag(const string& tag,
}
}
void item_t::parse_tags(const char * p,
scope_t& scope,
bool overwrite_existing,
optional<date_t::year_type> current_year)
void item_t::parse_tags(const char * p,
scope_t& scope,
bool overwrite_existing)
{
if (const char * b = std::strchr(p, '[')) {
if (*(b + 1) != '\0' &&
@ -149,10 +148,10 @@ void item_t::parse_tags(const char * p,
if (char * p = std::strchr(buf, '=')) {
*p++ = '\0';
_date_eff = parse_date(p, current_year);
_date_eff = parse_date(p);
}
if (buf[0])
_date = parse_date(buf, current_year);
_date = parse_date(buf);
}
}
}
@ -202,10 +201,9 @@ void item_t::parse_tags(const char * p,
}
}
void item_t::append_note(const char * p,
scope_t& scope,
bool overwrite_existing,
optional<date_t::year_type> current_year)
void item_t::append_note(const char * p,
scope_t& scope,
bool overwrite_existing)
{
if (note) {
*note += '\n';
@ -214,7 +212,7 @@ void item_t::append_note(const char * p,
note = p;
}
parse_tags(p, scope, overwrite_existing, current_year);
parse_tags(p, scope, overwrite_existing);
}
namespace {

View file

@ -162,14 +162,12 @@ public:
const optional<value_t>& value = none,
const bool overwrite_existing = true);
virtual void parse_tags(const char * p,
scope_t& scope,
bool overwrite_existing = true,
optional<date_t::year_type> current_year = none);
virtual void append_note(const char * p,
scope_t& scope,
bool overwrite_existing = true,
optional<date_t::year_type> current_year = none);
virtual void parse_tags(const char * p,
scope_t& scope,
bool overwrite_existing = true);
virtual void append_note(const char * p,
scope_t& scope,
bool overwrite_existing = true);
static bool use_effective_date;

View file

@ -105,8 +105,7 @@ account_t * journal_t::find_account_re(const string& regexp)
return master->find_account_re(regexp);
}
bool journal_t::add_xact(xact_t * xact,
optional<date_t::year_type> current_year)
bool journal_t::add_xact(xact_t * xact)
{
xact->journal = this;
@ -115,17 +114,16 @@ bool journal_t::add_xact(xact_t * xact,
return false;
}
extend_xact(xact, current_year);
extend_xact(xact);
xacts.push_back(xact);
return true;
}
void journal_t::extend_xact(xact_base_t * xact,
optional<date_t::year_type> current_year)
void journal_t::extend_xact(xact_base_t * xact)
{
foreach (auto_xact_t * auto_xact, auto_xacts)
auto_xact->extend_xact(*xact, current_year);
auto_xact->extend_xact(*xact);
}
bool journal_t::remove_xact(xact_t * xact)

View file

@ -140,10 +140,8 @@ public:
account_t * find_account(const string& name, bool auto_create = true);
account_t * find_account_re(const string& regexp);
bool add_xact(xact_t * xact,
optional<date_t::year_type> current_year = none);
void extend_xact(xact_base_t * xact,
optional<date_t::year_type> current_year = none);
bool add_xact(xact_t * xact);
void extend_xact(xact_base_t * xact);
bool remove_xact(xact_t * xact);
xacts_list::iterator xacts_begin() {

View file

@ -186,7 +186,7 @@ value_t period_command(call_scope_t& args)
out << std::endl;
date_interval_t interval(arg);
interval.dump(out, report.session.current_year);
interval.dump(out);
return NULL_VALUE;
}

View file

@ -148,8 +148,8 @@ void report_t::normalize_options(const string& verb)
if (HANDLED(period_)) {
date_interval_t interval(HANDLER(period_).str());
optional<date_t> begin = interval.begin(session.current_year);
optional<date_t> end = interval.end(session.current_year);
optional<date_t> begin = interval.begin();
optional<date_t> end = interval.end();
if (! HANDLED(begin_) && begin) {
string predicate = "date>=[" + to_iso_extended_string(*begin) + "]";

View file

@ -393,7 +393,7 @@ public:
OPTION_(report_t, begin_, DO_(args) { // -b
date_interval_t interval(args.get<string>(1));
optional<date_t> begin = interval.begin(parent->session.current_year);
optional<date_t> begin = interval.begin();
if (! begin)
throw_(std::invalid_argument,
_("Could not determine beginning of period '%1'")
@ -543,8 +543,9 @@ public:
OPTION_(report_t, end_, DO_(args) { // -e
date_interval_t interval(args.get<string>(1));
// Use begin() here so that if the user says --end=2008, we end on
// 2008/01/01 instead of 2009/01/01 (which is what end() would return).
optional<date_t> end = interval.begin(parent->session.current_year);
// 2008/01/01 instead of 2009/01/01 (which is what end() would
// return).
optional<date_t> end = interval.begin();
if (! end)
throw_(std::invalid_argument,
_("Could not determine end of period '%1'")
@ -665,13 +666,12 @@ public:
OPTION_(report_t, now_, DO_(args) {
date_interval_t interval(args.get<string>(1));
optional<date_t> begin = interval.begin(parent->session.current_year);
optional<date_t> begin = interval.begin();
if (! begin)
throw_(std::invalid_argument,
_("Could not determine beginning of period '%1'")
<< args.get<string>(1));
ledger::epoch = parent->terminus = datetime_t(*begin);
parent->session.current_year = ledger::epoch->date().year();
});
OPTION__

View file

@ -60,9 +60,7 @@ void set_session_context(session_t * session)
}
session_t::session_t()
: flush_on_next_data_file(false),
current_year(CURRENT_DATE().year()),
journal(new journal_t)
: flush_on_next_data_file(false), journal(new journal_t)
{
TRACE_CTOR(session_t, "");

View file

@ -56,8 +56,7 @@ class session_t : public symbol_scope_t
friend void set_session_context(session_t * session);
public:
bool flush_on_next_data_file;
date_t::year_type current_year;
bool flush_on_next_data_file;
std::auto_ptr<journal_t> journal;
explicit session_t();

View file

@ -93,18 +93,17 @@ namespace {
static const std::size_t MAX_LINE = 1024;
public:
parse_context_t& context;
instance_t * parent;
accounts_map account_aliases;
const path * original_file;
path pathname;
std::istream& in;
char linebuf[MAX_LINE + 1];
std::size_t linenum;
istream_pos_type line_beg_pos;
istream_pos_type curr_pos;
optional<date_t::year_type> current_year;
parse_context_t& context;
instance_t * parent;
accounts_map account_aliases;
const path * original_file;
path pathname;
std::istream& in;
char linebuf[MAX_LINE + 1];
std::size_t linenum;
istream_pos_type line_beg_pos;
istream_pos_type curr_pos;
optional<datetime_t> prev_epoch;
instance_t(parse_context_t& _context,
std::istream& _in,
@ -207,11 +206,15 @@ instance_t::instance_t(parse_context_t& _context,
pathname(original_file ? *original_file : "/dev/stdin"), in(_in)
{
TRACE_CTOR(instance_t, "...");
DEBUG("times.epoch", "Saving epoch " << epoch);
prev_epoch = epoch; // declared in times.h
}
instance_t::~instance_t()
{
TRACE_DTOR(instance_t);
epoch = prev_epoch;
DEBUG("times.epoch", "Restored epoch to " << epoch);
}
void instance_t::parse()
@ -420,7 +423,7 @@ void instance_t::clock_in_directive(char * line, bool /*capitalized*/)
position.end_line = linenum;
position.sequence = context.sequence++;
time_xact_t event(position, parse_datetime(datetime, current_year),
time_xact_t event(position, parse_datetime(datetime),
p ? context.top_account()->find_account(p) : NULL,
n ? n : "",
end ? end : "");
@ -449,7 +452,7 @@ void instance_t::clock_out_directive(char * line, bool /*capitalized*/)
position.end_line = linenum;
position.sequence = context.sequence++;
time_xact_t event(position, parse_datetime(datetime, current_year),
time_xact_t event(position, parse_datetime(datetime),
p ? context.top_account()->find_account(p) : NULL,
n ? n : "",
end ? end : "");
@ -503,7 +506,12 @@ void instance_t::nomarket_directive(char * line)
void instance_t::year_directive(char * line)
{
current_year = lexical_cast<unsigned short>(skip_ws(line + 1));
unsigned short year(lexical_cast<unsigned short>(skip_ws(line + 1)));
DEBUG("times.epoch", "Setting current year to " << year);
// This must be set to the last day of the year, otherwise partial
// dates like "11/01" will refer to last year's november, not the
// current year.
epoch = datetime_t(date_t(year, 12, 31));
}
void instance_t::option_directive(char * line)
@ -554,7 +562,7 @@ void instance_t::automated_xact_directive(char * line)
item = ae.get();
// This is a trailing note, and possibly a metadata info tag
item->append_note(p + 1, context.scope, true, current_year);
item->append_note(p + 1, context.scope, true);
item->pos->end_pos = curr_pos;
item->pos->end_line++;
@ -634,7 +642,7 @@ void instance_t::period_xact_directive(char * line)
pe->journal = &context.journal;
if (pe->finalize()) {
context.journal.extend_xact(pe.get(), current_year);
context.journal.extend_xact(pe.get());
context.journal.period_xacts.push_back(pe.get());
pe->pos->end_pos = curr_pos;
@ -1324,7 +1332,7 @@ post_t * instance_t::parse_post(char * line,
// Parse the optional note
if (next && *next == ';') {
post->append_note(++next, context.scope, true, current_year);
post->append_note(++next, context.scope, true);
next = line + len;
DEBUG("textual.parse", "line " << linenum << ": "
<< "Parsed a posting note");
@ -1343,8 +1351,7 @@ post_t * instance_t::parse_post(char * line,
if (! context.state_stack.empty()) {
foreach (const state_t& state, context.state_stack)
if (state.type() == typeid(string))
post->parse_tags(boost::get<string>(state).c_str(), context.scope,
true, current_year);
post->parse_tags(boost::get<string>(state).c_str(), context.scope, true);
}
TRACE_STOP(post_details, 1);
@ -1407,9 +1414,9 @@ xact_t * instance_t::parse_xact(char * line,
if (char * p = std::strchr(line, '=')) {
*p++ = '\0';
xact->_date_eff = parse_date(p, current_year);
xact->_date_eff = parse_date(p);
}
xact->_date = parse_date(line, current_year);
xact->_date = parse_date(line);
// Parse the optional cleared flag: *
@ -1456,7 +1463,7 @@ xact_t * instance_t::parse_xact(char * line,
// Parse the xact note
if (next && *next == ';')
xact->append_note(++next, context.scope, false, current_year);
xact->append_note(++next, context.scope, false);
TRACE_STOP(xact_text, 1);
@ -1483,7 +1490,7 @@ xact_t * instance_t::parse_xact(char * line,
if (*p == ';') {
// This is a trailing note, and possibly a metadata info tag
item->append_note(p + 1, context.scope, true, current_year);
item->append_note(p + 1, context.scope, true);
item->pos->end_pos = curr_pos;
item->pos->end_line++;
}
@ -1542,7 +1549,7 @@ xact_t * instance_t::parse_xact(char * line,
foreach (const state_t& state, context.state_stack)
if (state.type() == typeid(string))
xact->parse_tags(boost::get<string>(state).c_str(), context.scope,
false, current_year);
false);
}
TRACE_STOP(xact_details, 1);

View file

@ -194,7 +194,6 @@ namespace {
std::deque<shared_ptr<date_io_t> > readers;
date_t parse_date_mask_routine(const char * date_str, date_io_t& io,
optional_year year,
date_traits_t * traits = NULL)
{
VERIFY(std::strlen(date_str) < 127);
@ -229,29 +228,26 @@ namespace {
*traits = io.traits;
if (! io.traits.has_year) {
when = date_t(year ? *year : CURRENT_DATE().year(),
when.month(), when.day());
when = date_t(CURRENT_DATE().year(), when.month(), when.day());
if (! year && when.month() > CURRENT_DATE().month())
if (when.month() > CURRENT_DATE().month())
when -= gregorian::years(1);
}
}
return when;
}
date_t parse_date_mask(const char * date_str, optional_year year,
date_traits_t * traits = NULL)
date_t parse_date_mask(const char * date_str, date_traits_t * traits = NULL)
{
if (input_date_io.get()) {
date_t when = parse_date_mask_routine(date_str, *input_date_io.get(),
year, traits);
traits);
if (! when.is_not_a_date())
return when;
}
foreach (shared_ptr<date_io_t>& reader, readers) {
date_t when = parse_date_mask_routine(date_str, *reader.get(),
year, traits);
date_t when = parse_date_mask_routine(date_str, *reader.get(), traits);
if (! when.is_not_a_date())
return when;
}
@ -312,7 +308,7 @@ string_to_month_of_year(const std::string& str)
return none;
}
datetime_t parse_datetime(const char * str, optional_year)
datetime_t parse_datetime(const char * str)
{
datetime_t when = input_datetime_io->parse(str);
if (when.is_not_a_date_time())
@ -320,18 +316,16 @@ datetime_t parse_datetime(const char * str, optional_year)
return when;
}
date_t parse_date(const char * str, optional_year current_year)
date_t parse_date(const char * str)
{
return parse_date_mask(str, current_year);
return parse_date_mask(str);
}
date_t date_specifier_t::begin(const optional_year& current_year) const
date_t date_specifier_t::begin() const
{
assert(year || current_year);
year_type the_year = year ? *year : static_cast<year_type>(*current_year);
year_type the_year = year ? *year : year_type(CURRENT_DATE().year());
month_type the_month = month ? *month : date_t::month_type(1);
day_type the_day = day ? *day : date_t::day_type(1);
day_type the_day = day ? *day : date_t::day_type(1);
#if !defined(NO_ASSERTS)
if (day)
@ -348,14 +342,14 @@ date_t date_specifier_t::begin(const optional_year& current_year) const
static_cast<date_t::day_type>(the_day));
}
date_t date_specifier_t::end(const optional_year& current_year) const
date_t date_specifier_t::end() const
{
if (day || wday)
return begin(current_year) + gregorian::days(1);
return begin() + gregorian::days(1);
else if (month)
return begin(current_year) + gregorian::months(1);
return begin() + gregorian::months(1);
else if (year)
return begin(current_year) + gregorian::years(1);
return begin() + gregorian::years(1);
else {
assert(false);
return date_t();
@ -1053,8 +1047,8 @@ void date_interval_t::stabilize(const optional<date_t>& date)
// want a date early enough that the range will be correct, but late
// enough that we don't spend hundreds of thousands of loops skipping
// through time.
optional<date_t> initial_start = start ? start : begin(date->year());
optional<date_t> initial_finish = finish ? finish : end(date->year());
optional<date_t> initial_start = start ? start : begin();
optional<date_t> initial_finish = finish ? finish : end();
#if defined(DEBUG_ON)
if (initial_start)
@ -1117,13 +1111,8 @@ void date_interval_t::stabilize(const optional<date_t>& date)
#endif
}
else if (range) {
if (date) {
start = range->begin(date->year());
finish = range->end(date->year());
} else {
start = range->begin();
finish = range->end();
}
start = range->begin();
finish = range->end();
}
aligned = true;
}
@ -1229,7 +1218,7 @@ date_interval_t& date_interval_t::operator++()
return *this;
}
void date_interval_t::dump(std::ostream& out, optional_year current_year)
void date_interval_t::dump(std::ostream& out)
{
out << _("--- Before stabilization ---") << std::endl;
@ -1243,7 +1232,7 @@ void date_interval_t::dump(std::ostream& out, optional_year current_year)
if (duration)
out << _("duration: ") << duration->to_string() << std::endl;
stabilize(begin(current_year));
stabilize(begin());
out << std::endl
<< _("--- After stabilization ---") << std::endl;
@ -1318,7 +1307,7 @@ date_parser_t::lexer_t::token_t date_parser_t::lexer_t::next_token()
try {
date_traits_t traits;
date_t when = parse_date_mask(possible_date.c_str(), none, &traits);
date_t when = parse_date_mask(possible_date.c_str(), &traits);
if (! when.is_not_a_date()) {
begin = i;
return token_t(token_t::TOK_DATE,

View file

@ -77,27 +77,23 @@ extern optional<datetime_t> epoch;
#define CURRENT_DATE() \
(epoch ? epoch->date() : boost::gregorian::day_clock::universal_day())
extern date_time::weekdays start_of_week;
extern date_time::weekdays start_of_week;
optional<date_time::weekdays>
string_to_day_of_week(const std::string& str);
optional<date_time::months_of_year>
string_to_month_of_year(const std::string& str);
typedef optional<date_t::year_type> optional_year;
datetime_t parse_datetime(const char * str);
datetime_t parse_datetime(const char * str, optional_year current_year = none);
inline datetime_t parse_datetime(const std::string& str,
optional_year current_year = none) {
return parse_datetime(str.c_str(), current_year);
inline datetime_t parse_datetime(const std::string& str) {
return parse_datetime(str.c_str());
}
date_t parse_date(const char * str, optional_year current_year = none);
date_t parse_date(const char * str);
inline date_t parse_date(const std::string& str,
optional_year current_year = none) {
return parse_date(str.c_str(), current_year);
inline date_t parse_date(const std::string& str) {
return parse_date(str.c_str());
}
enum format_type_t {
@ -329,12 +325,11 @@ public:
TRACE_DTOR(date_specifier_t);
}
date_t begin(const optional_year& current_year = none) const;
date_t end(const optional_year& current_year = none) const;
date_t begin() const;
date_t end() const;
bool is_within(const date_t& date,
const optional_year& current_year = none) const {
return date >= begin(current_year) && date < end(current_year);
bool is_within(const date_t& date) const {
return date >= begin() && date < end();
}
optional<date_duration_t> implied_duration() const {
@ -404,27 +399,26 @@ public:
TRACE_DTOR(date_range_t);
}
optional<date_t> begin(const optional_year& current_year = none) const {
optional<date_t> begin() const {
if (range_begin)
return range_begin->begin(current_year);
return range_begin->begin();
else
return none;
}
optional<date_t> end(const optional_year& current_year = none) const {
optional<date_t> end() const {
if (range_end) {
if (end_inclusive)
return range_end->end(current_year);
return range_end->end();
else
return range_end->begin(current_year);
return range_end->begin();
} else {
return none;
}
}
bool is_within(const date_t& date,
const optional_year& current_year = none) const {
optional<date_t> b = begin(current_year);
optional<date_t> e = end(current_year);
bool is_within(const date_t& date) const {
optional<date_t> b = begin();
optional<date_t> e = end();
bool after_begin = b ? date >= *b : true;
bool before_end = e ? date < *e : true;
return after_begin && before_end;
@ -482,19 +476,19 @@ public:
TRACE_DTOR(date_specifier_or_range_t);
}
optional<date_t> begin(const optional_year& current_year = none) const {
optional<date_t> begin() const {
if (specifier_or_range.type() == typeid(date_specifier_t))
return boost::get<date_specifier_t>(specifier_or_range).begin(current_year);
return boost::get<date_specifier_t>(specifier_or_range).begin();
else if (specifier_or_range.type() == typeid(date_range_t))
return boost::get<date_range_t>(specifier_or_range).begin(current_year);
return boost::get<date_range_t>(specifier_or_range).begin();
else
return none;
}
optional<date_t> end(const optional_year& current_year = none) const {
optional<date_t> end() const {
if (specifier_or_range.type() == typeid(date_specifier_t))
return boost::get<date_specifier_t>(specifier_or_range).end(current_year);
return boost::get<date_specifier_t>(specifier_or_range).end();
else if (specifier_or_range.type() == typeid(date_range_t))
return boost::get<date_range_t>(specifier_or_range).end(current_year);
return boost::get<date_range_t>(specifier_or_range).end();
else
return none;
}
@ -571,11 +565,11 @@ public:
return is_valid();
}
optional<date_t> begin(const optional_year& current_year = none) const {
return start ? start : (range ? range->begin(current_year) : none);
optional<date_t> begin() const {
return start ? start : (range ? range->begin() : none);
}
optional<date_t> end(const optional_year& current_year = none) const {
return finish ? finish : (range ? range->end(current_year) : none);
optional<date_t> end() const {
return finish ? finish : (range ? range->end() : none);
}
void parse(const string& str);
@ -590,7 +584,7 @@ public:
/** Find the current or next period containing date. Returns true if the
date_interval_t object has been altered to reflect the interval
containing date, or false if no such period can be found. */
bool find_period(const date_t& date);
bool find_period(const date_t& date = CURRENT_DATE());
optional<date_t> inclusive_end() const {
if (end_of_duration)
@ -601,7 +595,7 @@ public:
date_interval_t& operator++();
void dump(std::ostream& out, optional_year current_year = none);
void dump(std::ostream& out);
#if defined(HAVE_BOOST_SERIALIZATION)
private:

View file

@ -207,7 +207,7 @@ void expr_t::token_t::next(std::istream& in, const parse_flags_t& pflags,
length++;
date_interval_t timespan(buf);
optional<date_t> begin = timespan.begin(CURRENT_DATE().year());
optional<date_t> begin = timespan.begin();
if (! begin)
throw_(parse_error,
_("Date specifier does not refer to a starting date"));

View file

@ -629,8 +629,7 @@ namespace {
} // unnamed namespace
void auto_xact_t::extend_xact(xact_base_t& xact,
optional<date_t::year_type> current_year)
void auto_xact_t::extend_xact(xact_base_t& xact)
{
posts_list initial_posts(xact.posts.begin(), xact.posts.end());
@ -680,10 +679,8 @@ void auto_xact_t::extend_xact(xact_base_t& xact,
if (deferred_notes) {
foreach (deferred_tag_data_t& data, *deferred_notes) {
if (data.apply_to_post == NULL)
initial_post->parse_tags(data.tag_data.c_str(),
bound_scope,
data.overwrite_existing,
current_year);
initial_post->parse_tags(data.tag_data.c_str(), bound_scope,
data.overwrite_existing);
}
}
if (check_exprs) {
@ -778,10 +775,8 @@ void auto_xact_t::extend_xact(xact_base_t& xact,
if (deferred_notes) {
foreach (deferred_tag_data_t& data, *deferred_notes) {
if (data.apply_to_post == post)
new_post->parse_tags(data.tag_data.c_str(),
bound_scope,
data.overwrite_existing,
current_year);
new_post->parse_tags(data.tag_data.c_str(), bound_scope,
data.overwrite_existing);
}
}
}

View file

@ -196,15 +196,13 @@ public:
virtual void parse_tags(const char * p,
scope_t&,
bool overwrite_existing = true,
optional<date_t::year_type> = none) {
bool overwrite_existing = true) {
if (! deferred_notes)
deferred_notes = deferred_notes_list();
deferred_notes->push_back(deferred_tag_data_t(p, overwrite_existing));
}
virtual void extend_xact(xact_base_t& xact,
optional<date_t::year_type> current_year);
virtual void extend_xact(xact_base_t& xact);
#if defined(HAVE_BOOST_SERIALIZATION)
private: