Skip to content

Commit 0700d17

Browse files
authored
Fix bug in comparing Generalized and UTC Time (#30)
The prior time comparison did not correctly handle wildly different times. This patch resolves the issue.
1 parent 1c5387a commit 0700d17

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed

Sources/SwiftASN1/Basic ASN1 Types/GeneralizedTime.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,12 @@ public struct GeneralizedTime: DERImplicitlyTaggable, Hashable, Sendable {
200200
extension GeneralizedTime: Comparable {
201201
@inlinable
202202
public static func <(lhs: GeneralizedTime, rhs: GeneralizedTime) -> Bool {
203-
if lhs.year < rhs.year { return true }
204-
if lhs.month < rhs.month { return true }
205-
if lhs.day < rhs.day { return true }
206-
if lhs.hours < rhs.hours { return true }
207-
if lhs.minutes < rhs.minutes { return true }
208-
if lhs.seconds < rhs.seconds { return true }
203+
if lhs.year < rhs.year { return true } else if lhs.year > rhs.year { return false }
204+
if lhs.month < rhs.month { return true } else if lhs.month > rhs.month { return false }
205+
if lhs.day < rhs.day { return true } else if lhs.day > rhs.day { return false }
206+
if lhs.hours < rhs.hours { return true } else if lhs.hours > rhs.hours { return false }
207+
if lhs.minutes < rhs.minutes { return true } else if lhs.minutes > rhs.minutes { return false }
208+
if lhs.seconds < rhs.seconds { return true } else if lhs.seconds > rhs.seconds { return false }
209209
return lhs.fractionalSeconds < rhs.fractionalSeconds
210210
}
211211
}

Sources/SwiftASN1/Basic ASN1 Types/UTCTime.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,11 @@ public struct UTCTime: DERImplicitlyTaggable, Hashable, Sendable {
184184
extension UTCTime: Comparable {
185185
@inlinable
186186
public static func <(lhs: UTCTime, rhs: UTCTime) -> Bool {
187-
if lhs.year < rhs.year { return true }
188-
if lhs.month < rhs.month { return true }
189-
if lhs.day < rhs.day { return true }
190-
if lhs.hours < rhs.hours { return true }
191-
if lhs.minutes < rhs.minutes { return true }
187+
if lhs.year < rhs.year { return true } else if lhs.year > rhs.year { return false }
188+
if lhs.month < rhs.month { return true } else if lhs.month > rhs.month { return false }
189+
if lhs.day < rhs.day { return true } else if lhs.day > rhs.day { return false }
190+
if lhs.hours < rhs.hours { return true } else if lhs.hours > rhs.hours { return false }
191+
if lhs.minutes < rhs.minutes { return true } else if lhs.minutes > rhs.minutes { return false }
192192
return lhs.seconds < rhs.seconds
193193
}
194194
}

Tests/SwiftASN1Tests/GeneralizedTimeTests.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,11 @@ final class GeneralizedTimeTests: XCTestCase {
208208
transformationsAndResults.append((modify(\.fractionalSeconds, of: original, by: 0.1), .greaterThan))
209209
transformationsAndResults.append((modify(\.fractionalSeconds, of: original, by: -0.1), .lessThan))
210210

211+
transformationsAndResults.append((
212+
try GeneralizedTime(year: 2019, month: 08, day: 08, hours: 08, minutes: 08, seconds: 08, fractionalSeconds: 0.205),
213+
.lessThan
214+
))
215+
211216
for (newValue, expectedResult) in transformationsAndResults {
212217
switch expectedResult {
213218
case .lessThan:

Tests/SwiftASN1Tests/UTCTimeTests.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ final class UTCTimeTests: XCTestCase {
4343
transformationsAndResults.append((modify(transform, of: original, by: -1), .lessThan))
4444
}
4545

46+
transformationsAndResults.append((
47+
try UTCTime(year: 2019, month: 08, day: 08, hours: 08, minutes: 08, seconds: 08),
48+
.lessThan
49+
))
50+
4651
for (newValue, expectedResult) in transformationsAndResults {
4752
switch expectedResult {
4853
case .lessThan:

0 commit comments

Comments
 (0)