我该如何处理这个欧式时间戳?

How can I handle this European-style timestamp?

我正在尝试检查视频字幕文件(.srt 格式)中的数千行,并连续几天在互联网上搜索解决方案,但无济于事。子包含 in/out 个时间戳,如下例所示:

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

时分秒毫秒(注意毫秒部分前的欧式逗号,代表小数点)。我打算做的是将时间戳解析成它的两个组件并检查它们之间的区别,因为很多都是错误的。我构建了一个简单的测试函数来处理普通的 hh:mm:ss 部分,效果很好:

    -(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);
      }

然而,无论什么我都试过,我就是无法显示小数部分。我怎样才能 modify/change 我的代码做到这一点?非常感谢任何帮助。

您需要在格式字符串中指定毫秒:

    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);

然后打印

time difference:612.111

符合预期