Midnight UTC with unix

I was looking for a numerical representation for the unix date and time for midnight (in UTC), it seems to be a reasonable choice for this. However, since I'm not sure about my math skills, it’s

date = date - date % (24 * 60 * 60);

where dateis the unix timestamp, a way to do this? Is there a simpler method?

+3
source share
3 answers

Yes, your formula is a great way to do this. Another way to achieve this:

date = (date / 86400) * 86400;

, , () . , , 86400 , .

+5

/?

C UNIX.

time.h .

#include <time.h>

struct tm date_tm;
time_t date;

localtime_r(NULL, &date_tm);
date_tm.tm_sec = 0;
date_tm.tm_min = 0;
date_tm.tm_hour = 0;

date = mktime(&date_tm);

, roundabout to-string/from-string , . (%F %Z C99 / POSIX SUS.)

#define DATE_FORMAT "%F %Z"  /* yyyy-mm-dd t-z */

char date_str[15];
struct tm date_tm;
time_t date;

localtime_r(NULL, &date_tm);
strftime(date_str, sizeof(date_str), DATE_FORMAT, &date_tm);
strptime(date_str, DATE_FORMAT, &date_tm);

date = mktime(&date_tm);

, , UTC. UNIX- 86400 UNIX UNIX, .

+1

strtotime .

0

Source: https://habr.com/ru/post/1710678/


All Articles