diff options
-rw-r--r-- | 3party/opening_hours/opening_hours.cpp | 39 | ||||
-rw-r--r-- | 3party/opening_hours/opening_hours.hpp | 9 | ||||
-rw-r--r-- | 3party/opening_hours/opening_hours_tests/opening_hours_tests.cpp | 36 | ||||
-rw-r--r-- | editor/ui2oh.cpp | 23 |
4 files changed, 98 insertions, 9 deletions
diff --git a/3party/opening_hours/opening_hours.cpp b/3party/opening_hours/opening_hours.cpp index cac81f87d8..441cc6a776 100644 --- a/3party/opening_hours/opening_hours.cpp +++ b/3party/opening_hours/opening_hours.cpp @@ -277,6 +277,16 @@ std::ostream & operator<<(std::ostream & ost, Time const & time) return ost; } +bool operator==(Time const & lhs, Time const & rhs) +{ + if (lhs.IsEmpty() && rhs.IsEmpty()) + return true; + + return lhs.GetType() == rhs.GetType() && + lhs.GetHours() == rhs.GetHours() && + lhs.GetMinutes() == rhs.GetMinutes(); +} + // TimespanPeriod ---------------------------------------------------------------------------------- TimespanPeriod::TimespanPeriod(HourMinutes const & hm): m_hourMinutes(hm), @@ -301,6 +311,16 @@ std::ostream & operator<<(std::ostream & ost, TimespanPeriod const p) return ost; } +bool operator==(TimespanPeriod const & lhs, TimespanPeriod const & rhs) +{ + if (lhs.IsEmpty() && rhs.IsEmpty()) + return true; + + return lhs.GetType() == rhs.GetType() && + lhs.GetHourMinutes() == rhs.GetHourMinutes() && + lhs.GetMinutes() == rhs.GetMinutes(); +} + // Timespan ---------------------------------------------------------------------------------------- bool Timespan::HasExtendedHours() const { @@ -349,6 +369,25 @@ std::ostream & operator<<(std::ostream & ost, osmoh::TTimespans const & timespan return ost; } +bool operator==(Timespan const & lhs, Timespan const & rhs) +{ + if (lhs.IsEmpty() && rhs.IsEmpty()) + return true; + + if (lhs.IsEmpty() != rhs.IsEmpty() || + lhs.HasStart() != rhs.HasStart() || + lhs.HasEnd() != rhs.HasEnd() || + lhs.HasPlus() != rhs.HasPlus() || + lhs.HasPeriod() != rhs.HasPeriod()) + { + return false; + } + + return lhs.GetStart() == rhs.GetStart() && + lhs.GetEnd() == rhs.GetEnd() && + lhs.GetPeriod() == lhs.GetPeriod(); +} + // NthWeekdayOfTheMonthEntry ----------------------------------------------------------------------- std::ostream & operator<<(std::ostream & ost, NthWeekdayOfTheMonthEntry const entry) { diff --git a/3party/opening_hours/opening_hours.hpp b/3party/opening_hours/opening_hours.hpp index df4b9c5a9b..faf3119121 100644 --- a/3party/opening_hours/opening_hours.hpp +++ b/3party/opening_hours/opening_hours.hpp @@ -30,9 +30,11 @@ #include <type_traits> #include <vector> +// Implemented in accordance with the specification +// http://wiki.openstreetmap.org/wiki/Key:opening_hours/specification + namespace osmoh { - class HourMinutes { public: @@ -170,6 +172,7 @@ inline constexpr Time::TMinutes operator ""_min(unsigned long long int m) } std::ostream & operator<<(std::ostream & ost, Time const & time); +bool operator==(Time const & lhs, Time const & rhs); class TimespanPeriod { @@ -189,6 +192,8 @@ public: bool IsHoursMinutes() const { return m_type == Type::HourMinutes; } bool IsMinutes() const { return m_type == Type::Minutes; } + Type GetType() const { return m_type; } + HourMinutes const & GetHourMinutes() const { return m_hourMinutes; } HourMinutes::TMinutes GetMinutes() const { return m_minutes; } HourMinutes::TMinutes::rep GetMinutesCount() const { return GetMinutes().count(); } @@ -201,6 +206,7 @@ private: }; std::ostream & operator<<(std::ostream & ost, TimespanPeriod const p); +bool operator==(TimespanPeriod const & lhs, TimespanPeriod const & rhs); class Timespan { @@ -244,6 +250,7 @@ using TTimespans = std::vector<Timespan>; std::ostream & operator<<(std::ostream & ost, Timespan const & span); std::ostream & operator<<(std::ostream & ost, osmoh::TTimespans const & timespans); +bool operator==(Timespan const & lhs, Timespan const & rhs); class NthWeekdayOfTheMonthEntry { diff --git a/3party/opening_hours/opening_hours_tests/opening_hours_tests.cpp b/3party/opening_hours/opening_hours_tests/opening_hours_tests.cpp index d59b7ba6a9..6ef034e938 100644 --- a/3party/opening_hours/opening_hours_tests/opening_hours_tests.cpp +++ b/3party/opening_hours/opening_hours_tests/opening_hours_tests.cpp @@ -1077,6 +1077,12 @@ BOOST_AUTO_TEST_CASE(OpeningHoursRuleSequence_TestParseUnparse) auto const parsedUnparsed = ParseAndUnparse<osmoh::TRuleSequences>(rule); BOOST_CHECK_EQUAL(parsedUnparsed, rule); } + { + auto const rule = "Sa-Su 00:00-24:00"; + + auto const parsedUnparsed = ParseAndUnparse<osmoh::TRuleSequences>(rule); + BOOST_CHECK_EQUAL(parsedUnparsed, rule); + } } BOOST_AUTO_TEST_CASE(OpeningHours_TestIsActive) @@ -1426,6 +1432,26 @@ BOOST_AUTO_TEST_CASE(OpeningHours_TestIsActive) BOOST_CHECK(GetTimeTuple("2015-11-11 06:01", kDateTimeFmt, time)); BOOST_CHECK(!IsActive(rules[0], time)); } + { + TRuleSequences rules; + BOOST_CHECK(Parse("Mo-We 00:00-24:00", rules)); + + std::tm time{}; + BOOST_CHECK(GetTimeTuple("2016-10-03 05:35", kDateTimeFmt, time)); + BOOST_CHECK(IsActive(rules[0], time)); + + BOOST_CHECK(GetTimeTuple("2017-01-17 15:35", kDateTimeFmt, time)); + BOOST_CHECK(IsActive(rules[0], time)); + + BOOST_CHECK(GetTimeTuple("2017-05-31 23:35", kDateTimeFmt, time)); + BOOST_CHECK(IsActive(rules[0], time)); + + BOOST_CHECK(GetTimeTuple("2017-02-10 05:35", kDateTimeFmt, time)); + BOOST_CHECK(!IsActive(rules[0], time)); + + BOOST_CHECK(GetTimeTuple("2017-05-21 06:01", kDateTimeFmt, time)); + BOOST_CHECK(!IsActive(rules[0], time)); + } } BOOST_AUTO_TEST_CASE(OpeningHours_TestIsOpen) @@ -1541,6 +1567,16 @@ BOOST_AUTO_TEST_CASE(OpeningHours_TestIsOpen) BOOST_CHECK(IsOpen(rules, "2016-06-05 13:06")); BOOST_CHECK(IsOpen(rules, "2016-05-31 18:28")); } + { + TRuleSequences rules; + BOOST_CHECK(Parse("Mo-We 00:00-24:00", rules)); + + BOOST_CHECK(IsOpen(rules, "2016-10-03 05:35")); + BOOST_CHECK(IsOpen(rules, "2017-01-17 15:35")); + BOOST_CHECK(IsOpen(rules, "2017-05-31 23:35")); + BOOST_CHECK(!IsOpen(rules, "2017-02-10 05:35")); + BOOST_CHECK(!IsOpen(rules, "2017-05-21 06:01")); + } } diff --git a/editor/ui2oh.cpp b/editor/ui2oh.cpp index 592a686d53..2121936501 100644 --- a/editor/ui2oh.cpp +++ b/editor/ui2oh.cpp @@ -10,6 +10,10 @@ namespace { +using osmoh::operator ""_h; + +osmoh::Timespan const kTwentyFourHours = {0_h, 24_h}; + editor::ui::TOpeningDays MakeOpeningDays(osmoh::Weekdays const & wds) { set<osmoh::Weekday> openingDays; @@ -146,9 +150,8 @@ osmoh::Weekdays MakeWeekdays(editor::ui::TimeTable const & tt) osmoh::TTimespans MakeTimespans(editor::ui::TimeTable const & tt) { - if (tt.IsTwentyFourHours()) - return {}; + return {kTwentyFourHours}; auto const & excludeTime = tt.GetExcludeTime(); if (excludeTime.empty()) @@ -212,13 +215,12 @@ bool ExcludeRulePart(osmoh::RuleSequence const & rulePart, editor::ui::TimeTable auto const twentyFourHoursGuard = [](editor::ui::TimeTable & tt) { - using osmoh::operator ""_h; if (tt.IsTwentyFourHours()) { tt.SetTwentyFourHours(false); // TODO(mgsergio): Consider TimeTable refactoring: // get rid of separation of TwentyFourHours and OpeningTime. - tt.SetOpeningTime({0_h, 24_h}); + tt.SetOpeningTime(kTwentyFourHours); } }; @@ -350,14 +352,19 @@ bool MakeTimeTableSet(osmoh::OpeningHours const & oh, ui::TimeTableSet & tts) else tt.SetOpeningDays(kWholeWeek); - if (rulePart.HasTimes()) + auto const & times = rulePart.GetTimes(); + + bool isTwentyFourHours = + times.empty() || (times.size() == 1 && times.front() == kTwentyFourHours); + + if (isTwentyFourHours) { - tt.SetTwentyFourHours(false); - SetUpTimeTable(rulePart.GetTimes(), tt); + tt.SetTwentyFourHours(true); } else { - tt.SetTwentyFourHours(true); + tt.SetTwentyFourHours(false); + SetUpTimeTable(rulePart.GetTimes(), tt); } // Check size as well since ExcludeRulePart can add new time tables. |