Skip to content

Instantly share code, notes, and snippets.

@YOCKOW
Last active March 25, 2024 17:41
Show Gist options
  • Save YOCKOW/12d9607cb30f40b79fb2 to your computer and use it in GitHub Desktop.
Save YOCKOW/12d9607cb30f40b79fb2 to your computer and use it in GitHub Desktop.

Revisions

  1. YOCKOW revised this gist May 28, 2016. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion TimeSpecification.swift
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,8 @@
    /* Now, migrated to GitHub https://github.com/YOCKOW/SwiftTimeSpecification */
    /* *** *** *** *** *** *** *** *** *** *** *** *** ***
    Now, migrated to GitHub:
    https://github.com/YOCKOW/SwiftTimeSpecification
    This gist will not be updated.
    *** *** *** *** *** *** *** *** *** *** *** *** *** */

    /* TimeSpecification.swift
    * © 2016 YOCKOW.
  2. YOCKOW revised this gist May 28, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions TimeSpecification.swift
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    /* Now, migrated to GitHub https://github.com/YOCKOW/SwiftTimeSpecification */

    /* TimeSpecification.swift
    * © 2016 YOCKOW.
    * You can do whatever you want with this code AT YOUR OWN RISK.
  3. YOCKOW revised this gist Mar 18, 2016. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions TimeSpecification.swift
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    /* TimeSpecification.swift
    * © 2016 YOCKOW.
    * You can do whatever you want with this code AT YOUR OWN RISK.
    * © 2016 YOCKOW.
    * You can do whatever you want with this code AT YOUR OWN RISK.
    */

    // <Usage>
    @@ -10,7 +10,7 @@
    // /* ... */
    // }

    // You can play at http://swiftlang.ng.bluemix.net/#/repl/1da55a59c8b035a279b9d93de20330ae21f837c09a5cf28fb00ae7c9441b3464
    // You can play at http://swiftlang.ng.bluemix.net/#/repl/6232922e1f9833c8800ffdbe1e9f6b65f5deb8f78a6ceb8c47caae3444ac3f3f

    #if os(Linux)
    import Glibc
  4. YOCKOW revised this gist Mar 18, 2016. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions TimeSpecification.swift
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    /*
    TimeSpecification.swift
    © 2016 YOCKOW.
    */
    /* TimeSpecification.swift
    * © 2016 YOCKOW.
    * You can do whatever you want with this code AT YOUR OWN RISK.
    */

    // <Usage>
    // let now:TimeSpecification? = Clock.Calendar.timeSpecification()
  5. YOCKOW revised this gist Mar 18, 2016. No changes.
  6. YOCKOW revised this gist Mar 18, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions TimeSpecification.swift
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@
    #if os(Linux)
    import Glibc
    private typealias CTimeSpec = timespec
    #elseif os(OSX) || os(iOS) || os(watchOS) || (tvOS)
    #elseif os(OSX) || os(iOS) || os(watchOS) || os(tvOS)
    import Darwin
    private let mach_task_self:()->mach_port_t = { return mach_task_self_ }
    private typealias CTimeSpec = mach_timespec_t
    @@ -115,7 +115,7 @@ public enum Clock {
    clock_id = (self == .Calendar) ? CLOCK_REALTIME : CLOCK_MONOTONIC
    successful = clock_gettime(clock_id, &result)

    #elseif os(OSX) || os(iOS) || os(watchOS) || (tvOS)
    #elseif os(OSX) || os(iOS) || os(watchOS) || os(tvOS)
    clock_id = (self == .Calendar) ? CALENDAR_CLOCK : SYSTEM_CLOCK

    var clock_name: clock_serv_t = 0
  7. YOCKOW revised this gist Mar 18, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions TimeSpecification.swift
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,8 @@
    // /* ... */
    // }

    // You can play at http://swiftlang.ng.bluemix.net/#/repl/1da55a59c8b035a279b9d93de20330ae21f837c09a5cf28fb00ae7c9441b3464

    #if os(Linux)
    import Glibc
    private typealias CTimeSpec = timespec
  8. YOCKOW revised this gist Mar 18, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion TimeSpecification.swift
    Original file line number Diff line number Diff line change
    @@ -54,7 +54,7 @@ public func + (lhs:TimeSpecification, rhs:TimeSpecification) -> TimeSpecificatio
    return result
    }

    public func - (lhs:TimeSpecification, rhs:HTBTimeSpecification) -> TimeSpecification {
    public func - (lhs:TimeSpecification, rhs:TimeSpecification) -> TimeSpecification {
    var result = lhs
    result.seconds -= rhs.seconds
    result.nanoseconds -= rhs.nanoseconds
  9. YOCKOW revised this gist Mar 18, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion TimeSpecification.swift
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    */

    // <Usage>
    // let now:TimeSpecification? = Clock.Calendar.timespec
    // let now:TimeSpecification? = Clock.Calendar.timeSpecification()
    // if now != nil {
    // let (sec, nsec) = (now!.seconds, now!.nanoseconds)
    // /* ... */
  10. YOCKOW revised this gist Mar 18, 2016. 2 changed files with 130 additions and 84 deletions.
    84 changes: 0 additions & 84 deletions TimeSpec.swift
    Original file line number Diff line number Diff line change
    @@ -1,84 +0,0 @@
    /*
    TimeSpec.swift
    © 2016 YOCKOW.
    */

    // <Usage>
    // let now:TimeSpec? = Clock.Calendar.timespec
    // if now != nil {
    // let (sec, nsec) = (now!.tv_sec, now!.tv_nsec)
    // /* ... */
    // }

    #if os(Linux)
    import Glibc
    public typealias TimeSpec = timespec
    #elseif os(OSX) || os(iOS) || os(watchOS) || (tvOS)
    import Darwin
    private let mach_task_self:()->mach_port_t = { return mach_task_self_ }
    public typealias TimeSpec = mach_timespec_t
    #endif

    extension Double {
    public init(_ ts: TimeSpec) {
    self = Double(ts.tv_nsec) * 1.0E-9 + Double(ts.tv_sec)
    }
    }

    extension TimeSpec {
    public var double:Double {
    return Double(self)
    }
    }

    public func + (left:TimeSpec, right:TimeSpec) -> TimeSpec {
    var result = left
    result.tv_sec += right.tv_sec
    result.tv_nsec += right.tv_nsec
    if (result.tv_nsec >= 1_000_000_000) {
    result.tv_sec += 1
    result.tv_nsec -= 1_000_000_000
    }
    return result
    }

    public func - (left:TimeSpec, right:TimeSpec) -> TimeSpec {
    var result = left
    result.tv_sec -= right.tv_sec
    result.tv_nsec -= right.tv_nsec
    if (result.tv_nsec < 0) {
    result.tv_sec -= 1
    result.tv_nsec += 1_000_000_000
    }
    return result
    }

    public enum Clock {
    case Calendar
    case System

    public var timespec:TimeSpec? {
    var result:TimeSpec = TimeSpec(tv_sec:0, tv_nsec:0)

    let clock_id:CInt
    var return_value:CInt = -1

    #if os(Linux)
    clock_id = (self == .Calendar) ? CLOCK_REALTIME : CLOCK_MONOTONIC
    return_value = clock_gettime(clock_id, &result)

    #elseif os(OSX) || os(iOS) || os(watchOS) || (tvOS)
    clock_id = (self == .Calendar) ? CALENDAR_CLOCK : SYSTEM_CLOCK

    var clock_name: clock_serv_t = 0

    return_value = host_get_clock_service(mach_host_self(), clock_id, &clock_name)
    if return_value != 0 { return nil }

    return_value = clock_get_time(clock_name, &result)
    _ = mach_port_deallocate(mach_task_self(), clock_name)
    #endif

    return (return_value == 0) ? result : nil
    }
    }
    130 changes: 130 additions & 0 deletions TimeSpecification.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,130 @@
    /*
    TimeSpecification.swift
    © 2016 YOCKOW.
    */

    // <Usage>
    // let now:TimeSpecification? = Clock.Calendar.timespec
    // if now != nil {
    // let (sec, nsec) = (now!.seconds, now!.nanoseconds)
    // /* ... */
    // }

    #if os(Linux)
    import Glibc
    private typealias CTimeSpec = timespec
    #elseif os(OSX) || os(iOS) || os(watchOS) || (tvOS)
    import Darwin
    private let mach_task_self:()->mach_port_t = { return mach_task_self_ }
    private typealias CTimeSpec = mach_timespec_t
    #else
    // Unknown OS.
    #endif

    public struct TimeSpecification {
    var seconds:Int64
    var nanoseconds:Int32

    private init(_ cts:CTimeSpec) {
    seconds = Int64(cts.tv_sec)
    nanoseconds = Int32(cts.tv_nsec)
    }
    }

    extension Double {
    public init(_ ts: TimeSpecification) {
    self = Double(ts.nanoseconds) * 1.0E-9 + Double(ts.seconds)
    }
    }

    extension TimeSpecification {
    public func toDouble() -> Double {
    return Double(self)
    }
    }

    public func + (lhs:TimeSpecification, rhs:TimeSpecification) -> TimeSpecification {
    var result = lhs
    result.seconds += rhs.seconds
    result.nanoseconds += rhs.nanoseconds
    if (result.nanoseconds >= 1_000_000_000) {
    result.seconds += 1
    result.nanoseconds -= 1_000_000_000
    }
    return result
    }

    public func - (lhs:TimeSpecification, rhs:HTBTimeSpecification) -> TimeSpecification {
    var result = lhs
    result.seconds -= rhs.seconds
    result.nanoseconds -= rhs.nanoseconds
    if (result.nanoseconds < 0) {
    result.seconds -= 1
    result.nanoseconds += 1_000_000_000
    }
    return result
    }

    public func == (lhs:TimeSpecification, rhs:TimeSpecification) -> Bool {
    if lhs.seconds != rhs.seconds { return false }
    if lhs.nanoseconds != rhs.nanoseconds { return false }
    return true
    }

    public func != (lhs:TimeSpecification, rhs:TimeSpecification) -> Bool {
    return (lhs == rhs) ? false : true
    }

    public func < (lhs:TimeSpecification, rhs:TimeSpecification) -> Bool {
    if lhs.seconds < rhs.seconds { return true }
    if lhs.seconds > rhs.seconds { return false }
    // then, lhs.seconds == rhs.seconds
    if (lhs.nanoseconds < rhs.nanoseconds) { return true }
    return false
    }

    public func > (lhs:TimeSpecification, rhs:TimeSpecification) -> Bool {
    if lhs.seconds > rhs.seconds { return true }
    if lhs.seconds < rhs.seconds { return false }
    // then, lhs.seconds == rhs.seconds
    if (lhs.nanoseconds > rhs.nanoseconds) { return true }
    return false
    }

    public func <= (lhs:TimeSpecification, rhs:TimeSpecification) -> Bool {
    return !(lhs > rhs)
    }

    public func >= (lhs:TimeSpecification, rhs:TimeSpecification) -> Bool {
    return !(lhs < rhs)
    }

    public enum Clock {
    case Calendar
    case System

    public func timeSpecification() -> TimeSpecification? {
    var result:CTimeSpec = CTimeSpec(tv_sec:0, tv_nsec:0)

    let clock_id:CInt
    var successful:CInt = -1

    #if os(Linux)
    clock_id = (self == .Calendar) ? CLOCK_REALTIME : CLOCK_MONOTONIC
    successful = clock_gettime(clock_id, &result)

    #elseif os(OSX) || os(iOS) || os(watchOS) || (tvOS)
    clock_id = (self == .Calendar) ? CALENDAR_CLOCK : SYSTEM_CLOCK

    var clock_name: clock_serv_t = 0

    successful = host_get_clock_service(mach_host_self(), clock_id, &clock_name)
    if successful != 0 { return nil }

    successful = clock_get_time(clock_name, &result)
    _ = mach_port_deallocate(mach_task_self(), clock_name)
    #endif

    return (successful == 0) ? TimeSpecification(result) : nil
    }
    }
  11. YOCKOW revised this gist Mar 12, 2016. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions TimeSpec.swift
    Original file line number Diff line number Diff line change
    @@ -3,14 +3,13 @@
    © 2016 YOCKOW.
    */


    // <Usage>
    // let now:TimeSpec? = Clock.Calendar.timespec
    // if now != nil {
    // let (sec, nsec) = (now!.tv_sec, now!.tv_nsec)
    // /* ... */
    // }


    #if os(Linux)
    import Glibc
    public typealias TimeSpec = timespec
    @@ -61,8 +60,8 @@ public enum Clock {
    public var timespec:TimeSpec? {
    var result:TimeSpec = TimeSpec(tv_sec:0, tv_nsec:0)

    let clock_id:Int32
    var return_value:Int32 = -1
    let clock_id:CInt
    var return_value:CInt = -1

    #if os(Linux)
    clock_id = (self == .Calendar) ? CLOCK_REALTIME : CLOCK_MONOTONIC
  12. YOCKOW renamed this gist Mar 11, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Clock.swift → TimeSpec.swift
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    /*
    Clock.swift
    TimeSpec.swift
    © 2016 YOCKOW.
    */

  13. YOCKOW revised this gist Mar 11, 2016. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions Clock.swift
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,14 @@
    © 2016 YOCKOW.
    */


    // <Usage>
    // let now:TimeSpec? = Clock.Calendar.timespec
    // if now != nil {
    // /* ... */
    // }


    #if os(Linux)
    import Glibc
    public typealias TimeSpec = timespec
  14. YOCKOW created this gist Mar 11, 2016.
    77 changes: 77 additions & 0 deletions Clock.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,77 @@
    /*
    Clock.swift
    © 2016 YOCKOW.
    */

    #if os(Linux)
    import Glibc
    public typealias TimeSpec = timespec
    #elseif os(OSX) || os(iOS) || os(watchOS) || (tvOS)
    import Darwin
    private let mach_task_self:()->mach_port_t = { return mach_task_self_ }
    public typealias TimeSpec = mach_timespec_t
    #endif

    extension Double {
    public init(_ ts: TimeSpec) {
    self = Double(ts.tv_nsec) * 1.0E-9 + Double(ts.tv_sec)
    }
    }

    extension TimeSpec {
    public var double:Double {
    return Double(self)
    }
    }

    public func + (left:TimeSpec, right:TimeSpec) -> TimeSpec {
    var result = left
    result.tv_sec += right.tv_sec
    result.tv_nsec += right.tv_nsec
    if (result.tv_nsec >= 1_000_000_000) {
    result.tv_sec += 1
    result.tv_nsec -= 1_000_000_000
    }
    return result
    }

    public func - (left:TimeSpec, right:TimeSpec) -> TimeSpec {
    var result = left
    result.tv_sec -= right.tv_sec
    result.tv_nsec -= right.tv_nsec
    if (result.tv_nsec < 0) {
    result.tv_sec -= 1
    result.tv_nsec += 1_000_000_000
    }
    return result
    }

    public enum Clock {
    case Calendar
    case System

    public var timespec:TimeSpec? {
    var result:TimeSpec = TimeSpec(tv_sec:0, tv_nsec:0)

    let clock_id:Int32
    var return_value:Int32 = -1

    #if os(Linux)
    clock_id = (self == .Calendar) ? CLOCK_REALTIME : CLOCK_MONOTONIC
    return_value = clock_gettime(clock_id, &result)

    #elseif os(OSX) || os(iOS) || os(watchOS) || (tvOS)
    clock_id = (self == .Calendar) ? CALENDAR_CLOCK : SYSTEM_CLOCK

    var clock_name: clock_serv_t = 0

    return_value = host_get_clock_service(mach_host_self(), clock_id, &clock_name)
    if return_value != 0 { return nil }

    return_value = clock_get_time(clock_name, &result)
    _ = mach_port_deallocate(mach_task_self(), clock_name)
    #endif

    return (return_value == 0) ? result : nil
    }
    }