Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sleep until next Sunday

Tags:

c++

boost

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);
    // ...
}
like image 793
FrozenHeart Avatar asked Oct 11 '13 07:10

FrozenHeart


People also ask

Is it OK to sleep on a Sunday night?

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.

What is the best time to go to bed to sleep?

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.

Do you try to sleep all at once?

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.

Is it bad to fall asleep at night before bedtime?

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.


2 Answers

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)

like image 102
sehe Avatar answered Nov 15 '22 20:11

sehe


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:

  1. Add a scheduled task(or whatever the windows/osx terminology is) / cronjob (linux) and set it to run on Sunday.

  2. 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.

like image 25
dutt Avatar answered Nov 15 '22 22:11

dutt