Home > OS >  How to convert a time pattern from a string to TimeInterval in Swift?
How to convert a time pattern from a string to TimeInterval in Swift?

Time:10-15

I have an array of strings that contains a time pattern (e.g '30 min'):

let arrayOfStrings: [String] = ["Episode 1 - 23 min", "Episode 2 - 42 min", "Episode 3 - 45 min"] 

func convertToTimeInterval(from string: String) -> TimeInterval {
    // logic here...
    return timeInterval // Double
}

So using such a method, I want from the results:

arrayOfStrings.forEach { string in 
   print(convertToTimeInterval(from: string))
}

// Prints
1380 // timeinterval from "23 min"
2520 // timeinterval from "42 min"
2700 // timeinterval from "45 min"
  • I have tried splitting from the the 'min' character but the problem is that it has to support multiple language format. So there is no way to know in advance the character to split from.
  • I have also tried getting the CharacterSet.separatedBy but the string can contains multiple decimal that have nothing to do with the time (e.g Episode 1)

Thanks for your help!

CodePudding user response:

A rather underestimated but powerful API in Objective-C and Swift is (NS)Scanner.

Assuming there is always a hyphen and a whitespace character before the duration value just scanUpTo "- " then scan the pattern and then scan the numeric value as Double and multiply it with 60.

func convertToTimeInterval(from string: String) -> TimeInterval {
    let scanner = Scanner(string: string)
    scanner.scanUpToString("- ")
    scanner.scanString("- ")
    return (scanner.scanDouble() ?? 0.0) * 60.0
}

CodePudding user response:

you could try this approach, where each string in the array is split into sections, and the minutes extracted and returned as a Double/TimeInterval.

This should (not tested) cater for multiple languages that uses Western Arabic numerals

let arrayOfStrings: [String] = ["Episode 1 - 23 min", "Episode 2 - 42 min", "Episode 3 - 45 min"]

arrayOfStrings.forEach { string in
     let t = convertToTimeInterval(from: string)
     print("--> timeInterval: \(t) minutes or \(t * 60) seconds")
}

func convertToTimeInterval(from string: String) -> TimeInterval {
    if let last = string.split(separator: "-").last {
        let term2 = last.split(separator: " ")
        if let minutes = term2.first {
            if let result = Double(minutes.trimmingCharacters(in: .whitespacesAndNewlines)) {
                return result
            }
        }
    }
    return 0.0  // <-- adjust as desired
}
  • Related