Skip to content

Instantly share code, notes, and snippets.

@maakcode
Created March 21, 2021 01:32
Show Gist options
  • Save maakcode/57aa3224ff3f220a9dcd9f406407860e to your computer and use it in GitHub Desktop.
Save maakcode/57aa3224ff3f220a9dcd9f406407860e to your computer and use it in GitHub Desktop.
Composable TimeInterval extensions
extension TimeInterval {
static func milliseconds(_ value: Self) -> Self {
Self(value * 1e-3)
}
static func seconds(_ value: Self) -> Self {
Self(value)
}
static func minutes(_ value: Self) -> Self {
seconds(60) * value
}
static func hours(_ value: Self) -> Self {
minutes(60) * value
}
func miliseconds(_ value: Self) -> Self {
self + Self(value * 1e-3)
}
func seconds(_ value: Self) -> Self {
self + Self(value)
}
func minutes(_ value: Self) -> Self {
self + Self.seconds(60) * value
}
func hours(_ value: Self) -> Self {
self + Self.minutes(60) * value
}
}
@maakcode
Copy link
Author

Examples

UIView.animate(withDuration: .milliseconds(800)) {
    // animations
}

let t: TimeInterval = .hours(1).minutes(15).seconds(51)
print(t) // 4551

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment