Hello, i am trying to write an function that takes for an argument:
a string filled with numbers that mark the number of miliseconds elapsed since 1970 epoch.
The function should return a time_t value containing number of seconds elapsed since epoch.
My current implementation looks like this:
Code:
time_t util_extract_seconds_from_jstime_string(const char *jstime)
{
time_t retval;
char *seconds = (char *)malloc((strlen(jstime) - 2)*sizeof(char));
strncpy(seconds,jstime,strlen(jstime) - 3);
seconds[strlen(seconds)] = '\0';
retval = (time_t) _strtoi64(seconds, NULL, 10);
seconds = NULL;
free(seconds);
return retval;
}
However this implementation is rather sloppy cause of the additional malloc used and cause of the _strtoi64 function call. I would like to have a portable function.
Any suggestions?