diff options
author | Sergey Magidovich <mgsergio@mapswithme.com> | 2015-12-22 11:26:12 +0300 |
---|---|---|
committer | Sergey Yershov <yershov@corp.mail.ru> | 2016-03-23 16:04:01 +0300 |
commit | 832abf1f54c1466e7d547e7c9eaab4eb03c71d29 (patch) | |
tree | a7881bc7ad9a06b546cab9bbb981ef7535e9de0c /editor/ui2oh.cpp | |
parent | db00bcfdee2da905c7d0ba8b00495f78e1fd3ba3 (diff) |
Implement from OpeningHours to TimeTableSet conversion.
Diffstat (limited to 'editor/ui2oh.cpp')
-rw-r--r-- | editor/ui2oh.cpp | 82 |
1 files changed, 79 insertions, 3 deletions
diff --git a/editor/ui2oh.cpp b/editor/ui2oh.cpp index 6ba3b24723..0b348e60d6 100644 --- a/editor/ui2oh.cpp +++ b/editor/ui2oh.cpp @@ -1,19 +1,95 @@ #include "editor/ui2oh.hpp" +#include "base/enumerate.hpp" + +#include "std/algorithm.hpp" #include "std/array.hpp" #include "std/string.hpp" #include "3party/opening_hours/opening_hours.hpp" +namespace +{ +void SetUpWeekdays(osmoh::Weekdays const & wds, editor::ui::TimeTable & tt) +{ + set<osmoh::Weekday> workingDays; + for (auto const & wd : wds.GetWeekdayRanges()) + { + if (wd.HasSunday()) + workingDays.insert(osmoh::Weekday::Sunday); + if (wd.HasMonday()) + workingDays.insert(osmoh::Weekday::Monday); + if (wd.HasTuesday()) + workingDays.insert(osmoh::Weekday::Tuesday); + if (wd.HasWednesday()) + workingDays.insert(osmoh::Weekday::Wednesday); + if (wd.HasThursday()) + workingDays.insert(osmoh::Weekday::Thursday); + if (wd.HasFriday()) + workingDays.insert(osmoh::Weekday::Friday); + if (wd.HasSaturday()) + workingDays.insert(osmoh::Weekday::Saturday); + } + tt.SetWorkingDays(workingDays); +} + +void SetUpTimeTable(osmoh::TTimespans spans, editor::ui::TimeTable & tt) +{ + using namespace osmoh; + + sort(begin(spans), end(spans), [](Timespan const & a, Timespan const & b) + { + auto const start1 = a.GetStart().GetHourMinutes().GetDuration(); + auto const start2 = b.GetStart().GetHourMinutes().GetDuration(); + + return start1 < start2; + }); + + // Take first start and last end as opening time span. + tt.SetOpeningTime({spans.front().GetStart(), spans.back().GetEnd()}); + + // Add an end of a span of index i and start of following span + // as exclude time. + for (auto i = 0; i + 1 < spans.size(); ++i) + tt.AddExcludeTime({spans[i].GetEnd(), spans[i + 1].GetStart()}); +} +} // namespace + namespace editor { osmoh::OpeningHours ConvertOpeningHours(ui::TimeTableSet const & tt) { - return string(); // TODO(mgsergio): // Just a dummy. + return string(); // TODO(mgsergio): Implement me. } -ui::TimeTableSet ConvertOpeningHours(osmoh::OpeningHours const & oh) +bool ConvertOpeningHours(osmoh::OpeningHours const & oh, ui::TimeTableSet & tts) { - return {}; + if (oh.HasYearSelector() || oh.HasWeekSelector() || oh.HasMonthSelector()) + return false; + + tts = ui::TimeTableSet(); + if (oh.IsTwentyFourHours()) + return true; + + for (auto const & p : my::enumerate(oh.GetRule())) + { + auto const & rulePart = p.item; + ui::TimeTable tt; + + if (rulePart.HasWeekdays()) + SetUpWeekdays(rulePart.GetWeekdays(), tt); + + if (rulePart.HasTimes()) + { + tt.SetTwentyFourHours(false); + SetUpTimeTable(rulePart.GetTimes(), tt); + } + + bool const appended = p.index == 0 ? tts.Replace(tt, 0) : tts.Append(tt); + if (!appended) + return false; + } + + return true; } } // namespace editor |