简体   繁体   中英

“round” time_t to day/hour?

I have a unix time_t , is there any easy way to convert this to a time_t so it:

  1. Represents midnight of the day of the time_t ?
  2. Represents the start of the hour of the time_t ?

Something like this:

time_t t = time(NULL);

t -= (t % 86400); 

The constant 86400 = 24 * 60 * 60 - a useful number to remember, I think... ;)

Let the computer remember the celestial constants for you:

time_t      arg, start_of_hour, start_of_day;
struct tm   *temp;

temp = localtime(&arg);
temp->tm_sec  = 0;
temp->tm_min  = 0;
start_of_hour = mktime(temp);
temp->tm_hour = 0;
start_of_day  = mktime(temp);

Or use gmtime if you prefer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM