Skip to content

Commit

Permalink
Fix bug in comparing Generalized and UTC Time (#30)
Browse files Browse the repository at this point in the history
The prior time comparison did not correctly handle wildly different
times. This patch resolves the issue.
  • Loading branch information
Lukasa authored May 18, 2023
1 parent 1c5387a commit 0700d17
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
12 changes: 6 additions & 6 deletions Sources/SwiftASN1/Basic ASN1 Types/GeneralizedTime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,12 @@ public struct GeneralizedTime: DERImplicitlyTaggable, Hashable, Sendable {
extension GeneralizedTime: Comparable {
@inlinable
public static func <(lhs: GeneralizedTime, rhs: GeneralizedTime) -> Bool {
if lhs.year < rhs.year { return true }
if lhs.month < rhs.month { return true }
if lhs.day < rhs.day { return true }
if lhs.hours < rhs.hours { return true }
if lhs.minutes < rhs.minutes { return true }
if lhs.seconds < rhs.seconds { return true }
if lhs.year < rhs.year { return true } else if lhs.year > rhs.year { return false }
if lhs.month < rhs.month { return true } else if lhs.month > rhs.month { return false }
if lhs.day < rhs.day { return true } else if lhs.day > rhs.day { return false }
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
}
}
10 changes: 5 additions & 5 deletions Sources/SwiftASN1/Basic ASN1 Types/UTCTime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,11 @@ public struct UTCTime: DERImplicitlyTaggable, Hashable, Sendable {
extension UTCTime: Comparable {
@inlinable
public static func <(lhs: UTCTime, rhs: UTCTime) -> Bool {
if lhs.year < rhs.year { return true }
if lhs.month < rhs.month { return true }
if lhs.day < rhs.day { return true }
if lhs.hours < rhs.hours { return true }
if lhs.minutes < rhs.minutes { return true }
if lhs.year < rhs.year { return true } else if lhs.year > rhs.year { return false }
if lhs.month < rhs.month { return true } else if lhs.month > rhs.month { return false }
if lhs.day < rhs.day { return true } else if lhs.day > rhs.day { return false }
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 }
return lhs.seconds < rhs.seconds
}
}
5 changes: 5 additions & 0 deletions Tests/SwiftASN1Tests/GeneralizedTimeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ final class GeneralizedTimeTests: XCTestCase {
transformationsAndResults.append((modify(\.fractionalSeconds, of: original, by: 0.1), .greaterThan))
transformationsAndResults.append((modify(\.fractionalSeconds, of: original, by: -0.1), .lessThan))

transformationsAndResults.append((
try GeneralizedTime(year: 2019, month: 08, day: 08, hours: 08, minutes: 08, seconds: 08, fractionalSeconds: 0.205),
.lessThan
))

for (newValue, expectedResult) in transformationsAndResults {
switch expectedResult {
case .lessThan:
Expand Down
5 changes: 5 additions & 0 deletions Tests/SwiftASN1Tests/UTCTimeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ final class UTCTimeTests: XCTestCase {
transformationsAndResults.append((modify(transform, of: original, by: -1), .lessThan))
}

transformationsAndResults.append((
try UTCTime(year: 2019, month: 08, day: 08, hours: 08, minutes: 08, seconds: 08),
.lessThan
))

for (newValue, expectedResult) in transformationsAndResults {
switch expectedResult {
case .lessThan:
Expand Down

0 comments on commit 0700d17

Please sign in to comment.