diff options
author | John Wiegley <johnw@newartisans.com> | 2009-11-17 22:12:17 -0500 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2009-11-17 22:12:17 -0500 |
commit | d6cb382b20a1c65fde043af0ef41f21570027b27 (patch) | |
tree | 088a1cddde518aa7f32220386d23bb27dab93012 | |
parent | a866f39210534052bc77c975344706c84c39a4a9 (diff) | |
download | fork-ledger-d6cb382b20a1c65fde043af0ef41f21570027b27.tar.gz fork-ledger-d6cb382b20a1c65fde043af0ef41f21570027b27.tar.bz2 fork-ledger-d6cb382b20a1c65fde043af0ef41f21570027b27.zip |
Moved date_interval_t::duration_t to date_duration_t
-rw-r--r-- | src/times.cc | 58 | ||||
-rw-r--r-- | src/times.h | 156 |
2 files changed, 106 insertions, 108 deletions
diff --git a/src/times.cc b/src/times.cc index 5cccec21..2fb8479b 100644 --- a/src/times.cc +++ b/src/times.cc @@ -319,16 +319,16 @@ date_t parse_date(const char * str, optional<date_t::year_type> current_year) } std::ostream& operator<<(std::ostream& out, - const date_interval_t::duration_t& duration) + const date_duration_t& duration) { - if (duration.quantum == date_interval_t::duration_t::DAYS) + if (duration.quantum == date_duration_t::DAYS) out << duration.length << " day(s)"; - else if (duration.quantum == date_interval_t::duration_t::WEEKS) + else if (duration.quantum == date_duration_t::WEEKS) out << duration.length << " week(s)"; - else if (duration.quantum == date_interval_t::duration_t::MONTHS) + else if (duration.quantum == date_duration_t::MONTHS) out << duration.length << " month(s)"; else { - assert(duration.quantum == date_interval_t::duration_t::YEARS); + assert(duration.quantum == date_duration_t::YEARS); out << duration.length << " year(s)"; } return out; @@ -395,8 +395,8 @@ void date_interval_t::stabilize(const optional<date_t>& date) date_t when = start ? *start : *date; - if (duration->quantum == duration_t::MONTHS || - duration->quantum == duration_t::YEARS) { + if (duration->quantum == date_duration_t::MONTHS || + duration->quantum == date_duration_t::YEARS) { DEBUG("times.interval", "stabilize: monthly or yearly duration"); start = date_t(when.year(), gregorian::Jan, 1); @@ -405,7 +405,7 @@ void date_interval_t::stabilize(const optional<date_t>& date) start = date_t(when - gregorian::days(400)); - if (duration->quantum == duration_t::WEEKS) { + if (duration->quantum == date_duration_t::WEEKS) { // Move it to a Sunday while (start->day_of_week() != start_of_week) *start += gregorian::days(1); @@ -603,20 +603,20 @@ namespace { date_t finish; bool parse_specifier = false; - optional<date_interval_t::duration_t> duration; + optional<date_duration_t> duration; assert(look_for_start || look_for_finish); if (word == _("year")) { - duration = date_interval_t::duration_t(date_interval_t::duration_t::YEARS, 1); + duration = date_duration_t(date_duration_t::YEARS, 1); start = gregorian::date(start.year(), 1, 1); } else if (word == _("month")) { - duration = date_interval_t::duration_t(date_interval_t::duration_t::MONTHS, 1); + duration = date_duration_t(date_duration_t::MONTHS, 1); start = gregorian::date(start.year(), start.month(), 1); } else if (word == _("today") || word == _("day")) { - duration = date_interval_t::duration_t(date_interval_t::duration_t::DAYS, 1); + duration = date_duration_t(date_duration_t::DAYS, 1); } else { parse_specifier = true; @@ -657,41 +657,41 @@ void date_interval_t::parse(std::istream& in) int quantity = lexical_cast<int>(word); read_lower_word(in, word); if (word == _("days")) - duration = duration_t(duration_t::DAYS, quantity); + duration = date_duration_t(date_duration_t::DAYS, quantity); else if (word == _("weeks")) - duration = duration_t(duration_t::WEEKS, quantity); + duration = date_duration_t(date_duration_t::WEEKS, quantity); else if (word == _("months")) - duration = duration_t(duration_t::MONTHS, quantity); + duration = date_duration_t(date_duration_t::MONTHS, quantity); else if (word == _("quarters")) - duration = duration_t(duration_t::MONTHS, 3 * quantity); + duration = date_duration_t(date_duration_t::MONTHS, 3 * quantity); else if (word == _("years")) - duration = duration_t(duration_t::YEARS, quantity); + duration = date_duration_t(date_duration_t::YEARS, quantity); } else if (word == _("day")) - duration = duration_t(duration_t::DAYS, 1); + duration = date_duration_t(date_duration_t::DAYS, 1); else if (word == _("week")) - duration = duration_t(duration_t::WEEKS, 1); + duration = date_duration_t(date_duration_t::WEEKS, 1); else if (word == _("month")) - duration = duration_t(duration_t::MONTHS, 1); + duration = date_duration_t(date_duration_t::MONTHS, 1); else if (word == _("quarter")) - duration = duration_t(duration_t::MONTHS, 3); + duration = date_duration_t(date_duration_t::MONTHS, 3); else if (word == _("year")) - duration = duration_t(duration_t::YEARS, 1); + duration = date_duration_t(date_duration_t::YEARS, 1); } else if (word == _("daily")) - duration = duration_t(duration_t::DAYS, 1); + duration = date_duration_t(date_duration_t::DAYS, 1); else if (word == _("weekly")) - duration = duration_t(duration_t::WEEKS, 1); + duration = date_duration_t(date_duration_t::WEEKS, 1); else if (word == _("biweekly")) - duration = duration_t(duration_t::WEEKS, 2); + duration = date_duration_t(date_duration_t::WEEKS, 2); else if (word == _("monthly")) - duration = duration_t(duration_t::MONTHS, 1); + duration = date_duration_t(date_duration_t::MONTHS, 1); else if (word == _("bimonthly")) - duration = duration_t(duration_t::MONTHS, 2); + duration = date_duration_t(date_duration_t::MONTHS, 2); else if (word == _("quarterly")) - duration = duration_t(duration_t::MONTHS, 3); + duration = date_duration_t(date_duration_t::MONTHS, 3); else if (word == _("yearly")) - duration = duration_t(duration_t::YEARS, 1); + duration = date_duration_t(date_duration_t::YEARS, 1); else if (word == _("this") || word == _("last") || word == _("next") || word == _("today")) { parse_date_words(in, word, *this); diff --git a/src/times.h b/src/times.h index 676ec450..c50b0366 100644 --- a/src/times.h +++ b/src/times.h @@ -59,7 +59,6 @@ inline bool is_valid(const datetime_t& moment) { } typedef boost::gregorian::date date_t; -typedef boost::gregorian::date_duration date_duration_t; typedef boost::gregorian::date_iterator date_iterator_t; inline bool is_valid(const date_t& moment) { @@ -182,88 +181,88 @@ private: #endif // HAVE_BOOST_SERIALIZATION }; -class date_interval_t : public equality_comparable<date_interval_t> +struct date_duration_t { -public: - struct duration_t - { - enum skip_quantum_t { - DAYS, WEEKS, MONTHS, YEARS - } quantum; - int length; - - duration_t() : quantum(DAYS), length(0) { - TRACE_CTOR(date_interval_t::duration_t, ""); - } - duration_t(skip_quantum_t _quantum, int _length) - : quantum(_quantum), length(_length) { - TRACE_CTOR(date_interval_t::duration_t, "skip_quantum_t, int"); - } - duration_t(const duration_t& dur) - : quantum(dur.quantum), length(dur.length) { - TRACE_CTOR(date_interval_t::duration_t, "copy"); - } - ~duration_t() throw() { - TRACE_DTOR(date_interval_t::duration_t); - } - - date_t add(const date_t& date) const { - switch (quantum) { - case DAYS: - return date + gregorian::days(length); - case WEEKS: - return date + gregorian::weeks(length); - case MONTHS: - return date + gregorian::months(length); - case YEARS: - return date + gregorian::years(length); - default: - assert(false); return date_t(); - } + enum skip_quantum_t { + DAYS, WEEKS, MONTHS, YEARS + } quantum; + int length; + + date_duration_t() : quantum(DAYS), length(0) { + TRACE_CTOR(date_duration_t, ""); + } + date_duration_t(skip_quantum_t _quantum, int _length) + : quantum(_quantum), length(_length) { + TRACE_CTOR(date_duration_t, "skip_quantum_t, int"); + } + date_duration_t(const date_duration_t& dur) + : quantum(dur.quantum), length(dur.length) { + TRACE_CTOR(date_duration_t, "copy"); + } + ~date_duration_t() throw() { + TRACE_DTOR(date_duration_t); + } + + date_t add(const date_t& date) const { + switch (quantum) { + case DAYS: + return date + gregorian::days(length); + case WEEKS: + return date + gregorian::weeks(length); + case MONTHS: + return date + gregorian::months(length); + case YEARS: + return date + gregorian::years(length); + default: + assert(false); return date_t(); } + } - date_t subtract(const date_t& date) const { - switch (quantum) { - case DAYS: - return date - gregorian::days(length); - case WEEKS: - return date - gregorian::weeks(length); - case MONTHS: - return date - gregorian::months(length); - case YEARS: - return date - gregorian::years(length); - default: - assert(false); return date_t(); - } + date_t subtract(const date_t& date) const { + switch (quantum) { + case DAYS: + return date - gregorian::days(length); + case WEEKS: + return date - gregorian::weeks(length); + case MONTHS: + return date - gregorian::months(length); + case YEARS: + return date - gregorian::years(length); + default: + assert(false); return date_t(); } + } #if defined(HAVE_BOOST_SERIALIZATION) - private: - /** Serialization. */ +private: + /** Serialization. */ - friend class boost::serialization::access; + friend class boost::serialization::access; - template<class Archive> - void serialize(Archive& ar, const unsigned int /* version */) { - ar & quantum; - ar & length; - } + template<class Archive> + void serialize(Archive& ar, const unsigned int /* version */) { + ar & quantum; + ar & length; + } #endif // HAVE_BOOST_SERIALIZATION - }; - - static date_t add_duration(const date_t& date, - const duration_t& duration); - static date_t subtract_duration(const date_t& date, - const duration_t& duration); - - optional<date_t> start; - bool aligned; - optional<duration_t> skip_duration; - std::size_t factor; - optional<date_t> next; - optional<duration_t> duration; - optional<date_t> end_of_duration; - optional<date_t> finish; +}; + +class date_interval_t : public equality_comparable<date_interval_t> +{ +public: + static date_t add_duration(const date_t& date, + const date_duration_t& duration); + static date_t subtract_duration(const date_t& date, + const date_duration_t& duration); + + optional<date_t> start; // the real start, after adjustment + optional<date_t> finish; // the real end, likewise + bool aligned; + optional<date_duration_t> skip_duration; + std::size_t factor; + optional<date_t> next; + optional<date_duration_t> duration; + optional<date_t> end_of_duration; explicit date_interval_t() : aligned(false), factor(1) { TRACE_CTOR(date_interval_t, ""); @@ -274,13 +273,13 @@ public: } date_interval_t(const date_interval_t& other) : start(other.start), + finish(other.finish), aligned(other.aligned), skip_duration(other.skip_duration), factor(other.factor), next(other.next), duration(other.duration), - end_of_duration(other.end_of_duration), - finish(other.finish) { + end_of_duration(other.end_of_duration) { TRACE_CTOR(date_interval_t, "copy"); } ~date_interval_t() throw() { @@ -333,13 +332,13 @@ private: template<class Archive> void serialize(Archive& ar, const unsigned int /* version */) { ar & start; + ar & finish; ar & aligned; ar & skip_duration; ar & factor; ar & next; ar & duration; ar & end_of_duration; - ar & finish; } #endif // HAVE_BOOST_SERIALIZATION }; @@ -347,8 +346,7 @@ private: void times_initialize(); void times_shutdown(); -std::ostream& operator<<(std::ostream& out, - const date_interval_t::duration_t& duration); +std::ostream& operator<<(std::ostream& out, const date_duration_t& duration); } // namespace ledger |