Skip to content

Foundation/Unit: add {m,u,p,n}second units to UnitDuration #4708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Sources/Foundation/Unit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -627,12 +627,20 @@ public final class UnitDuration : Dimension {
*/

private struct Symbol {
static let picoseconds = "ps"
static let nanoseconds = "ns"
static let microseconds = "µs"
static let milliseconds = "ms"
static let seconds = "s"
static let minutes = "m"
static let hours = "h"
}

private struct Coefficient {
static let picoseconds = 1e-12
static let nanoseconds = 1e-9
static let microseconds = 1e-6
static let milliseconds = 1e-3
static let seconds = 1.0
static let minutes = 60.0
static let hours = 3600.0
Expand All @@ -642,6 +650,30 @@ public final class UnitDuration : Dimension {
self.init(symbol: symbol, converter: UnitConverterLinear(coefficient: coefficient))
}

public class var picoseconds: UnitDuration {
get {
return UnitDuration(symbol: Symbol.picoseconds, coefficient: Coefficient.picoseconds)
}
}

public class var nanoseconds: UnitDuration {
get {
return UnitDuration(symbol: Symbol.nanoseconds, coefficient: Coefficient.nanoseconds)
}
}

public class var microseconds: UnitDuration {
get {
return UnitDuration(symbol: Symbol.microseconds, coefficient: Coefficient.microseconds)
}
}

public class var milliseconds: UnitDuration {
get {
return UnitDuration(symbol: Symbol.milliseconds, coefficient: Coefficient.milliseconds)
}
}

public class var seconds: UnitDuration {
get {
return UnitDuration(symbol: Symbol.seconds, coefficient: Coefficient.seconds)
Expand Down