TimeUnit
public struct TimeUnit : @unchecked Sendable
TimeUnit for use in adding and subtracting units from Temporal.DateTime, and Temporal.Time.
TimeUnit uses Calendar.Component under the hood to ensure date oddities are accounted for.
let twoHoursFromNow = Temporal.Time.now() + .hours(1)
let fiveMinutesAgo = Temporal.Time.now() - .minutes(5)
let yesterday = Temporal.Date.now() - .days(1)
let sixMonthsAgo = Temporal.Date.now() - .months(6)
Attention
Don’t useTimeUnit to calculate dates, use DateUnit instead.
Also make sure to use the most applicable Unit, e.g. don’t use .minutes(60) if you really want .hours(1).
There are not always 24 hours in a day, 60 minutes in an hour, etc.
-
Declaration
Swift
public let calendarComponent: Calendar.Component -
Declaration
Swift
public let value: Int -
One second. Equivalent to 1 x
Calendar.Component.secondDeclaration
Swift
public static let oneSecond: TimeUnit -
One minute. Equivalent to 1 x
Calendar.Component.minuteDeclaration
Swift
public static let oneMinute: TimeUnit -
One hour. Equivalent to 1 x
Calendar.Component.hourDeclaration
Swift
public static let oneHour: TimeUnit -
TimeUnitamount of hours.One hour is 1 x
Calendar.Component.hourlet twoHours = TimeUnit.hours(2) // or let twoHours: TimeUnit = .hours(2)Declaration
Swift
public static func hours(_ value: Int) -> TimeUnitParameters
valueAmount of hours in this
TimeUnitReturn Value
A
TimeUnitwith the defined number of hours. -
TimeUnitamount of minutes.One minute is 1 x
Calendar.Component.minutelet fiveMinutes = TimeUnit.minutes(5) // or let fiveMinutes: TimeUnit = .minutes(5)Declaration
Swift
public static func minutes(_ value: Int) -> TimeUnitParameters
valueAmount of minutes in this
TimeUnitReturn Value
A
TimeUnitwith the defined number of minutes. -
TimeUnitamount of seconds.One second is 1 x
Calendar.Component.secondslet thirtySeconds = TimeUnit.seconds(30) // or let thirtySeconds: TimeUnit = .seconds(30)Declaration
Swift
public static func seconds(_ value: Int) -> TimeUnitParameters
valueAmount of seconds in this
TimeUnitReturn Value
A
TimeUnitwith the defined number of seconds. -
TimeUnitamount of milliseconds.One second is 1 x
Calendar.Component.nanosecond*NSEC_PER_MSEClet oneMillisecond = TimeUnit.milliseconds(1) // or let oneMillisecond: TimeUnit = .milliseconds(1)Declaration
Swift
public static func milliseconds(_ value: Int) -> TimeUnitParameters
valueAmount of milliseconds in this
TimeUnitReturn Value
A
TimeUnitwith the defined number of milliseconds. -
TimeUnitamount of nanoseconds.One second is 1 x
Calendar.Component.nanosecondlet tenNanoseconds = TimeUnit.nanoseconds(10) // or let tenNanoseconds: TimeUnit = .nanoseconds(10)Declaration
Swift
public static func nanoseconds(_ value: Int) -> TimeUnitParameters
valueAmount of nanoseconds in this
TimeUnitReturn Value
A
TimeUnitwith the defined number of nanoseconds.
View on GitHub