How can I handle this European-style timestamp?

36 Views Asked by At

I’m trying to check through thousands of lines in video subtitle files (in .srt format) and searched the internet for days on end for a solution, but to no avail. The subs contain in/out timestamps as shown in the following example:

61
00:08:35,504 --> 00:08:38,629
Do you know where he left the car keys?

in hours, minutes, seconds and milliseconds (notice the European-style comma before the millisecond part, representing the decimal point). What I plan to do is parse the timestamp into its two components and check the difference between them, since many are faulty. I built a simple test function to handle the plain hh:mm:ss part which works well:

    -(IBAction)checkSubLength:(id)sender
      {
      NSString *inStr = @"10:10:45";
      NSString *outStr = @"10:20:57";

      NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
      [dateFormatter setDateFormat:@"hh:mm:ss"];

      NSDate *inTime = [dateFormatter dateFromString:inStr];
      NSDate *outTime = [dateFormatter dateFromString:outStr];
      NSTimeInterval distanceBetweenDates = [outTime timeIntervalSinceDate:inTime];

      NSLog(@"time difference:%.3f", distanceBetweenDates);
      }

However, I just can’t get the fractional part to display no matter what I try. How can I modify/change my code do that? Any help much appreciated.

1

There are 1 best solutions below

1
Andreas Oetjen On BEST ANSWER

You need to specify the millis in the format string:

    NSString *inStr = @"10:10:45,111";
    NSString *outStr = @"10:20:57,222";

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"hh:mm:ss,SSS"];

    NSDate *inTime = [dateFormatter dateFromString:inStr];
    NSDate *outTime = [dateFormatter dateFromString:outStr];
    NSTimeInterval distanceBetweenDates = [outTime timeIntervalSinceDate:inTime];

    NSLog(@"time difference:%.3f", distanceBetweenDates);

which then prints

time difference:612.111

as expected