diff options
author | Sergey Magidovich <mgsergio@mapswithme.com> | 2015-10-28 14:24:01 +0300 |
---|---|---|
committer | Sergey Magidovich <mgsergio@mapswithme.com> | 2015-10-28 14:45:14 +0300 |
commit | a2c252701e1f70ddf5d0d7df4bfbee8beec7c8ca (patch) | |
tree | 689964e0361d0a8a8ffab9507c0c73c29d5784b8 /3party | |
parent | 8515c3b55db3bf3139035d4a3e241d9f948c7761 (diff) |
Add check if given day is in Weekdays. No holidays support yet.
Diffstat (limited to '3party')
-rw-r--r-- | 3party/opening_hours/opening_hours_tests/osm_time_range_tests.cpp | 63 | ||||
-rw-r--r-- | 3party/opening_hours/osm_time_range.hpp | 7 | ||||
-rw-r--r-- | 3party/opening_hours/rules_evaluation.cpp | 32 |
3 files changed, 85 insertions, 17 deletions
diff --git a/3party/opening_hours/opening_hours_tests/osm_time_range_tests.cpp b/3party/opening_hours/opening_hours_tests/osm_time_range_tests.cpp index 7875849f4d..b8a2863d1e 100644 --- a/3party/opening_hours/opening_hours_tests/osm_time_range_tests.cpp +++ b/3party/opening_hours/opening_hours_tests/osm_time_range_tests.cpp @@ -86,10 +86,10 @@ std::string ParseAndUnparse(std::string const & str) return sstr.str(); } -bool GetTimeTuple(std::string const & strTime, std::tm & tm) +bool GetTimeTuple(std::string const & strTime, std::string const & fmt, std::tm & tm) { std::stringstream sstr(strTime); - sstr >> std::get_time(&tm, "%H:%M"); + sstr >> std::get_time(&tm, fmt.data()); return !sstr.fail(); } } // namespace @@ -900,30 +900,61 @@ BOOST_AUTO_TEST_CASE(OpeningHoursRuleSequence_TestParseUnparse) } } -BOOST_AUTO_TEST_CASE(OpenigHours_TestIsActiveTime) +BOOST_AUTO_TEST_CASE(OpenigHours_TestIsActive) { using namespace osmoh; { - Timespan span; - span.SetStart(10_h + 15_min); - span.SetEnd(13_h + 49_min); + TTimespans spans; + BOOST_CHECK(Parse("10:15-13:49", spans)); std::tm time; - BOOST_CHECK(GetTimeTuple("12:41", time)); - BOOST_CHECK(IsActive(span, time)); + auto const fmt = "%H:%M"; + BOOST_CHECK(GetTimeTuple("12:41", fmt, time)); + BOOST_CHECK(IsActive(spans[0], time)); + + BOOST_CHECK(GetTimeTuple("10:15", fmt, time)); + BOOST_CHECK(IsActive(spans[0], time)); + + BOOST_CHECK(GetTimeTuple("10:14", fmt, time)); + BOOST_CHECK(!IsActive(spans[0], time)); + + BOOST_CHECK(GetTimeTuple("13:49", fmt, time)); + BOOST_CHECK(IsActive(spans[0], time)); + + BOOST_CHECK(GetTimeTuple("13:50", fmt, time)); + BOOST_CHECK(!IsActive(spans[0], time)); + } + { + Weekdays range; + BOOST_CHECK(Parse("Su-Sa", range)); + + std::tm time; + auto const fmt = "%w"; + BOOST_CHECK(GetTimeTuple("4", fmt, time)); + BOOST_CHECK(IsActive(range, time)); + + BOOST_CHECK(GetTimeTuple("0", fmt, time)); + BOOST_CHECK(IsActive(range, time)); + + BOOST_CHECK(GetTimeTuple("6", fmt, time)); + BOOST_CHECK(IsActive(range, time)); + + + BOOST_CHECK(Parse("Mo-Tu", range)); + BOOST_CHECK(GetTimeTuple("0", fmt, time)); + BOOST_CHECK(!IsActive(range, time)); - BOOST_CHECK(GetTimeTuple("10:15", time)); - BOOST_CHECK(IsActive(span, time)); + BOOST_CHECK(GetTimeTuple("5", fmt, time)); + BOOST_CHECK(!IsActive(range, time)); - BOOST_CHECK(GetTimeTuple("10:14", time)); - BOOST_CHECK(!IsActive(span, time)); - BOOST_CHECK(GetTimeTuple("13:49", time)); - BOOST_CHECK(IsActive(span, time)); + BOOST_CHECK(Parse("Mo", range)); + BOOST_CHECK(GetTimeTuple("1", fmt, time)); + BOOST_CHECK(IsActive(range, time)); - BOOST_CHECK(GetTimeTuple("13:50", time)); - BOOST_CHECK(!IsActive(span, time)); + BOOST_CHECK(GetTimeTuple("5", fmt, time)); + BOOST_CHECK(!IsActive(range, time)); } } diff --git a/3party/opening_hours/osm_time_range.hpp b/3party/opening_hours/osm_time_range.hpp index 807b2bf9aa..490faec2ae 100644 --- a/3party/opening_hours/osm_time_range.hpp +++ b/3party/opening_hours/osm_time_range.hpp @@ -194,7 +194,7 @@ enum class Weekday Saturday }; -inline constexpr Weekday operator ""_day(uint64_t day) +inline constexpr Weekday ToWeekday(uint64_t const day) { using TDay = decltype(day); return ((day <= static_cast<TDay>(Weekday::None) || @@ -203,6 +203,11 @@ inline constexpr Weekday operator ""_day(uint64_t day) : static_cast<Weekday>(day)); } +inline constexpr Weekday operator ""_day(uint64_t day) +{ + return ToWeekday(day); +} + std::ostream & operator<<(std::ostream & ost, Weekday const wday); class WeekdayRange diff --git a/3party/opening_hours/rules_evaluation.cpp b/3party/opening_hours/rules_evaluation.cpp index e9f9b78bf9..20657654fa 100644 --- a/3party/opening_hours/rules_evaluation.cpp +++ b/3party/opening_hours/rules_evaluation.cpp @@ -42,4 +42,36 @@ bool IsActive(Timespan const & span, std::tm const & time) } return false; } + +bool IsActive(WeekdayRange const & range, std::tm const & date) +{ + if (range.IsEmpty()) + return false; + + auto const wday = ToWeekday(date.tm_wday + 1); + if (wday == Weekday::None) + return false; + + if (range.HasEnd()) + return range.GetStart() <= wday && wday <= range.GetEnd(); + return range.GetStart() == wday; +} + +bool IsActive(Holiday const & holiday, std::tm const & date) +{ + return false; +} + +bool IsActive(Weekdays const & weekdays, std::tm const & date) +{ + for (auto const & wr : weekdays.GetWeekdayRanges()) + if (IsActive(wr, date)) + return true; + + for (auto const & hd : weekdays.GetHolidays()) + if (IsActive(hd, date)) + return true; + + return false; +} } // namespace osmoh |