Quantcast
Channel: how to convert datetime to unix timestamp in c? - Stack Overflow
Viewing all articles
Browse latest Browse all 8

Answer by Mohammadreza Askari for how to convert datetime to unix timestamp in c?

$
0
0

for anyone using gcc, following code is working for me:

#define YEAR_OFFSET 1900struct tm* dt = (struct tm*)malloc(sizeof(struct tm));char *strVar = "2022-08-13 23:09:47"; //! "YYYY-MM-DD HH:MM:SS"time_t timeVar;if(sscanf(strVar, "%d-%hhd-%hhd %hhd:%hhd:%hhd", &dt->tm_year, &dt->tm_mon, &dt->tm_mday, &dt->tm_hour, &dt->tm_min, &dt->tm_sec)==6){    dt->tm_year -= YEAR_OFFSET;    dt->tm_mon -= 1;    timeVar = mktime(&tmVar);}else    ;// bad formatfree(dt);

Viewing all articles
Browse latest Browse all 8

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>