forked from organicmaps/organicmaps
operator() -> UpdateState
This commit is contained in:
parent
3e45bcf446
commit
2b61410cbc
5 changed files with 113 additions and 115 deletions
|
@ -902,7 +902,7 @@ OSMTimeRange OSMTimeRange::FromString(std::string const & rules)
|
|||
return timeRange;
|
||||
}
|
||||
|
||||
OSMTimeRange & OSMTimeRange::operator () (time_t timestamp)
|
||||
OSMTimeRange & OSMTimeRange::UpdateState(time_t timestamp)
|
||||
{
|
||||
std::tm stm = *localtime(×tamp);
|
||||
|
||||
|
@ -940,10 +940,10 @@ OSMTimeRange & OSMTimeRange::operator () (time_t timestamp)
|
|||
return *this;
|
||||
}
|
||||
|
||||
OSMTimeRange & OSMTimeRange::operator () (std::string const & timestr, char const * timefmt)
|
||||
OSMTimeRange & OSMTimeRange::UpdateState(std::string const & timestr, char const * timefmt)
|
||||
{
|
||||
std::tm when = {};
|
||||
std::stringstream ss(timestr);
|
||||
ss >> std::get_time(&when, timefmt);
|
||||
return this->operator()(std::mktime(&when));
|
||||
return UpdateState(std::mktime(&when));
|
||||
}
|
||||
|
|
|
@ -1,25 +1,25 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015 Mail.Ru Group
|
||||
Copyright (c) 2015 Mail.Ru Group
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
@ -30,89 +30,87 @@
|
|||
|
||||
namespace osmoh
|
||||
{
|
||||
|
||||
|
||||
class Time
|
||||
{
|
||||
public:
|
||||
public:
|
||||
enum EFlags
|
||||
{
|
||||
eNone = 0,
|
||||
eHours = 1,
|
||||
eMinutes = 2,
|
||||
ePlus = 4,
|
||||
eMinus = 8,
|
||||
eExt = 16,
|
||||
eSunrise = 32,
|
||||
eSunset = 64
|
||||
};
|
||||
|
||||
uint8_t hours;
|
||||
uint8_t minutes;
|
||||
uint8_t flags;
|
||||
|
||||
Time() : hours(0), minutes(0), flags(eNone) {}
|
||||
inline Time & Hours(uint8_t h) { hours = h; flags |= eHours; return *this; }
|
||||
inline Time & Minutes(uint8_t m) { minutes = m; flags |= eMinutes; return *this; }
|
||||
inline Time & Sunset() { flags = eSunset; return *this; }
|
||||
inline Time & Sunrise() { flags = eSunrise; return *this; }
|
||||
|
||||
friend std::ostream & operator << (std::ostream & s, Time const & t);
|
||||
eNone = 0,
|
||||
eHours = 1,
|
||||
eMinutes = 2,
|
||||
ePlus = 4,
|
||||
eMinus = 8,
|
||||
eExt = 16,
|
||||
eSunrise = 32,
|
||||
eSunset = 64
|
||||
};
|
||||
|
||||
class TimeSpan
|
||||
{
|
||||
public:
|
||||
Time from;
|
||||
Time to;
|
||||
uint8_t flags;
|
||||
Time period;
|
||||
uint8_t hours;
|
||||
uint8_t minutes;
|
||||
uint8_t flags;
|
||||
|
||||
TimeSpan() : flags(Time::eNone) {}
|
||||
Time() : hours(0), minutes(0), flags(eNone) {}
|
||||
Time & Hours(uint8_t h) { hours = h; flags |= eHours; return *this; }
|
||||
Time & Minutes(uint8_t m) { minutes = m; flags |= eMinutes; return *this; }
|
||||
Time & Sunset() { flags = eSunset; return *this; }
|
||||
Time & Sunrise() { flags = eSunrise; return *this; }
|
||||
|
||||
friend std::ostream & operator << (std::ostream & s, TimeSpan const & span);
|
||||
friend std::ostream & operator << (std::ostream & s, Time const & t);
|
||||
};
|
||||
|
||||
class TimeSpan
|
||||
{
|
||||
public:
|
||||
Time from;
|
||||
Time to;
|
||||
uint8_t flags;
|
||||
Time period;
|
||||
|
||||
TimeSpan() : flags(Time::eNone) {}
|
||||
|
||||
friend std::ostream & operator << (std::ostream & s, TimeSpan const & span);
|
||||
};
|
||||
|
||||
class Weekday
|
||||
{
|
||||
public:
|
||||
uint8_t weekdays;
|
||||
uint16_t nth;
|
||||
int32_t offset;
|
||||
|
||||
Weekday() : weekdays(0), nth(0), offset(0) {}
|
||||
|
||||
friend std::ostream & operator << (std::ostream & s, Weekday const & w);
|
||||
};
|
||||
|
||||
class State
|
||||
{
|
||||
public:
|
||||
enum EState {
|
||||
eUnknown = 0,
|
||||
eClosed = 1,
|
||||
eOpen = 2
|
||||
};
|
||||
|
||||
class Weekday
|
||||
{
|
||||
public:
|
||||
uint8_t weekdays;
|
||||
uint16_t nth;
|
||||
int32_t offset;
|
||||
uint8_t state;
|
||||
std::string comment;
|
||||
|
||||
Weekday() : weekdays(0), nth(0), offset(0) {}
|
||||
State() : state(eUnknown) {}
|
||||
};
|
||||
|
||||
friend std::ostream & operator << (std::ostream & s, Weekday const & w);
|
||||
};
|
||||
|
||||
class State
|
||||
{
|
||||
public:
|
||||
enum EState {
|
||||
eUnknown = 0,
|
||||
eClosed = 1,
|
||||
eOpen = 2
|
||||
};
|
||||
|
||||
uint8_t state;
|
||||
std::string comment;
|
||||
|
||||
State() : state(eUnknown) {}
|
||||
};
|
||||
|
||||
class TimeRule
|
||||
{
|
||||
public:
|
||||
std::vector<Weekday> weekdays;
|
||||
std::vector<TimeSpan> timespan;
|
||||
State state;
|
||||
uint8_t int_flags = 0;
|
||||
};
|
||||
class TimeRule
|
||||
{
|
||||
public:
|
||||
std::vector<Weekday> weekdays;
|
||||
std::vector<TimeSpan> timespan;
|
||||
State state;
|
||||
uint8_t int_flags = 0;
|
||||
};
|
||||
} // namespace osmoh
|
||||
|
||||
class OSMTimeRange
|
||||
{
|
||||
public:
|
||||
public:
|
||||
OSMTimeRange() = default;
|
||||
|
||||
bool IsValid() const { return m_valid; }
|
||||
|
@ -121,13 +119,13 @@ public:
|
|||
bool IsUnknown() const { return m_state == osmoh::State::eUnknown; }
|
||||
std::string const & Comment() const { return m_comment; }
|
||||
|
||||
OSMTimeRange & operator()(time_t timestamp);
|
||||
OSMTimeRange & operator()(std::string const & timestr,
|
||||
char const * timefmt="%d-%m-%Y %R");
|
||||
OSMTimeRange & UpdateState(time_t timestamp);
|
||||
OSMTimeRange & UpdateState(std::string const & timestr,
|
||||
char const * timefmt="%d-%m-%Y %R");
|
||||
|
||||
static OSMTimeRange FromString(std::string const & rules);
|
||||
|
||||
private:
|
||||
private:
|
||||
bool m_valid{false};
|
||||
osmoh::State::EState m_state{osmoh::State::eUnknown};
|
||||
std::vector<osmoh::TimeRule> m_rules;
|
||||
|
|
|
@ -120,57 +120,57 @@ UNIT_TEST(OpeningHours_TimeHit)
|
|||
{
|
||||
OSMTimeRange oh = OSMTimeRange::FromString("06:13-15:00; 16:30+");
|
||||
TEST(oh.IsValid(), ("Incorrect schedule string"));
|
||||
TEST(oh("12-12-2013 7:00").IsOpen(), ());
|
||||
TEST(oh("12-12-2013 16:00").IsClosed(), ());
|
||||
TEST(oh("12-12-2013 20:00").IsOpen(), ());
|
||||
TEST(oh.UpdateState("12-12-2013 7:00").IsOpen(), ());
|
||||
TEST(oh.UpdateState("12-12-2013 16:00").IsClosed(), ());
|
||||
TEST(oh.UpdateState("12-12-2013 20:00").IsOpen(), ());
|
||||
}
|
||||
{
|
||||
OSMTimeRange oh = OSMTimeRange::FromString("We-Sa; Mo[1,3] closed; Su[-1,-2] closed; Fr[2] open; Fr[-2], Fr open; Su[-2] -2 days");
|
||||
TEST(oh.IsValid(), ("Incorrect schedule string"));
|
||||
TEST(oh("20-03-2015 18:00").IsOpen(), ());
|
||||
TEST(oh("17-03-2015 18:00").IsClosed(), ());
|
||||
TEST(oh.UpdateState("20-03-2015 18:00").IsOpen(), ());
|
||||
TEST(oh.UpdateState("17-03-2015 18:00").IsClosed(), ());
|
||||
}
|
||||
|
||||
{
|
||||
OSMTimeRange oh = OSMTimeRange::FromString("We-Fr; Mo[1,3] closed; Su[-1,-2] closed");
|
||||
TEST(oh.IsValid(), ("Incorrect schedule string"));
|
||||
TEST(oh("20-03-2015 18:00").IsOpen(), ());
|
||||
TEST(oh("17-03-2015 18:00").IsClosed(), ());
|
||||
TEST(oh.UpdateState("20-03-2015 18:00").IsOpen(), ());
|
||||
TEST(oh.UpdateState("17-03-2015 18:00").IsClosed(), ());
|
||||
}
|
||||
{
|
||||
OSMTimeRange oh = OSMTimeRange::FromString("We-Fr; Mo[1,3] +1 day closed; Su[-1,-2] -3 days closed");
|
||||
TEST(oh.IsValid(), ("Incorrect schedule string"));
|
||||
TEST(oh("20-03-2015 18:00").IsOpen(), ());
|
||||
TEST(oh("17-03-2015 18:00").IsClosed(), ());
|
||||
TEST(oh.UpdateState("20-03-2015 18:00").IsOpen(), ());
|
||||
TEST(oh.UpdateState("17-03-2015 18:00").IsClosed(), ());
|
||||
}
|
||||
{
|
||||
OSMTimeRange oh = OSMTimeRange::FromString("Mo-Su 14:30-17:00; Mo[1] closed; Su[-1] closed");
|
||||
TEST(oh.IsValid(), ("Incorrect schedule string"));
|
||||
TEST(oh("09-03-2015 16:00").IsOpen(), ());
|
||||
TEST(oh("02-03-2015 16:00").IsClosed(), ());
|
||||
TEST(oh("22-03-2015 16:00").IsOpen(), ());
|
||||
TEST(oh("29-03-2015 16:00").IsClosed(), ());
|
||||
TEST(oh.UpdateState("09-03-2015 16:00").IsOpen(), ());
|
||||
TEST(oh.UpdateState("02-03-2015 16:00").IsClosed(), ());
|
||||
TEST(oh.UpdateState("22-03-2015 16:00").IsOpen(), ());
|
||||
TEST(oh.UpdateState("29-03-2015 16:00").IsClosed(), ());
|
||||
}
|
||||
{
|
||||
OSMTimeRange oh = OSMTimeRange::FromString("PH,Tu-Su 10:00-18:00; Sa[1] 10:00-18:00 open \"Eintritt ins gesamte Haus frei\"; Jan 1,Dec 24,Dec 25,easter -2 days: closed");
|
||||
TEST(oh.IsValid(), ("Incorrect schedule string"));
|
||||
TEST(oh("03-03-2015 16:00").IsOpen(), ());
|
||||
TEST(oh.UpdateState("03-03-2015 16:00").IsOpen(), ());
|
||||
TEST(oh.Comment().empty(), ());
|
||||
TEST(oh("07-03-2015 16:00").IsOpen(), ());
|
||||
TEST(oh.UpdateState("07-03-2015 16:00").IsOpen(), ());
|
||||
TEST(oh.Comment().empty() == false, ());
|
||||
}
|
||||
{
|
||||
OSMTimeRange oh = OSMTimeRange::FromString("Mo-Su 11:00+; Mo [1,3] off");
|
||||
TEST(oh.IsValid(), ("Incorrect schedule string"));
|
||||
TEST(oh("04-03-2015 16:00").IsOpen(), ());
|
||||
TEST(oh("09-03-2015 16:00").IsOpen(), ());
|
||||
TEST(oh("02-03-2015 16:00").IsClosed(), ());
|
||||
TEST(oh("16-03-2015 16:00").IsClosed(), ());
|
||||
TEST(oh.UpdateState("04-03-2015 16:00").IsOpen(), ());
|
||||
TEST(oh.UpdateState("09-03-2015 16:00").IsOpen(), ());
|
||||
TEST(oh.UpdateState("02-03-2015 16:00").IsClosed(), ());
|
||||
TEST(oh.UpdateState("16-03-2015 16:00").IsClosed(), ());
|
||||
}
|
||||
{
|
||||
OSMTimeRange oh = OSMTimeRange::FromString("08:00-16:00 open, 16:00-03:00 open \"public room\"");
|
||||
TEST(oh.IsValid(), ("Incorrect schedule string"));
|
||||
TEST(oh("01-03-2015 20:00").IsOpen(), ());
|
||||
TEST(oh("01-03-2015 20:00").Comment() == "public room", ());
|
||||
TEST(oh.UpdateState("01-03-2015 20:00").IsOpen(), ());
|
||||
TEST(oh.UpdateState("01-03-2015 20:00").Comment() == "public room", ());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,8 +34,8 @@ inline EPlaceState PlaceStateCheck(string const & openingHours, time_t timestamp
|
|||
OSMTimeRange oh = OSMTimeRange::FromString(openingHours);
|
||||
auto future = system_clock::from_time_t(timestamp);
|
||||
future += minutes(15);
|
||||
size_t nowState = oh(timestamp).IsOpen() ? 0 : 1;
|
||||
size_t futureState = oh(system_clock::to_time_t(future)).IsOpen() ? 0 : 1;
|
||||
size_t nowState = oh.UpdateState(timestamp).IsOpen() ? 0 : 1;
|
||||
size_t futureState = oh.UpdateState(system_clock::to_time_t(future)).IsOpen() ? 0 : 1;
|
||||
|
||||
EPlaceState state[2][2] = {{EPlaceState::Open, EPlaceState::CloseSoon},
|
||||
{EPlaceState::OpenSoon, EPlaceState::Closed}};
|
||||
|
|
|
@ -41,7 +41,7 @@ void ProcessMetadata(FeatureType const & ft, Result::Metadata & meta)
|
|||
// Lib opening_hours is not built for Linux since stdlib doesn't have required functions.
|
||||
string const openHours = src.Get(feature::Metadata::FMD_OPEN_HOURS);
|
||||
if (!openHours.empty())
|
||||
meta.m_isClosed = OSMTimeRange::FromString(openHours)(time(nullptr)).IsClosed();
|
||||
meta.m_isClosed = OSMTimeRange::FromString(openHours).UpdateState(time(nullptr)).IsClosed();
|
||||
#endif
|
||||
|
||||
meta.m_stars = 0;
|
||||
|
|
Loading…
Add table
Reference in a new issue