How can I sleep until next Sunday using boost? Can i convert boost::gregorian::date
object to something that boost::this_thread::sleep_until
can handle? Is it possible?
#include <boost/date_time.hpp>
int main()
{
boost::gregorian::date current_date(boost::gregorian::day_clock::local_day());
boost::gregorian::greg_weekday next_sunday_date(boost::gregorian::Sunday);
boost::gregorian::date next_weekday_date = next_weekday(current_date, next_sunday_date);
// ...
}
Here's the good news: You can sleep normally, even on Sunday nights. If your insomnia persists, consider speaking with a sleep specialist about treatment options that may be helpful, including cognitive behavioral therapy for insomnia (CBTI). In rare cases, the use a sleeping pill on Sunday nights may be helpful.
Going to bed and rising at the same time every night is actually the best way to feel rested, instead of relying on sleeping in to "catch up" on lost sleep throughout the week. Instead of sleeping in on weekends, try going to bed thirty minutes to an hour earlier than your typical bedtime during the week.
Don't try to sleep all at once. Many people make the mistake of trying to replicate night sleep during the day. "Most night shift workers will go to sleep within 10 or 15 minutes, but after four hours, their sleep becomes fragmented," Drake said. "They fall asleep and wake up and fall asleep and wake up.
Falling asleep in the hours before bedtime will make it especially difficult to get to sleep. Remember: If your insomnia is especially entrenched, cut out the naps completely until your sleep improves. As noted above, try to keep a regular sleep schedule, including week nights and weekend nights.
Here's what I came up with.
Note that I made the code generally more readable. This is important, not just for future maintenance, but also because it will allow you to "see the forest for the trees" - in turn allowing you to remember the important concepts mentally.
At least, that helps me.
Edit DyP contributed a way to use sleep_until
(which would behave more accurately in rare circumstances, e.g. where the clock would change during the sleep).
#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
}
See it compiling on Coliru (obviously times out)
This sounds unstable. What if the user turns the computer off or goes into hibernation, or just does a restart?
I would do this in one of two ways:
Add a scheduled task(or whatever the windows/osx terminology is) / cronjob (linux) and set it to run on Sunday.
Add it to autostart and periodically(once per 10/30/60 minutes) check if it's Sunday.
Both ways handle restart/shut off/hibernation better than sleeping for 5 days.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With