Fix to pass time stamp to initializer

This commit is contained in:
Yasuhiro Hatta 2019-01-11 00:51:57 +09:00
parent 1f2b6ad74f
commit 9d45ad0a3a
1 changed files with 4 additions and 4 deletions

View File

@ -11,9 +11,9 @@ public struct ULID {
public let ulidString: String
public init() {
let now = UInt64(Date().timeIntervalSince1970 * 1000.0)
let timestamp = withUnsafePointer(to: now.bigEndian) { (pointer) -> String in
public init(timestamp: Date = Date()) {
let now = UInt64(timestamp.timeIntervalSince1970 * 1000.0)
let timestampBase32 = withUnsafePointer(to: now.bigEndian) { (pointer) -> String in
var data = Data(bytes: pointer, count: 8)
data.insert(contentsOf: [0x00, 0x00], at: 0)
return String(data.base32EncodedString().suffix(10))
@ -21,7 +21,7 @@ public struct ULID {
let randomness = (0 ..< 16).map { _ in
Base32.crockfordsEncodingTable[Int.random(in: 0 ..< Base32.crockfordsEncodingTable.count)]
}
self.ulidString = timestamp + String(randomness)
self.ulidString = timestampBase32 + String(randomness)
}
public init(ulidString string: String) {