Search code examples
cmillisecondsstrptime

Add milliseconds to a C date


I have parsed a date in C with strptime.

Now I have something like this:

debugLog(DEB_INFO, "observationDateConverted: %d-%d-%d %d:%d:%d\n", 
                        result.tm_year+1900, 
                        result.tm_mon + 1, 
                        result.tm_mday, 
                        result.tm_hour, 
                        result.tm_min, 
                        result.tm_sec);

With the latest date received in result (struct tm)

I have a bunch of milliseconds like this: 1396682344000 Which I want to add to that date to know the ending date.

How should I proceed?


Solution

  • General approach: Turn the struct tm into a timestamp (64bit int) and then add millis/1000.

    time_t totalseconds = mktime(&result) + (millis / 1000);
    

    You can then use the functions from C's time API to convert the time stamp back to struct tm .