49 lines
1.3 KiB
Swift
49 lines
1.3 KiB
Swift
|
|
//
|
||
|
|
// TimeStringToSeconds.swift
|
||
|
|
// WOKA
|
||
|
|
//
|
||
|
|
// Created by MacBook Pro on 11/06/24.
|
||
|
|
//
|
||
|
|
|
||
|
|
import UIKit
|
||
|
|
|
||
|
|
extension String {
|
||
|
|
|
||
|
|
func timeStringToSeconds() -> Int? {
|
||
|
|
let components = self.split(separator: ":")
|
||
|
|
|
||
|
|
switch components.count {
|
||
|
|
case 2:
|
||
|
|
// MM:SS format
|
||
|
|
let minutes = Int(components[0]) ?? 0
|
||
|
|
let seconds = Int(components[1]) ?? 0
|
||
|
|
return (minutes * 60) + seconds
|
||
|
|
case 3:
|
||
|
|
// HH:MM:SS format
|
||
|
|
let hours = Int(components[0]) ?? 0
|
||
|
|
let minutes = Int(components[1]) ?? 0
|
||
|
|
let seconds = Int(components[2]) ?? 0
|
||
|
|
return (hours * 3600) + (minutes * 60) + seconds
|
||
|
|
default:
|
||
|
|
// Invalid format
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func checkHourZero() -> String{
|
||
|
|
// Function to format the time
|
||
|
|
// Split the time string by ":"
|
||
|
|
let components = self.split(separator: ":")
|
||
|
|
|
||
|
|
// Check if the time string starts with "00"
|
||
|
|
if components.count == 3 && components[0] == "00" {
|
||
|
|
// Remove the initial "00:"
|
||
|
|
return "\(components[1]):\(components[2])"
|
||
|
|
} else {
|
||
|
|
// Return the original time string
|
||
|
|
return self
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|