#include "session_name.h" #include "lib/utils/bts_assert.h" #include 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; } SessionName 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,t}; } std::optional 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::nullopt; } std::optional 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::nullopt; } std::optional 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())); } SessionSpan session_span(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 one_week = boost::gregorian::date_duration(7); 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); if (closest_earlier_end < closest_earlier_start) // we are in the session return {closest_earlier_start, closest_earlier_end + one_week }; else // need to wait for the next session return {closest_earlier_start+one_week, closest_earlier_end + one_week }; } SessionSpan session_span(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(); auto today = ref_date.date() ; auto tomorrow = today + boost::gregorian::date_duration(1) ; auto yesterday = today - boost::gregorian::date_duration(1) ; if (time_of_day_start < time_of_day_end) { // scheduled start/scheduled end do not span midnight if (t <= time_of_day_end) { // we are inside the schedule return { datetime::PTime(today,time_of_day_start), datetime::PTime(today,time_of_day_end) }; } else { // too late for today, tomorrow is the time for the session return { datetime::PTime(tomorrow,time_of_day_start), datetime::PTime(tomorrow,time_of_day_end) }; } } else { // the start/stop span over midnight if (t <= time_of_day_end) { // we are inside the the schedule return { datetime::PTime(yesterday,time_of_day_start), datetime::PTime(today,time_of_day_end) }; } else { // too late, skip to next session return { datetime::PTime(today,time_of_day_start), datetime::PTime(tomorrow,time_of_day_end) }; } } } SessionSpan session_span(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_span(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_span(now, parse_time(session_cfg.start_time()), parse_time(session_cfg.end_time())); } } // namespace jetfix } // namespace bts