如何使用objective-c格式化从youtube api v3 reuqest返回的持续时间?
Posted
技术标签:
【中文标题】如何使用objective-c格式化从youtube api v3 reuqest返回的持续时间?【英文标题】:How to format the duration returned from youtube api v3 reuqest using objective-c? 【发布时间】:2015-10-01 04:28:35 【问题描述】:如何使用objective-c将这个时间段“PT1H20M10S”格式化为“1:20:10”? 这是从对 youtube 的请求中返回的。它给了我一个时间段。如何将“PT1H20M10S”设置为像这样“1:20:10”在我的应用程序中可读?
【问题讨论】:
Convert string to date in my iPhone app的可能重复 这是一个重复的@user2027279 吗? Alaa Agwa 你希望它是一个以秒为单位的实际时间,还是只是一个字符串格式? 我认为两个简单的搜索就可以给出答案,您只需要搜索如何从 NSString 解析 NSDate 以及如何格式化该日期,两者都使用 NSDateFormatter 【参考方案1】:您可以使用以下代码:
- (NSString *)parseDuration:(NSString *)duration
NSInteger hours = 0;
NSInteger minutes = 0;
NSInteger seconds = 0;
NSRange timeRange = [duration rangeOfString:@"T"];
duration = [duration substringFromIndex:timeRange.location];
while (duration.length > 1)
duration = [duration substringFromIndex:1];
NSScanner *scanner = [NSScanner.alloc initWithString:duration];
NSString *part = [NSString.alloc init];
[scanner scanCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:&part];
NSRange partRange = [duration rangeOfString:part];
duration = [duration substringFromIndex:partRange.location + partRange.length];
NSString *timeUnit = [duration substringToIndex:1];
if ([timeUnit isEqualToString:@"H"])
hours = [part integerValue];
else if ([timeUnit isEqualToString:@"M"])
minutes = [part integerValue];
else if ([timeUnit isEqualToString:@"S"])
seconds = [part integerValue];
return [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
您还可以阅读 ISO_8601 持续时间here。
【讨论】:
不错的答案。为你+1。 :D【参考方案2】:使用字符串扩展:
extension String
func parseVideoDurationOfYoutubeAPI(videoDuration: String?) -> String
var videoDurationString = videoDuration! as NSString
var hours: Int = 0
var minutes: Int = 0
var seconds: Int = 0
let timeRange = videoDurationString.rangeOfString("T")
videoDurationString = videoDurationString.substringFromIndex(timeRange.location)
while videoDurationString.length > 1
videoDurationString = videoDurationString.substringFromIndex(1)
let scanner = NSScanner(string: videoDurationString as String) as NSScanner
var part: NSString?
scanner.scanCharactersFromSet(NSCharacterSet.decimalDigitCharacterSet(), intoString: &part)
let partRange: NSRange = videoDurationString.rangeOfString(part! as String)
videoDurationString = videoDurationString.substringFromIndex(partRange.location + partRange.length)
let timeUnit: String = videoDurationString.substringToIndex(1)
if (timeUnit == "H")
hours = Int(part as! String)!
else if (timeUnit == "M")
minutes = Int(part as! String)!
else if (timeUnit == "S")
seconds = Int(part! as String)!
else
return String(format: "%02d:%02d:%02d", hours, minutes, seconds)
用法:
override func viewWillAppear(animated: Bool)
let youtubeVideoDurationString = "PT15M51S"
let result_Hour_Minute_Second = youtubeVideoDurationString.parseVideoDurationOfYoutubeAPI("PT15M51S") as String
print("result_Hour_Minute_Second: \(result_Hour_Minute_Second)")
【讨论】:
【参考方案3】:您可以使用扫描仪将数字提取为
NSString *answerString;
NSScanner *scanner = [NSScannerscannerWithString:originalString]; NSCharacterSet *numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
[scanner scanUpToCharactersFromSet:numbers intoString:NULL];
[scanner scanCharactersFromSet:numbers intoString:&numberString];
int number = [answerString integerValue];
【讨论】:
【参考方案4】:试试这个代码..我用这个代码得到了正确的输出..
func formatDurations (sender : String) ->String
var timeDuration : NSString!
let string: NSString = sender
if string.rangeOfString("H").location == NSNotFound && string.rangeOfString("M").location == NSNotFound
if string.rangeOfString("S").location == NSNotFound
timeDuration = NSString(format: "00:00")
else
var secs: NSString = sender
secs = secs.substringFromIndex(secs.rangeOfString("PT").location + "PT".characters.count)
secs = secs.substringToIndex(secs.rangeOfString("S").location)
timeDuration = NSString(format: "00:%02d", secs.integerValue)
else if string.rangeOfString("H").location == NSNotFound
var mins: NSString = sender
mins = mins.substringFromIndex(mins.rangeOfString("PT").location + "PT".characters.count)
mins = mins.substringToIndex(mins.rangeOfString("M").location)
if string.rangeOfString("S").location == NSNotFound
timeDuration = NSString(format: "%02d:00", mins.integerValue)
else
var secs: NSString = sender
secs = secs.substringFromIndex(secs.rangeOfString("M").location + "M".characters.count)
secs = secs.substringToIndex(secs.rangeOfString("S").location)
timeDuration = NSString(format: "%02d:%02d", mins.integerValue, secs.integerValue)
else
var hours: NSString = sender
hours = hours.substringFromIndex(hours.rangeOfString("PT").location + "PT".characters.count)
hours = hours.substringToIndex(hours.rangeOfString("H").location)
if string.rangeOfString("M").location == NSNotFound && string.rangeOfString("S").location == NSNotFound
timeDuration = NSString(format: "%02d:00:00", hours.integerValue)
else if string.rangeOfString("M").location == NSNotFound
var secs: NSString = sender
secs = secs.substringFromIndex(secs.rangeOfString("H").location + "H".characters.count)
secs = secs.substringToIndex(secs.rangeOfString("S").location)
timeDuration = NSString(format: "%02d:00:%02d", hours.integerValue, secs.integerValue)
else if string.rangeOfString("S").location == NSNotFound
var mins: NSString = sender
mins = mins.substringFromIndex(mins.rangeOfString("H").location + "H".characters.count)
mins = mins.substringToIndex(mins.rangeOfString("M").location)
timeDuration = NSString(format: "%02d:%02d:00", hours.integerValue, mins.integerValue)
else
var secs: NSString = sender
secs = secs.substringFromIndex(secs.rangeOfString("M").location + "M".characters.count)
secs = secs.substringToIndex(secs.rangeOfString("S").location)
var mins: NSString = sender
mins = mins.substringFromIndex(mins.rangeOfString("H").location + "H".characters.count)
mins = mins.substringToIndex(mins.rangeOfString("M").location)
timeDuration = NSString(format: "%02d:%02d:%02d", hours.integerValue, mins.integerValue, secs.integerValue)
return timeDuration as String
【讨论】:
【参考方案5】:如果视频长度为一小时,则格式为 PT#H#M#S,如果最长为 24 小时,则格式为 P#DT#H#M#S。在下面检查我修改过的方法:
NSString *formattedDuration = @"";
NSString *durationregX = @"^(P\\d+DT)[A-Z0-9]+$";
NSPredicate *checking = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", durationregX];
BOOL isDayLengthVideo = [checking evaluateWithObject: duration];
long getDayHours = 0;
if(isDayLengthVideo)
NSArray *sliceDuration = [duration componentsSeparatedByString:@"DT"];
NSString *getDay = [sliceDuration[0] stringByReplacingOccurrencesOfString:@"P" withString:@""];
getDayHours = (long)[getDay integerValue] * 24;
formattedDuration = [[[sliceDuration[1] stringByReplacingOccurrencesOfString:@"H" withString:@":"] stringByReplacingOccurrencesOfString:@"M" withString:@":"] stringByReplacingOccurrencesOfString:@"S" withString:@""];
else
formattedDuration = [[[[duration stringByReplacingOccurrencesOfString:@"PT" withString:@""] stringByReplacingOccurrencesOfString:@"H" withString:@":"] stringByReplacingOccurrencesOfString:@"M" withString:@":"] stringByReplacingOccurrencesOfString:@"S" withString:@""];
NSString *clean_duration = @"";
NSArray *components = [formattedDuration componentsSeparatedByString:@":"];
NSInteger loopchecker = 0;
for (NSString *component in components)
loopchecker++;
clean_duration = clean_duration.length > 0 ? [NSString stringWithFormat:@"%@:", clean_duration] : clean_duration; // ""
if(component.length < 2)
clean_duration = loopchecker == 1 && isDayLengthVideo ? [NSString stringWithFormat:@"%ld", ([component integerValue] + getDayHours)] : [NSString stringWithFormat:@"%@0%@", clean_duration, component];
return clean_duration;
continue;
clean_duration = [NSString stringWithFormat: @"%@%@", clean_duration, component];
return clean_duration;
【讨论】:
【参考方案6】:简单的 Swift 4.2 代码
func parseDuration(videoDuration: String?) -> String
var hours: Int = 0
var minutes: Int = 0
var seconds: Int = 0
if let videoDuration = videoDuration
var lastIndex = videoDuration.startIndex
if let indexT = videoDuration.index(of: "T")
lastIndex = videoDuration.index(after: indexT)
if let indexH = videoDuration.index(of: "H")
let hrs = String(videoDuration[lastIndex..<indexT])
hours = Int(hrs) ?? 0
lastIndex = videoDuration.index(after: indexH)
if let indexM = videoDuration.index(of: "M")
let min = String(videoDuration[lastIndex..<indexM])
minutes = Int(min) ?? 0
lastIndex = videoDuration.index(after: indexM)
if let indexS = videoDuration.index(of: "S")
let sec = String(videoDuration[lastIndex..<indexS])
seconds = Int(sec) ?? 0
return String(format: "%02d:%02d:%02d", hours, minutes, seconds)
【讨论】:
以上是关于如何使用objective-c格式化从youtube api v3 reuqest返回的持续时间?的主要内容,如果未能解决你的问题,请参考以下文章
IOS - 使用 Youtube API 检查 Youtube 视频是不是存在
YouTube Objective-C API 视频 maxResults
Objective-C 如何格式化字符串从 09:00 到 9 和 09:00 到 9:30 的所有时间?