Skip to content

Instantly share code, notes, and snippets.

@Jeehut
Created March 26, 2018 11:19
Show Gist options
  • Save Jeehut/4185b17199a7e769e447d1a3beeb6c42 to your computer and use it in GitHub Desktop.
Save Jeehut/4185b17199a7e769e447d1a3beeb6c42 to your computer and use it in GitHub Desktop.
import Foundation
//: ## The Problem
// property example
let pollingInterval: TimeInterval = 5
// method example
func animate(duration: TimeInterval = 20, animations: () -> Void) {
// do something
}
// method usage example
animate(duration: 0.5, animations: { /* do something */ })
let intervalThreshold: TimeInterval = 6 * 60 * 60 // 6 hours
//: ## DispatchTimeInterval
// property example
let pollingInterval2: DispatchTimeInterval = .seconds(5)
// method example
func animate2(duration: DispatchTimeInterval = .seconds(20), animations: () -> Void) {
// do something
}
// method usage example
animate2(duration: .milliseconds(500), animations: { /* do something */ })
//: ## TimeInterval Extension
extension TimeInterval {
// MARK: - Computed Type Properties
internal static var secondsPerDay: Double { return 24 * 60 * 60 }
internal static var secondsPerHour: Double { return 60 * 60 }
internal static var secondsPerMinute: Double { return 60 }
internal static var millisecondsPerSecond: Double { return 1_000 }
internal static var microsecondsPerSecond: Double { return 1_000 * 1_000 }
internal static var nanosecondsPerSecond: Double { return 1_000 * 1_000 * 1_000 }
// MARK: - Computed Instance Properties
/// - Returns: The `TimeInterval` in days.
public var days: Double {
return self / TimeInterval.secondsPerDay
}
/// - Returns: The `TimeInterval` in hours.
public var hours: Double {
return self / TimeInterval.secondsPerHour
}
/// - Returns: The `TimeInterval` in minutes.
public var minutes: Double {
return self / TimeInterval.secondsPerMinute
}
/// - Returns: The `TimeInterval` in seconds.
public var seconds: Double {
return self
}
/// - Returns: The `TimeInterval` in milliseconds.
public var milliseconds: Double {
return self * TimeInterval.millisecondsPerSecond
}
/// - Returns: The `TimeInterval` in microseconds.
public var microseconds: Double {
return self * TimeInterval.microsecondsPerSecond
}
/// - Returns: The `TimeInterval` in nanoseconds.
public var nanoseconds: Double {
return self * TimeInterval.nanosecondsPerSecond
}
// MARK: - Type Methods
/// - Returns: The time in days using the `TimeInterval` type.
public static func days(_ value: Double) -> TimeInterval {
return value * secondsPerDay
}
/// - Returns: The time in hours using the `TimeInterval` type.
public static func hours(_ value: Double) -> TimeInterval {
return value * secondsPerHour
}
/// - Returns: The time in minutes using the `TimeInterval` type.
public static func minutes(_ value: Double) -> TimeInterval {
return value * secondsPerMinute
}
/// - Returns: The time in seconds using the `TimeInterval` type.
public static func seconds(_ value: Double) -> TimeInterval {
return value
}
/// - Returns: The time in milliseconds using the `TimeInterval` type.
public static func milliseconds(_ value: Double) -> TimeInterval {
return value / millisecondsPerSecond
}
/// - Returns: The time in microseconds using the `TimeInterval` type.
public static func microseconds(_ value: Double) -> TimeInterval {
return value / microsecondsPerSecond
}
/// - Returns: The time in nanoseconds using the `TimeInterval` type.
public static func nanoseconds(_ value: Double) -> TimeInterval {
return value / nanosecondsPerSecond
}
}
//: ## Solution Example
// property example
let pollingInterval3: TimeInterval = .seconds(5)
// method example
func animate3(duration: TimeInterval = .seconds(20), animations: () -> Void) {
// do something
}
// method usage example
animate3(duration: .milliseconds(500), animations: { /* do something */ })
let intervalThreshold3: TimeInterval = .hours(6)
//: ## One more thing...
let timeInterval: TimeInterval = 60 * 60 * 6
timeInterval.days // => 0.25
timeInterval.hours // => 6
timeInterval.minutes // => 360
timeInterval.seconds // => 21600
timeInterval.milliseconds // => 21600000
timeInterval.microseconds // => 21600000000
timeInterval.nanoseconds // => 21600000000000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment