1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#if canImport(TestSupport)
import TestSupport
#endif
#if canImport(FoundationEssentials)
@testable import FoundationEssentials
#endif
#if FOUNDATION_FRAMEWORK
@testable import Foundation
#endif
final class ISO8601FormatStyleParsingTests: XCTestCase {
/// See also the format-only tests in DateISO8601FormatStyleEssentialsTests
func test_ISO8601Parse() throws {
let iso8601 = Date.ISO8601FormatStyle()
// Date is: "2022-01-28 15:35:46"
XCTAssertEqual(try? iso8601.parse("2022-01-28T15:35:46Z"), Date(timeIntervalSinceReferenceDate: 665076946.0))
var iso8601Pacific = iso8601
iso8601Pacific.timeZone = TimeZone(secondsFromGMT: -3600 * 8)!
XCTAssertEqual(try? iso8601Pacific.timeSeparator(.omitted).parse("2022-01-28T073546-0800"), Date(timeIntervalSinceReferenceDate: 665076946.0))
// Day-only results: the default time is midnight for parsed date when the time piece is missing
// Date is: "2022-01-28 00:00:00"
XCTAssertEqual(try? iso8601.year().month().day().dateSeparator(.dash).parse("2022-01-28"), Date(timeIntervalSinceReferenceDate: 665020800.0))
// Date is: "2022-01-28 00:00:00"
XCTAssertEqual(try? iso8601.year().month().day().dateSeparator(.omitted).parse("20220128"), Date(timeIntervalSinceReferenceDate: 665020800.0))
// Time-only results: we use the default date of the format style, 1970-01-01, to supplement the parsed date without year, month or day
// Date is: "1970-01-23 00:00:00"
XCTAssertEqual(try? iso8601.weekOfYear().day().dateSeparator(.dash).parse("W04-05"), Date(timeIntervalSinceReferenceDate: -976406400.0))
// Date is: "1970-01-28 15:35:46"
XCTAssertEqual(try? iso8601.day().time(includingFractionalSeconds: false).timeSeparator(.colon).parse("028T15:35:46"), Date(timeIntervalSinceReferenceDate: -975918254.0))
// Date is: "1970-01-01 15:35:46"
XCTAssertEqual(try? iso8601.time(includingFractionalSeconds: false).timeSeparator(.colon).parse("15:35:46"), Date(timeIntervalSinceReferenceDate: -978251054.0))
// Date is: "1970-01-01 15:35:46"
XCTAssertEqual(try? iso8601.time(includingFractionalSeconds: false).timeZone(separator: .omitted).parse("15:35:46Z"), Date(timeIntervalSinceReferenceDate: -978251054.0))
// Date is: "1970-01-01 15:35:46"
XCTAssertEqual(try? iso8601.time(includingFractionalSeconds: false).timeZone(separator: .colon).parse("15:35:46Z"), Date(timeIntervalSinceReferenceDate: -978251054.0))
// Date is: "1970-01-01 15:35:46"
XCTAssertEqual(try? iso8601.timeZone(separator: .colon).time(includingFractionalSeconds: false).timeSeparator(.colon).parse("15:35:46Z"), Date(timeIntervalSinceReferenceDate: -978251054.0))
}
func test_weekOfYear() throws {
let iso8601 = Date.ISO8601FormatStyle()
// Test some dates around the 2019 - 2020 end of year, and 2026 which has W53
let dates = [
("2019-W52-07", "2019-12-29"),
("2020-W01-01", "2019-12-30"),
("2020-W01-02", "2019-12-31"),
("2020-W01-03", "2020-01-01"),
("2026-W53-01", "2026-12-28"),
("2026-W53-02", "2026-12-29"),
("2026-W53-03", "2026-12-30"),
("2026-W53-04", "2026-12-31"),
("2026-W53-05", "2027-01-01"),
("2026-W53-06", "2027-01-02"),
("2026-W53-07", "2027-01-03"),
("2027-W01-01", "2027-01-04"),
("2027-W01-02", "2027-01-05")
]
for d in dates {
let parsedWoY = try iso8601.year().weekOfYear().day().parse(d.0)
let parsedY = try iso8601.year().month().day().parse(d.1)
XCTAssertEqual(parsedWoY, parsedY)
}
}
func test_zeroLeadingDigits() {
// The parser allows for an arbitrary number of 0 pads in digits, including none.
let iso8601 = Date.ISO8601FormatStyle()
// Date is: "2022-01-28 15:35:46"
XCTAssertEqual(try? iso8601.parse("2022-01-28T15:35:46Z"), Date(timeIntervalSinceReferenceDate: 665076946.0))
XCTAssertEqual(try? iso8601.parse("002022-01-28T15:35:46Z"), Date(timeIntervalSinceReferenceDate: 665076946.0))
XCTAssertEqual(try? iso8601.parse("2022-0001-28T15:35:46Z"), Date(timeIntervalSinceReferenceDate: 665076946.0))
XCTAssertEqual(try? iso8601.parse("2022-01-0028T15:35:46Z"), Date(timeIntervalSinceReferenceDate: 665076946.0))
XCTAssertEqual(try? iso8601.parse("2022-1-28T15:35:46Z"), Date(timeIntervalSinceReferenceDate: 665076946.0))
XCTAssertEqual(try? iso8601.parse("2022-01-28T15:35:06Z"), Date(timeIntervalSinceReferenceDate: 665076906.0))
XCTAssertEqual(try? iso8601.parse("2022-01-28T15:35:6Z"), Date(timeIntervalSinceReferenceDate: 665076906.0))
XCTAssertEqual(try? iso8601.parse("2022-01-28T15:05:46Z"), Date(timeIntervalSinceReferenceDate: 665075146.0))
XCTAssertEqual(try? iso8601.parse("2022-01-28T15:5:46Z"), Date(timeIntervalSinceReferenceDate: 665075146.0))
}
func test_timeZones() {
let iso8601 = Date.ISO8601FormatStyle()
let date = Date(timeIntervalSinceReferenceDate: 665076946.0)
var iso8601Pacific = iso8601
iso8601Pacific.timeZone = TimeZone(secondsFromGMT: -3600 * 8)!
// Has a seconds component (-28830)
var iso8601PacificIsh = iso8601
iso8601PacificIsh.timeZone = TimeZone(secondsFromGMT: -3600 * 8 - 30)!
XCTAssertEqual(try? iso8601Pacific.timeSeparator(.omitted).parse("2022-01-28T073546-0800"), date)
XCTAssertEqual(try? iso8601Pacific.timeSeparator(.omitted).timeZoneSeparator(.colon).parse("2022-01-28T073546-08:00"), date)
XCTAssertEqual(try? iso8601PacificIsh.timeSeparator(.omitted).parse("2022-01-28T073516-080030"), date)
XCTAssertEqual(try? iso8601PacificIsh.timeSeparator(.omitted).timeZoneSeparator(.colon).parse("2022-01-28T073516-08:00:30"), date)
var iso8601gmtP1 = iso8601
iso8601gmtP1.timeZone = TimeZone(secondsFromGMT: 3600)!
XCTAssertEqual(try? iso8601gmtP1.timeSeparator(.omitted).parse("2022-01-28T163546+0100"), date)
XCTAssertEqual(try? iso8601gmtP1.timeSeparator(.omitted).parse("2022-01-28T163546+010000"), date)
XCTAssertEqual(try? iso8601gmtP1.timeSeparator(.omitted).timeZoneSeparator(.colon).parse("2022-01-28T163546+01:00"), date)
XCTAssertEqual(try? iso8601gmtP1.timeSeparator(.omitted).timeZoneSeparator(.colon).parse("2022-01-28T163546+01:00:00"), date)
// Due to a quirk of the original implementation, colons are allowed to be present in the time zone even if the time zone separator is omitted
XCTAssertEqual(try? iso8601gmtP1.timeSeparator(.omitted).parse("2022-01-28T163546+01:00"), date)
XCTAssertEqual(try? iso8601gmtP1.timeSeparator(.omitted).parse("2022-01-28T163546+01:00:00"), date)
}
func test_fractionalSeconds() {
let expectedDate = Date(timeIntervalSinceReferenceDate: 646876592.34567)
var iso8601 = Date.ISO8601FormatStyle().year().month().day().time(includingFractionalSeconds: true)
iso8601.timeZone = .gmt
XCTAssertEqual(try? iso8601.parse("2021-07-01T23:56:32.34567"), expectedDate)
}
func test_specialTimeZonesAndSpaces() {
let reference = try! Date("2020-03-05T12:00:00+00:00", strategy: .iso8601)
let tests : [(String, Date.ISO8601FormatStyle)] = [
("2020-03-05T12:00:00+00:00", Date.ISO8601FormatStyle()),
("2020-03-05T12:00:00+0000", Date.ISO8601FormatStyle()),
("2020-03-05T12:00:00GMT", Date.ISO8601FormatStyle()),
("2020-03-05T12:00:00UTC", Date.ISO8601FormatStyle()),
("2020-03-05T11:00:00-01:00", Date.ISO8601FormatStyle().year().month().day().time(includingFractionalSeconds: false).timeSeparator(.colon).timeZone(separator: .colon)),
("2020-03-05T12:00:00Z", Date.ISO8601FormatStyle().year().month().day().time(includingFractionalSeconds: false).timeSeparator(.colon).timeZone(separator: .colon)), // allow Z
("2020-03-05T12:00:00z", Date.ISO8601FormatStyle().year().month().day().time(includingFractionalSeconds: false).timeSeparator(.colon).timeZone(separator: .colon)), // allow z
("2020-03-05T12:00:00UTC", Date.ISO8601FormatStyle().year().month().day().time(includingFractionalSeconds: false).timeSeparator(.colon).timeZone(separator: .colon)), // allow UTC
("2020-03-05T12:00:00GMT", Date.ISO8601FormatStyle().year().month().day().time(includingFractionalSeconds: false).timeSeparator(.colon).timeZone(separator: .colon)), // allow GMT
("2020-03-05T13:00:00UTC+1:00", Date.ISO8601FormatStyle().year().month().day().time(includingFractionalSeconds: false).timeSeparator(.colon).timeZone(separator: .colon)), // allow UTC offsets
("2020-03-05T11:00:00GMT-1:00", Date.ISO8601FormatStyle().year().month().day().time(includingFractionalSeconds: false).timeSeparator(.colon).timeZone(separator: .colon)), // allow GMT offsets
("2020-03-05 12:00:00+0000", Date.ISO8601FormatStyle().year().month().day().dateTimeSeparator(.space).time(includingFractionalSeconds: false).timeZone(separator: .omitted)),
("2020-03-05 11:00:00-0100", Date.ISO8601FormatStyle().year().month().day().dateTimeSeparator(.space).time(includingFractionalSeconds: false).timeZone(separator: .omitted)),
("2020-03-05 11:00:00-0100", Date.ISO8601FormatStyle().year().month().day().dateTimeSeparator(.space).time(includingFractionalSeconds: false).timeZone(separator: .omitted)), // spaces allowed between date/time and time
("2020-03-05 11:00:00 -0100", Date.ISO8601FormatStyle().year().month().day().dateTimeSeparator(.space).time(includingFractionalSeconds: false).timeZone(separator: .omitted)), // spaces allowed between date/time and time/timeZone
("2020-03-05 11:00:00 -0100", Date.ISO8601FormatStyle().year().month().day().dateTimeSeparator(.space).time(includingFractionalSeconds: false).timeZone(separator: .omitted)), // spaces allowed between date/time and time/timeZone
("2020-03-05 10:30:00 -0130", Date.ISO8601FormatStyle().year().month().day().dateTimeSeparator(.space).time(includingFractionalSeconds: false).timeZone(separator: .omitted)), // spaces allowed between date/time and time/timeZone - half hour offset
("2020-03-05 10:30:00 -0130", Date.ISO8601FormatStyle().year().month().day().dateTimeSeparator(.space).time(includingFractionalSeconds: false).timeZone(separator: .colon)), // separator can be colon, or missing
("2020-03-05 12:00:00 GMT", Date.ISO8601FormatStyle().year().month().day().dateTimeSeparator(.space).time(includingFractionalSeconds: false).timeZone(separator: .colon)), // spaces between time zone and GMT
("2020-03-05 11:00:00 GMT-0100", Date.ISO8601FormatStyle().year().month().day().dateTimeSeparator(.space).time(includingFractionalSeconds: false).timeZone(separator: .colon)), // spaces between time zone and GMT, GMT has offset
("2020-03-05 11:00:00 gMt-0100", Date.ISO8601FormatStyle().year().month().day().dateTimeSeparator(.space).time(includingFractionalSeconds: false).timeZone(separator: .colon)), // spaces between time zone and GMT, GMT has offset, GMT has different capitalization
("2020-03-05 12:00:00 GMT -0100", Date.ISO8601FormatStyle().year().month().day().dateTimeSeparator(.space).time(includingFractionalSeconds: false).timeZone(separator: .colon)), // spaces after GMT cause rest of time to be ignored (note here time of 12:00:00 instead of 11:00:00)
("2020-03-05 12:00:00 GMT +0100", Date.ISO8601FormatStyle().year().month().day().dateTimeSeparator(.space).time(includingFractionalSeconds: false).timeZone(separator: .colon)), // spaces after GMT cause rest of time to be ignored (note here time of 12:00:00 instead of 11:00:00)
("2020-03-05 12:00:00 Z +0100", Date.ISO8601FormatStyle().year().month().day().dateTimeSeparator(.space).time(includingFractionalSeconds: false).timeZone(separator: .colon)), // values after Z are ignored, spaces
("2020-03-05 12:00:00 Z+0100", Date.ISO8601FormatStyle().year().month().day().dateTimeSeparator(.space).time(includingFractionalSeconds: false).timeZone(separator: .colon)), // values after Z are ignored, no spaces
]
for (parseMe, style) in tests {
let parsed = try? style.parse(parseMe)
XCTAssertEqual(parsed, reference, """
parsing : \(parseMe)
expected: \(reference) \(reference.timeIntervalSinceReferenceDate)
result : \(parsed != nil ? parsed!.debugDescription : "nil") \(parsed != nil ? parsed!.timeIntervalSinceReferenceDate : 0)
""")
}
}
#if canImport(FoundationInternationalization) || FOUNDATION_FRAMEWORK
func test_chileTimeZone() {
var iso8601Chile = Date.ISO8601FormatStyle().year().month().day()
iso8601Chile.timeZone = TimeZone(name: "America/Santiago")!
let date = try? iso8601Chile.parse("2023-09-03")
XCTAssertNotNil(date)
}
#endif
}
@available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *)
final class DateISO8601FormatStylePatternMatchingTests : XCTestCase {
func _matchFullRange(_ str: String, formatStyle: Date.ISO8601FormatStyle, expectedUpperBound: String.Index?, expectedDate: Date?, file: StaticString = #filePath, line: UInt = #line) {
_matchRange(str, formatStyle: formatStyle, range: nil, expectedUpperBound: expectedUpperBound, expectedDate: expectedDate, file: file, line: line)
}
func _matchRange(_ str: String, formatStyle: Date.ISO8601FormatStyle, range: Range<String.Index>?, expectedUpperBound: String.Index?, expectedDate: Date?, file: StaticString = #filePath, line: UInt = #line) {
// FIXME: Need tests that starts from somewhere else
let m = try? formatStyle.consuming(str, startingAt: str.startIndex, in: range ?? str.startIndex..<str.endIndex)
let upperBound = m?.upperBound
let match = m?.output
let upperBoundDescription = upperBound?.utf16Offset(in: str)
let expectedUpperBoundDescription = expectedUpperBound?.utf16Offset(in: str)
XCTAssertEqual(upperBound, expectedUpperBound, "found upperBound: \(String(describing: upperBoundDescription)); expected: \(String(describing: expectedUpperBoundDescription))", file: file, line: line)
if let match, let expectedDate {
XCTAssertEqual(
match.timeIntervalSinceReferenceDate,
expectedDate.timeIntervalSinceReferenceDate,
accuracy: 0.001,
file: file,
line: line
)
}
}
func testMatchDefaultISO8601Style() throws {
let iso8601FormatStyle = Date.ISO8601FormatStyle()
func verify(_ str: String, expectedUpperBound: String.Index?, expectedDate: Date?, file: StaticString = #filePath, line: UInt = #line) {
_matchFullRange(str, formatStyle: iso8601FormatStyle, expectedUpperBound: expectedUpperBound, expectedDate: expectedDate, file: file, line: line)
}
// dateFormatter.date(from: "2021-07-01 15:56:32")!
let expectedDate = Date(timeIntervalSinceReferenceDate: 646847792.0)
let str = "2021-07-01T15:56:32Z"
verify(str, expectedUpperBound: str.endIndex, expectedDate: expectedDate)
verify("\(str) text", expectedUpperBound: str.endIndex, expectedDate: expectedDate)
verify("some \(str)", expectedUpperBound: nil, expectedDate: nil) // We can't find a matched date because the matching starts at the first character
verify("9999-37-40T35:70:99Z", expectedUpperBound: nil, expectedDate: nil) // This is not a valid date
}
func testPartialMatchISO8601() throws {
var expectedDate: Date?
var expectedLength: Int?
func verify(_ str: String, _ style: Date.ISO8601FormatStyle, file: StaticString = #filePath, line: UInt = #line) {
let expectedUpperBoundStrIndx = (expectedLength != nil) ? str.index(str.startIndex, offsetBy: expectedLength!) : nil
_matchFullRange(str, formatStyle: style, expectedUpperBound: expectedUpperBoundStrIndx, expectedDate: expectedDate, file: file, line: line)
}
let gmt = TimeZone(secondsFromGMT: 0)!
let pst = TimeZone(secondsFromGMT: -3600*8)!
// dateFormatter.date(from: "2021-07-01 23:56:32")!
expectedDate = Date(timeIntervalSinceReferenceDate: 646876592.0)
expectedLength = "2021-07-01T23:56:32Z".count
// This format requires a time zone
do {
expectedDate = nil
expectedLength = nil
verify("2021-07-01T23:56:32", .iso8601WithTimeZone())
verify("2021-07-01T235632", .iso8601WithTimeZone())
verify("2021-07-01 23:56:32Z", .iso8601WithTimeZone())
}
// This format matches up before the time zone, and creates the date using the specified time zone
do {
// dateFormatter.date(from: "2021-07-01 23:56:32")!
expectedDate = Date(timeIntervalSinceReferenceDate: 646876592.0)
expectedLength = "2021-07-01T23:56:32".count
verify("2021-07-01T23:56:32", .iso8601(timeZone: gmt))
verify("2021-07-01T23:56:32Z", .iso8601(timeZone: gmt))
verify("2021-07-01T15:56:32Z", .iso8601(timeZone: pst))
verify("2021-07-01T15:56:32+0000", .iso8601(timeZone: pst))
verify("2021-07-01T15:56:32+00:00", .iso8601(timeZone: pst))
}
do {
expectedLength = "2021-07-01T23:56:32.34567".count
// fractionalDateFormatter.date(from: "2021-07-01 23:56:32.34567")!
expectedDate = Date(timeIntervalSinceReferenceDate: 646876592.345)
verify("2021-07-01T23:56:32.34567", .iso8601(timeZone: gmt, includingFractionalSeconds: true))
verify("2021-07-01T23:56:32.34567Z", .iso8601(timeZone: gmt, includingFractionalSeconds: true))
}
do {
expectedLength = "20210701T235632".count
// dateFormatter.date(from: "2021-07-01 23:56:32")!
expectedDate = Date(timeIntervalSinceReferenceDate: 646876592.0)
verify("20210701T235632", .iso8601(timeZone: gmt, dateSeparator: .omitted, timeSeparator: .omitted))
verify("20210701 235632", .iso8601(timeZone: gmt, dateSeparator: .omitted, dateTimeSeparator: .space, timeSeparator: .omitted))
}
// This format matches the date part only, and creates the date using the specified time zone
do {
// dateFormatter.date(from: "2021-07-01 00:00:00")!
expectedDate = Date(timeIntervalSinceReferenceDate: 646790400.0)
expectedLength = "2021-07-01".count
verify("2021-07-01", .iso8601Date(timeZone: gmt))
verify("2021-07-01T15:56:32+08:00", .iso8601Date(timeZone: gmt))
verify("2021-07-01 15:56:32+08:00", .iso8601Date(timeZone: gmt))
verify("2021-07-01 i love summer", .iso8601Date(timeZone: gmt))
}
do {
// dateFormatter.date(from: "2021-07-01 00:00:00")!
expectedDate = Date(timeIntervalSinceReferenceDate: 646790400.0)
expectedLength = "20210701".count
verify("20210701", .iso8601Date(timeZone: gmt, dateSeparator: .omitted))
verify("20210701T155632+0800", .iso8601Date(timeZone: gmt, dateSeparator: .omitted))
verify("20210701 155632+0800", .iso8601Date(timeZone: gmt, dateSeparator: .omitted))
}
}
func testFullMatch() {
var expectedDate: Date
func verify(_ str: String, _ style: Date.ISO8601FormatStyle, file: StaticString = #filePath, line: UInt = #line) {
_matchFullRange(str, formatStyle: style, expectedUpperBound: str.endIndex, expectedDate: expectedDate, file: file, line: line)
}
do {
// dateFormatter.date(from: "2021-07-01 23:56:32")!
expectedDate = Date(timeIntervalSinceReferenceDate: 646876592.0)
verify("2021-07-01T23:56:32Z", .iso8601WithTimeZone())
verify("20210701 23:56:32Z", .iso8601WithTimeZone(dateSeparator: .omitted, dateTimeSeparator: .space))
verify("2021-07-01 15:56:32-0800", .iso8601WithTimeZone(dateTimeSeparator: .space))
verify("2021-07-01T15:56:32-08:00", .iso8601WithTimeZone(timeZoneSeparator: .colon))
}
do {
// fractionalDateFormatter.date(from: "2021-07-01 23:56:32.314")!
expectedDate = Date(timeIntervalSinceReferenceDate: 646876592.3139999)
verify("2021-07-01T23:56:32.314Z", .iso8601WithTimeZone(includingFractionalSeconds: true))
verify("2021-07-01T235632.314Z", .iso8601WithTimeZone(includingFractionalSeconds: true, timeSeparator: .omitted))
verify("2021-07-01T23:56:32.314000Z", .iso8601WithTimeZone(includingFractionalSeconds: true))
}
}
}
|