这就是我想出的。
请注意,我使代码通常更具可读性。这很重要,不仅是为了将来的维护,还因为它可以让你“只见树木不见森林”——反过来让你在头脑中记住重要的概念。
至少,这对我有帮助。
编辑DyP 提供了一种使用方式sleep_until
(在极少数情况下会表现得更准确,例如在睡眠期间时钟会改变的地方)。
#include <boost/date_time.hpp>
#include <boost/date_time/time_clock.hpp>
#include <iostream>
#include <thread>
#include <chrono>
int main()
{
using namespace boost::gregorian;
using boost::posix_time::ptime;
using clock = boost::posix_time::microsec_clock; // or: boost::posix_time::second_clock;
auto today = date(day_clock::local_day());
auto at_sunday = greg_weekday(Sunday);
auto next_sunday = next_weekday(today, at_sunday);
#if 1
auto as_tm = to_tm(next_sunday);
auto as_time_t = mktime(&as_tm);
auto as_time_point = std::chrono::system_clock::from_time_t(as_time_t);
std::this_thread::sleep_until(as_time_point);
#else
auto duration = (ptime(next_sunday) - clock::local_time());
auto msecs = duration.total_milliseconds();
std::cout << msecs << "\n";
std::this_thread::sleep_for(std::chrono::milliseconds(msecs));
#endif
}
看到它在 Coliru 上编译(显然超时)