Question

I want to set a time_t variable to the next date/time at 10pm. So for example, if it was 11pm it would set the time_t to the 10pm the next day (23 hours later) or if it was 5pm it would set it to 10pm (in 5 hours).

I can think of a number of ways to do this (such as calculating the seconds until the next 10pm and adding that value), but they all feel like rather a hackish approach. What's the best way to accomplish this?

Was it helpful?

Solution

Convert it to a struct tm, using localtime for example. If it's past 10pm, add one day. Set hour to 10pm.

struct tm *tm;
tm = localtime(t);
if (tm->tm_hour >= 22)
    tm->tm_mday++;

tm->hour = 22;

Convert back to time_t using mktime.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top