Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in comparing Generalized and UTC Time #30

Merged
merged 1 commit into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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