adds first version of the app
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
import Foundation
|
||||
|
||||
/// The created date of a block: its date line in the page file and its label in the margin.
|
||||
public enum CreatedDate {
|
||||
static let months = ["jan", "feb", "mrt", "apr", "mei", "jun", "jul", "aug", "sep", "okt", "nov", "dec"]
|
||||
|
||||
/// The date line above the first line of a block, as `<!-- 2026-10-16 08:55 -->`.
|
||||
public static func line(for date: Date, timeZone: TimeZone = .current) -> String {
|
||||
let c = calendar(timeZone).dateComponents([.year, .month, .day, .hour, .minute], from: date)
|
||||
return "<!-- \(pad(c.year, 4))-\(pad(c.month))-\(pad(c.day)) \(pad(c.hour)):\(pad(c.minute)) -->"
|
||||
}
|
||||
|
||||
/// The date a page file line holds; nil when the line is not exactly a date line.
|
||||
///
|
||||
/// A line that does not come back the same when written again (`2026-02-30`, or a time
|
||||
/// skipped by a clock change) is not a date, so the page file round trip stays exact.
|
||||
public static func date(fromLine line: String, timeZone: TimeZone = .current) -> Date? {
|
||||
guard let match = line.wholeMatch(of: /<!-- (\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}) -->/) else { return nil }
|
||||
let components = DateComponents(
|
||||
year: Int(match.1), month: Int(match.2), day: Int(match.3), hour: Int(match.4), minute: Int(match.5)
|
||||
)
|
||||
guard let date = calendar(timeZone).date(from: components), self.line(for: date, timeZone: timeZone) == line else { return nil }
|
||||
return date
|
||||
}
|
||||
|
||||
/// The margin label: `16 okt 08:55`, with the year when it is not the year of `now`.
|
||||
public static func label(for date: Date, now: Date, timeZone: TimeZone = .current) -> String {
|
||||
let calendar = calendar(timeZone)
|
||||
let c = calendar.dateComponents([.year, .month, .day, .hour, .minute], from: date)
|
||||
let month = months[((c.month ?? 1) - 1) % 12]
|
||||
let year = c.year == calendar.component(.year, from: now) ? "" : " \(c.year ?? 0)"
|
||||
return "\(c.day ?? 0) \(month)\(year) \(pad(c.hour)):\(pad(c.minute))"
|
||||
}
|
||||
|
||||
private static func calendar(_ timeZone: TimeZone) -> Calendar {
|
||||
var calendar = Calendar(identifier: .gregorian)
|
||||
calendar.timeZone = timeZone
|
||||
return calendar
|
||||
}
|
||||
|
||||
private static func pad(_ value: Int?, _ width: Int = 2) -> String {
|
||||
let digits = String(value ?? 0)
|
||||
return String(repeating: "0", count: max(0, width - digits.count)) + digits
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user