Convert time_t to struct tm with gmtime() , then convert struct tm to plain text (preferably in ISO 8601 format) using strftime() . The result will be portable, readable and machine readable.
To return to time_t , you simply parse the string back into struct tm and use mktime() .
For reference:
Code example:
// Converting from time_t to string time_t t = time(NULL); struct tm *ptm = gmtime(&t); char buf[256]; strftime(buf, sizeof buf, "%F %T", ptm); // buf is now "2015-05-15 22:55:13" // Converting from string to time_t char *buf = "2015-05-15 22:55:13"; struct tm tm; strptime(buf, "%F %T", &tm); time_t t = mktime(&tm);
mu is too short
source share