minInclusive, maxInclusive, minExclusive, and maxExclusive on xs:duration were ignored: duration has no total order, so the generic comparator returned no value and the bounds were skipped. A DurationValue now implements the XSD 1.0 Datatypes 3.2.6.2 partial order, comparing two durations by adding each to the four reference dateTimes the spec fixes (a month is 28 to 31 days, so the order is partial). A value incomparable to the bound, such as P1M against P30D, is outside the ordered range and is rejected. XSTS: invalid instances accepted 355 to 257. Baseline updated; regression tests cover the total-order cases, the exclusive bounds, and the incomparable case. Refs #147, #129.
112 lines
4.9 KiB
Swift
112 lines
4.9 KiB
Swift
extension PureXML.Schema {
|
|
/// An `xs:duration` as its two independent signed components, months and
|
|
/// seconds, with the XSD 1.0 order relation. Because a month is 28 to 31
|
|
/// days, the order is partial: P and Q compare by adding each to the four
|
|
/// reference dateTimes the spec fixes, and durations whose sign disagrees
|
|
/// across those references are incomparable (for example `P1M` and `P30D`).
|
|
/// See XSD 1.0 Datatypes 3.2.6.2. Used by the ordering facets on duration,
|
|
/// which the generic total-order path cannot represent.
|
|
struct DurationValue {
|
|
let months: Int
|
|
let seconds: Double
|
|
|
|
enum Order { case lessThan, equal, greaterThan, incomparable }
|
|
|
|
/// The four reference dateTimes (year, month, day) at T00:00:00Z.
|
|
private static let references: [(year: Int, month: Int, day: Int)] = [
|
|
(1696, 9, 1), (1697, 2, 1), (1903, 3, 1), (1903, 7, 1),
|
|
]
|
|
|
|
func compare(to other: DurationValue) -> Order {
|
|
var sawLess = false
|
|
var sawGreater = false
|
|
var sawEqual = false
|
|
for reference in Self.references {
|
|
let here = Self.instant(reference, addingMonths: months, seconds: seconds)
|
|
let there = Self.instant(reference, addingMonths: other.months, seconds: other.seconds)
|
|
if here < there {
|
|
sawLess = true
|
|
} else if here > there {
|
|
sawGreater = true
|
|
} else {
|
|
sawEqual = true
|
|
}
|
|
}
|
|
// P < Q only when s+P < s+Q at every reference; > likewise; equal
|
|
// when all four coincide (which holds iff the components are equal).
|
|
if sawLess, !sawGreater, !sawEqual { return .lessThan }
|
|
if sawGreater, !sawLess, !sawEqual { return .greaterThan }
|
|
if sawEqual, !sawLess, !sawGreater { return .equal }
|
|
return .incomparable
|
|
}
|
|
|
|
/// The instant, in seconds, of `reference` + (months, seconds): add the
|
|
/// months with calendar rollover (clamping the day to the resulting
|
|
/// month's length, per the spec), then add the seconds component.
|
|
private static func instant(
|
|
_ reference: (year: Int, month: Int, day: Int),
|
|
addingMonths addMonths: Int,
|
|
seconds: Double,
|
|
) -> Double {
|
|
let total = (reference.month - 1) + addMonths
|
|
let yearsCarried = Int((Double(total) / 12.0).rounded(.down))
|
|
let newYear = reference.year + yearsCarried
|
|
let newMonth = total - yearsCarried * 12 + 1
|
|
let newDay = Swift.min(reference.day, DateTimeValue.daysInMonth(year: newYear, month: newMonth))
|
|
let days = DateTimeValue.daysFromCivil(year: newYear, month: newMonth, day: newDay)
|
|
return Double(days) * 86400 + seconds
|
|
}
|
|
|
|
/// Parses the lexical form `-?P(nY)?(nM)?(nD)?(T(nH)?(nM)?(nS)?)?`. The
|
|
/// lexical recognizer has already accepted the value, so this trusts the
|
|
/// shape and only reads out the components.
|
|
init?(_ string: String) {
|
|
var characters = Substring(string)
|
|
var sign = 1.0
|
|
if characters.first == "-" {
|
|
sign = -1
|
|
characters = characters.dropFirst()
|
|
}
|
|
guard characters.first == "P" else { return nil }
|
|
characters = characters.dropFirst()
|
|
|
|
var inTime = false
|
|
var years = 0
|
|
var monthsPart = 0
|
|
var days = 0
|
|
var secondsPart = 0.0
|
|
var sawComponent = false
|
|
var number = ""
|
|
|
|
func consume(_ designator: Character) -> Bool {
|
|
guard !number.isEmpty, let magnitude = Double(number) else { return false }
|
|
switch (inTime, designator) {
|
|
case (false, "Y"): years = Int(magnitude)
|
|
case (false, "M"): monthsPart = Int(magnitude)
|
|
case (false, "D"): days = Int(magnitude)
|
|
case (true, "H"): secondsPart += magnitude * 3600
|
|
case (true, "M"): secondsPart += magnitude * 60
|
|
case (true, "S"): secondsPart += magnitude
|
|
default: return false
|
|
}
|
|
number = ""
|
|
sawComponent = true
|
|
return true
|
|
}
|
|
|
|
for character in characters {
|
|
if character == "T" {
|
|
inTime = true
|
|
} else if character.isNumber || character == "." {
|
|
number.append(character)
|
|
} else if !consume(character) {
|
|
return nil
|
|
}
|
|
}
|
|
guard sawComponent, number.isEmpty else { return nil }
|
|
months = Int(sign) * (years * 12 + monthsPart)
|
|
seconds = sign * (Double(days) * 86400 + secondsPart)
|
|
}
|
|
}
|
|
}
|