forked from organicmaps/organicmaps-tmp
Add check if given day is in Weekdays. No holidays support yet.
This commit is contained in:
parent
8515c3b55d
commit
a2c252701e
3 changed files with 85 additions and 17 deletions
|
@ -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", time));
|
||||
BOOST_CHECK(IsActive(span, time));
|
||||
BOOST_CHECK(GetTimeTuple("10:15", fmt, time));
|
||||
BOOST_CHECK(IsActive(spans[0], time));
|
||||
|
||||
BOOST_CHECK(GetTimeTuple("10:14", time));
|
||||
BOOST_CHECK(!IsActive(span, time));
|
||||
BOOST_CHECK(GetTimeTuple("10:14", fmt, time));
|
||||
BOOST_CHECK(!IsActive(spans[0], time));
|
||||
|
||||
BOOST_CHECK(GetTimeTuple("13:49", time));
|
||||
BOOST_CHECK(IsActive(span, time));
|
||||
BOOST_CHECK(GetTimeTuple("13:49", fmt, time));
|
||||
BOOST_CHECK(IsActive(spans[0], time));
|
||||
|
||||
BOOST_CHECK(GetTimeTuple("13:50", time));
|
||||
BOOST_CHECK(!IsActive(span, 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("5", fmt, time));
|
||||
BOOST_CHECK(!IsActive(range, time));
|
||||
|
||||
|
||||
BOOST_CHECK(Parse("Mo", range));
|
||||
BOOST_CHECK(GetTimeTuple("1", fmt, time));
|
||||
BOOST_CHECK(IsActive(range, time));
|
||||
|
||||
BOOST_CHECK(GetTimeTuple("5", fmt, time));
|
||||
BOOST_CHECK(!IsActive(range, time));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue