Is this legit? Im trying to get to a time_t as fast as possible given a string formatted like YYYYMMDDHHMMSS.
static time_t ConvertToSecSince1970(char *szYYYYMMDDHHMMSS)
{
struct tm Tm;
memset(&Tm, 0, sizeof(Tm));
Tm.tm_year = makeInt(szYYYYMMDDHHMMSS + 0, 4) - 1900;
Tm.tm_mon = makeInt(szYYYYMMDDHHMMSS + 4, 2) - 1;
Tm.tm_mday = makeInt(szYYYYMMDDHHMMSS + 6, 2);
Tm.tm_hour = makeInt(szYYYYMMDDHHMMSS + 8, 2);
Tm.tm_min = makeInt(szYYYYMMDDHHMMSS + 10, 2);
Tm.tm_sec = makeInt(szYYYYMMDDHHMMSS + 12, 2);
return mktime(&Tm);
}
It seems to produce the same answer if I created TM using:
strptime(szYYYYMMDDHHMMSS, "%Y%m%d%H%M%S", &Tm);
I am worried that tm_yday, tm_wday, tm_isdst, tm_gmtoff, tm_zone are important. My dates are UTC so I figured gmtoff = 0 and tm_zone = 0 might work.
By the way, Here is makeInt:
inline int makeInt(const char *p, int size)
{
const char *endp;
int intval = 0;
endp = p + size;
while (p < endp)
{
intval = intval * 10 + *p - '0';
p++;
}
return intval;
}
mktime()ignores thetm_wdayandtm_ydayfields, and calculates new values for them based on the other fields. The same applies to the BSD extensionstm_gmtoffandtm_zone, except that they are calculated from the local time zone.Note however that
mktime()uses local time, not UTC, so if your input dates are UTC then your timezone must be set to UTC.