Set time_t to next 10pm in C?
Вопрос
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?
Решение
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
.
Не связан с StackOverflow