diff --git a/3party/opening_hours/opening_hours.cpp b/3party/opening_hours/opening_hours.cpp index e847d45615..0adc164e1c 100644 --- a/3party/opening_hours/opening_hours.cpp +++ b/3party/opening_hours/opening_hours.cpp @@ -335,7 +335,7 @@ std::ostream & operator<<(std::ostream & ost, NthWeekdayOfTheMonthEntry const en } // WeekdayRange ------------------------------------------------------------------------------------ -bool WeekdayRange::HasWday(Weekday const & wday) const +bool WeekdayRange::HasWday(Weekday const wday) const { if (IsEmpty() || wday == Weekday::None) return false; @@ -343,10 +343,11 @@ bool WeekdayRange::HasWday(Weekday const & wday) const if (!HasEnd()) return GetStart() == wday; - return GetStart() <= wday && wday <= GetEnd(); + return (GetStart() <= GetEnd()) + ? GetStart() <= wday && wday <= GetEnd() + : wday >= GetEnd() || GetStart() <= wday; } - std::ostream & operator<<(std::ostream & ost, Weekday const wday) { switch (wday) diff --git a/3party/opening_hours/opening_hours.hpp b/3party/opening_hours/opening_hours.hpp index d9f229f80b..20f17d86ce 100644 --- a/3party/opening_hours/opening_hours.hpp +++ b/3party/opening_hours/opening_hours.hpp @@ -306,7 +306,7 @@ class WeekdayRange using TNths = std::vector; public: - bool HasWday(Weekday const & wday) const; + bool HasWday(Weekday const wday) const; bool HasSunday() const { return HasWday(Weekday::Sunday); } bool HasMonday() const { return HasWday(Weekday::Monday); } 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 d86ff88e91..8098a35a1e 100644 --- a/3party/opening_hours/opening_hours_tests/opening_hours_tests.cpp +++ b/3party/opening_hours/opening_hours_tests/opening_hours_tests.cpp @@ -392,7 +392,19 @@ BOOST_AUTO_TEST_CASE(OpeningHours_TestWeekdayRange) range.AddNth(entry); BOOST_CHECK(range.HasNth()); } + { + WeekdayRange range; + range.SetStart(Weekday::Monday); + range.SetEnd(Weekday::Sunday); + BOOST_CHECK(range.HasSunday()); + BOOST_CHECK(range.HasMonday()); + BOOST_CHECK(range.HasTuesday()); + BOOST_CHECK(range.HasWednesday()); + BOOST_CHECK(range.HasThursday()); + BOOST_CHECK(range.HasFriday()); + BOOST_CHECK(range.HasSaturday()); + } } BOOST_AUTO_TEST_CASE(OpeningHours_Holidays) diff --git a/3party/opening_hours/rules_evaluation.cpp b/3party/opening_hours/rules_evaluation.cpp index ea266c7e3d..3762b8360d 100644 --- a/3party/opening_hours/rules_evaluation.cpp +++ b/3party/opening_hours/rules_evaluation.cpp @@ -93,15 +93,6 @@ uint8_t GetWeekNumber(std::tm const & date) return weekNumber; } -bool IsBetweenLooped(osmoh::Weekday const start, - osmoh::Weekday const end, - osmoh::Weekday const p) -{ - if (start <= end) - return start <= p && p <= end; - return p >= end || start <= p; -} - osmoh::RuleState ModifierToRuleState(osmoh::RuleSequence::Modifier const modifier) { using Modifier = osmoh::RuleSequence::Modifier; @@ -239,10 +230,7 @@ bool IsActive(WeekdayRange const & range, std::tm const & date) if (wday == Weekday::None) return false; - if (range.HasEnd()) - return IsBetweenLooped(range.GetStart(), range.GetEnd(), wday); - - return range.GetStart() == wday; + return range.HasWday(wday); } bool IsActive(Holiday const & holiday, std::tm const & date) diff --git a/editor/editor_tests/ui2oh_test.cpp b/editor/editor_tests/ui2oh_test.cpp index 2bed9f8432..4c38baab8e 100644 --- a/editor/editor_tests/ui2oh_test.cpp +++ b/editor/editor_tests/ui2oh_test.cpp @@ -110,4 +110,37 @@ UNIT_TEST(ConvertOpeningHours) TEST(!ConvertOpeningHours(oh, tts), ()); } + { + OpeningHours oh("Mo-Su 11:00-24:00"); + TEST(oh.IsValid(), ()); + + TimeTableSet tts; + + TEST(ConvertOpeningHours(oh, tts), ()); + TEST_EQUAL(tts.Size(), 1, ()); + + auto const tt = tts.Front(); + TEST(!tt.IsTwentyFourHours(), ()); + TEST_EQUAL(tt.GetWorkingDays().size(), 7, ()); + TEST_EQUAL(tt.GetOpeningTime().GetStart().GetHourMinutes().GetHoursCount(), 11, ()); + TEST_EQUAL(tt.GetOpeningTime().GetEnd().GetHourMinutes().GetHoursCount(), 24, ()); + } + { + TimeTableSet tts; + + TEST(ConvertOpeningHours("Mo-Fr 08:00-10:00\n Su, Sa 13:00-22:00", tts), ()); + TEST_EQUAL(tts.Size(), 2, ()); + + { + auto const tt = tts.Get(0); + TEST(!tt.IsTwentyFourHours(), ()); + TEST_EQUAL(tt.GetWorkingDays().size(), 5, ()); + } + + { + auto const tt = tts.Get(1); + TEST(!tt.IsTwentyFourHours(), ()); + TEST_EQUAL(tt.GetWorkingDays().size(), 2, ()); + } + } } diff --git a/editor/ui2oh.cpp b/editor/ui2oh.cpp index fcf4c72c41..de27826ab1 100644 --- a/editor/ui2oh.cpp +++ b/editor/ui2oh.cpp @@ -64,6 +64,9 @@ osmoh::OpeningHours ConvertOpeningHours(ui::TimeTableSet const & tt) bool ConvertOpeningHours(osmoh::OpeningHours const & oh, ui::TimeTableSet & tts) { + if (!oh.IsValid()) + return false; + if (oh.HasYearSelector() || oh.HasWeekSelector() || oh.HasMonthSelector()) return false; @@ -92,4 +95,10 @@ bool ConvertOpeningHours(osmoh::OpeningHours const & oh, ui::TimeTableSet & tts) return true; } + +bool ConvertOpeningHours(string oh, ui::TimeTableSet & tts) +{ + replace(begin(oh), end(oh), '\n', ';'); + return ConvertOpeningHours(osmoh::OpeningHours(oh), tts); +} } // namespace editor diff --git a/editor/ui2oh.hpp b/editor/ui2oh.hpp index 3cf31a7e43..8255d73069 100644 --- a/editor/ui2oh.hpp +++ b/editor/ui2oh.hpp @@ -2,6 +2,8 @@ #include "editor/opening_hours_ui.hpp" +#include "std/string.hpp" + namespace osmoh { class OpeningHours; @@ -11,4 +13,5 @@ namespace editor { osmoh::OpeningHours ConvertOpeningHours(ui::TimeTableSet const & tts); bool ConvertOpeningHours(osmoh::OpeningHours const & oh, ui::TimeTableSet & tts); +bool ConvertOpeningHours(string oh, ui::TimeTableSet & tts); } // namespace editor