Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

A time in History stays the same however often it is written

Țugui Dragoș3b5d95b64c0d
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/LogTime.swift b/Packages/PeelCore/Sources/PeelCore/Removal/LogTime.swift
index 3d16ef1e..282a1fc4 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/LogTime.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/LogTime.swift
@@ -5,7 +5,17 @@ import Foundation
enum LogTime {
static let encoding = JSONEncoder.DateEncodingStrategy.custom { date, encoder in
var container = encoder.singleValueContainer()
- try container.encode(date.formatted(milliseconds))
+ try container.encode(text(for: date))
+ }
+
+ /// `date` to the nearest millisecond, as the logs write it. The digits come from whole milliseconds rather
+ /// than from the formatter, which cuts the fraction: a time read back from a log is a hair under what was
+ /// written, so cut, it would lose a millisecond each time the log is written again.
+ static func text(for date: Date) -> String {
+ let total = (date.timeIntervalSince1970 * 1_000).rounded()
+ let seconds = (total / 1_000).rounded(.down)
+ let fraction = String(format: "%03d", Int(total - seconds * 1_000))
+ return "\(Date(timeIntervalSince1970: seconds).formatted(.iso8601).dropLast()).\(fraction)Z"
}
static let decoding = JSONDecoder.DateDecodingStrategy.custom { decoder in
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/RemovalHistoryTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/RemovalHistoryTests.swift
index faf1f4fc..021ae6c5 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/RemovalHistoryTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/RemovalHistoryTests.swift
@@ -98,6 +98,23 @@ struct RemovalHistoryTests {
#expect(try decoder.decode([RemovalRecord].self, from: stored).first?.date == Date(timeIntervalSince1970: 5.25))
}
+ /// A time read from a log is written back as it was read, since every change writes the whole log again: a
+ /// time that lost a millisecond each time could move behind a part that moved after it.
+ @Test func aTimeReadBackIsWrittenAsItWas() throws {
+ let encoder = JSONEncoder()
+ encoder.dateEncodingStrategy = LogTime.encoding
+ let decoder = JSONDecoder()
+ decoder.dateDecodingStrategy = LogTime.decoding
+ // Read back, .002 is a hair under two milliseconds, as a binary fraction is.
+ let written = try encoder.encode([Date(timeIntervalSinceReferenceDate: 800_000_015.002_07)])
+
+ let again = try encoder.encode(try decoder.decode([Date].self, from: written))
+
+ #expect(String(decoding: written, as: UTF8.self) == #"["2026-05-09T06:13:35.002Z"]"#)
+ #expect(String(decoding: again, as: UTF8.self) == String(decoding: written, as: UTF8.self))
+ #expect(LogTime.text(for: Date(timeIntervalSince1970: 5.2506)) == "1970-01-01T00:00:05.251Z", "the nearest millisecond")
+ }
+
/// `removals.json` is a file any process can rewrite, and Swift's `+` traps on overflow, so the sizes read
/// from it are checked and added up without trapping.
@Test func aSizeNobodyCouldHaveMeasuredNeitherCrashesNorCounts() throws {