summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2009-11-18 01:27:34 -0500
committerJohn Wiegley <johnw@newartisans.com>2009-11-18 01:28:01 -0500
commitfe9af7ace7a9b25a3eefe481b6b7a38ad2f758ed (patch)
treeba08b43c1757db16a8febf9bbea546f22a2c4ada
parent3e91c3bf2c3662c40f0fe7c9cf197f6b0c725269 (diff)
downloadfork-ledger-fe9af7ace7a9b25a3eefe481b6b7a38ad2f758ed.tar.gz
fork-ledger-fe9af7ace7a9b25a3eefe481b6b7a38ad2f758ed.tar.bz2
fork-ledger-fe9af7ace7a9b25a3eefe481b6b7a38ad2f758ed.zip
Added a "range" member to date_interval_t
This is used to define the beginning/ending ranges of the time period, before it becomes fixed (by calling stabilize()) and then sets the values of start and end.
-rw-r--r--src/times.cc19
-rw-r--r--src/times.h45
2 files changed, 34 insertions, 30 deletions
diff --git a/src/times.cc b/src/times.cc
index 3070249c..076cef3d 100644
--- a/src/times.cc
+++ b/src/times.cc
@@ -194,7 +194,7 @@ namespace {
std::vector<shared_ptr<date_io_t> > readers;
date_t parse_date_mask_routine(const char * date_str, date_io_t& io,
- optional<date_t::year_type> year,
+ optional_year year,
date_traits_t * traits = NULL)
{
date_t when;
@@ -231,8 +231,7 @@ namespace {
return when;
}
- date_t parse_date_mask(const char * date_str,
- optional<date_t::year_type> year,
+ date_t parse_date_mask(const char * date_str, optional_year year,
date_traits_t * traits = NULL)
{
if (input_date_io.get()) {
@@ -305,7 +304,7 @@ string_to_month_of_year(const std::string& str)
return none;
}
-datetime_t parse_datetime(const char * str, optional<date_t::year_type>)
+datetime_t parse_datetime(const char * str, optional_year)
{
datetime_t when = input_datetime_io->parse(str);
if (when.is_not_a_date_time())
@@ -313,13 +312,12 @@ datetime_t parse_datetime(const char * str, optional<date_t::year_type>)
return when;
}
-date_t parse_date(const char * str, optional<date_t::year_type> current_year)
+date_t parse_date(const char * str, optional_year current_year)
{
return parse_date_mask(str, current_year);
}
-date_t
-date_specifier_t::begin(const optional<date_t::year_type>& current_year) const
+date_t date_specifier_t::begin(const optional_year& current_year) const
{
assert(year || current_year);
@@ -340,8 +338,7 @@ date_specifier_t::begin(const optional<date_t::year_type>& current_year) const
static_cast<date_t::day_type>(the_day));
}
-date_t
-date_specifier_t::end(const optional<date_t::year_type>& current_year) const
+date_t date_specifier_t::end(const optional_year& current_year) const
{
if (day || wday)
return begin(current_year) + gregorian::days(1);
@@ -418,8 +415,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;
- optional<date_t> initial_finish = finish;
+ optional<date_t> initial_start = start ? start : begin(date->year());
+ optional<date_t> initial_finish = finish ? finish : end(date->year());
#if defined(DEBUG_ON)
if (initial_start)
diff --git a/src/times.h b/src/times.h
index b13f48b1..94ae4752 100644
--- a/src/times.h
+++ b/src/times.h
@@ -84,19 +84,19 @@ string_to_day_of_week(const std::string& str);
optional<date_time::months_of_year>
string_to_month_of_year(const std::string& str);
-datetime_t parse_datetime(const char * str,
- optional<date_t::year_type> current_year = none);
+typedef optional<date_t::year_type> optional_year;
+
+datetime_t parse_datetime(const char * str, optional_year current_year = none);
inline datetime_t parse_datetime(const std::string& str,
- optional<date_t::year_type> current_year = none) {
+ optional_year current_year = none) {
return parse_datetime(str.c_str(), current_year);
}
-date_t parse_date(const char * str,
- optional<date_t::year_type> current_year = none);
+date_t parse_date(const char * str, optional_year current_year = none);
inline date_t parse_date(const std::string& str,
- optional<date_t::year_type> current_year = none) {
+ optional_year current_year = none) {
return parse_date(str.c_str(), current_year);
}
@@ -210,11 +210,11 @@ public:
day = date.day();
}
- date_t begin(const optional<date_t::year_type>& current_year = none) const;
- date_t end(const optional<date_t::year_type>& current_year = none) const;
+ date_t begin(const optional_year& current_year = none) const;
+ date_t end(const optional_year& current_year = none) const;
bool is_within(const date_t& date,
- const optional<date_t::year_type>& current_year = none) const {
+ const optional_year& current_year = none) const {
return date >= begin(current_year) && date < end(current_year);
}
@@ -242,15 +242,13 @@ class date_range_t
optional<date_specifier_t> range_end;
public:
- optional<date_t>
- begin(const optional<date_t::year_type>& current_year = none) const {
+ optional<date_t> begin(const optional_year& current_year = none) const {
if (range_begin)
return range_begin->begin(current_year);
else
return none;
}
- optional<date_t>
- end(const optional<date_t::year_type>& current_year = none) const {
+ optional<date_t> end(const optional_year& current_year = none) const {
if (range_end)
return range_end->end(current_year);
else
@@ -258,7 +256,7 @@ public:
}
bool is_within(const date_t& date,
- const optional<date_t::year_type>& current_year = none) const {
+ const optional_year& current_year = none) const {
optional<date_t> b = begin(current_year);
optional<date_t> e = end(current_year);
bool after_begin = b ? date >= *b : true;
@@ -353,8 +351,7 @@ class date_specifier_or_range_t
value_type specifier_or_range;
public:
- optional<date_t>
- begin(const optional<date_t::year_type>& current_year = none) const {
+ optional<date_t> begin(const optional_year& current_year = none) const {
if (specifier_or_range.type() == typeid(date_specifier_t))
return boost::get<date_specifier_t>(specifier_or_range).begin(current_year);
else if (specifier_or_range.type() == typeid(date_range_t))
@@ -362,8 +359,7 @@ public:
else
return none;
}
- optional<date_t>
- end(const optional<date_t::year_type>& current_year = none) const {
+ optional<date_t> end(const optional_year& current_year = none) const {
if (specifier_or_range.type() == typeid(date_specifier_t))
return boost::get<date_specifier_t>(specifier_or_range).end(current_year);
else if (specifier_or_range.type() == typeid(date_range_t))
@@ -393,6 +389,8 @@ public:
static date_t subtract_duration(const date_t& date,
const date_duration_t& duration);
+ optional<date_specifier_or_range_t> range;
+
optional<date_t> start; // the real start, after adjustment
optional<date_t> finish; // the real end, likewise
bool aligned;
@@ -410,7 +408,8 @@ public:
parse(str);
}
date_interval_t(const date_interval_t& other)
- : start(other.start),
+ : range(other.range),
+ start(other.start),
finish(other.finish),
aligned(other.aligned),
skip_duration(other.skip_duration),
@@ -433,6 +432,13 @@ 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> end(const optional_year& current_year = none) const {
+ return finish ? finish : (range ? range->end(current_year) : none);
+ }
+
void parse(std::istream& in);
void parse(const string& str) {
@@ -469,6 +475,7 @@ private:
template<class Archive>
void serialize(Archive& ar, const unsigned int /* version */) {
+ ar & range;
ar & start;
ar & finish;
ar & aligned;