MCPcopy Create free account
hub / github.com/comaps/comaps / FixTimeSpans

Function FixTimeSpans

libs/editor/opening_hours_ui.cpp:36–97  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

34}
35
36bool FixTimeSpans(osmoh::Timespan openingTime, osmoh::TTimespans & spans)
37{
38 using osmoh::operator""_h;
39
40 if (spans.empty())
41 return true;
42
43 for (auto & span : spans)
44 if (span.HasExtendedHours())
45 span.GetEnd().GetHourMinutes().AddDuration(24_h);
46
47 std::sort(std::begin(spans), std::end(spans), [](osmoh::Timespan const & s1, osmoh::Timespan const s2)
48 {
49 auto const start1 = s1.GetStart().GetHourMinutes();
50 auto const start2 = s2.GetStart().GetHourMinutes();
51
52 // If two spans start at the same point the longest span should be leftmost.
53 if (start1 == start2)
54 return SpanLength(s1) > SpanLength(s2);
55
56 return start1 < start2;
57 });
58
59 osmoh::TTimespans result{spans.front()};
60 for (size_t i = 1, j = 0; i < spans.size(); ++i)
61 {
62 auto const start2 = spans[i].GetStart().GetHourMinutes().GetDuration();
63 auto const end1 = spans[j].GetEnd().GetHourMinutes().GetDuration();
64 auto const end2 = spans[i].GetEnd().GetHourMinutes().GetDuration();
65
66 // The first one includes the second.
67 if (start2 < end1 && end2 <= end1)
68 {
69 continue;
70 }
71 // Two spans have non-empty intersection.
72 else if (start2 <= end1)
73 {
74 result.back().SetEnd(spans[i].GetEnd());
75 }
76 // The scond span starts after the end of the first one.
77 else
78 {
79 result.push_back(spans[i]);
80 ++j;
81 }
82 }
83
84 // Check that all exclude time spans are included in opening time.
85 if (openingTime.HasExtendedHours())
86 openingTime.GetEnd().GetHourMinutes().AddDuration(24_h);
87
88 if (!DoesIncludeAll(openingTime, spans))
89 return false;
90
91 for (auto & span : result)
92 if (span.HasExtendedHours())
93 span.GetEnd().GetHourMinutes().AddDuration(-24_h);

Callers 2

ReplaceExcludeTimeMethod · 0.85
IsValidMethod · 0.85

Calls 15

SpanLengthFunction · 0.85
DoesIncludeAllFunction · 0.85
HasExtendedHoursMethod · 0.80
frontMethod · 0.80
backMethod · 0.80
beginFunction · 0.50
endFunction · 0.50
emptyMethod · 0.45
AddDurationMethod · 0.45
GetEndMethod · 0.45
GetStartMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected