Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. import UIKit
  2.  
  3. // Requires iOS 11+ / macOS 10.13+
  4. extension ISO8601DateFormatter {
  5. static var ISO8601WithFractionalSeconds: ISO8601DateFormatter {
  6. let formatter = ISO8601DateFormatter()
  7. formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
  8. return formatter
  9. }
  10.  
  11. static var ISO8601NoFractionalSeconds: ISO8601DateFormatter {
  12. let formatter = ISO8601DateFormatter()
  13. formatter.formatOptions = [.withInternetDateTime]
  14. return formatter
  15. }
  16. }
  17.  
  18. /*
  19. * You can create a single parser function to handle datetimes with and without fractional seconds
  20. */
  21. func getDateFrom(from dateString: String) -> Date? {
  22. if let date = ISO8601DateFormatter.ISO8601WithFractionalSeconds.date(from: dateString) {
  23. return date
  24. }
  25. if let date = ISO8601DateFormatter.ISO8601NoFractionalSeconds.date(from: dateString) {
  26. return date
  27. }
  28. return nil
  29. }
  30.  
  31. func timeit(task: () -> Date?) -> Date? {
  32. let start = DispatchTime.now()
  33. let date = task()
  34. let end = DispatchTime.now()
  35. let dt = Double(end.uptimeNanoseconds - start.uptimeNanoseconds) / 1_000_000
  36. print("Runtime: \(dt) ms")
  37. return date
  38. }
  39.  
  40. let datetimeWithoutFractional = "2019-09-30T12:34:56Z"
  41. let datetimeWithFractional = "2019-09-30T12:34:56.789Z"
  42.  
  43. print("Parsing fractional seconds...")
  44. let parsedWithFractional = timeit { getDateFrom(from: datetimeWithFractional) }
  45. print("Result: \(parsedWithFractional)")
  46.  
  47. print("Parsing no fractional seconds...")
  48. let parsedWithoutFractional = timeit { getDateFrom(from: datetimeWithoutFractional) }
  49. print("Result: \(parsedWithoutFractional)")
  50.  
  51. /*
  52. * Results of a few runs... from a 2017 MacBook Pro (13 inch)
  53. *
  54. * Parsing fractional seconds...
  55. * Runtime: 36.45441 ms
  56. * Result: Optional(2019-09-30 12:34:56 +0000)
  57. * Parsing no fractional seconds...
  58. * Runtime: 1.881623 ms
  59. * Result: Optional(2019-09-30 12:34:56 +0000)
  60. *
  61. * Parsing fractional seconds...
  62. * Runtime: 38.708586 ms
  63. * Result: Optional(2019-09-30 12:34:56 +0000)
  64. * Parsing no fractional seconds...
  65. * Runtime: 1.904698 ms
  66. * Result: Optional(2019-09-30 12:34:56 +0000)
  67. *
  68. * Parsing fractional seconds...
  69. * Runtime: 36.550756 ms
  70. * Result: Optional(2019-09-30 12:34:56 +0000)
  71. * Parsing no fractional seconds...
  72. * Runtime: 2.093251 ms
  73. * Result: Optional(2019-09-30 12:34:56 +0000)
  74. *
  75. * Parsing fractional seconds...
  76. * Runtime: 35.279147 ms
  77. * Result: Optional(2019-09-30 12:34:56 +0000)
  78. * Parsing no fractional seconds...
  79. * Runtime: 1.863788 ms
  80. * Result: Optional(2019-09-30 12:34:56 +0000)
  81. *
  82. * Parsing fractional seconds...
  83. * Runtime: 37.178746 ms
  84. * Result: Optional(2019-09-30 12:34:56 +0000)
  85. * Parsing no fractional seconds...
  86. * Runtime: 1.925432 ms
  87. * Result: Optional(2019-09-30 12:34:56 +0000)
  88. *
  89. * Average results:
  90. * With fractional: ~37 ms
  91. * Without fractional: ~2 ms
  92. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement