Last active
September 30, 2019 16:00
-
-
Save ccampo133/3f0e26552d67d6eaded7a43b1e6c6b3d to your computer and use it in GitHub Desktop.
DateTime Parsing Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
// Requires iOS 11+ / macOS 10.13+ | |
extension ISO8601DateFormatter { | |
static var ISO8601WithFractionalSeconds: ISO8601DateFormatter { | |
let formatter = ISO8601DateFormatter() | |
formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds] | |
return formatter | |
} | |
static var ISO8601NoFractionalSeconds: ISO8601DateFormatter { | |
let formatter = ISO8601DateFormatter() | |
formatter.formatOptions = [.withInternetDateTime] | |
return formatter | |
} | |
} | |
/* | |
* You can create a single parser function to handle datetimes with and without fractional seconds | |
*/ | |
func getDateFrom(from dateString: String) -> Date? { | |
if let date = ISO8601DateFormatter.ISO8601WithFractionalSeconds.date(from: dateString) { | |
return date | |
} | |
if let date = ISO8601DateFormatter.ISO8601NoFractionalSeconds.date(from: dateString) { | |
return date | |
} | |
return nil | |
} | |
func timeit(task: () -> Date?) -> Date? { | |
let start = DispatchTime.now() | |
let date = task() | |
let end = DispatchTime.now() | |
let dt = Double(end.uptimeNanoseconds - start.uptimeNanoseconds) / 1_000_000 | |
print("Runtime: \(dt) ms") | |
return date | |
} | |
let datetimeWithoutFractional = "2019-09-30T12:34:56Z" | |
let datetimeWithFractional = "2019-09-30T12:34:56.789Z" | |
print("Parsing fractional seconds...") | |
let parsedWithFractional = timeit { getDateFrom(from: datetimeWithFractional) } | |
print("Result: \(parsedWithFractional)") | |
print("Parsing no fractional seconds...") | |
let parsedWithoutFractional = timeit { getDateFrom(from: datetimeWithoutFractional) } | |
print("Result: \(parsedWithoutFractional)") | |
/* | |
* Results of a few runs... from a 2017 MacBook Pro (13 inch) | |
* | |
* Parsing fractional seconds... | |
* Runtime: 36.45441 ms | |
* Result: Optional(2019-09-30 12:34:56 +0000) | |
* Parsing no fractional seconds... | |
* Runtime: 1.881623 ms | |
* Result: Optional(2019-09-30 12:34:56 +0000) | |
* | |
* Parsing fractional seconds... | |
* Runtime: 38.708586 ms | |
* Result: Optional(2019-09-30 12:34:56 +0000) | |
* Parsing no fractional seconds... | |
* Runtime: 1.904698 ms | |
* Result: Optional(2019-09-30 12:34:56 +0000) | |
* | |
* Parsing fractional seconds... | |
* Runtime: 36.550756 ms | |
* Result: Optional(2019-09-30 12:34:56 +0000) | |
* Parsing no fractional seconds... | |
* Runtime: 2.093251 ms | |
* Result: Optional(2019-09-30 12:34:56 +0000) | |
* | |
* Parsing fractional seconds... | |
* Runtime: 35.279147 ms | |
* Result: Optional(2019-09-30 12:34:56 +0000) | |
* Parsing no fractional seconds... | |
* Runtime: 1.863788 ms | |
* Result: Optional(2019-09-30 12:34:56 +0000) | |
* | |
* Parsing fractional seconds... | |
* Runtime: 37.178746 ms | |
* Result: Optional(2019-09-30 12:34:56 +0000) | |
* Parsing no fractional seconds... | |
* Runtime: 1.925432 ms | |
* Result: Optional(2019-09-30 12:34:56 +0000) | |
* | |
* Average results: | |
* With fractional: ~37 ms | |
* Without fractional: ~2 ms | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment