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`.
This commit is contained in:
Clinton Nkwocha authored and GitHub committed 2024-06-19 08:26:50 -07:00
1 parent e61ef9579a
commit 41bee14cff
3 files changed
+695 -46

No files matched your search

@@ -105,6 +105,24 @@ public struct GeneralizedTime: DERImplicitlyTaggable, BERImplicitlyTaggable, Has
}
set {
self._fractionalSeconds = newValue
self._rawFractionalSeconds = ArraySlice<UInt8>()
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<UInt8> {
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<UInt8>
/// 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<UInt8>()
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<UInt8>
) 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
}
}
@@ -38,9 +38,10 @@ enum TimeUtilities {
}
// There may be some fractional seconds.
var fractionalSeconds: Double = 0
var rawFractionalSeconds = ArraySlice<UInt8>()
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<UInt8> {
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[..<nonDecimalASCIIIndex]
self = self[nonDecimalASCIIIndex...]
return rawFractionalSeconds
}
@inlinable
mutating func append(fractionalSeconds: Double) throws {
// Fractional seconds may not be negative and may not be 1 or more.
guard fractionalSeconds >= 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<UInt8>) 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
}
}
+554 -2
View File
@@ -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<UInt8>()
)
),
(
"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<UInt8>()
)
),
(
"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<UInt8>([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<UInt8>([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<UInt8>([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<UInt8>()
)
), // 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<UInt8>()
)
), // 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<UInt8>()
)
), // 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<UInt8>()
)
), // 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<UInt8>()
)
), // 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<UInt8>()
)
), // 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<UInt8>()
)
), // 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<UInt8>()
)
), // 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<UInt8>()
)
), // 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<UInt8>()
)
), // 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<UInt8>()
)
), // 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<UInt8>()
)
), // 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<UInt8>()
)
), // 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<UInt8>()
)
), // 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<UInt8>()
)
), // only 31 days in December
(
"19851106210627.10000000000000001Z",
try .init(
year: 1985,
month: 11,
day: 6,
hours: 21,
minutes: 6,
seconds: 27,
rawFractionalSeconds: ArraySlice<UInt8>([
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<UInt8>()
)
)
// 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<UInt8>()
)
)
// 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<UInt8>()
)
)
// 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<UInt8>()
)
)
// 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<UInt8>()
)
)
// 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<UInt8>()
)
)
// 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<UInt8>()
)
)
// 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<UInt8>()
)
)
// 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<UInt8>()
)
)
// 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<UInt8>()
)
)
// 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<UInt8>()
)
)
// 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<UInt8>()
)
)
// 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<UInt8>()
)
)
// 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<UInt8>()
)
)
// 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<UInt8>()
)
)
// 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<UInt8>()
)
)
// 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<UInt8>()
)
)
// 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<UInt8>()
)
)
// 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<UInt8>()
)
)
// 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<UInt8>()
)
)
// 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<UInt8>()
)
)
// 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<UInt8>()
)
)
// 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<UInt8>()
)
)
// 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<UInt8>()
)
)
// 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<UInt8>()
)
)
// 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<UInt8>()
)
)
// 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<UInt8>()
)
)
// 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