Cotton code
44 lines · 1.7 KB · SwiftRaw
// A time column's value, and where a clock's hands point for it: arithmetic// of the clock, not a look. The house's words for it are the app's// (`CottonUI/Editors/ColumnValues.swift`).import Foundation/// A clock time with no day: a time column's whole value, a date column's/// optional part.public struct ClockTime: Equatable, Hashable, Sendable, Comparable {    public var hour: Int    public var minute: Int    public init(hour: Int, minute: Int) {        self.hour = hour        self.minute = minute    }    /// Minutes since midnight — the order and the identity.    public var key: Int { hour * 60 + minute }    public static func < (left: ClockTime, right: ClockTime) -> Bool { left.key < right.key }    /// Reads a time column's `"HH:mm"`. Anything else is no time at all.    public static func read(_ value: RecordValue) -> ClockTime? {        guard case .text(let raw) = value else { return nil }        let parts = raw.split(separator: ":")        guard parts.count == 2, let hour = Int(parts[0]), let minute = Int(parts[1]),            (0..<24).contains(hour), (0..<60).contains(minute)        else { return nil }        return ClockTime(hour: hour, minute: minute)    }    public var stored: RecordValue { .text(String(format: "%02d:%02d", hour, minute)) }}/// The live hands drawn on the card's clock object.public enum CardEnamelPrint {    /// The two hands' angles, in degrees clockwise from twelve.    public static func hands(_ time: ClockTime) -> (hour: Double, minute: Double) {        let minute = Double(time.minute) * 6        let hour = Double(time.hour % 12) * 30 + Double(time.minute) * 0.5        return (hour, minute)    }}