Skip to content

Commit fb5ed11

Browse files
committed
Format time
1 parent d3d94a2 commit fb5ed11

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

SupportCompanion/Extensions/Extensions.swift

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,39 @@ extension Notification.Name {
9090
static let handleIncomingURL = Notification.Name("handleIncomingURL")
9191
}
9292

93+
extension TimeInterval {
94+
/// Formats a TimeInterval into `hh:mm:ss` or `mm:ss`
95+
func formattedTime() -> String {
96+
let hours = Int(self) / 3600
97+
let minutes = (Int(self) % 3600) / 60
98+
let seconds = Int(self) % 60
99+
100+
if hours > 0 {
101+
return String(format: "%02d:%02d:%02d", hours, minutes, seconds)
102+
} else {
103+
return String(format: "%02d:%02d", minutes, seconds)
104+
}
105+
}
106+
107+
// func to format time to a string with "minutes" or "seconds" or "hourds" depending on the time
108+
func formattedTimeUnit() -> String {
109+
let hours = Int(self) / 3600
110+
let minutes = (Int(self) % 3600) / 60
111+
let seconds = Int(self) % 60
112+
113+
if hours > 0 {
114+
let hourUnit = hours == 1 ? "hour" : "hours"
115+
return String(format: "%d \(hourUnit)", hours)
116+
} else if minutes > 0 {
117+
let minuteUnit = minutes == 1 ? "minute" : "minutes"
118+
return String(format: "%d \(minuteUnit)", minutes)
119+
} else {
120+
let secondUnit = seconds == 1 ? "second" : "seconds"
121+
return String(format: "%d \(secondUnit)", seconds)
122+
}
123+
}
124+
}
125+
93126
extension Color {
94127
// Orange shades
95128
static let orangeLight = Color(hue: 0.1, saturation: 0.9, brightness: 0.75) // Softer orange for light mode
@@ -102,4 +135,4 @@ extension Color {
102135

103136
// Gray shades - darker gray for light mode
104137
static let grayLight = Color(hue: 0, saturation: 0, brightness: 0.3)
105-
}
138+
}

0 commit comments

Comments
 (0)