Cotton code
95 lines · 4.4 KB · SwiftRaw
// The app's date spellings, stated once.//// These spellings are NOT negotiable per surface, and they are the APP's, never// the device locale's — which is why every formatter here pins `en_US_POSIX`// and writes an explicit pattern. A phone set to US English must still read// "17 Aug", not "Aug 17".//// EVERY function takes its `now` and its `Calendar`; nothing reads the wall// clock or `Calendar.current` from inside. A fixed-`now` test over a function// that defaulted to the clock is a time bomb with a computable detonation date.// The default argument is at the CALL SITE's convenience only.import CottonKitimport CottonValuesimport Foundationpublic enum DateFormats {    /// `7:30 pm` — lowercase am/pm, ALWAYS, and no leading zero on the hour.    public static func houseTime(_ at: Date, calendar: Calendar = .current) -> String {        CottonDate.string(at, pattern: "h:mm a", calendar: calendar).lowercased()    }    /// `Mon 17 Aug` — short weekday, NO comma, short month, no year. The year    /// is not this format's job.    public static func weekdayDate(_ at: Date, calendar: Calendar = .current) -> String {        CottonDate.string(at, pattern: "EEE d MMM", calendar: calendar)    }    /// `8 August 2026` — the spelled-out form, for a record's own screen.    public static func longDate(_ at: Date, calendar: Calendar = .current) -> String {        CottonDate.string(at, pattern: "d MMMM yyyy", calendar: calendar)    }}/// The heading a list gives the day it is today, in the `dates` set's words.public enum DayHeads {    public static var today: String { ShellLines.line("dates", "heading.today") }    /// A month's short name off the dates set ("Oct"), `month` 1–12.    public static func month(_ month: Int) -> String {        let keys = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]        return keys.indices.contains(month - 1) ? ShellLines.line("dates", "months.\(keys[month - 1])") : ""    }}/// What a slip says of its date column, by the heading it stands under (R7/// b, c): the heading already named the day, so a slip under a day heading/// says the time alone, and one under a month heading says the day alone.public enum DayWords: Hashable, Sendable {    /// No heading names the day: the day and the time.    case full    /// Under a day heading: `3:24 am` or `All day`.    case timeUnderDay    /// Under a month heading: `Fri 2 Oct`.    case dayUnderMonth    /// A list with no heading over the slip (a chat list): the clock alone    /// today, `Yesterday`, else the weekday date (R7 d).    case noHeading    /// A date column's value as this context says it, from the core's    /// parts; nil for `.full`, which is the face's own words.    public func words(_ parts: DayParts?) -> String? {        guard let parts else { return nil }        switch self {        case .full: return nil        case .timeUnderDay: return parts.underDay        case .dayUnderMonth: return parts.weekdayDate        case .noHeading: return parts.recent        }    }}extension DateFormats {    // ── R7: the day and the time as the lists, pages and receipts say them ──    //    // The architect's ruling of 5 Oct 2026 (R7): a day is said ONCE and in ONE    // way, the year only when it is not this year, the time alone under a day    // heading, and never a relative count of days ("In 3 days", "2d ago").    // Every spelling below takes its `now` and its calendar: the heading a    // record sits under, the day its slip shows and the day its page shows all    // come from the same calendar day in the person's own zone (R7 g).    /// The caption a record's date line reads: the clock today, `Yesterday`,    /// else the weekday date, its year only outside this one (R7 d, h: a    /// relative count of days appears nowhere, so "2d ago" is gone).    public static func recordDateLabel(_ at: Date, now: Date, calendar: Calendar = .current) -> String {        if calendar.isDate(at, inSameDayAs: now) { return houseTime(at, calendar: calendar) }        if let yesterday = calendar.date(byAdding: .day, value: -1, to: now), calendar.isDate(at, inSameDayAs: yesterday) {            return "Yesterday"        }        let sameYear = calendar.component(.year, from: at) == calendar.component(.year, from: now)        return CottonDate.string(at, pattern: sameYear ? "EEE d MMM" : "EEE d MMM yyyy", calendar: calendar)    }}