C provides difftime
to figure out the number of seconds and timegm
is helpful for the UTC timezone. Watch out for that tricky tm_mon
element (number of months since January, so August is really 7 not 8).
Maybe extend on the result from difftime
by adding the msec difference?
Something like this?
#include <stdio.h>
#include <time.h>
#include <unistd.h>
struct TIME {
struct tm tm;
unsigned int tm_msec;
};
double difftime_ms( struct TIME *time2, struct TIME *time1 )
{
time_t t1 = timegm( &time1->tm );
time_t t2 = timegm( &time2->tm );
return ( 1000.0 * difftime( t2, t1 ) + ( time2->tm_msec - time1->tm_msec ) );
}
int main()
{
struct TIME time1 = { .tm = { .tm_year = 2022, .tm_mon = (8-1), .tm_mday = 13, .tm_hour = 8, .tm_min = 17, .tm_sec = 20} , .tm_msec = 000 };
struct TIME time2 = { .tm = { .tm_year = 2022, .tm_mon = (8-1), .tm_mday = 13, .tm_hour = 8, .tm_min = 17, .tm_sec = 20} , .tm_msec = 512 };
printf( "Difference is %.2f msec\n", difftime_ms( &time2, &time1 ) );
return 0;
}