From f1614025ea60725ffb7cbf2daa9b6c5056fdebfc Mon Sep 17 00:00:00 2001 From: YOCKOW Date: Mon, 4 Sep 2017 16:48:33 +0900 Subject: [PATCH] `timeSpecification` of `Clock` is now a computed property. --- README.md | 19 ++++---- Sources/TimeSpecification.swift | 80 +++++++++++++++++---------------- 2 files changed, 49 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index f9a1f4a..f1f0913 100644 --- a/README.md +++ b/README.md @@ -9,9 +9,7 @@ Its prototype is [YOCKOW's Gist](https://gist.github.com/YOCKOW/12d9607cb30f40b7 ## Class, Structure, Enumeration ``` -public struct TimeSpecification: Comparable, - ExpressibleByIntegerLiteral, - ExpressibleByFloatLiteral { +public struct TimeSpecification { public var seconds:Int64 = 0 public var nanoseconds:Int32 = 0 /* ... */ @@ -20,7 +18,7 @@ public enum Clock { case Calendar case System - public func timeSpecification() -> TimeSpecification? { + public var timeSpecification: TimeSpecification? { /* ... */ } } @@ -35,13 +33,12 @@ Then, you can use it in your project: # Sample Code ``` import TimeSpecification -let start = Clock.System.timeSpecification() -// your code -let end = Clock.System.timeSpecification() -if start != nil && end != nil { - let duration = end! - start! - // For example, duration == TimeSpecification(seconds:0, nanoseconds:100) - print("\(duration)") +func time(_ body:() -> Void) { + guard let start = Clock.System.timeSpecification else { return } + body() + guard let end = Clock.System.timeSpecification else { return } + let duration = end - start + print("\(duration)") } ``` diff --git a/Sources/TimeSpecification.swift b/Sources/TimeSpecification.swift index 90b1383..e0b7928 100644 --- a/Sources/TimeSpecification.swift +++ b/Sources/TimeSpecification.swift @@ -16,9 +16,7 @@ // UNKNOWN OS #endif -public struct TimeSpecification: Comparable, - ExpressibleByIntegerLiteral, - ExpressibleByFloatLiteral { +public struct TimeSpecification { public var seconds:Int64 = 0 public var nanoseconds:Int32 = 0 { didSet { self.normalize() } @@ -45,20 +43,22 @@ public struct TimeSpecification: Comparable, } } -/* Comparable */ -public func ==(lhs:TimeSpecification, rhs:TimeSpecification) -> Bool { - return (lhs.seconds == rhs.seconds && lhs.nanoseconds == rhs.nanoseconds) ? true : false +/// Comparable +extension TimeSpecification: Comparable { + public static func ==(lhs:TimeSpecification, rhs:TimeSpecification) -> Bool { + return (lhs.seconds == rhs.seconds && lhs.nanoseconds == rhs.nanoseconds) ? true : false + } + public static func <(lhs:TimeSpecification, rhs:TimeSpecification) -> Bool { + if lhs.seconds < rhs.seconds { return true } + if lhs.seconds > rhs.seconds { return false } + // then, in the case of (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, in the case of (lhs.seconds == rhs.seconds) - if (lhs.nanoseconds < rhs.nanoseconds) { return true } - return false -} - -/* ExpressibleByIntegerLiteral */ -extension TimeSpecification { + +/// ExpressibleByIntegerLiteral +extension TimeSpecification: ExpressibleByIntegerLiteral { public typealias IntegerLiteralType = Int64 public init(integerLiteral value:Int64) { self.seconds = value @@ -66,8 +66,8 @@ extension TimeSpecification { } } -/* ExpressibleByFloatLiteral */ -extension TimeSpecification { +/// ExpressibleByFloatLiteral +extension TimeSpecification: ExpressibleByFloatLiteral { public typealias FloatLiteralType = Double public init(floatLiteral value:Double) { self.seconds = Int64(floor(value)) @@ -75,24 +75,26 @@ extension TimeSpecification { } } -/* Sum And Difference */ -public func + (lhs:TimeSpecification, rhs:TimeSpecification) -> TimeSpecification { - var result = lhs - result.seconds += rhs.seconds - result.nanoseconds += rhs.nanoseconds // always normalized - return result -} -public func - (lhs:TimeSpecification, rhs:TimeSpecification) -> TimeSpecification { - var result = lhs - result.seconds -= rhs.seconds - result.nanoseconds -= rhs.nanoseconds // always normalized - return result -} -public func += (lhs:inout TimeSpecification, rhs:TimeSpecification) { - lhs = lhs + rhs // always normalized -} -public func -= (lhs:inout TimeSpecification, rhs:TimeSpecification) { - lhs = lhs - rhs // always normalized +/// Sum And Difference +extension TimeSpecification { + public static func +(lhs:TimeSpecification, rhs:TimeSpecification) -> TimeSpecification { + var result = lhs + result.seconds += rhs.seconds + result.nanoseconds += rhs.nanoseconds // always normalized + return result + } + public static func -(lhs:TimeSpecification, rhs:TimeSpecification) -> TimeSpecification { + var result = lhs + result.seconds -= rhs.seconds + result.nanoseconds -= rhs.nanoseconds // always normalized + return result + } + public static func +=(lhs:inout TimeSpecification, rhs:TimeSpecification) { + lhs = lhs + rhs // always normalized + } + public static func -=(lhs:inout TimeSpecification, rhs:TimeSpecification) { + lhs = lhs - rhs // always normalized + } } /* Clock */ @@ -106,7 +108,7 @@ public enum Clock { case Calendar case System - public func timeSpecification() -> TimeSpecification? { + public var timeSpecification: TimeSpecification? { var c_timespec:CTimeSpec = CTimeSpec(tv_sec:0, tv_nsec:0) let clock_id:CInt @@ -115,11 +117,11 @@ public enum Clock { #if os(Linux) clock_id = (self == .Calendar) ? CLOCK_REALTIME : CLOCK_MONOTONIC retval = clock_gettime(clock_id, &c_timespec) - #elseif os(OSX) || os(iOS) || os(watchOS) || os(tvOS) + #elseif os(macOS) || os(iOS) || os(watchOS) || os(tvOS) var clock_name: clock_serv_t = 0 clock_id = (self == .Calendar) ? CALENDAR_CLOCK : SYSTEM_CLOCK retval = host_get_clock_service(mach_host_self(), clock_id, &clock_name) - if retval != 0 { return nil } + guard retval == 0 else { return nil } retval = clock_get_time(clock_name, &c_timespec) _ = mach_port_deallocate(mach_task_self(), clock_name) #endif