// The day range and the house formats, case for case. // // Every one takes its `now` and its calendar; nothing here reads a clock, so // none of these has a detonation date. import CottonKit import CottonValues import XCTest @testable import CottonUI private func calendar(_ zone: String) -> Calendar { var out = Calendar(identifier: .gregorian) out.timeZone = TimeZone(identifier: zone)! out.locale = Locale(identifier: "en_US_POSIX") return out } private let ist = calendar("Asia/Kolkata") private let utc = calendar("UTC") private let losAngeles = calendar("America/Los_Angeles") private func at(_ iso: String, _ cal: Calendar = ist) -> Date { CottonDate.parse(iso, calendar: cal)! } final class DateFormatsTests: XCTestCase { // ── houseTime ───────────────────────────────────────────────────────── func testHouseTimeWritesAmPmInLowerCase() { XCTAssertEqual(DateFormats.houseTime(at("2026-08-17T19:30:00+05:30"), calendar: ist), "7:30 pm") XCTAssertEqual(DateFormats.houseTime(at("2026-08-17T07:05:00+05:30"), calendar: ist), "7:05 am") } func testHouseTimeWritesNoonAndMidnightWithoutALeadingZero() { XCTAssertEqual(DateFormats.houseTime(at("2026-08-17T12:00:00+05:30"), calendar: ist), "12:00 pm") XCTAssertEqual(DateFormats.houseTime(at("2026-08-17T00:00:00+05:30"), calendar: ist), "12:00 am") } // ── weekdayDate ─────────────────────────────────────────────────────── func testWeekdayDateWritesTheOneFormWithNoComma() { XCTAssertEqual(DateFormats.weekdayDate(at("2026-08-17T09:00:00+05:30"), calendar: ist), "Mon 17 Aug") } func testWeekdayDateKeepsTheSameFormInAnotherYear() { // The year is not this format's job. XCTAssertEqual(DateFormats.weekdayDate(at("2025-08-18T09:00:00+05:30"), calendar: ist), "Mon 18 Aug") } // ── R7: one day, one time ───────────────────────────────────────────── /// RULE: Where no heading names the day (a chat list, Recent) a slip says the time alone today, Yesterday, then the weekday date — the record's date label is this and no longer "2d ago". func testWhereNoHeadingNamesTheDayASlipSaysTheTimeThenYesterdayThenTheDay() { let now = at("2026-10-05T23:30:00+05:30") XCTAssertEqual(DateFormats.recordDateLabel(at("2026-10-05T03:24:00+05:30"), now: now, calendar: ist), "3:24 am") XCTAssertEqual(DateFormats.recordDateLabel(at("2026-10-04T03:24:00+05:30"), now: now, calendar: ist), "Yesterday") XCTAssertEqual(DateFormats.recordDateLabel(at("2026-10-02T03:24:00+05:30"), now: now, calendar: ist), "Fri 2 Oct") XCTAssertEqual(DateFormats.recordDateLabel(at("2025-10-02T03:24:00+05:30"), now: now, calendar: ist), "Thu 2 Oct 2025") XCTAssertEqual(DateFormats.recordDateLabel(at("2026-10-03T03:24:00+05:30"), now: now, calendar: ist), "Sat 3 Oct") } func testLongDateSpellsTheMonthOutWithTheYear() { XCTAssertEqual(DateFormats.longDate(at("2026-08-08T09:00:00+05:30"), calendar: ist), "8 August 2026") } // ── hasClockTime ────────────────────────────────────────────────────── // ── shortDate ───────────────────────────────────────────────────────── // ── parsing ─────────────────────────────────────────────────────────── func testMillisecondsAreAUnitYouHaveToSayOutLoud() throws { // The phone's `string | number | Date` union let a caller hand over // seconds where the row holds milliseconds, and nothing caught it. let millis = 1_786_000_000_000.0 let instant = try XCTUnwrap(CottonDate.parse(millis: millis)) XCTAssertEqual(instant.timeIntervalSince1970, 1_786_000_000, accuracy: 0.001) XCTAssertNil(CottonDate.parse(millis: .nan)) XCTAssertNil(CottonDate.parse(millis: .infinity)) } func testACalendarDayIsLOCALMidnightEvenWestOfGreenwich() { // The bug the parsing seam exists for: `new Date("2026-01-01")` is UTC // midnight, which is 31 December for everyone west of Greenwich — a // transaction's own screen printed the day before the one on the paper. // The TypeScript pins it with a `2026-01-01` case; the port dropped it. XCTAssertEqual(DateFormats.longDate(at("2026-01-01", losAngeles), calendar: losAngeles), "1 January 2026") XCTAssertEqual(DateFormats.longDate(at("2026-01-01", ist), calendar: ist), "1 January 2026") } // ── A calendar day, in any timezone ─────────────────────────────────── func testTheHarnessReallyDoesHonourAWestOfGreenwichZone() { XCTAssertLessThan(losAngeles.timeZone.secondsFromGMT(), 0) XCTAssertGreaterThan(ist.timeZone.secondsFromGMT(), 0) } // ── The record date line ────────────────────────────────────────────── /// RULE: A record's date line is the clock today, Yesterday, then the weekday date — never "5m ago" or "3d ago" (R7 d, h). func testRecordDateIsTheClockTodayThenYesterdayThenTheDay() { let now = at("2026-08-17T12:00:00+05:30") XCTAssertEqual(DateFormats.recordDateLabel(now.addingTimeInterval(-30), now: now, calendar: ist), "11:59 am") XCTAssertEqual(DateFormats.recordDateLabel(now.addingTimeInterval(-3 * 3600), now: now, calendar: ist), "9:00 am") XCTAssertEqual(DateFormats.recordDateLabel(now.addingTimeInterval(-24 * 3600), now: now, calendar: ist), "Yesterday") XCTAssertEqual(DateFormats.recordDateLabel(now.addingTimeInterval(-3 * 24 * 3600), now: now, calendar: ist), "Fri 14 Aug") XCTAssertEqual(DateFormats.recordDateLabel(now.addingTimeInterval(-9 * 24 * 3600), now: now, calendar: ist), "Sat 8 Aug") } // ── The parsing seam ────────────────────────────────────────────────── func testABareDayIsLocalMidnightAndAnInstantIsAnInstant() throws { let day = try XCTUnwrap(CottonDate.parse("2026-08-08", calendar: utc)) XCTAssertEqual(utc.component(.hour, from: day), 0) XCTAssertTrue(CottonDate.isDayOnly("2026-08-08")) XCTAssertFalse(CottonDate.isDayOnly("2026-08-08T00:00:00Z")) XCTAssertFalse(CottonDate.isDayOnly("2026-8-08")) XCTAssertFalse(CottonDate.isDayOnly("20260808")) XCTAssertNotNil(CottonDate.parse("2026-08-08T02:00:00Z", calendar: utc)) XCTAssertNotNil(CottonDate.parse("2026-08-08T02:00:00.500Z", calendar: utc)) XCTAssertNil(CottonDate.parse("last Tuesday", calendar: utc)) } // ── a day is a day ────────────────────────────────────────────────── /// RULE: A bare day and a zoneless stamp read as written in any zone, and a month ("2027-11") opens as its first day with no clock. func testADayAndAMonthReadAsWrittenInAnyZone() { for zone in [ist, utc, losAngeles, calendar("Australia/Melbourne")] { let opened = WhenValue.read(.text("2027-11"), calendar: zone) XCTAssertNil(opened?.time) XCTAssertEqual(opened.map { zone.component(.day, from: $0.day) }, 1) } // A month column's words ("Nov 2027", any value) are the core's // (`page_dates_tests.rs`, the month-and-year case); Swift draws them. // A zoneless stamp is the house's wall clock, read as written in any zone. for zone in [ist, utc, losAngeles] { XCTAssertEqual(FaceText.parse("2026-10-04T07:00:00", calendar: zone).map { zone.component(.hour, from: $0.date) }, 7) } XCTAssertNil(FaceText.month("2027-13")) XCTAssertNil(FaceText.month("2027-11-08")) } }