Skip to content

Commit

Permalink
Added TimeInterval and timespec reciprocal constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
orchetect committed Jan 9, 2021
1 parent 5a108ef commit 1fde9fb
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Sources/OTCore/TimeInterval.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// TimeInterval.swift
// OTCore
//
// Created by Steffan Andrews on 2021-01-09.
// Copyright © 2020 Steffan Andrews. All rights reserved.

import Foundation

extension TimeInterval {

/// OTCore:
/// Convenience constructor from `timespec`
public init(_ time: timespec) {

self = time.doubleValue

}

}
43 changes: 43 additions & 0 deletions Sources/OTCore/Timespec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,49 @@ public func clock_gettime_monotonic_raw() -> timespec {
}


// MARK: - Timespec constructors

extension timespec {

/// OTCore:
/// Convenience constructor from floating point seconds value
public init<T: BinaryFloatingPoint>(seconds floatingPoint: T) {

self.init()

let intVal = Int(floatingPoint * 1_000_000_000)

tv_nsec = intVal % 1_000_000_000

tv_sec = intVal / 1_000_000_000

}

/// OTCore:
/// Convenience constructor from `TimeInterval`
public init(_ interval: TimeInterval) {

self.init(seconds: interval)

}

}


// MARK: - Timespec properties

extension timespec {

/// OTCore:
/// Return a `TimeInterval`
public var doubleValue: TimeInterval {

Double(tv_sec) + (Double(tv_nsec) / 1_000_000_000)

}

}

// MARK: - Timespec operators and comparison

/// OTCore
Expand Down
31 changes: 31 additions & 0 deletions Tests/OTCoreTests/TimeInterval Tests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// TimeInterval Tests.swift
// OTCore
//
// Created by Steffan Andrews on 2021-01-09.
// Copyright © 2021 Steffan Andrews. All rights reserved.
//

import Foundation

#if !os(watchOS)

import XCTest
@testable import OTCore

class TimeInterval_Tests: XCTestCase {

override func setUp() { super.setUp() }
override func tearDown() { super.tearDown() }

func testTimeInterval_init_Timespec() {

let ti = TimeInterval(timespec(tv_sec: 1, tv_nsec: 234_567_891))

XCTAssertEqual(ti, TimeInterval(1.234_567_891))

}

}

#endif
30 changes: 30 additions & 0 deletions Tests/OTCoreTests/Timespec Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,36 @@ class Timespec_Tests: XCTestCase {

}

func testTimespec_inits() {

// (seconds:)

var ts = timespec(seconds: 1.234_567_891)

XCTAssertEqual(ts.tv_sec, 1)
XCTAssertEqual(ts.tv_nsec, 234_567_891)

// (_ interval:)

ts = timespec(TimeInterval(2.987_654_321))

XCTAssertEqual(ts.tv_sec, 2)
XCTAssertEqual(ts.tv_nsec, 987_654_321)

}

func testTimespec_doubleValue() {

var ts = timespec(tv_sec: 1, tv_nsec: 234_567_891)

XCTAssertEqual(ts.doubleValue, 1.234_567_891)

ts = timespec(tv_sec: 2, tv_nsec: 987_654_321)

XCTAssertEqual(ts.doubleValue, 2.987_654_321)

}

func testTimespecOperators() {

// assuming all tv_sec and tv_nsec values are positive integers when forming original timespec()'s
Expand Down

0 comments on commit 1fde9fb

Please sign in to comment.