Files
spark/src/server/sparkd/position/import/DownloadWindow.cpp
T
2019-08-15 16:37:48 -04:00

81 lines
2.6 KiB
C++

#include "server/sparkd/position/import/DownloadWindow.h"
#include "lib/config/config.h"
#include "lib/config/protos/position_cfg.pb.h"
#include "lib/utils/bts_assert.h"
#include <boost/date_time/gregorian/gregorian.hpp>
namespace bts {
namespace position {
using namespace boost::posix_time;
DownloadWindow::DownloadWindow(const DownloadWindow &src)
: _start(src.start()), _stop(src.stop()) {}
DownloadWindow ::DownloadWindow(datetime::PTime now, time_duration start_time,
time_duration stop_time) {
bts_assert(start_time != stop_time);
auto now_time = now.time_of_day();
auto today = now.date();
auto tomorrow = today + boost::gregorian::date_duration(1);
auto yesterday = today - boost::gregorian::date_duration(1);
if (start_time < stop_time) {
// The window opens and closes in the same day
// are we past the end of the window?
if (now_time > stop_time) {
// Today's window has closed and will only open up again tomorrow
_start = datetime::PTime(tomorrow, start_time);
_stop = datetime::PTime(tomorrow, stop_time);
} else {
// Today's window is either open now or will open later
// today
_start = datetime::PTime(today, start_time);
_stop = datetime::PTime(today, stop_time);
}
} else {
// The window opens one day and closes the next
if (now_time < stop_time) {
// Yesterdays window is still open, it close today
_start = datetime::PTime(yesterday, start_time);
_stop = datetime::PTime(now.date(), stop_time);
} else {
// Today's window is either open now or will open later
// today (and close tomorrow)
_start = datetime::PTime(today, start_time);
_stop = datetime::PTime(tomorrow, stop_time);
}
}
bts_assert(_start < _stop);
}
DownloadWindow::DownloadWindow(datetime::PTime const &start,
datetime::PTime const &stop)
: _start(start), _stop(stop) {
bts_assert(_start < _stop);
}
DownloadWindow DownloadWindow::next() const {
return DownloadWindow(_start + boost::gregorian::date_duration(1),
_stop + boost::gregorian::date_duration(1));
}
bool DownloadWindow::is_inside(datetime::PTime const &time) const {
return time >= _start and time <= _stop;
}
datetime::PTime DownloadWindow::start() const { return _start; }
datetime::PTime DownloadWindow::stop() const { return _stop; }
std::ostream &operator<<(std::ostream &out, DownloadWindow const &w) {
out << "[ " << w.start() << " ... " << w.stop() << " ]";
return out;
}
} // namespace position
} // namespace bts