Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想在晚上 10 点将 time_t 变量设置为下一个日期/时间。例如,如果是晚上 11 点,它会将 time_t 设置为第二天(23 小时后)晚上 10 点,或者如果是下午 5 点,它将设置为晚上 10 点(5 小时后)。
我可以想到很多方法来做到这一点(例如计算直到下一个晚上 10 点的秒数并添加该值),但它们都感觉像是一种相当老套的方法。实现这一目标的最佳方法是什么?
将其转换为 a struct tm,localtime例如使用。如果超过晚上 10 点,则增加一天。将时间设置为晚上 10 点。
struct tm
localtime
struct tm *tm; tm = localtime(t); if (tm->tm_hour >= 22) tm->tm_mday++; tm->hour = 22;
转换回time_t使用mktime.
time_t
mktime