TimeUnit

public struct TimeUnit

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 use TimeUnit 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.second

    Declaration

    Swift

    public static let oneSecond: TimeUnit
  • One minute. Equivalent to 1 x Calendar.Component.minute

    Declaration

    Swift

    public static let oneMinute: TimeUnit
  • One hour. Equivalent to 1 x Calendar.Component.hour

    Declaration

    Swift

    public static let oneHour: TimeUnit
  • TimeUnit amount of hours.

    One hour is 1 x Calendar.Component.hour

    let twoHours = TimeUnit.hours(2)
    // or
    let twoHours: TimeUnit = .hours(2)
    

    Declaration

    Swift

    public static func hours(_ value: Int) -> TimeUnit

    Parameters

    value

    Amount of hours in this TimeUnit

    Return Value

    A TimeUnit with the defined number of hours.

  • TimeUnit amount of minutes.

    One minute is 1 x Calendar.Component.minute

    let fiveMinutes = TimeUnit.minutes(5)
    // or
    let fiveMinutes: TimeUnit = .minutes(5)
    

    Declaration

    Swift

    public static func minutes(_ value: Int) -> TimeUnit

    Parameters

    value

    Amount of minutes in this TimeUnit

    Return Value

    A TimeUnit with the defined number of minutes.

  • TimeUnit amount of seconds.

    One second is 1 x Calendar.Component.seconds

    let thirtySeconds = TimeUnit.seconds(30)
    // or
    let thirtySeconds: TimeUnit = .seconds(30)
    

    Declaration

    Swift

    public static func seconds(_ value: Int) -> TimeUnit

    Parameters

    value

    Amount of seconds in this TimeUnit

    Return Value

    A TimeUnit with the defined number of seconds.

  • TimeUnit amount of milliseconds.

    One second is 1 x Calendar.Component.nanosecond * NSEC_PER_MSEC

    let oneMillisecond = TimeUnit.milliseconds(1)
    // or
    let oneMillisecond: TimeUnit = .milliseconds(1)
    

    Declaration

    Swift

    public static func milliseconds(_ value: Int) -> TimeUnit

    Parameters

    value

    Amount of milliseconds in this TimeUnit

    Return Value

    A TimeUnit with the defined number of milliseconds.

  • TimeUnit amount of nanoseconds.

    One second is 1 x Calendar.Component.nanosecond

    let tenNanoseconds = TimeUnit.nanoseconds(10)
    // or
    let tenNanoseconds: TimeUnit = .nanoseconds(10)
    

    Declaration

    Swift

    public static func nanoseconds(_ value: Int) -> TimeUnit

    Parameters

    value

    Amount of nanoseconds in this TimeUnit

    Return Value

    A TimeUnit with the defined number of nanoseconds.