Merge branch 'development'
This commit is contained in:
commit
8c0ee65de7
19
README.md
19
README.md
|
@ -9,9 +9,7 @@ Its prototype is [YOCKOW's Gist](https://gist.github.com/YOCKOW/12d9607cb30f40b7
|
||||||
|
|
||||||
## Class, Structure, Enumeration
|
## Class, Structure, Enumeration
|
||||||
```
|
```
|
||||||
public struct TimeSpecification: Comparable,
|
public struct TimeSpecification {
|
||||||
ExpressibleByIntegerLiteral,
|
|
||||||
ExpressibleByFloatLiteral {
|
|
||||||
public var seconds:Int64 = 0
|
public var seconds:Int64 = 0
|
||||||
public var nanoseconds:Int32 = 0
|
public var nanoseconds:Int32 = 0
|
||||||
/* ... */
|
/* ... */
|
||||||
|
@ -20,7 +18,7 @@ public enum Clock {
|
||||||
case Calendar
|
case Calendar
|
||||||
case System
|
case System
|
||||||
|
|
||||||
public func timeSpecification() -> TimeSpecification? {
|
public var timeSpecification: TimeSpecification? {
|
||||||
/* ... */
|
/* ... */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,13 +33,12 @@ Then, you can use it in your project:
|
||||||
# Sample Code
|
# Sample Code
|
||||||
```
|
```
|
||||||
import TimeSpecification
|
import TimeSpecification
|
||||||
let start = Clock.System.timeSpecification()
|
|
||||||
// your code
|
|
||||||
let end = Clock.System.timeSpecification()
|
|
||||||
|
|
||||||
if start != nil && end != nil {
|
func time(_ body:() -> Void) {
|
||||||
let duration = end! - start!
|
guard let start = Clock.System.timeSpecification else { return }
|
||||||
// For example, duration == TimeSpecification(seconds:0, nanoseconds:100)
|
body()
|
||||||
print("\(duration)")
|
guard let end = Clock.System.timeSpecification else { return }
|
||||||
|
let duration = end - start
|
||||||
|
print("\(duration)")
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
|
@ -16,9 +16,7 @@
|
||||||
// UNKNOWN OS
|
// UNKNOWN OS
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
public struct TimeSpecification: Comparable,
|
public struct TimeSpecification {
|
||||||
ExpressibleByIntegerLiteral,
|
|
||||||
ExpressibleByFloatLiteral {
|
|
||||||
public var seconds:Int64 = 0
|
public var seconds:Int64 = 0
|
||||||
public var nanoseconds:Int32 = 0 {
|
public var nanoseconds:Int32 = 0 {
|
||||||
didSet { self.normalize() }
|
didSet { self.normalize() }
|
||||||
|
@ -45,20 +43,22 @@ public struct TimeSpecification: Comparable,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Comparable */
|
/// Comparable
|
||||||
public func ==(lhs:TimeSpecification, rhs:TimeSpecification) -> Bool {
|
extension TimeSpecification: Comparable {
|
||||||
return (lhs.seconds == rhs.seconds && lhs.nanoseconds == rhs.nanoseconds) ? true : false
|
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 }
|
/// ExpressibleByIntegerLiteral
|
||||||
if lhs.seconds > rhs.seconds { return false }
|
extension TimeSpecification: ExpressibleByIntegerLiteral {
|
||||||
// then, in the case of (lhs.seconds == rhs.seconds)
|
|
||||||
if (lhs.nanoseconds < rhs.nanoseconds) { return true }
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ExpressibleByIntegerLiteral */
|
|
||||||
extension TimeSpecification {
|
|
||||||
public typealias IntegerLiteralType = Int64
|
public typealias IntegerLiteralType = Int64
|
||||||
public init(integerLiteral value:Int64) {
|
public init(integerLiteral value:Int64) {
|
||||||
self.seconds = value
|
self.seconds = value
|
||||||
|
@ -66,8 +66,8 @@ extension TimeSpecification {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ExpressibleByFloatLiteral */
|
/// ExpressibleByFloatLiteral
|
||||||
extension TimeSpecification {
|
extension TimeSpecification: ExpressibleByFloatLiteral {
|
||||||
public typealias FloatLiteralType = Double
|
public typealias FloatLiteralType = Double
|
||||||
public init(floatLiteral value:Double) {
|
public init(floatLiteral value:Double) {
|
||||||
self.seconds = Int64(floor(value))
|
self.seconds = Int64(floor(value))
|
||||||
|
@ -75,24 +75,26 @@ extension TimeSpecification {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Sum And Difference */
|
/// Sum And Difference
|
||||||
public func + (lhs:TimeSpecification, rhs:TimeSpecification) -> TimeSpecification {
|
extension TimeSpecification {
|
||||||
var result = lhs
|
public static func +(lhs:TimeSpecification, rhs:TimeSpecification) -> TimeSpecification {
|
||||||
result.seconds += rhs.seconds
|
var result = lhs
|
||||||
result.nanoseconds += rhs.nanoseconds // always normalized
|
result.seconds += rhs.seconds
|
||||||
return result
|
result.nanoseconds += rhs.nanoseconds // always normalized
|
||||||
}
|
return result
|
||||||
public func - (lhs:TimeSpecification, rhs:TimeSpecification) -> TimeSpecification {
|
}
|
||||||
var result = lhs
|
public static func -(lhs:TimeSpecification, rhs:TimeSpecification) -> TimeSpecification {
|
||||||
result.seconds -= rhs.seconds
|
var result = lhs
|
||||||
result.nanoseconds -= rhs.nanoseconds // always normalized
|
result.seconds -= rhs.seconds
|
||||||
return result
|
result.nanoseconds -= rhs.nanoseconds // always normalized
|
||||||
}
|
return result
|
||||||
public func += (lhs:inout TimeSpecification, rhs:TimeSpecification) {
|
}
|
||||||
lhs = lhs + rhs // always normalized
|
public static func +=(lhs:inout TimeSpecification, rhs:TimeSpecification) {
|
||||||
}
|
lhs = lhs + rhs // always normalized
|
||||||
public 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 */
|
/* Clock */
|
||||||
|
@ -106,7 +108,7 @@ public enum Clock {
|
||||||
case Calendar
|
case Calendar
|
||||||
case System
|
case System
|
||||||
|
|
||||||
public func timeSpecification() -> TimeSpecification? {
|
public var timeSpecification: TimeSpecification? {
|
||||||
var c_timespec:CTimeSpec = CTimeSpec(tv_sec:0, tv_nsec:0)
|
var c_timespec:CTimeSpec = CTimeSpec(tv_sec:0, tv_nsec:0)
|
||||||
|
|
||||||
let clock_id:CInt
|
let clock_id:CInt
|
||||||
|
@ -115,11 +117,11 @@ public enum Clock {
|
||||||
#if os(Linux)
|
#if os(Linux)
|
||||||
clock_id = (self == .Calendar) ? CLOCK_REALTIME : CLOCK_MONOTONIC
|
clock_id = (self == .Calendar) ? CLOCK_REALTIME : CLOCK_MONOTONIC
|
||||||
retval = clock_gettime(clock_id, &c_timespec)
|
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
|
var clock_name: clock_serv_t = 0
|
||||||
clock_id = (self == .Calendar) ? CALENDAR_CLOCK : SYSTEM_CLOCK
|
clock_id = (self == .Calendar) ? CALENDAR_CLOCK : SYSTEM_CLOCK
|
||||||
retval = host_get_clock_service(mach_host_self(), clock_id, &clock_name)
|
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)
|
retval = clock_get_time(clock_name, &c_timespec)
|
||||||
_ = mach_port_deallocate(mach_task_self(), clock_name)
|
_ = mach_port_deallocate(mach_task_self(), clock_name)
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue