Desired language/libraries?
I'll assume C with standard UNIX libraries.
time.h
would be perfectly suitable.
#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);
I suppose the roundabout to-string/from-string method would work too, but I wouldn't recommend it. (%F
and %Z
should be required by C99 and/or some POSIX or SUS specification.)
#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);
Hmm, I didn't notice at first that you want UTC. Since one UNIX day is guaranteed to always be 86400 UNIX seconds in UNIX time, I don't see any problem with your original solution.