Skip to content

Commit

Permalink
fix epoch
Browse files Browse the repository at this point in the history
  • Loading branch information
lgaches committed Feb 6, 2017
1 parent 433dfc4 commit 61cc261
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
13 changes: 8 additions & 5 deletions Sources/ObjectId.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,15 @@ public struct ObjectId {
}

public var epochSeconds: Int32 {
return _storage[0...3].makeInt32()
let timeData = Data(bytes: _storage[0...3])
return Int32(bigEndian:timeData.withUnsafeBytes { $0.pointee } )
}



public var epoch: Date {
let epoch = _storage[0...3].makeInt32()

let timeData = Data(bytes: _storage[0...3])
let epoch = UInt32(bigEndian: timeData.withUnsafeBytes { $0.pointee } )

return Date(timeIntervalSince1970: Double(epoch))
}
}
Expand All @@ -146,7 +149,7 @@ extension ObjectId: Hashable {
}

public var hashValue: Int {
let epoch = _storage[0...3].makeInt32()
let epoch = self.epochSeconds
let random = _storage[4...7].makeInt32()
let increment = _storage[8...11].makeInt32()

Expand Down
17 changes: 16 additions & 1 deletion Tests/BSONTests/BSONPublicTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,22 @@ final class BSONPublicTests: XCTestCase {

XCTAssertLessThan(timeId.epoch.timeIntervalSinceNow, 2)
}


func testObjectIdString() throws {
let stringId = try ObjectId("589488560239f4563ddc6ca0")
XCTAssertEqual(stringId.epochSeconds, 1486129238)
XCTAssertEqual(stringId.hexString, "589488560239f4563ddc6ca0")
XCTAssertEqual(stringId.epoch.timeIntervalSince1970, 1486129238)
}

func testObjectIdHash() throws {
let firstId = try ObjectId("589488560239f4563ddc6ca0")
let secondId = try ObjectId("589488560239f4563ddc6ca0")
let thirdId = try ObjectId("589488560239f4563ddc6cab")
XCTAssertEqual(firstId.hashValue, secondId.hashValue)
XCTAssertNotEqual(firstId.hashValue, thirdId.hashValue)
}

func testExtendedJSON() throws {

let simpleJson = "{\"kaas\": 4.2}"
Expand Down

0 comments on commit 61cc261

Please sign in to comment.