From 41bee14cff978f6faede89196c8a10466787ef65 Mon Sep 17 00:00:00 2001 From: Clinton Nkwocha <32041805+clintonpi@users.noreply.github.com> Date: Wed, 19 Jun 2024 16:26:50 +0100 Subject: [PATCH] Add `rawFractionalSeconds` property to `GeneralizedTime` (#53) Motivation: There is a possible overflow when computing a `Double` value for `fractionalSeconds` from an `ArraySlice` of bytes. Hence, access to this original `ArraySlice` is necessary. Modifications: - Add `rawFractionalSeconds` property to `GeneralizedTime`. - Add a new `internal init` to `GeneralizedTime` that accepts `rawFractionalSeconds` instead of `fractionalSeconds` and generates the later from the former. - Adjust the algorithm for computing `fractionalSeconds`, from a mathematical to a `String` computation. This is due to the precision mismatch between the mathematical computation and the native `Double` computation. This issue, which previously did not surface, now does, due to the newly included round-trip conversion from `fractionalSeconds` to `rawFractionalSeconds`. Result: - The `rawFractionalSeconds` property now provides the original `ArraySlice` from which `fractionalSeconds` was computed. - `GeneralizedTime` can now compute `fractionalSeconds` from the provided `rawFractionalSeconds`. --- .../Basic ASN1 Types/GeneralizedTime.swift | 77 ++- .../Basic ASN1 Types/TimeUtilities.swift | 108 ++-- .../SwiftASN1Tests/GeneralizedTimeTests.swift | 556 +++++++++++++++++- 3 files changed, 695 insertions(+), 46 deletions(-) diff --git a/Sources/SwiftASN1/Basic ASN1 Types/GeneralizedTime.swift b/Sources/SwiftASN1/Basic ASN1 Types/GeneralizedTime.swift index ed5ad0b..1cb426e 100644 --- a/Sources/SwiftASN1/Basic ASN1 Types/GeneralizedTime.swift +++ b/Sources/SwiftASN1/Basic ASN1 Types/GeneralizedTime.swift @@ -105,6 +105,24 @@ public struct GeneralizedTime: DERImplicitlyTaggable, BERImplicitlyTaggable, Has } set { self._fractionalSeconds = newValue + self._rawFractionalSeconds = ArraySlice() + try! self._rawFractionalSeconds.append(fractionalSeconds: self._fractionalSeconds) + + try! self._validate() + } + } + + /// The ArraySlice of bytes from which the fractional seconds will be computed. (Preserved due to a possible overflow + /// when computing a Double from this ArraySlice.) + @inlinable + public var rawFractionalSeconds: ArraySlice { + get { + return self._rawFractionalSeconds + } + set { + self._rawFractionalSeconds = newValue + self._fractionalSeconds = try! Double(fromRawFractionalSeconds: self._rawFractionalSeconds) + try! self._validate() } } @@ -115,7 +133,10 @@ public struct GeneralizedTime: DERImplicitlyTaggable, BERImplicitlyTaggable, Has @usableFromInline var _hours: Int @usableFromInline var _minutes: Int @usableFromInline var _seconds: Int + /// `_fractionalSeconds` is a cached value and `_rawFractionalSeconds` is the source of truth for the numerical + /// fractonal seconds. (No information is lost in the conversion from `_fractionalSeconds` to `_rawFractionalSeconds`.) @usableFromInline var _fractionalSeconds: Double + @usableFromInline var _rawFractionalSeconds: ArraySlice /// Construct a new ``GeneralizedTime`` from individual components. /// @@ -144,6 +165,41 @@ public struct GeneralizedTime: DERImplicitlyTaggable, BERImplicitlyTaggable, Has self._minutes = minutes self._seconds = seconds self._fractionalSeconds = fractionalSeconds + self._rawFractionalSeconds = ArraySlice() + try self._rawFractionalSeconds.append(fractionalSeconds: self._fractionalSeconds) + + try self._validate() + } + + /// Construct a new ``GeneralizedTime`` from individual components. + /// + /// - parameters: + /// - year: The numerical year + /// - month: The numerical month + /// - day: The numerical day + /// - hours: The numerical hours + /// - minutes: The numerical minutes + /// - seconds: The numerical seconds + /// - rawFractionalSeconds: The ArraySlice of bytes from which the fractional seconds will be computed. + /// (Preserved due to a possible overflow when computing a Double from this ArraySlice.) + @inlinable + public init( + year: Int, + month: Int, + day: Int, + hours: Int, + minutes: Int, + seconds: Int, + rawFractionalSeconds: ArraySlice + ) throws { + self._year = year + self._month = month + self._day = day + self._hours = hours + self._minutes = minutes + self._seconds = seconds + self._rawFractionalSeconds = rawFractionalSeconds + self._fractionalSeconds = try Double(fromRawFractionalSeconds: self._rawFractionalSeconds) try self._validate() } @@ -216,6 +272,11 @@ public struct GeneralizedTime: DERImplicitlyTaggable, BERImplicitlyTaggable, Has reason: "Invalid fractional seconds for GeneralizedTime \(self._fractionalSeconds)" ) } + + // When `rawFractionalSeconds` is converted to a `Double`, it must be equal to `fractionalSeconds`. + assert( + (try? Double(fromRawFractionalSeconds: self._rawFractionalSeconds)) == self._fractionalSeconds + ) } } @@ -228,6 +289,20 @@ extension GeneralizedTime: Comparable { if lhs.hours < rhs.hours { return true } else if lhs.hours > rhs.hours { return false } if lhs.minutes < rhs.minutes { return true } else if lhs.minutes > rhs.minutes { return false } if lhs.seconds < rhs.seconds { return true } else if lhs.seconds > rhs.seconds { return false } - return lhs.fractionalSeconds < rhs.fractionalSeconds + if lhs.fractionalSeconds < rhs.fractionalSeconds { + return true + } else if lhs.fractionalSeconds > rhs.fractionalSeconds { + return false + } + + for (lhsByte, rhsByte) in zip(lhs.rawFractionalSeconds, rhs.rawFractionalSeconds) { + if lhsByte != rhsByte { + return lhsByte < rhsByte + } + } + + // Since the above `zip` iteration stops at the length of the shorter `Sequence`, finally, + // compare the length of the two `Sequence`s. + return lhs.rawFractionalSeconds.count < rhs.rawFractionalSeconds.count } } diff --git a/Sources/SwiftASN1/Basic ASN1 Types/TimeUtilities.swift b/Sources/SwiftASN1/Basic ASN1 Types/TimeUtilities.swift index 697ca8d..a02347a 100644 --- a/Sources/SwiftASN1/Basic ASN1 Types/TimeUtilities.swift +++ b/Sources/SwiftASN1/Basic ASN1 Types/TimeUtilities.swift @@ -38,9 +38,10 @@ enum TimeUtilities { } // There may be some fractional seconds. - var fractionalSeconds: Double = 0 + var rawFractionalSeconds = ArraySlice() if bytes.first == UInt8(ascii: ".") { - fractionalSeconds = try bytes._readFractionalSeconds() + bytes.removeFirst() + rawFractionalSeconds = try bytes._readRawFractionalSeconds() } // The next character _must_ be Z, or the encoding is invalid. @@ -60,7 +61,7 @@ enum TimeUtilities { hours: rawHour, minutes: rawMinutes, seconds: rawSeconds, - fractionalSeconds: fractionalSeconds + rawFractionalSeconds: rawFractionalSeconds ) } @@ -178,41 +179,44 @@ extension ArraySlice where Element == UInt8 { return (first &* 10) &+ (second) } - /// This may only be called if there's a leading period: we precondition on this fact. @inlinable - mutating func _readFractionalSeconds() throws -> Double { - precondition(self.popFirst() == UInt8(ascii: ".")) - - var numerator = 0 - var denominator = 1 - - while let nextASCII = self.first, let next = Int(fromDecimalASCII: nextASCII) { - self = self.dropFirst() - - let (newNumerator, multiplyOverflow) = numerator.multipliedReportingOverflow(by: 10) - let (newDenominator, secondMultiplyOverflow) = denominator.multipliedReportingOverflow(by: 10) - let (newNumeratorWithAdded, addingOverflow) = newNumerator.addingReportingOverflow(next) - - // If the new denominator overflows, we just cap to the old value. - if !secondMultiplyOverflow { - denominator = newDenominator - } - - // If the numerator overflows, we don't support the result. - if multiplyOverflow || addingOverflow { - throw ASN1Error.invalidASN1Object(reason: "Numerator overflow when calculating fractional seconds") - } - - numerator = newNumeratorWithAdded + mutating func _readRawFractionalSeconds() throws -> ArraySlice { + guard let nonDecimalASCIIIndex = self.firstIndex(where: { Int(fromDecimalASCII: $0) == nil }) else { + throw ASN1Error.invalidASN1Object( + reason: "Invalid fractional seconds" + ) } - // Ok, we're either at the end or the next character is a Z. One final check: there may not have - // been any trailing zeros here. This means the number may not be 0 mod 10. - if numerator % 10 == 0 { - throw ASN1Error.invalidASN1Object(reason: "Trailing zeros in fractional seconds") + // If `nonDecimalASCIIIndex == self.startIndex`, then it means that there is a decimal point + // but there are no fractional seconds + if nonDecimalASCIIIndex == self.startIndex { + throw ASN1Error.invalidASN1Object( + reason: "Invalid fractional seconds" + ) } - return Double(numerator) / Double(denominator) + let rawFractionalSeconds = self[..= 0 && fractionalSeconds < 1 else { + throw ASN1Error.invalidASN1Object( + reason: "Invalid fractional seconds: \(fractionalSeconds)" + ) + } + + if fractionalSeconds != 0 { + let fractionalSecondsAsString = String(fractionalSeconds) + + assert(fractionalSecondsAsString.starts(with: "0."), "Invalid fractional seconds") + assert(fractionalSecondsAsString.last != "0", "Trailing zeros in fractional seconds") + + self.append(contentsOf: fractionalSecondsAsString.utf8.dropFirst(2)) + } } } @@ -226,16 +230,9 @@ extension Array where Element == UInt8 { self._appendTwoDigitDecimal(generalizedTime.minutes) self._appendTwoDigitDecimal(generalizedTime.seconds) - // Ok, tricky moment here. Is the fractional part non-zero? If it is, we need to write it out as well. - if generalizedTime.fractionalSeconds != 0 { - let stringified = String(generalizedTime.fractionalSeconds) - assert(stringified.starts(with: "0.")) - - self.append(contentsOf: stringified.utf8.dropFirst(1)) - // Remove any trailing zeros from self, they are forbidden. - while self.last == 0 { - self = self.dropLast() - } + if generalizedTime.rawFractionalSeconds.count > 0 { + self.append(UInt8(ascii: ".")) + self.append(contentsOf: generalizedTime.rawFractionalSeconds) } self.append(UInt8(ascii: "Z")) @@ -300,3 +297,28 @@ extension Int { self = converted } } + +extension Double { + @inlinable + init(fromRawFractionalSeconds rawFractionalSeconds: ArraySlice) throws { + if rawFractionalSeconds.count == 0 { + self = 0 + return + } + + if rawFractionalSeconds.last == UInt8(ascii: "0") { + throw ASN1Error.invalidASN1Object(reason: "Trailing zeros in raw fractional seconds") + } + + let rawFractionalSecondsAsString = String(decoding: rawFractionalSeconds, as: UTF8.self) + let fractionalSecondsAsString = "0.\(rawFractionalSecondsAsString)" + + guard let fractionalSeconds = Double(fractionalSecondsAsString) else { + throw ASN1Error.invalidASN1Object( + reason: "Invalid raw fractional seconds" + ) + } + + self = fractionalSeconds + } +} diff --git a/Tests/SwiftASN1Tests/GeneralizedTimeTests.swift b/Tests/SwiftASN1Tests/GeneralizedTimeTests.swift index 816f368..1e77889 100644 --- a/Tests/SwiftASN1Tests/GeneralizedTimeTests.swift +++ b/Tests/SwiftASN1Tests/GeneralizedTimeTests.swift @@ -32,82 +32,337 @@ final class GeneralizedTimeTests: XCTestCase { "19920521000000Z", try .init(year: 1992, month: 5, day: 21, hours: 0, minutes: 0, seconds: 0, fractionalSeconds: 0) ), + ( + "19920521000000Z", + try .init( + year: 1992, + month: 5, + day: 21, + hours: 0, + minutes: 0, + seconds: 0, + rawFractionalSeconds: ArraySlice() + ) + ), ( "19920622123421Z", try .init(year: 1992, month: 6, day: 22, hours: 12, minutes: 34, seconds: 21, fractionalSeconds: 0) ), + ( + "19920622123421Z", + try .init( + year: 1992, + month: 6, + day: 22, + hours: 12, + minutes: 34, + seconds: 21, + rawFractionalSeconds: ArraySlice() + ) + ), ( "19920722132100.3Z", try .init(year: 1992, month: 7, day: 22, hours: 13, minutes: 21, seconds: 0, fractionalSeconds: 0.3) ), + ( + "19920722132100.3Z", + try .init( + year: 1992, + month: 7, + day: 22, + hours: 13, + minutes: 21, + seconds: 0, + rawFractionalSeconds: ArraySlice([51]) + ) + ), ( "19851106210627.3Z", try .init(year: 1985, month: 11, day: 6, hours: 21, minutes: 6, seconds: 27, fractionalSeconds: 0.3) ), + ( + "19851106210627.3Z", + try .init( + year: 1985, + month: 11, + day: 6, + hours: 21, + minutes: 6, + seconds: 27, + rawFractionalSeconds: ArraySlice([51]) + ) + ), ( "19851106210627.14159Z", try .init(year: 1985, month: 11, day: 6, hours: 21, minutes: 6, seconds: 27, fractionalSeconds: 0.14159) ), + ( + "19851106210627.14159Z", + try .init( + year: 1985, + month: 11, + day: 6, + hours: 21, + minutes: 6, + seconds: 27, + rawFractionalSeconds: ArraySlice([49, 52, 49, 53, 57]) + ) + ), ( "20210131000000Z", try .init(year: 2021, month: 1, day: 31, hours: 0, minutes: 0, seconds: 0, fractionalSeconds: 0) + ), + ( + "20210131000000Z", + try .init( + year: 2021, + month: 1, + day: 31, + hours: 0, + minutes: 0, + seconds: 0, + rawFractionalSeconds: ArraySlice() + ) ), // only 31 days in January ( "20210228000000Z", try .init(year: 2021, month: 2, day: 28, hours: 0, minutes: 0, seconds: 0, fractionalSeconds: 0) + ), + ( + "20210228000000Z", + try .init( + year: 2021, + month: 2, + day: 28, + hours: 0, + minutes: 0, + seconds: 0, + rawFractionalSeconds: ArraySlice() + ) ), // only 28 days in February 2021 ( "20200229000000Z", try .init(year: 2020, month: 2, day: 29, hours: 0, minutes: 0, seconds: 0, fractionalSeconds: 0) + ), + ( + "20200229000000Z", + try .init( + year: 2020, + month: 2, + day: 29, + hours: 0, + minutes: 0, + seconds: 0, + rawFractionalSeconds: ArraySlice() + ) ), // only 29 days in February 2020 ( "21000228000000Z", try .init(year: 2100, month: 2, day: 28, hours: 0, minutes: 0, seconds: 0, fractionalSeconds: 0) + ), + ( + "21000228000000Z", + try .init( + year: 2100, + month: 2, + day: 28, + hours: 0, + minutes: 0, + seconds: 0, + rawFractionalSeconds: ArraySlice() + ) ), // only 28 days in February 2100 ( "20000229000000Z", try .init(year: 2000, month: 2, day: 29, hours: 0, minutes: 0, seconds: 0, fractionalSeconds: 0) + ), + ( + "20000229000000Z", + try .init( + year: 2000, + month: 2, + day: 29, + hours: 0, + minutes: 0, + seconds: 0, + rawFractionalSeconds: ArraySlice() + ) ), // only 29 days in February 2000 ( "20210331000000Z", try .init(year: 2021, month: 3, day: 31, hours: 0, minutes: 0, seconds: 0, fractionalSeconds: 0) + ), + ( + "20210331000000Z", + try .init( + year: 2021, + month: 3, + day: 31, + hours: 0, + minutes: 0, + seconds: 0, + rawFractionalSeconds: ArraySlice() + ) ), // only 31 days in March ( "20210430000000Z", try .init(year: 2021, month: 4, day: 30, hours: 0, minutes: 0, seconds: 0, fractionalSeconds: 0) + ), + ( + "20210430000000Z", + try .init( + year: 2021, + month: 4, + day: 30, + hours: 0, + minutes: 0, + seconds: 0, + rawFractionalSeconds: ArraySlice() + ) ), // only 30 days in April ( "20210531000000Z", try .init(year: 2021, month: 5, day: 31, hours: 0, minutes: 0, seconds: 0, fractionalSeconds: 0) + ), + ( + "20210531000000Z", + try .init( + year: 2021, + month: 5, + day: 31, + hours: 0, + minutes: 0, + seconds: 0, + rawFractionalSeconds: ArraySlice() + ) ), // only 31 days in May ( "20210630000000Z", try .init(year: 2021, month: 6, day: 30, hours: 0, minutes: 0, seconds: 0, fractionalSeconds: 0) + ), + ( + "20210630000000Z", + try .init( + year: 2021, + month: 6, + day: 30, + hours: 0, + minutes: 0, + seconds: 0, + rawFractionalSeconds: ArraySlice() + ) ), // only 30 days in June ( "20210731000000Z", try .init(year: 2021, month: 7, day: 31, hours: 0, minutes: 0, seconds: 0, fractionalSeconds: 0) + ), + ( + "20210731000000Z", + try .init( + year: 2021, + month: 7, + day: 31, + hours: 0, + minutes: 0, + seconds: 0, + rawFractionalSeconds: ArraySlice() + ) ), // only 31 days in July ( "20210831000000Z", try .init(year: 2021, month: 8, day: 31, hours: 0, minutes: 0, seconds: 0, fractionalSeconds: 0) + ), + ( + "20210831000000Z", + try .init( + year: 2021, + month: 8, + day: 31, + hours: 0, + minutes: 0, + seconds: 0, + rawFractionalSeconds: ArraySlice() + ) ), // only 31 days in August ( "20210930000000Z", try .init(year: 2021, month: 9, day: 30, hours: 0, minutes: 0, seconds: 0, fractionalSeconds: 0) + ), + ( + "20210930000000Z", + try .init( + year: 2021, + month: 9, + day: 30, + hours: 0, + minutes: 0, + seconds: 0, + rawFractionalSeconds: ArraySlice() + ) ), // only 30 days in September ( "20211031000000Z", try .init(year: 2021, month: 10, day: 31, hours: 0, minutes: 0, seconds: 0, fractionalSeconds: 0) + ), + ( + "20211031000000Z", + try .init( + year: 2021, + month: 10, + day: 31, + hours: 0, + minutes: 0, + seconds: 0, + rawFractionalSeconds: ArraySlice() + ) ), // only 31 days in October ( "20211130000000Z", try .init(year: 2021, month: 11, day: 30, hours: 0, minutes: 0, seconds: 0, fractionalSeconds: 0) + ), + ( + "20211130000000Z", + try .init( + year: 2021, + month: 11, + day: 30, + hours: 0, + minutes: 0, + seconds: 0, + rawFractionalSeconds: ArraySlice() + ) ), // only 30 days in November ( "20211231000000Z", try .init(year: 2021, month: 12, day: 31, hours: 0, minutes: 0, seconds: 0, fractionalSeconds: 0) + ), + ( + "20211231000000Z", + try .init( + year: 2021, + month: 12, + day: 31, + hours: 0, + minutes: 0, + seconds: 0, + rawFractionalSeconds: ArraySlice() + ) ), // only 31 days in December + ( + "19851106210627.10000000000000001Z", + try .init( + year: 1985, + month: 11, + day: 6, + hours: 21, + minutes: 6, + seconds: 27, + rawFractionalSeconds: ArraySlice([ + 49, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 49, + ]) + ) + ), // `fractionalSeconds` loses precision and becomes 0.1 (as opposed to 0.10000000000000001). + // But `GeneralizedTime` round-trips when serialized and deserialized, as `rawFractionalSeconds` is preserved. // Invalid representations ("19920520240000Z", nil), // midnight may not be 2400000 @@ -137,8 +392,8 @@ final class GeneralizedTimeTests: XCTestCase { ("20210931000000Z", nil), // only 30 days in September ("20211032000000Z", nil), // only 31 days in October ("20211131000000Z", nil), // only 30 days in November - ("20211232000000Z", nil), // only 31 days in December - ("20200101000000.9223372036854775808Z", nil), // Fractional part will overflow a 64-bit integer by adding + ("19920521000000.", nil), // invalid fractional seconds and missing trailing Z + ("19920521000000.Z", nil), // invalid fractional seconds ] @@ -164,58 +419,355 @@ final class GeneralizedTimeTests: XCTestCase { // Invalid year, negative mustFail(try .init(year: -1, month: 1, day: 1, hours: 1, minutes: 1, seconds: 1, fractionalSeconds: 0)) + mustFail( + try .init( + year: -1, + month: 1, + day: 1, + hours: 1, + minutes: 1, + seconds: 1, + rawFractionalSeconds: ArraySlice() + ) + ) // Invalid month, zero. mustFail(try .init(year: 2000, month: 0, day: 1, hours: 1, minutes: 1, seconds: 1, fractionalSeconds: 0)) + mustFail( + try .init( + year: 2000, + month: 0, + day: 1, + hours: 1, + minutes: 1, + seconds: 1, + rawFractionalSeconds: ArraySlice() + ) + ) // Invalid month, negative. mustFail(try .init(year: 2000, month: -1, day: 1, hours: 1, minutes: 1, seconds: 1, fractionalSeconds: 0)) + mustFail( + try .init( + year: 2000, + month: -1, + day: 1, + hours: 1, + minutes: 1, + seconds: 1, + rawFractionalSeconds: ArraySlice() + ) + ) // Invalid month, too large. mustFail(try .init(year: 2000, month: 13, day: 1, hours: 1, minutes: 1, seconds: 1, fractionalSeconds: 0)) + mustFail( + try .init( + year: 2000, + month: 13, + day: 1, + hours: 1, + minutes: 1, + seconds: 1, + rawFractionalSeconds: ArraySlice() + ) + ) // Invalid day, zero. mustFail(try .init(year: 2000, month: 1, day: 0, hours: 1, minutes: 1, seconds: 1, fractionalSeconds: 0)) + mustFail( + try .init( + year: 2000, + month: 1, + day: 0, + hours: 1, + minutes: 1, + seconds: 1, + rawFractionalSeconds: ArraySlice() + ) + ) // Invalid day, negative. mustFail(try .init(year: 2000, month: 1, day: -1, hours: 1, minutes: 1, seconds: 1, fractionalSeconds: 0)) + mustFail( + try .init( + year: 2000, + month: 1, + day: -1, + hours: 1, + minutes: 1, + seconds: 1, + rawFractionalSeconds: ArraySlice() + ) + ) // only 31 days in January mustFail(try .init(year: 2000, month: 1, day: 32, hours: 1, minutes: 1, seconds: 1, fractionalSeconds: 0)) + mustFail( + try .init( + year: 2000, + month: 1, + day: 32, + hours: 1, + minutes: 1, + seconds: 1, + rawFractionalSeconds: ArraySlice() + ) + ) // only 28 days in February 2021 mustFail(try .init(year: 2021, month: 2, day: 29, hours: 1, minutes: 1, seconds: 1, fractionalSeconds: 0)) + mustFail( + try .init( + year: 2021, + month: 2, + day: 29, + hours: 1, + minutes: 1, + seconds: 1, + rawFractionalSeconds: ArraySlice() + ) + ) // only 29 days in February 2020 mustFail(try .init(year: 2020, month: 2, day: 30, hours: 1, minutes: 1, seconds: 1, fractionalSeconds: 0)) + mustFail( + try .init( + year: 2020, + month: 2, + day: 30, + hours: 1, + minutes: 1, + seconds: 1, + rawFractionalSeconds: ArraySlice() + ) + ) // only 28 days in February 2100 mustFail(try .init(year: 2100, month: 2, day: 29, hours: 1, minutes: 1, seconds: 1, fractionalSeconds: 0)) + mustFail( + try .init( + year: 2100, + month: 2, + day: 29, + hours: 1, + minutes: 1, + seconds: 1, + rawFractionalSeconds: ArraySlice() + ) + ) // only 29 days in February 2000 mustFail(try .init(year: 2000, month: 2, day: 30, hours: 1, minutes: 1, seconds: 1, fractionalSeconds: 0)) + mustFail( + try .init( + year: 2000, + month: 2, + day: 30, + hours: 1, + minutes: 1, + seconds: 1, + rawFractionalSeconds: ArraySlice() + ) + ) // only 31 days in March mustFail(try .init(year: 2000, month: 3, day: 32, hours: 1, minutes: 1, seconds: 1, fractionalSeconds: 0)) + mustFail( + try .init( + year: 2000, + month: 3, + day: 32, + hours: 1, + minutes: 1, + seconds: 1, + rawFractionalSeconds: ArraySlice() + ) + ) // only 30 days in April mustFail(try .init(year: 2000, month: 4, day: 31, hours: 1, minutes: 1, seconds: 1, fractionalSeconds: 0)) + mustFail( + try .init( + year: 2000, + month: 4, + day: 31, + hours: 1, + minutes: 1, + seconds: 1, + rawFractionalSeconds: ArraySlice() + ) + ) // only 31 days in May mustFail(try .init(year: 2000, month: 5, day: 32, hours: 1, minutes: 1, seconds: 1, fractionalSeconds: 0)) + mustFail( + try .init( + year: 2000, + month: 5, + day: 32, + hours: 1, + minutes: 1, + seconds: 1, + rawFractionalSeconds: ArraySlice() + ) + ) // only 30 days in June mustFail(try .init(year: 2000, month: 6, day: 31, hours: 1, minutes: 1, seconds: 1, fractionalSeconds: 0)) + mustFail( + try .init( + year: 2000, + month: 6, + day: 31, + hours: 1, + minutes: 1, + seconds: 1, + rawFractionalSeconds: ArraySlice() + ) + ) // only 31 days in July mustFail(try .init(year: 2000, month: 7, day: 32, hours: 1, minutes: 1, seconds: 1, fractionalSeconds: 0)) + mustFail( + try .init( + year: 2000, + month: 7, + day: 32, + hours: 1, + minutes: 1, + seconds: 1, + rawFractionalSeconds: ArraySlice() + ) + ) // only 31 days in August mustFail(try .init(year: 2000, month: 8, day: 32, hours: 1, minutes: 1, seconds: 1, fractionalSeconds: 0)) + mustFail( + try .init( + year: 2000, + month: 8, + day: 32, + hours: 1, + minutes: 1, + seconds: 1, + rawFractionalSeconds: ArraySlice() + ) + ) // only 30 days in September mustFail(try .init(year: 2000, month: 9, day: 31, hours: 1, minutes: 1, seconds: 1, fractionalSeconds: 0)) + mustFail( + try .init( + year: 2000, + month: 9, + day: 31, + hours: 1, + minutes: 1, + seconds: 1, + rawFractionalSeconds: ArraySlice() + ) + ) // only 31 days in October mustFail(try .init(year: 2000, month: 10, day: 32, hours: 1, minutes: 1, seconds: 1, fractionalSeconds: 0)) + mustFail( + try .init( + year: 2000, + month: 10, + day: 32, + hours: 1, + minutes: 1, + seconds: 1, + rawFractionalSeconds: ArraySlice() + ) + ) // only 30 days in November mustFail(try .init(year: 2000, month: 11, day: 31, hours: 1, minutes: 1, seconds: 1, fractionalSeconds: 0)) + mustFail( + try .init( + year: 2000, + month: 11, + day: 31, + hours: 1, + minutes: 1, + seconds: 1, + rawFractionalSeconds: ArraySlice() + ) + ) // only 31 days in December mustFail(try .init(year: 2000, month: 11, day: 32, hours: 1, minutes: 1, seconds: 1, fractionalSeconds: 0)) + mustFail( + try .init( + year: 2000, + month: 11, + day: 32, + hours: 1, + minutes: 1, + seconds: 1, + rawFractionalSeconds: ArraySlice() + ) + ) // Invalid hour, negative mustFail(try .init(year: 2000, month: 1, day: 1, hours: -1, minutes: 1, seconds: 1, fractionalSeconds: 0)) + mustFail( + try .init( + year: 2000, + month: 1, + day: 1, + hours: -1, + minutes: 1, + seconds: 1, + rawFractionalSeconds: ArraySlice() + ) + ) // Invalid hour, 24 mustFail(try .init(year: 2000, month: 1, day: 1, hours: 24, minutes: 0, seconds: 0, fractionalSeconds: 0)) + mustFail( + try .init( + year: 2000, + month: 1, + day: 1, + hours: 24, + minutes: 0, + seconds: 0, + rawFractionalSeconds: ArraySlice() + ) + ) // Invalid minute, negative mustFail(try .init(year: 2000, month: 1, day: 1, hours: 0, minutes: -1, seconds: 1, fractionalSeconds: 0)) + mustFail( + try .init( + year: 2000, + month: 1, + day: 1, + hours: 0, + minutes: -1, + seconds: 1, + rawFractionalSeconds: ArraySlice() + ) + ) // Invalid minute, 60 mustFail(try .init(year: 2000, month: 1, day: 1, hours: 0, minutes: 60, seconds: 0, fractionalSeconds: 0)) + mustFail( + try .init( + year: 2000, + month: 1, + day: 1, + hours: 0, + minutes: 60, + seconds: 0, + rawFractionalSeconds: ArraySlice() + ) + ) // Invalid second, negative mustFail(try .init(year: 2000, month: 1, day: 1, hours: 0, minutes: 0, seconds: -1, fractionalSeconds: 0)) + mustFail( + try .init( + year: 2000, + month: 1, + day: 1, + hours: 0, + minutes: 0, + seconds: -1, + rawFractionalSeconds: ArraySlice() + ) + ) // Invalid second, 62 (we allow some leap seconds) mustFail(try .init(year: 2000, month: 1, day: 1, hours: 0, minutes: 0, seconds: 62, fractionalSeconds: 0)) + mustFail( + try .init( + year: 2000, + month: 1, + day: 1, + hours: 0, + minutes: 0, + seconds: 62, + rawFractionalSeconds: ArraySlice() + ) + ) // Invalid fractional seconds, negative mustFail(try .init(year: 2000, month: 1, day: 1, hours: 0, minutes: 0, seconds: 0, fractionalSeconds: -0.5)) // Invalid fractional seconds, greater than one