Moved date_interval_t::duration_t to date_duration_t
This commit is contained in:
parent
a866f39210
commit
d6cb382b20
2 changed files with 111 additions and 113 deletions
58
src/times.cc
58
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,
|
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)";
|
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)";
|
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)";
|
out << duration.length << " month(s)";
|
||||||
else {
|
else {
|
||||||
assert(duration.quantum == date_interval_t::duration_t::YEARS);
|
assert(duration.quantum == date_duration_t::YEARS);
|
||||||
out << duration.length << " year(s)";
|
out << duration.length << " year(s)";
|
||||||
}
|
}
|
||||||
return out;
|
return out;
|
||||||
|
|
@ -395,8 +395,8 @@ void date_interval_t::stabilize(const optional<date_t>& date)
|
||||||
|
|
||||||
date_t when = start ? *start : *date;
|
date_t when = start ? *start : *date;
|
||||||
|
|
||||||
if (duration->quantum == duration_t::MONTHS ||
|
if (duration->quantum == date_duration_t::MONTHS ||
|
||||||
duration->quantum == duration_t::YEARS) {
|
duration->quantum == date_duration_t::YEARS) {
|
||||||
DEBUG("times.interval", "stabilize: monthly or yearly duration");
|
DEBUG("times.interval", "stabilize: monthly or yearly duration");
|
||||||
|
|
||||||
start = date_t(when.year(), gregorian::Jan, 1);
|
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));
|
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
|
// Move it to a Sunday
|
||||||
while (start->day_of_week() != start_of_week)
|
while (start->day_of_week() != start_of_week)
|
||||||
*start += gregorian::days(1);
|
*start += gregorian::days(1);
|
||||||
|
|
@ -603,20 +603,20 @@ namespace {
|
||||||
date_t finish;
|
date_t finish;
|
||||||
bool parse_specifier = false;
|
bool parse_specifier = false;
|
||||||
|
|
||||||
optional<date_interval_t::duration_t> duration;
|
optional<date_duration_t> duration;
|
||||||
|
|
||||||
assert(look_for_start || look_for_finish);
|
assert(look_for_start || look_for_finish);
|
||||||
|
|
||||||
if (word == _("year")) {
|
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);
|
start = gregorian::date(start.year(), 1, 1);
|
||||||
}
|
}
|
||||||
else if (word == _("month")) {
|
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);
|
start = gregorian::date(start.year(), start.month(), 1);
|
||||||
}
|
}
|
||||||
else if (word == _("today") || word == _("day")) {
|
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 {
|
else {
|
||||||
parse_specifier = true;
|
parse_specifier = true;
|
||||||
|
|
@ -657,41 +657,41 @@ void date_interval_t::parse(std::istream& in)
|
||||||
int quantity = lexical_cast<int>(word);
|
int quantity = lexical_cast<int>(word);
|
||||||
read_lower_word(in, word);
|
read_lower_word(in, word);
|
||||||
if (word == _("days"))
|
if (word == _("days"))
|
||||||
duration = duration_t(duration_t::DAYS, quantity);
|
duration = date_duration_t(date_duration_t::DAYS, quantity);
|
||||||
else if (word == _("weeks"))
|
else if (word == _("weeks"))
|
||||||
duration = duration_t(duration_t::WEEKS, quantity);
|
duration = date_duration_t(date_duration_t::WEEKS, quantity);
|
||||||
else if (word == _("months"))
|
else if (word == _("months"))
|
||||||
duration = duration_t(duration_t::MONTHS, quantity);
|
duration = date_duration_t(date_duration_t::MONTHS, quantity);
|
||||||
else if (word == _("quarters"))
|
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"))
|
else if (word == _("years"))
|
||||||
duration = duration_t(duration_t::YEARS, quantity);
|
duration = date_duration_t(date_duration_t::YEARS, quantity);
|
||||||
}
|
}
|
||||||
else if (word == _("day"))
|
else if (word == _("day"))
|
||||||
duration = duration_t(duration_t::DAYS, 1);
|
duration = date_duration_t(date_duration_t::DAYS, 1);
|
||||||
else if (word == _("week"))
|
else if (word == _("week"))
|
||||||
duration = duration_t(duration_t::WEEKS, 1);
|
duration = date_duration_t(date_duration_t::WEEKS, 1);
|
||||||
else if (word == _("month"))
|
else if (word == _("month"))
|
||||||
duration = duration_t(duration_t::MONTHS, 1);
|
duration = date_duration_t(date_duration_t::MONTHS, 1);
|
||||||
else if (word == _("quarter"))
|
else if (word == _("quarter"))
|
||||||
duration = duration_t(duration_t::MONTHS, 3);
|
duration = date_duration_t(date_duration_t::MONTHS, 3);
|
||||||
else if (word == _("year"))
|
else if (word == _("year"))
|
||||||
duration = duration_t(duration_t::YEARS, 1);
|
duration = date_duration_t(date_duration_t::YEARS, 1);
|
||||||
}
|
}
|
||||||
else if (word == _("daily"))
|
else if (word == _("daily"))
|
||||||
duration = duration_t(duration_t::DAYS, 1);
|
duration = date_duration_t(date_duration_t::DAYS, 1);
|
||||||
else if (word == _("weekly"))
|
else if (word == _("weekly"))
|
||||||
duration = duration_t(duration_t::WEEKS, 1);
|
duration = date_duration_t(date_duration_t::WEEKS, 1);
|
||||||
else if (word == _("biweekly"))
|
else if (word == _("biweekly"))
|
||||||
duration = duration_t(duration_t::WEEKS, 2);
|
duration = date_duration_t(date_duration_t::WEEKS, 2);
|
||||||
else if (word == _("monthly"))
|
else if (word == _("monthly"))
|
||||||
duration = duration_t(duration_t::MONTHS, 1);
|
duration = date_duration_t(date_duration_t::MONTHS, 1);
|
||||||
else if (word == _("bimonthly"))
|
else if (word == _("bimonthly"))
|
||||||
duration = duration_t(duration_t::MONTHS, 2);
|
duration = date_duration_t(date_duration_t::MONTHS, 2);
|
||||||
else if (word == _("quarterly"))
|
else if (word == _("quarterly"))
|
||||||
duration = duration_t(duration_t::MONTHS, 3);
|
duration = date_duration_t(date_duration_t::MONTHS, 3);
|
||||||
else if (word == _("yearly"))
|
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") ||
|
else if (word == _("this") || word == _("last") || word == _("next") ||
|
||||||
word == _("today")) {
|
word == _("today")) {
|
||||||
parse_date_words(in, word, *this);
|
parse_date_words(in, word, *this);
|
||||||
|
|
|
||||||
46
src/times.h
46
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 date_t;
|
||||||
typedef boost::gregorian::date_duration date_duration_t;
|
|
||||||
typedef boost::gregorian::date_iterator date_iterator_t;
|
typedef boost::gregorian::date_iterator date_iterator_t;
|
||||||
|
|
||||||
inline bool is_valid(const date_t& moment) {
|
inline bool is_valid(const date_t& moment) {
|
||||||
|
|
@ -182,29 +181,26 @@ private:
|
||||||
#endif // HAVE_BOOST_SERIALIZATION
|
#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 {
|
enum skip_quantum_t {
|
||||||
DAYS, WEEKS, MONTHS, YEARS
|
DAYS, WEEKS, MONTHS, YEARS
|
||||||
} quantum;
|
} quantum;
|
||||||
int length;
|
int length;
|
||||||
|
|
||||||
duration_t() : quantum(DAYS), length(0) {
|
date_duration_t() : quantum(DAYS), length(0) {
|
||||||
TRACE_CTOR(date_interval_t::duration_t, "");
|
TRACE_CTOR(date_duration_t, "");
|
||||||
}
|
}
|
||||||
duration_t(skip_quantum_t _quantum, int _length)
|
date_duration_t(skip_quantum_t _quantum, int _length)
|
||||||
: quantum(_quantum), length(_length) {
|
: quantum(_quantum), length(_length) {
|
||||||
TRACE_CTOR(date_interval_t::duration_t, "skip_quantum_t, int");
|
TRACE_CTOR(date_duration_t, "skip_quantum_t, int");
|
||||||
}
|
}
|
||||||
duration_t(const duration_t& dur)
|
date_duration_t(const date_duration_t& dur)
|
||||||
: quantum(dur.quantum), length(dur.length) {
|
: quantum(dur.quantum), length(dur.length) {
|
||||||
TRACE_CTOR(date_interval_t::duration_t, "copy");
|
TRACE_CTOR(date_duration_t, "copy");
|
||||||
}
|
}
|
||||||
~duration_t() throw() {
|
~date_duration_t() throw() {
|
||||||
TRACE_DTOR(date_interval_t::duration_t);
|
TRACE_DTOR(date_duration_t);
|
||||||
}
|
}
|
||||||
|
|
||||||
date_t add(const date_t& date) const {
|
date_t add(const date_t& date) const {
|
||||||
|
|
@ -251,19 +247,22 @@ public:
|
||||||
#endif // HAVE_BOOST_SERIALIZATION
|
#endif // HAVE_BOOST_SERIALIZATION
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class date_interval_t : public equality_comparable<date_interval_t>
|
||||||
|
{
|
||||||
|
public:
|
||||||
static date_t add_duration(const date_t& date,
|
static date_t add_duration(const date_t& date,
|
||||||
const duration_t& duration);
|
const date_duration_t& duration);
|
||||||
static date_t subtract_duration(const date_t& date,
|
static date_t subtract_duration(const date_t& date,
|
||||||
const duration_t& duration);
|
const date_duration_t& duration);
|
||||||
|
|
||||||
optional<date_t> start;
|
optional<date_t> start; // the real start, after adjustment
|
||||||
|
optional<date_t> finish; // the real end, likewise
|
||||||
bool aligned;
|
bool aligned;
|
||||||
optional<duration_t> skip_duration;
|
optional<date_duration_t> skip_duration;
|
||||||
std::size_t factor;
|
std::size_t factor;
|
||||||
optional<date_t> next;
|
optional<date_t> next;
|
||||||
optional<duration_t> duration;
|
optional<date_duration_t> duration;
|
||||||
optional<date_t> end_of_duration;
|
optional<date_t> end_of_duration;
|
||||||
optional<date_t> finish;
|
|
||||||
|
|
||||||
explicit date_interval_t() : aligned(false), factor(1) {
|
explicit date_interval_t() : aligned(false), factor(1) {
|
||||||
TRACE_CTOR(date_interval_t, "");
|
TRACE_CTOR(date_interval_t, "");
|
||||||
|
|
@ -274,13 +273,13 @@ public:
|
||||||
}
|
}
|
||||||
date_interval_t(const date_interval_t& other)
|
date_interval_t(const date_interval_t& other)
|
||||||
: start(other.start),
|
: start(other.start),
|
||||||
|
finish(other.finish),
|
||||||
aligned(other.aligned),
|
aligned(other.aligned),
|
||||||
skip_duration(other.skip_duration),
|
skip_duration(other.skip_duration),
|
||||||
factor(other.factor),
|
factor(other.factor),
|
||||||
next(other.next),
|
next(other.next),
|
||||||
duration(other.duration),
|
duration(other.duration),
|
||||||
end_of_duration(other.end_of_duration),
|
end_of_duration(other.end_of_duration) {
|
||||||
finish(other.finish) {
|
|
||||||
TRACE_CTOR(date_interval_t, "copy");
|
TRACE_CTOR(date_interval_t, "copy");
|
||||||
}
|
}
|
||||||
~date_interval_t() throw() {
|
~date_interval_t() throw() {
|
||||||
|
|
@ -333,13 +332,13 @@ private:
|
||||||
template<class Archive>
|
template<class Archive>
|
||||||
void serialize(Archive& ar, const unsigned int /* version */) {
|
void serialize(Archive& ar, const unsigned int /* version */) {
|
||||||
ar & start;
|
ar & start;
|
||||||
|
ar & finish;
|
||||||
ar & aligned;
|
ar & aligned;
|
||||||
ar & skip_duration;
|
ar & skip_duration;
|
||||||
ar & factor;
|
ar & factor;
|
||||||
ar & next;
|
ar & next;
|
||||||
ar & duration;
|
ar & duration;
|
||||||
ar & end_of_duration;
|
ar & end_of_duration;
|
||||||
ar & finish;
|
|
||||||
}
|
}
|
||||||
#endif // HAVE_BOOST_SERIALIZATION
|
#endif // HAVE_BOOST_SERIALIZATION
|
||||||
};
|
};
|
||||||
|
|
@ -347,8 +346,7 @@ private:
|
||||||
void times_initialize();
|
void times_initialize();
|
||||||
void times_shutdown();
|
void times_shutdown();
|
||||||
|
|
||||||
std::ostream& operator<<(std::ostream& out,
|
std::ostream& operator<<(std::ostream& out, const date_duration_t& duration);
|
||||||
const date_interval_t::duration_t& duration);
|
|
||||||
|
|
||||||
} // namespace ledger
|
} // namespace ledger
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue