Fix bugs in WeekdayRange::HasWday. Add ConvertOpeningHours(string, TimeTableSet &).

This commit is contained in:
Sergey Magidovich 2015-12-22 17:51:26 +03:00 committed by Sergey Yershov
parent 8c08017809
commit 54bcbe2b3d
7 changed files with 63 additions and 17 deletions

View file

@ -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)

View file

@ -306,7 +306,7 @@ class WeekdayRange
using TNths = std::vector<NthWeekdayOfTheMonthEntry>;
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); }

View file

@ -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)

View file

@ -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)

View file

@ -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, ());
}
}
}

View file

@ -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

View file

@ -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