@@ -68,7 +68,7 @@ extension Date {
68
68
let result = v. withUTF8 { buffer -> ( Int , Date ) ? in
69
69
let view = BufferView ( unsafeBufferPointer: buffer) !
70
70
71
- guard let comps = try ? componentsStyle. components ( from : value , in: view) else {
71
+ guard let comps = try ? componentsStyle. components ( in: view) else {
72
72
return nil
73
73
}
74
74
@@ -324,7 +324,7 @@ extension DateComponents {
324
324
let result = v. withUTF8 { buffer -> ( Int , DateComponents ) ? in
325
325
let view = BufferView ( unsafeBufferPointer: buffer) !
326
326
327
- guard let comps = try ? components ( from : value , in: view) else {
327
+ guard let comps = try ? components ( in: view) else {
328
328
return nil
329
329
}
330
330
@@ -338,8 +338,8 @@ extension DateComponents {
338
338
let endIndex = value. utf8. index ( v. startIndex, offsetBy: result. 0 )
339
339
return ( endIndex, result. 1 )
340
340
}
341
-
342
- fileprivate func components( from inputString : String , in view: borrowing BufferView < UInt8 > ) throws -> ComponentsParseResult {
341
+
342
+ fileprivate func components( in view: borrowing BufferView < UInt8 > ) throws -> ComponentsParseResult {
343
343
// https://www.rfc-editor.org/rfc/rfc9110.html#http.date
344
344
// <day-name>, <day> <month> <year> <hour>:<minute>:<second> GMT
345
345
@@ -348,17 +348,17 @@ extension DateComponents {
348
348
349
349
// Despite the spec, we allow the weekday name to be optional.
350
350
guard let maybeWeekday1 = it. peek ( ) else {
351
- throw parseError ( inputString , exampleFormattedString: Date . HTTPFormatStyle ( ) . format ( Date . now) )
351
+ throw parseError ( view , exampleFormattedString: Date . HTTPFormatStyle ( ) . format ( Date . now) )
352
352
}
353
353
354
354
if isASCIIDigit ( maybeWeekday1) {
355
355
// This is the first digit of the day. Weekday is not present.
356
356
} else {
357
357
// Anything else must be a day-name (Mon, Tue, ... Sun)
358
358
guard let weekday1 = it. next ( ) , let weekday2 = it. next ( ) , let weekday3 = it. next ( ) else {
359
- throw parseError ( inputString , exampleFormattedString: Date . HTTPFormatStyle ( ) . format ( Date . now) )
359
+ throw parseError ( view , exampleFormattedString: Date . HTTPFormatStyle ( ) . format ( Date . now) )
360
360
}
361
-
361
+
362
362
dc. weekday = switch ( weekday1, weekday2, weekday3) {
363
363
case ( UInt8 ( ascii: " S " ) , UInt8 ( ascii: " u " ) , UInt8 ( ascii: " n " ) ) :
364
364
1
@@ -375,20 +375,20 @@ extension DateComponents {
375
375
case ( UInt8 ( ascii: " S " ) , UInt8 ( ascii: " a " ) , UInt8 ( ascii: " t " ) ) :
376
376
7
377
377
default :
378
- throw parseError ( inputString , exampleFormattedString: Date . HTTPFormatStyle ( ) . format ( Date . now) , extendedDescription: " Malformed weekday name " )
378
+ throw parseError ( view , exampleFormattedString: Date . HTTPFormatStyle ( ) . format ( Date . now) , extendedDescription: " Malformed weekday name " )
379
379
}
380
380
381
381
// Move past , and space to weekday
382
- try it. expectCharacter ( UInt8 ( ascii: " , " ) , input: inputString , onFailure: Date . HTTPFormatStyle ( ) . format ( Date . now) , extendedDescription: " Missing , after weekday " )
383
- try it. expectCharacter ( UInt8 ( ascii: " " ) , input: inputString , onFailure: Date . HTTPFormatStyle ( ) . format ( Date . now) , extendedDescription: " Missing space after weekday " )
382
+ try it. expectCharacter ( UInt8 ( ascii: " , " ) , input: view , onFailure: Date . HTTPFormatStyle ( ) . format ( Date . now) , extendedDescription: " Missing , after weekday " )
383
+ try it. expectCharacter ( UInt8 ( ascii: " " ) , input: view , onFailure: Date . HTTPFormatStyle ( ) . format ( Date . now) , extendedDescription: " Missing space after weekday " )
384
384
}
385
385
386
- dc. day = try it. digits ( minDigits: 2 , maxDigits: 2 , input: inputString , onFailure: Date . HTTPFormatStyle ( ) . format ( Date . now) , extendedDescription: " Missing or malformed day " )
387
- try it. expectCharacter ( UInt8 ( ascii: " " ) , input: inputString , onFailure: Date . HTTPFormatStyle ( ) . format ( Date . now) )
386
+ dc. day = try it. digits ( minDigits: 2 , maxDigits: 2 , input: view , onFailure: Date . HTTPFormatStyle ( ) . format ( Date . now) , extendedDescription: " Missing or malformed day " )
387
+ try it. expectCharacter ( UInt8 ( ascii: " " ) , input: view , onFailure: Date . HTTPFormatStyle ( ) . format ( Date . now) )
388
388
389
389
// month-name (Jan, Feb, ... Dec)
390
390
guard let month1 = it. next ( ) , let month2 = it. next ( ) , let month3 = it. next ( ) else {
391
- throw parseError ( inputString , exampleFormattedString: Date . HTTPFormatStyle ( ) . format ( Date . now) , extendedDescription: " Missing month " )
391
+ throw parseError ( view , exampleFormattedString: Date . HTTPFormatStyle ( ) . format ( Date . now) , extendedDescription: " Missing month " )
392
392
}
393
393
394
394
dc. month = switch ( month1, month2, month3) {
@@ -417,45 +417,45 @@ extension DateComponents {
417
417
case ( UInt8 ( ascii: " D " ) , UInt8 ( ascii: " e " ) , UInt8 ( ascii: " c " ) ) :
418
418
12
419
419
default :
420
- throw parseError ( inputString , exampleFormattedString: Date . HTTPFormatStyle ( ) . format ( Date . now) , extendedDescription: " Month \( String ( describing: dc. month) ) is out of bounds " )
420
+ throw parseError ( view , exampleFormattedString: Date . HTTPFormatStyle ( ) . format ( Date . now) , extendedDescription: " Month \( String ( describing: dc. month) ) is out of bounds " )
421
421
}
422
422
423
- try it. expectCharacter ( UInt8 ( ascii: " " ) , input: inputString , onFailure: Date . HTTPFormatStyle ( ) . format ( Date . now) )
423
+ try it. expectCharacter ( UInt8 ( ascii: " " ) , input: view , onFailure: Date . HTTPFormatStyle ( ) . format ( Date . now) )
424
424
425
- dc. year = try it. digits ( minDigits: 4 , maxDigits: 4 , input: inputString , onFailure: Date . HTTPFormatStyle ( ) . format ( Date . now) )
426
- try it. expectCharacter ( UInt8 ( ascii: " " ) , input: inputString , onFailure: Date . HTTPFormatStyle ( ) . format ( Date . now) )
425
+ dc. year = try it. digits ( minDigits: 4 , maxDigits: 4 , input: view , onFailure: Date . HTTPFormatStyle ( ) . format ( Date . now) )
426
+ try it. expectCharacter ( UInt8 ( ascii: " " ) , input: view , onFailure: Date . HTTPFormatStyle ( ) . format ( Date . now) )
427
427
428
- let hour = try it. digits ( minDigits: 2 , maxDigits: 2 , input: inputString , onFailure: Date . HTTPFormatStyle ( ) . format ( Date . now) )
428
+ let hour = try it. digits ( minDigits: 2 , maxDigits: 2 , input: view , onFailure: Date . HTTPFormatStyle ( ) . format ( Date . now) )
429
429
if hour < 0 || hour > 23 {
430
- throw parseError ( inputString , exampleFormattedString: Date . HTTPFormatStyle ( ) . format ( Date . now) , extendedDescription: " Hour \( hour) is out of bounds " )
430
+ throw parseError ( view , exampleFormattedString: Date . HTTPFormatStyle ( ) . format ( Date . now) , extendedDescription: " Hour \( hour) is out of bounds " )
431
431
}
432
432
dc. hour = hour
433
433
434
- try it. expectCharacter ( UInt8 ( ascii: " : " ) , input: inputString , onFailure: Date . HTTPFormatStyle ( ) . format ( Date . now) )
435
- let minute = try it. digits ( minDigits: 2 , maxDigits: 2 , input: inputString , onFailure: Date . HTTPFormatStyle ( ) . format ( Date . now) )
434
+ try it. expectCharacter ( UInt8 ( ascii: " : " ) , input: view , onFailure: Date . HTTPFormatStyle ( ) . format ( Date . now) )
435
+ let minute = try it. digits ( minDigits: 2 , maxDigits: 2 , input: view , onFailure: Date . HTTPFormatStyle ( ) . format ( Date . now) )
436
436
if minute < 0 || minute > 59 {
437
- throw parseError ( inputString , exampleFormattedString: Date . HTTPFormatStyle ( ) . format ( Date . now) , extendedDescription: " Minute \( minute) is out of bounds " )
437
+ throw parseError ( view , exampleFormattedString: Date . HTTPFormatStyle ( ) . format ( Date . now) , extendedDescription: " Minute \( minute) is out of bounds " )
438
438
}
439
439
dc. minute = minute
440
440
441
- try it. expectCharacter ( UInt8 ( ascii: " : " ) , input: inputString , onFailure: Date . HTTPFormatStyle ( ) . format ( Date . now) )
442
- let second = try it. digits ( minDigits: 2 , maxDigits: 2 , input: inputString , onFailure: Date . HTTPFormatStyle ( ) . format ( Date . now) )
441
+ try it. expectCharacter ( UInt8 ( ascii: " : " ) , input: view , onFailure: Date . HTTPFormatStyle ( ) . format ( Date . now) )
442
+ let second = try it. digits ( minDigits: 2 , maxDigits: 2 , input: view , onFailure: Date . HTTPFormatStyle ( ) . format ( Date . now) )
443
443
// second '60' is supported in the spec for leap seconds, but Foundation does not support leap seconds. 60 is adjusted to 59.
444
444
if second < 0 || second > 60 {
445
- throw parseError ( inputString , exampleFormattedString: Date . HTTPFormatStyle ( ) . format ( Date . now) , extendedDescription: " Second \( second) is out of bounds " )
445
+ throw parseError ( view , exampleFormattedString: Date . HTTPFormatStyle ( ) . format ( Date . now) , extendedDescription: " Second \( second) is out of bounds " )
446
446
}
447
447
// Foundation does not support leap seconds. We convert 60 seconds into 59 seconds.
448
448
if second == 60 {
449
449
dc. second = 59
450
450
} else {
451
451
dc. second = second
452
452
}
453
- try it. expectCharacter ( UInt8 ( ascii: " " ) , input: inputString , onFailure: Date . HTTPFormatStyle ( ) . format ( Date . now) )
453
+ try it. expectCharacter ( UInt8 ( ascii: " " ) , input: view , onFailure: Date . HTTPFormatStyle ( ) . format ( Date . now) )
454
454
455
455
// "GMT"
456
- try it. expectCharacter ( UInt8 ( ascii: " G " ) , input: inputString , onFailure: Date . HTTPFormatStyle ( ) . format ( Date . now) , extendedDescription: " Missing GMT time zone " )
457
- try it. expectCharacter ( UInt8 ( ascii: " M " ) , input: inputString , onFailure: Date . HTTPFormatStyle ( ) . format ( Date . now) , extendedDescription: " Missing GMT time zone " )
458
- try it. expectCharacter ( UInt8 ( ascii: " T " ) , input: inputString , onFailure: Date . HTTPFormatStyle ( ) . format ( Date . now) , extendedDescription: " Missing GMT time zone " )
456
+ try it. expectCharacter ( UInt8 ( ascii: " G " ) , input: view , onFailure: Date . HTTPFormatStyle ( ) . format ( Date . now) , extendedDescription: " Missing GMT time zone " )
457
+ try it. expectCharacter ( UInt8 ( ascii: " M " ) , input: view , onFailure: Date . HTTPFormatStyle ( ) . format ( Date . now) , extendedDescription: " Missing GMT time zone " )
458
+ try it. expectCharacter ( UInt8 ( ascii: " T " ) , input: view , onFailure: Date . HTTPFormatStyle ( ) . format ( Date . now) , extendedDescription: " Missing GMT time zone " )
459
459
460
460
// Time zone is always GMT, calendar is always Gregorian
461
461
dc. timeZone = . gmt
0 commit comments