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.
This commit is contained in:
parent
3e91c3bf2c
commit
fe9af7ace7
2 changed files with 34 additions and 30 deletions
19
src/times.cc
19
src/times.cc
|
|
@ -194,7 +194,7 @@ namespace {
|
||||||
std::vector<shared_ptr<date_io_t> > readers;
|
std::vector<shared_ptr<date_io_t> > readers;
|
||||||
|
|
||||||
date_t parse_date_mask_routine(const char * date_str, date_io_t& io,
|
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_traits_t * traits = NULL)
|
||||||
{
|
{
|
||||||
date_t when;
|
date_t when;
|
||||||
|
|
@ -231,8 +231,7 @@ namespace {
|
||||||
return when;
|
return when;
|
||||||
}
|
}
|
||||||
|
|
||||||
date_t parse_date_mask(const char * date_str,
|
date_t parse_date_mask(const char * date_str, optional_year year,
|
||||||
optional<date_t::year_type> year,
|
|
||||||
date_traits_t * traits = NULL)
|
date_traits_t * traits = NULL)
|
||||||
{
|
{
|
||||||
if (input_date_io.get()) {
|
if (input_date_io.get()) {
|
||||||
|
|
@ -305,7 +304,7 @@ string_to_month_of_year(const std::string& str)
|
||||||
return none;
|
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);
|
datetime_t when = input_datetime_io->parse(str);
|
||||||
if (when.is_not_a_date_time())
|
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;
|
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);
|
return parse_date_mask(str, current_year);
|
||||||
}
|
}
|
||||||
|
|
||||||
date_t
|
date_t date_specifier_t::begin(const optional_year& current_year) const
|
||||||
date_specifier_t::begin(const optional<date_t::year_type>& current_year) const
|
|
||||||
{
|
{
|
||||||
assert(year || current_year);
|
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));
|
static_cast<date_t::day_type>(the_day));
|
||||||
}
|
}
|
||||||
|
|
||||||
date_t
|
date_t date_specifier_t::end(const optional_year& current_year) const
|
||||||
date_specifier_t::end(const optional<date_t::year_type>& current_year) const
|
|
||||||
{
|
{
|
||||||
if (day || wday)
|
if (day || wday)
|
||||||
return begin(current_year) + gregorian::days(1);
|
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
|
// 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
|
// enough that we don't spend hundreds of thousands of loops skipping
|
||||||
// through time.
|
// through time.
|
||||||
optional<date_t> initial_start = start;
|
optional<date_t> initial_start = start ? start : begin(date->year());
|
||||||
optional<date_t> initial_finish = finish;
|
optional<date_t> initial_finish = finish ? finish : end(date->year());
|
||||||
|
|
||||||
#if defined(DEBUG_ON)
|
#if defined(DEBUG_ON)
|
||||||
if (initial_start)
|
if (initial_start)
|
||||||
|
|
|
||||||
45
src/times.h
45
src/times.h
|
|
@ -84,19 +84,19 @@ string_to_day_of_week(const std::string& str);
|
||||||
optional<date_time::months_of_year>
|
optional<date_time::months_of_year>
|
||||||
string_to_month_of_year(const std::string& str);
|
string_to_month_of_year(const std::string& str);
|
||||||
|
|
||||||
datetime_t parse_datetime(const char * str,
|
typedef optional<date_t::year_type> optional_year;
|
||||||
optional<date_t::year_type> current_year = none);
|
|
||||||
|
datetime_t parse_datetime(const char * str, optional_year current_year = none);
|
||||||
|
|
||||||
inline datetime_t parse_datetime(const std::string& str,
|
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);
|
return parse_datetime(str.c_str(), current_year);
|
||||||
}
|
}
|
||||||
|
|
||||||
date_t parse_date(const char * str,
|
date_t parse_date(const char * str, optional_year current_year = none);
|
||||||
optional<date_t::year_type> current_year = none);
|
|
||||||
|
|
||||||
inline date_t parse_date(const std::string& str,
|
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);
|
return parse_date(str.c_str(), current_year);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -210,11 +210,11 @@ public:
|
||||||
day = date.day();
|
day = date.day();
|
||||||
}
|
}
|
||||||
|
|
||||||
date_t begin(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<date_t::year_type>& current_year = none) const;
|
date_t end(const optional_year& current_year = none) const;
|
||||||
|
|
||||||
bool is_within(const date_t& date,
|
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);
|
return date >= begin(current_year) && date < end(current_year);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -242,15 +242,13 @@ class date_range_t
|
||||||
optional<date_specifier_t> range_end;
|
optional<date_specifier_t> range_end;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
optional<date_t>
|
optional<date_t> begin(const optional_year& current_year = none) const {
|
||||||
begin(const optional<date_t::year_type>& current_year = none) const {
|
|
||||||
if (range_begin)
|
if (range_begin)
|
||||||
return range_begin->begin(current_year);
|
return range_begin->begin(current_year);
|
||||||
else
|
else
|
||||||
return none;
|
return none;
|
||||||
}
|
}
|
||||||
optional<date_t>
|
optional<date_t> end(const optional_year& current_year = none) const {
|
||||||
end(const optional<date_t::year_type>& current_year = none) const {
|
|
||||||
if (range_end)
|
if (range_end)
|
||||||
return range_end->end(current_year);
|
return range_end->end(current_year);
|
||||||
else
|
else
|
||||||
|
|
@ -258,7 +256,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
bool is_within(const date_t& date,
|
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> b = begin(current_year);
|
||||||
optional<date_t> e = end(current_year);
|
optional<date_t> e = end(current_year);
|
||||||
bool after_begin = b ? date >= *b : true;
|
bool after_begin = b ? date >= *b : true;
|
||||||
|
|
@ -353,8 +351,7 @@ class date_specifier_or_range_t
|
||||||
value_type specifier_or_range;
|
value_type specifier_or_range;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
optional<date_t>
|
optional<date_t> begin(const optional_year& current_year = none) const {
|
||||||
begin(const optional<date_t::year_type>& current_year = none) const {
|
|
||||||
if (specifier_or_range.type() == typeid(date_specifier_t))
|
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(current_year);
|
||||||
else if (specifier_or_range.type() == typeid(date_range_t))
|
else if (specifier_or_range.type() == typeid(date_range_t))
|
||||||
|
|
@ -362,8 +359,7 @@ public:
|
||||||
else
|
else
|
||||||
return none;
|
return none;
|
||||||
}
|
}
|
||||||
optional<date_t>
|
optional<date_t> end(const optional_year& current_year = none) const {
|
||||||
end(const optional<date_t::year_type>& current_year = none) const {
|
|
||||||
if (specifier_or_range.type() == typeid(date_specifier_t))
|
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(current_year);
|
||||||
else if (specifier_or_range.type() == typeid(date_range_t))
|
else if (specifier_or_range.type() == typeid(date_range_t))
|
||||||
|
|
@ -393,6 +389,8 @@ public:
|
||||||
static date_t subtract_duration(const date_t& date,
|
static date_t subtract_duration(const date_t& date,
|
||||||
const date_duration_t& duration);
|
const date_duration_t& duration);
|
||||||
|
|
||||||
|
optional<date_specifier_or_range_t> range;
|
||||||
|
|
||||||
optional<date_t> start; // the real start, after adjustment
|
optional<date_t> start; // the real start, after adjustment
|
||||||
optional<date_t> finish; // the real end, likewise
|
optional<date_t> finish; // the real end, likewise
|
||||||
bool aligned;
|
bool aligned;
|
||||||
|
|
@ -410,7 +408,8 @@ public:
|
||||||
parse(str);
|
parse(str);
|
||||||
}
|
}
|
||||||
date_interval_t(const date_interval_t& other)
|
date_interval_t(const date_interval_t& other)
|
||||||
: start(other.start),
|
: range(other.range),
|
||||||
|
start(other.start),
|
||||||
finish(other.finish),
|
finish(other.finish),
|
||||||
aligned(other.aligned),
|
aligned(other.aligned),
|
||||||
skip_duration(other.skip_duration),
|
skip_duration(other.skip_duration),
|
||||||
|
|
@ -433,6 +432,13 @@ public:
|
||||||
return is_valid();
|
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(std::istream& in);
|
||||||
|
|
||||||
void parse(const string& str) {
|
void parse(const string& str) {
|
||||||
|
|
@ -469,6 +475,7 @@ private:
|
||||||
|
|
||||||
template<class Archive>
|
template<class Archive>
|
||||||
void serialize(Archive& ar, const unsigned int /* version */) {
|
void serialize(Archive& ar, const unsigned int /* version */) {
|
||||||
|
ar & range;
|
||||||
ar & start;
|
ar & start;
|
||||||
ar & finish;
|
ar & finish;
|
||||||
ar & aligned;
|
ar & aligned;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue