开发者

Of subtitles & lag times (yet another C overflow doubt)

I'm trying to calculate the time offset to be added to subtitle files to correct the lag. The part shown below is after tokenizing the hh:mm:ss,uuu (uuu stands for microseconds) into the time[] array. I'm converting the time into microseconds then adding the actual & lag time to get the final time.

The program computes the actual & lag time properly. However, it gives the wrong final hour time. Have I hit upon some overflow condition that can't be handled by the code below?

Edit: I have realized the error. I should be dividing rather than taking remainder for hour time.

int i;
int time[4];
unsigned long totalTime,totalLagTime;
...
for(i=0;i<4;i++)
{
    printf("time[%d] = %d\n",i,time[i]);
}

for(i=0;i<4;i++)
{
printf("lag time[%d] = %d\n",i,lagTime[i]);
}

totalTime = 1000*(3600*time开发者_Go百科[0] + 60*time[1] + time[2]) + time[3];

printf("total time is %u in milliseconds\n",totalTime);

totalLagTime = 1000*(3600*lagTime[0] + 60*lagTime[1] + lagTime[2]) + lagTime[3];

printf("total lag time is %u in milliseconds\n",totalLagTime);

totalTime += totalLagTime;

printf("Now, total time is %u in milliseconds\n",totalTime);

time[0] = totalTime % 3600000;

printf("hour time is %d\n",time[0]);

Test case:

00:01:24,320

time[0] = 0
time[1] = 1
time[2] = 24
time[3] = 320
lag time[0] = 10
lag time[1] = 10
lag time[2] = 10
lag time[3] = 10
total time is 84320 in milliseconds
total lag time is 36610010 in milliseconds
Now, total time is 36694330 in milliseconds
hour time is 694330


Shouldn't that be

time[0] = totalTime / 3600000;


You have a logic error: 36694330 mod 3600000 really is 694330.

What are you trying to do, exactly?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜