141 lines
5.1 KiB
C++
141 lines
5.1 KiB
C++
#include "session_name.h"
|
|
#include "lib/utils/bts_assert.h"
|
|
#include <boost/date_time/posix_time/posix_time.hpp>
|
|
|
|
namespace bts {
|
|
namespace jetfix {
|
|
|
|
boost::gregorian::greg_weekday parse_weekday (std::string weekday_str)
|
|
{
|
|
boost::to_lower (weekday_str);
|
|
for (int i = 0; i < 7; i++) {
|
|
auto gw_day = boost::gregorian::greg_weekday (i);
|
|
auto prefix = std::string (gw_day.as_short_string ());
|
|
boost::to_lower (prefix);
|
|
if (weekday_str.find (prefix) == 0)
|
|
return gw_day;
|
|
}
|
|
bts_assert (false);
|
|
return boost::gregorian::greg_weekday (0);
|
|
}
|
|
|
|
boost::posix_time::time_duration parse_time (std::string const &time_str)
|
|
{
|
|
try {
|
|
return boost::posix_time::duration_from_string (time_str);
|
|
}
|
|
catch (boost::bad_lexical_cast &)
|
|
{
|
|
bts_assert (false);
|
|
return boost::posix_time::time_duration ();
|
|
}
|
|
}
|
|
|
|
datetime::PTime closest_before (datetime::PTime const &ref_date,
|
|
boost::gregorian::greg_weekday weekday,
|
|
boost::posix_time::time_duration time_of_day)
|
|
{
|
|
if (ref_date.date ().day_of_week () == weekday) {
|
|
if (time_of_day < ref_date.time_of_day ())
|
|
return datetime::PTime (ref_date.date (),time_of_day);
|
|
else {
|
|
return datetime::PTime (ref_date.date () - boost::gregorian::date_duration (7),
|
|
time_of_day);
|
|
}
|
|
} else {
|
|
auto distance =
|
|
ref_date.date ().day_of_week ().as_number () -
|
|
weekday.as_number ();
|
|
if (distance < 0)
|
|
distance += 7;
|
|
return datetime::PTime (ref_date.date () - boost::gregorian::date_duration (distance),
|
|
time_of_day);
|
|
}
|
|
}
|
|
|
|
bool in_session (datetime::PTime const &ref_date,
|
|
boost::posix_time::time_duration time_of_day_start,
|
|
boost::posix_time::time_duration time_of_day_end)
|
|
{
|
|
auto t = ref_date.time_of_day ();
|
|
if (time_of_day_start < time_of_day_end)
|
|
return t >= time_of_day_start && t <= time_of_day_end;
|
|
else
|
|
return t >= time_of_day_start || t <= time_of_day_end;
|
|
}
|
|
|
|
bool in_session (datetime::PTime const &ref_date,
|
|
boost::gregorian::greg_weekday weekday_start,
|
|
boost::posix_time::time_duration time_of_day_start,
|
|
boost::gregorian::greg_weekday weekday_end,
|
|
boost::posix_time::time_duration time_of_day_end)
|
|
{
|
|
auto closest_earlier_start = closest_before (ref_date,weekday_start,time_of_day_start);
|
|
auto closest_earlier_end = closest_before (ref_date,weekday_end,time_of_day_end);
|
|
return closest_earlier_end<closest_earlier_start;
|
|
}
|
|
|
|
std::string session_name_from_ptime (datetime::PTime t,
|
|
boost::posix_time::time_duration time_of_day_start,
|
|
boost::posix_time::time_duration time_of_day_end)
|
|
{
|
|
std::string result (10,' ');
|
|
auto start_of_session = t;
|
|
|
|
if (time_of_day_start > time_of_day_end &&
|
|
start_of_session.time_of_day () <= time_of_day_end)
|
|
start_of_session -= boost::gregorian::date_duration (1);
|
|
|
|
sprintf (&result[0], "%04d-%02d-%02d",
|
|
(unsigned short) start_of_session.date ().year (),
|
|
start_of_session.date ().month ().as_number (),
|
|
start_of_session.date ().day ().as_number ());
|
|
return result;
|
|
}
|
|
|
|
std::string session_name (datetime::PTime const &ref_date,
|
|
boost::gregorian::greg_weekday weekday_start,
|
|
boost::posix_time::time_duration time_of_day_start,
|
|
boost::gregorian::greg_weekday weekday_end,
|
|
boost::posix_time::time_duration time_of_day_end)
|
|
{
|
|
if (in_session (ref_date,weekday_start,time_of_day_start,weekday_end,time_of_day_end))
|
|
return session_name_from_ptime (closest_before (ref_date,weekday_start,time_of_day_start),
|
|
boost::posix_time::time_duration(0, 0, 0),
|
|
boost::posix_time::time_duration(0, 0, 1));
|
|
else
|
|
return std::string ();
|
|
}
|
|
|
|
std::string session_name (datetime::PTime const &ref_date,
|
|
boost::posix_time::time_duration time_of_day_start,
|
|
boost::posix_time::time_duration time_of_day_end)
|
|
{
|
|
if (in_session (ref_date,time_of_day_start,time_of_day_end))
|
|
return session_name_from_ptime (ref_date, time_of_day_start, time_of_day_end);
|
|
else
|
|
return "";
|
|
}
|
|
|
|
std::string session_name (config::JetFIX::Connection const &session_cfg)
|
|
{
|
|
auto now = datetime::local_now ();
|
|
|
|
if (session_cfg.has_start_day ()) {
|
|
bts_assert (session_cfg.has_end_day ());
|
|
return session_name (now,
|
|
parse_weekday (session_cfg.start_day ()),
|
|
parse_time (session_cfg.start_time ()),
|
|
parse_weekday (session_cfg.end_day ()),
|
|
parse_time (session_cfg.end_time ()));
|
|
} else
|
|
return session_name (now,
|
|
parse_time (session_cfg.start_time ()),
|
|
parse_time (session_cfg.end_time ()));
|
|
}
|
|
|
|
|
|
}}
|
|
|
|
|