Files
chandra/lib/timefmt/timefmt.mbt
T
nandiandClaude Sonnet 5.5 59ea224d38
CI / test (push) Successful in 45s
12-hour clock and hover-dependent cursor
- timefmt.hhmm12 + render_digital(hour12=); compositor panel shows '7:51 PM'
  (wasm clock component keeps 24h by default)
- wp_cursor_shape_v1: server lets clients pick their cursor; nested window
  forwards the shape to the host. Launcher button/entries show a hand, the
  search field a text cursor, client windows what the client asked for.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-24 19:51:16 -08:00

78 lines
2.0 KiB
MoonBit

///| Pure time formatting helpers shared by the clock widgets.
///|
pub fn pad2(n : Int) -> String {
if n < 10 && n >= 0 { "0" + n.to_string() } else { n.to_string() }
}
///|
let weekdays : Array[String] = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
///|
let months : Array[String] = [
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
]
///| Weekday 0 = Sunday. Aborts on out-of-range input instead of guessing.
pub fn weekday_short(i : Int) -> String {
guard i >= 0 && i < 7 else { abort("weekday out of range: \{i}") }
weekdays[i]
}
///| Month 1..12.
pub fn month_short(m : Int) -> String {
guard m >= 1 && m <= 12 else { abort("month out of range: \{m}") }
months[m - 1]
}
///|
pub fn hhmm(hour : Int, minute : Int) -> String {
pad2(hour) + ":" + pad2(minute)
}
///| 12-hour clock, e.g. 14:07 -> "2:07 PM", 00:05 -> "12:05 AM".
pub fn hhmm12(hour : Int, minute : Int) -> String {
let h = hour % 12
let h = if h == 0 { 12 } else { h }
h.to_string() + ":" + pad2(minute) + (if hour >= 12 { " PM" } else { " AM" })
}
///|
pub fn hhmmss(hour : Int, minute : Int, second : Int) -> String {
hhmm(hour, minute) + ":" + pad2(second)
}
///| e.g. "Tue 24 Nov"
pub fn date_line(weekday : Int, day : Int, month : Int) -> String {
weekday_short(weekday) + " " + day.to_string() + " " + month_short(month)
}
///| The `bits` low bits of `digit`, most significant first.
pub fn bcd_bits(digit : Int, bits : Int) -> Array[Bool] {
let out = []
let mut i = bits - 1
while i >= 0 {
out.push(((digit >> i) & 1) == 1)
i = i - 1
}
out
}
///|
test "formatting" {
assert_eq(hhmm(9, 5), "09:05")
assert_eq(hhmmss(23, 59, 7), "23:59:07")
assert_eq(hhmm12(14, 7), "2:07 PM")
assert_eq(hhmm12(0, 5), "12:05 AM")
assert_eq(hhmm12(12, 0), "12:00 PM")
assert_eq(hhmm12(23, 59), "11:59 PM")
assert_eq(hhmm12(9, 30), "9:30 AM")
assert_eq(date_line(2, 24, 11), "Tue 24 Nov")
}
///|
test "bcd bits" {
assert_eq(bcd_bits(5, 4), [false, true, false, true])
assert_eq(bcd_bits(2, 2), [true, false])
}