[editor] opening hours - exclude time calculation logic and hours by default were changed

This commit is contained in:
Arsentiy Milchakov 2016-11-09 12:32:26 +03:00
parent d6ab3b9d0c
commit 813c8d684e
2 changed files with 50 additions and 22 deletions

View file

@ -31,6 +31,7 @@ UNIT_TEST(TestTimeTable_ExcludeTime)
{
using osmoh::operator""_h;
using osmoh::operator""_min;
using osmoh::HourMinutes;
{
auto tt = TimeTable::GetPredefinedTimeTable();
@ -123,34 +124,50 @@ UNIT_TEST(TestTimeTable_ExcludeTime)
TEST(tt.AddExcludeTime({10_h, 11_h}), ());
TEST(tt.AddExcludeTime({12_h, 13_h}), ());
TEST(tt.AddExcludeTime({15_h, 17_h}), ());
TEST_EQUAL(tt.GetPredefinedExcludeTime().GetStart().GetHourMinutes().GetHoursCount(), 18, ());
TEST_EQUAL(tt.GetPredefinedExcludeTime().GetStart().GetHourMinutes().GetMinutesCount(), 30, ());
TEST_EQUAL(tt.GetPredefinedExcludeTime().GetEnd().GetHourMinutes().GetHoursCount(), 22, ());
TEST_EQUAL(tt.GetPredefinedExcludeTime().GetStart().GetHourMinutes().GetHoursCount(), 20, ());
TEST_EQUAL(tt.GetPredefinedExcludeTime().GetStart().GetHourMinutes().GetMinutesCount(), 0, ());
TEST_EQUAL(tt.GetPredefinedExcludeTime().GetEnd().GetHourMinutes().GetHoursCount(), 21, ());
TEST_EQUAL(tt.GetPredefinedExcludeTime().GetEnd().GetHourMinutes().GetMinutesCount(), 0, ());
TEST(tt.AddExcludeTime({18_h, 23_h}), ());
TEST_EQUAL(tt.GetPredefinedExcludeTime().GetStart().GetHourMinutes().GetHoursCount(), 13, ());
TEST_EQUAL(tt.GetPredefinedExcludeTime().GetStart().GetHourMinutes().GetMinutesCount(), 30, ());
TEST_EQUAL(tt.GetPredefinedExcludeTime().GetEnd().GetHourMinutes().GetHoursCount(), 14, ());
TEST_EQUAL(tt.GetPredefinedExcludeTime().GetStart().GetHourMinutes().GetMinutesCount(), 30, ());
auto const predefinedStart = tt.GetPredefinedExcludeTime().GetStart().GetHourMinutes();
auto const predefinedEnd = tt.GetPredefinedExcludeTime().GetEnd().GetHourMinutes();
TEST(predefinedStart.GetHours() == HourMinutes::THours::zero(), ());
TEST(predefinedStart.GetMinutes() == HourMinutes::TMinutes::zero(), ());
TEST(predefinedEnd.GetHours() == HourMinutes::THours::zero(), ());
TEST(predefinedEnd.GetMinutes() == HourMinutes::TMinutes::zero(), ());
}
{
auto tt = TimeTable::GetPredefinedTimeTable();
tt.SetTwentyFourHours(false);
tt.SetOpeningTime({8_h, 7_h});
TEST_EQUAL(tt.GetPredefinedExcludeTime().GetStart().GetHourMinutes().GetHoursCount(), 13, ());
TEST_EQUAL(tt.GetPredefinedExcludeTime().GetStart().GetHourMinutes().GetMinutesCount(), 45, ());
TEST_EQUAL(tt.GetPredefinedExcludeTime().GetEnd().GetHourMinutes().GetHoursCount(), 1, ());
TEST_EQUAL(tt.GetPredefinedExcludeTime().GetEnd().GetHourMinutes().GetMinutesCount(), 15, ());
auto const predefinedStart = tt.GetPredefinedExcludeTime().GetStart().GetHourMinutes();
auto const predefinedEnd = tt.GetPredefinedExcludeTime().GetEnd().GetHourMinutes();
TEST(predefinedStart.GetHours() == HourMinutes::THours::zero(), ());
TEST(predefinedStart.GetMinutes() == HourMinutes::TMinutes::zero(), ());
TEST(predefinedEnd.GetHours() == HourMinutes::THours::zero(), ());
TEST(predefinedEnd.GetMinutes() == HourMinutes::TMinutes::zero(), ());
}
{
auto tt = TimeTable::GetPredefinedTimeTable();
tt.SetTwentyFourHours(false);
tt.SetOpeningTime({7_h, 7_h + 45_min});
tt.SetOpeningTime({7_h, 8_h + 45_min});
TEST(!tt.CanAddExcludeTime(), ());
}
{
auto tt = TimeTable::GetPredefinedTimeTable();
tt.SetTwentyFourHours(false);
tt.SetOpeningTime({19_h, 18_h});
auto const predefinedStart = tt.GetPredefinedExcludeTime().GetStart().GetHourMinutes();
auto const predefinedEnd = tt.GetPredefinedExcludeTime().GetEnd().GetHourMinutes();
TEST(predefinedStart.GetHours() == HourMinutes::THours::zero(), ());
TEST(predefinedStart.GetMinutes() == HourMinutes::TMinutes::zero(), ());
TEST(predefinedEnd.GetHours() == HourMinutes::THours::zero(), ());
TEST(predefinedEnd.GetMinutes() == HourMinutes::TMinutes::zero(), ());
}
}
UNIT_TEST(TestAppendTimeTable)

View file

@ -252,27 +252,38 @@ bool TimeTable::IsValid() const
osmoh::Timespan TimeTable::GetPredefinedOpeningTime() const
{
using osmoh::operator""_h;
return {10_h, 22_h};
return {9_h, 18_h};
}
osmoh::Timespan TimeTable::GetPredefinedExcludeTime() const
{
using osmoh::operator""_h;
using osmoh::operator""_min;
using osmoh::HourMinutes;
auto longestOpenSpan = GetLongetsOpenSpan(GetOpeningTime(), GetExcludeTime());
auto const longestOpenSpanLength = SpanLength(longestOpenSpan);
auto offset = longestOpenSpanLength / 4;
auto const startTime = longestOpenSpan.GetStart().GetHourMinutes().GetDuration();
auto const endTime = longestOpenSpan.GetEnd().GetHourMinutes().GetDuration();
// We do not support exclude time spans in extended working intervals.
if (endTime < startTime)
return {};
auto const remainder = offset % 10;
if (remainder && remainder != 5)
offset -= remainder;
auto const startHours = longestOpenSpan.GetStart().GetHourMinutes().GetHours();
auto const endHours = longestOpenSpan.GetEnd().GetHourMinutes().GetHours();
longestOpenSpan.GetStart().GetHourMinutes().AddDuration(osmoh::HourMinutes::TMinutes(offset));
longestOpenSpan.GetEnd().GetHourMinutes().AddDuration(-osmoh::HourMinutes::TMinutes(offset));
auto const period = endHours - startHours;
if (SpanLength(longestOpenSpan) < (30_min).count())
return osmoh::Timespan{};
// Cannot calculate exclude time when working time is less than 3 hours.
if (period < 3_h)
return {};
auto excludeTimeStart = startHours + HourMinutes::THours(period.count() / 2);
CHECK(excludeTimeStart < 24_h, ());
longestOpenSpan.SetStart(HourMinutes(excludeTimeStart));
longestOpenSpan.SetEnd(HourMinutes(excludeTimeStart + 1_h));
return longestOpenSpan;
}