@@ -3,13 +3,6 @@ import Foundation
3
3
import Nimble
4
4
5
5
extension BSONObjectID {
6
- // timestamp
7
- internal var timestamp : Int {
8
- var value = Int ( )
9
- _ = withUnsafeMutableBytes ( of: & value) { self . oid [ 0 ..< 4 ] . reversed ( ) . copyBytes ( to: $0) }
10
- return value
11
- }
12
-
13
6
// random value
14
7
internal var randomValue : Int {
15
8
var value = Int ( )
@@ -47,8 +40,14 @@ final class BSONObjectIDTests: BSONTestCase {
47
40
}
48
41
49
42
func testFieldAccessors( ) throws {
50
- let oid = try BSONObjectID ( " FEEEEEEEFBBBBBBBBBFAAAAA " )
51
- expect ( oid. timestamp) . to ( equal ( 0xFEEE_EEEE ) )
43
+ let format = DateFormatter ( )
44
+ format. dateFormat = " yyyy-MM-dd HH:mm:ss "
45
+ format. timeZone = TimeZone ( secondsFromGMT: 0 )
46
+ let timestamp = format. date ( from: " 2020-07-09 16:22:52 " )
47
+ // 5F07445 is the hex string for the above date
48
+ let oid = try BSONObjectID ( " 5F07445CFBBBBBBBBBFAAAAA " )
49
+
50
+ expect ( oid. timestamp) . to ( equal ( timestamp) )
52
51
expect ( oid. randomValue) . to ( equal ( 0xFB_BBBB_BBBB ) )
53
52
expect ( oid. counter) . to ( equal ( 0xFAAAAA ) )
54
53
}
@@ -62,13 +61,11 @@ final class BSONObjectIDTests: BSONTestCase {
62
61
}
63
62
64
63
func testTimestampCreation( ) throws {
65
- let id = BSONObjectID ( )
66
- let dateFromID = Date ( timeIntervalSince1970 : TimeInterval ( id . timestamp) )
64
+ let oid = BSONObjectID ( )
65
+ let dateFromID = oid . timestamp
67
66
let date = Date ( )
68
-
69
67
let format = DateFormatter ( )
70
- // should be ok to say the timestamps are within the same second but just to be safe, omit seconds
71
- format. dateFormat = " yyyy-MM-dd HH:mm "
68
+ format. dateFormat = " yyyy-MM-dd HH:mm:ss "
72
69
73
70
expect ( format. string ( from: dateFromID) ) . to ( equal ( format. string ( from: date) ) )
74
71
}
0 commit comments