Clock
A time of day without a date that wraps when minutes are added or taken away.
Adapted from Exercism’s problem specification (MIT). Listed under Dates & Time.
// FILE: clock.nomi
//# A clock that handles times without dates and wraps around midnight.
once minutes_per_day: PositiveInt = 1_440
/// A time of day without a date, held as minutes past midnight. Two clocks
/// that show the same time are equal.
pub opaque type Clock Int
impl Clock
/// Builds the clock that `hour` hours and `minute` minutes past midnight
/// shows. Either may be negative or past a day; the clock wraps.
pub fn new(hour: Int, minute: Int) -> Clock
Clock(wrap(hour * 60 + minute))
end
/// Moves the clock forward by `minutes`.
pub fn add(Clock(time), minutes: Int) -> Clock
Clock(wrap(time + minutes))
end
/// Moves the clock back by `minutes`.
pub fn subtract(Clock(time), minutes: Int) -> Clock
Clock(wrap(time - minutes))
end
end
impl Display for Clock
fn to_string(Clock(time)) -> String
"${two_digits(time / 60)}:${two_digits(time % 60)}"
end
end
/// Brings any count of minutes into one day, rounding toward the past, so
/// `-1` is `23:59`.
fn wrap(minutes: Int) -> Int
Int.modulo(minutes, minutes_per_day)
end
fn two_digits(n: Int) -> String
Int.to_string(n) |> String.pad_start(2, "0")
end
// FILE: clock_test.nomi
import clock.Clock
test "Create a new clock with an initial time"
[
("on the hour", (8, 0), "08:00"),
("past the hour", (11, 9), "11:09"),
("midnight is zero hours", (24, 0), "00:00"),
("hour rolls over", (25, 0), "01:00"),
("hour rolls over continuously", (100, 0), "04:00"),
("sixty minutes is next hour", (1, 60), "02:00"),
("minutes roll over", (0, 160), "02:40"),
("minutes roll over continuously", (0, 1723), "04:43"),
("hour and minutes roll over", (25, 160), "03:40"),
("hour and minutes roll over continuously", (201, 3001), "11:01"),
("hour and minutes roll over to exactly midnight", (72, 8640), "00:00"),
("negative hour", (-1, 15), "23:15"),
("negative hour rolls over", (-25, 0), "23:00"),
("negative hour rolls over continuously", (-91, 0), "05:00"),
("negative minutes", (1, -40), "00:20"),
("negative minutes roll over", (1, -160), "22:20"),
("negative minutes roll over continuously", (1, -4820), "16:40"),
("negative sixty minutes is previous hour", (2, -60), "01:00"),
("negative hour and minutes both roll over", (-25, -160), "20:20"),
("negative hour and minutes both roll over continuously", (-121, -5810), "22:10"),
]
|> subtests (description, (hour, minute), expected), description
assert "${Clock.new(hour, minute)}" == expected
end
end
test "Add minutes"
[
("add minutes", (10, 0), 3, "10:03"),
("add no minutes", (6, 41), 0, "06:41"),
("add to next hour", (0, 45), 40, "01:25"),
("add more than one hour", (10, 0), 61, "11:01"),
("add more than two hours with carry", (0, 45), 160, "03:25"),
("add across midnight", (23, 59), 2, "00:01"),
("add more than one day (1500 min = 25 hrs)", (5, 32), 1500, "06:32"),
("add more than two days", (1, 1), 3500, "11:21"),
]
|> subtests (description, (hour, minute), minutes, expected), description
clock = Clock.new(hour, minute)
assert "${Clock.add(clock, minutes)}" == expected
end
end
test "Subtract minutes"
[
("subtract minutes", (10, 3), 3, "10:00"),
("subtract to previous hour", (10, 3), 30, "09:33"),
("subtract more than an hour", (10, 3), 70, "08:53"),
("subtract across midnight", (0, 3), 4, "23:59"),
("subtract more than two hours", (0, 0), 160, "21:20"),
("subtract more than two hours with borrow", (6, 15), 160, "03:35"),
("subtract more than one day (1500 min = 25 hrs)", (5, 32), 1500, "04:32"),
("subtract more than two days", (2, 20), 3000, "00:20"),
]
|> subtests (description, (hour, minute), minutes, expected), description
clock = Clock.new(hour, minute)
assert "${Clock.subtract(clock, minutes)}" == expected
end
end
test "Compare two clocks for equality"
[
("clocks with same time", (15, 37), (15, 37), True),
("clocks a minute apart", (15, 36), (15, 37), False),
("clocks an hour apart", (14, 37), (15, 37), False),
("clocks with hour overflow", (10, 37), (34, 37), True),
("clocks with hour overflow by several days", (3, 11), (99, 11), True),
("clocks with negative hour", (22, 40), (-2, 40), True),
("clocks with negative hour that wraps", (17, 3), (-31, 3), True),
("clocks with negative hour that wraps multiple times", (13, 49), (-83, 49), True),
("clocks with minute overflow", (0, 1), (0, 1441), True),
("clocks with minute overflow by several days", (2, 2), (2, 4322), True),
("clocks with negative minute", (2, 40), (3, -20), True),
("clocks with negative minute that wraps", (4, 10), (5, -1490), True),
("clocks with negative minute that wraps multiple times", (6, 15), (6, -4305), True),
("clocks with negative hours and minutes", (7, 32), (-12, -268), True),
("clocks with negative hours and minutes that wrap", (18, 7), (-54, -11513), True),
("full clock and zeroed clock", (24, 0), (0, 0), True),
]
|> subtests (description, (hour1, minute1), (hour2, minute2), expected), description
assert (Clock.new(hour1, minute1) == Clock.new(hour2, minute2)) == expected
end
endok Create a new clock with an initial time (20 cases)
ok Add minutes (8 cases)
ok Subtract minutes (8 cases)
ok Compare two clocks for equality (16 cases)
test result: ok. 52 passed, 0 failed
How it’s written
Section titled “How it’s written”Clockis apub opaque type Clock Intholding minutes past midnight, always in0..1440. Two clocks that show the same time hold the same Int, so the built-in==is the canonical equality.Clock.addandClock.subtractare owner functions taking anIntof minutes. Animpl Add<Int, Clock> for Clock(clock + 3) was the other option; named functions match the exercise and keepIntminutes from looking like a duration type.- Rendering is an
impl Display, and tests compare"${clock}"with the canonicalHH:MMtext. - Parameters destructure the distinct directly (
fn add(Clock(time), minutes: Int)). wrapbrings any count of minutes into one day withInt.modulo, whose result takes the divisor’s sign, so-1minutes is1439.%takes the dividend’s sign and would leave it negative.