-1

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.

koen
  • 5,383
  • 7
  • 50
  • 89
Tom
  • 45
  • 7

1 Answers1

1

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

Andreas Oetjen
  • 9,889
  • 1
  • 24
  • 34
  • Now works perfectly (I think the uppercase 'SSS' caught me out). Thanks a lot Andreas! – Tom Jul 14 '20 at 14:11