Add check if given time is in timespan.

This commit is contained in:
Sergey Magidovich 2015-10-28 13:31:29 +03:00
parent 49f2e4c52f
commit 8515c3b55d
6 changed files with 114 additions and 27 deletions

View file

@ -17,5 +17,8 @@ HEADERS += osm_time_range.hpp \
osm_parsers.hpp \
osm_parsers_terminals.hpp \
parse.hpp \
rules_evalustion.hpp
SOURCES += osm_time_range.cpp parse.cpp
SOURCES += osm_time_range.cpp \
parse.cpp \
rules_evaluation.cpp \

View file

@ -24,8 +24,11 @@
#include "osm_time_range.hpp"
#include "parse.hpp"
#include "rules_evaluation.hpp"
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <locale>
#include <map>
@ -82,6 +85,13 @@ std::string ParseAndUnparse(std::string const & str)
return sstr.str();
}
bool GetTimeTuple(std::string const & strTime, std::tm & tm)
{
std::stringstream sstr(strTime);
sstr >> std::get_time(&tm, "%H:%M");
return !sstr.fail();
}
} // namespace
template <typename ParserResult>
@ -890,6 +900,34 @@ BOOST_AUTO_TEST_CASE(OpeningHoursRuleSequence_TestParseUnparse)
}
}
BOOST_AUTO_TEST_CASE(OpenigHours_TestIsActiveTime)
{
using namespace osmoh;
{
Timespan span;
span.SetStart(10_h + 15_min);
span.SetEnd(13_h + 49_min);
std::tm time;
BOOST_CHECK(GetTimeTuple("12:41", time));
BOOST_CHECK(IsActive(span, time));
BOOST_CHECK(GetTimeTuple("10:15", time));
BOOST_CHECK(IsActive(span, time));
BOOST_CHECK(GetTimeTuple("10:14", time));
BOOST_CHECK(!IsActive(span, time));
BOOST_CHECK(GetTimeTuple("13:49", time));
BOOST_CHECK(IsActive(span, time));
BOOST_CHECK(GetTimeTuple("13:50", time));
BOOST_CHECK(!IsActive(span, time));
}
}
/// How to run:
/// 1. copy opening-count.lst to where the binary is
/// 2. run with --log_level=message

View file

@ -287,7 +287,7 @@ bool Timespan::IsEmpty() const
bool Timespan::IsOpen() const
{
return GetStart().HasValue() && !GetEnd().HasValue();
return HasStart() && !HasEnd();
}
bool Timespan::HasStart() const
@ -1370,27 +1370,6 @@ std::ostream & operator<<(std::ostream & ost, TRuleSequences const & s)
});
return ost;
}
// std::ostream & operator << (std::ostream & s, State const & w)
// {
// static char const * st[] = {"unknown", "closed", "open"};
// s << ' ' << st[w.state] << " " << w.comment;
// return s;
// }
// std::ostream & operator << (std::ostream & s, TimeRule const & w)
// {
// for (auto const & e : w.weekdays)
// s << e;
// if (!w.weekdays.empty() && !w.timespan.empty())
// s << ' ';
// for (auto const & e : w.timespan)
// s << e;
// std::cout << "Weekdays size " << w.weekdays.size() <<
// " Timespan size " << w.timespan.size() << std::endl;
// return s << w.state;
// }
} // namespace osmoh
@ -1518,9 +1497,9 @@ std::ostream & operator<<(std::ostream & ost, TRuleSequences const & s)
// };
// osmoh::State::State false_state[3][3] = {
// {osmoh::State::Unknown, osmoh::State::eOpen , osmoh::State::Closed},
// {osmoh::State::Closed , osmoh::State::eClosed , osmoh::State::Closed},
// {osmoh::State::Open , osmoh::State::eOpen , osmoh::State::Open}
// {osmoh::State::Unknown, osmoh::State::Open , osmoh::State::Closed},
// {osmoh::State::Closed , osmoh::State::Closed, osmoh::State::Closed},
// {osmoh::State::Open , osmoh::State::Open , osmoh::State::Open}
// };
// m_state = osmoh::State::Unknown;

View file

@ -488,7 +488,8 @@ std::ostream & operator<<(std::ostream & ost, TWeekRanges const ranges);
class RuleSequence
{
public:
enum class Modifier {
enum class Modifier
{
DefaultOpen,
Open,
Closed,

View file

@ -0,0 +1,45 @@
#include "rules_evaluation.hpp"
// #include "rules_evaluation_private.hpp"
#include <tuple>
namespace
{
using THourMinutes = std::tuple<int, int>;
bool ToHourMinutes(osmoh::Time const & t, THourMinutes & hm)
{
if (!t.IsHoursMinutes())
return false;
hm = THourMinutes{t.GetHoursCount(), t.GetMinutesCount()};
return true;
}
bool ToHourMinutes(std::tm const & t, THourMinutes & hm)
{
hm = THourMinutes{t.tm_hour, t.tm_min};
return true;
}
} // namespace
namespace osmoh
{
bool IsActive(Timespan const & span, std::tm const & time)
{
if (span.HasStart() && span.HasEnd())
{
THourMinutes start;
THourMinutes end;
THourMinutes toBeChecked;
if (!ToHourMinutes(span.GetStart(), start))
return false;
if (!ToHourMinutes(span.GetEnd(), end))
return false;
if (!ToHourMinutes(time, toBeChecked))
return false;
return start <= toBeChecked && toBeChecked <= end;
}
return false;
}
} // namespace osmoh

View file

@ -0,0 +1,21 @@
#pragma once
#include "osm_time_range.hpp"
#include <ctime>
namespace osmoh
{
bool IsActive(Timespan const & spsn, std::tm const & date);
bool IsActive(WeekdayRange const & range, std::tm const & date);
bool IsActive(Holiday const & holiday, std::tm const & date);
bool IsActive(Weekdays const & weekdays, std::tm const & date);
bool IsActive(MonthDay const & MonthDay, std::tm const & date);
bool IsActive(MonthdayRange const & range, std::tm const & date);
bool IsActive(YearRange const & range, std::tm const & date);
bool IsActive(WeekRange const & range, std::tm const & date);
bool IsActive(RuleSequence const & rule, std::tm const & date);
} // namespace osmoh
// bool IsActive(Time const & time, std::tm const & date);
// bool IsActive(NthEntry const & nthEntry, std::tm const & date);