Fix issues about building on linux
This commit is contained in:
parent
41262719dd
commit
6a9e2ba863
|
@ -102,6 +102,16 @@ extension Interval {
|
|||
self.init(nanoseconds: seconds * pow(10, 9))
|
||||
}
|
||||
|
||||
/// The length of this interval in microseconds.
|
||||
public var microseconds: Double {
|
||||
return nanoseconds / pow(10, 3)
|
||||
}
|
||||
|
||||
/// The length of this interval in milliseconds.
|
||||
public var milliseconds: Double {
|
||||
return nanoseconds / pow(10, 6)
|
||||
}
|
||||
|
||||
/// The length of this interval in seconds.
|
||||
public var seconds: Double {
|
||||
return nanoseconds / pow(10, 9)
|
||||
|
|
|
@ -137,9 +137,9 @@ public struct Period {
|
|||
public func tidied(to unit: Unit) -> Period {
|
||||
var period = self
|
||||
if case .nanosecond = unit { return period }
|
||||
if period.nanoseconds.magnitude >= UInt(NSEC_PER_SEC) {
|
||||
period.seconds += period.nanoseconds / Int(NSEC_PER_SEC)
|
||||
period.nanoseconds %= Int(NSEC_PER_SEC)
|
||||
if period.nanoseconds.magnitude >= UInt(1.second.nanoseconds) {
|
||||
period.seconds += period.nanoseconds / Int(1.second.nanoseconds)
|
||||
period.nanoseconds %= Int(1.second.nanoseconds)
|
||||
}
|
||||
|
||||
if case .second = unit { return period }
|
||||
|
|
|
@ -30,10 +30,10 @@ public struct Time {
|
|||
/// Time(hour: 25) => nil
|
||||
/// Time(hour: 1, minute: 61) => nil
|
||||
public init?(hour: Int, minute: Int = 0, second: Int = 0, nanosecond: Int = 0) {
|
||||
guard (0..<24).contains(hour) else { return nil }
|
||||
guard (0..<60).contains(minute) else { return nil }
|
||||
guard (0..<60).contains(second) else { return nil }
|
||||
guard (0..<Int(NSEC_PER_SEC)).contains(nanosecond) else { return nil }
|
||||
guard (0..<24).contains(hour),
|
||||
(0..<60).contains(minute),
|
||||
(0..<60).contains(second),
|
||||
(0..<Int(1.second.nanoseconds)).contains(nanosecond) else { return nil }
|
||||
|
||||
self.hour = hour
|
||||
self.minute = minute
|
||||
|
@ -88,7 +88,7 @@ public struct Time {
|
|||
fmt = fmt.replacingOccurrences(of: "HH", with: "hh")
|
||||
fmt += " a"
|
||||
}
|
||||
var formatter: DateFormatter! = Time.FormatterCache.object(forKey: fmt as NSString)
|
||||
var formatter: DateFormatter! = Time.FormatterCache.object(forKey: NSString(string: fmt))
|
||||
if formatter == nil {
|
||||
formatter = DateFormatter()
|
||||
formatter?.locale = Locale(identifier: "en_US_POSIX")
|
||||
|
@ -97,7 +97,7 @@ public struct Time {
|
|||
formatter.dateFormat = fmt
|
||||
}
|
||||
if let date = formatter.date(from: string) {
|
||||
Time.FormatterCache.setObject(formatter, forKey: fmt as NSString)
|
||||
Time.FormatterCache.setObject(formatter, forKey: NSString(string: fmt))
|
||||
let components = Calendar.gregorian.dateComponents(in: TimeZone.autoupdatingCurrent, from: date)
|
||||
if let hour = components.hour,
|
||||
let minute = components.minute,
|
||||
|
|
|
@ -13,7 +13,7 @@ final class DateTimeTests: XCTestCase {
|
|||
func testInterval() {
|
||||
|
||||
XCTAssertTrue((-1).second.isNegative)
|
||||
XCTAssertEqual(1.1.second.magnitude, 1.1 * Constants.NSEC_PER_SEC)
|
||||
XCTAssertEqual(1.1.second.magnitude, 1.1.second.nanoseconds)
|
||||
|
||||
XCTAssertTrue(1.1.second.isLonger(than: 1.0.second))
|
||||
XCTAssertTrue(3.days.isShorter(than: 1.week))
|
||||
|
@ -26,13 +26,13 @@ final class DateTimeTests: XCTestCase {
|
|||
XCTAssertEqual(-(1.second), (-1).second)
|
||||
|
||||
XCTAssertEqual(1.nanoseconds, Interval(nanoseconds: 1))
|
||||
XCTAssertEqual(2.microseconds, Interval(nanoseconds: 2 * Constants.NSEC_PER_USEC))
|
||||
XCTAssertEqual(3.milliseconds, Interval(nanoseconds: 3 * Constants.NSEC_PER_MSEC))
|
||||
XCTAssertEqual(4.seconds, Interval(nanoseconds: 4 * Constants.NSEC_PER_SEC))
|
||||
XCTAssertEqual(5.1.minutes, Interval(nanoseconds: 5.1 * Constants.NSEC_PER_M))
|
||||
XCTAssertEqual(6.2.hours, Interval(nanoseconds: 6.2 * Constants.NSEC_PER_H))
|
||||
XCTAssertEqual(7.3.days, Interval(nanoseconds: 7.3 * Constants.NSEC_PER_D))
|
||||
XCTAssertEqual(8.4.weeks, Interval(nanoseconds: 8.4 * Constants.NSEC_PER_W))
|
||||
XCTAssertEqual(2.microseconds, Interval(nanoseconds: 2.microseconds.nanoseconds))
|
||||
XCTAssertEqual(3.milliseconds, Interval(nanoseconds: 3.milliseconds.nanoseconds))
|
||||
XCTAssertEqual(4.seconds, Interval(nanoseconds: 4.seconds.nanoseconds))
|
||||
XCTAssertEqual(5.1.minutes, Interval(nanoseconds: 5.1.minutes.nanoseconds))
|
||||
XCTAssertEqual(6.2.hours, Interval(nanoseconds: 6.2.hours.nanoseconds))
|
||||
XCTAssertEqual(7.3.days, Interval(nanoseconds: 7.3.days.nanoseconds))
|
||||
XCTAssertEqual(8.4.weeks, Interval(nanoseconds: 8.4.weeks.nanoseconds))
|
||||
}
|
||||
|
||||
func testTime() {
|
||||
|
@ -45,7 +45,7 @@ final class DateTimeTests: XCTestCase {
|
|||
XCTAssertEqual(t1?.minute, 12)
|
||||
XCTAssertEqual(t1?.second, 13)
|
||||
if let i = t1?.nanosecond.nanoseconds {
|
||||
XCTAssertTrue(i.isAlmostEqual(to: (0.456 * Constants.NSEC_PER_SEC).nanoseconds, leeway: 0.001.seconds))
|
||||
XCTAssertTrue(i.isAlmostEqual(to: (0.456.second.nanoseconds).nanoseconds, leeway: 0.001.seconds))
|
||||
}
|
||||
|
||||
let t2 = Time("11 pm")
|
||||
|
|
|
@ -65,14 +65,3 @@ extension Schedule {
|
|||
return makeIterator().isAlmostEqual(to: schedule.makeIterator(), leeway: leeway)
|
||||
}
|
||||
}
|
||||
|
||||
struct Constants {
|
||||
|
||||
static let NSEC_PER_USEC = Double(Foundation.NSEC_PER_USEC)
|
||||
static let NSEC_PER_MSEC = Double(Foundation.NSEC_PER_MSEC)
|
||||
static let NSEC_PER_SEC = Double(Foundation.NSEC_PER_SEC)
|
||||
static let NSEC_PER_M = Double(Foundation.NSEC_PER_SEC) * 60
|
||||
static let NSEC_PER_H = Double(Foundation.NSEC_PER_SEC) * 60 * 60
|
||||
static let NSEC_PER_D = Double(Foundation.NSEC_PER_SEC) * 60 * 60 * 24
|
||||
static let NSEC_PER_W = Double(Foundation.NSEC_PER_SEC) * 60 * 60 * 24 * 7
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue