Gigasecond
Find the moment one gigasecond, a thousand million seconds, after a given moment.
Adapted from Exercism’s problem specification (MIT). Listed under Dates & Time.
// FILE: gigasecond.nomi
//# The moment one gigasecond after a given one.
import std::calendar.{NaiveDateTime, Seconds}
/// One gigasecond: a thousand million seconds.
once gigasecond = Seconds(1_000_000_000)
/// The moment one gigasecond after `moment`.
pub fn add(moment: NaiveDateTime) -> NaiveDateTime
moment + gigasecond
end
// FILE: gigasecond_test.nomi
import
std::calendar.{Date, NaiveDateTime}
gigasecond
end
test "date only specification of time"
[
("date only specification of time", Date"2011-04-25", NaiveDateTime"2043-01-01T01:46:40"),
(
"second test for date only specification of time",
Date"1977-06-13",
NaiveDateTime"2009-02-19T01:46:40",
),
(
"third test for date only specification of time",
Date"1959-07-19",
NaiveDateTime"1991-03-27T01:46:40",
),
]
|> subtests (description, day, expected), description
assert NaiveDateTime.at_midnight(day) |> gigasecond.add() == expected
end
end
test "full time specified"
[
(
"full time specified",
NaiveDateTime"2015-01-24T22:00:00",
NaiveDateTime"2046-10-02T23:46:40",
),
(
"full time with day roll-over",
NaiveDateTime"2015-01-24T23:59:59",
NaiveDateTime"2046-10-03T01:46:39",
),
]
|> subtests (description, moment, expected), description
assert gigasecond.add(moment) == expected
end
end
test "does not mutate the input"
moment = NaiveDateTime"2015-01-24T23:59:59"
_later = gigasecond.add(moment)
assert moment == NaiveDateTime"2015-01-24T23:59:59"
endok date only specification of time (3 cases)
ok full time specified (2 cases)
ok does not mutate the input
test result: ok. 6 passed, 0 failed
How it’s written
Section titled “How it’s written”- The moment is std/calendar’s
NaiveDateTime. The canonical moments carry no zone or offset, and the answer is the wall-clock reading a gigasecond later, so a zonedDateTimewould add a zone the data does not have (style §22 keepsNaiveDateTimefor such readings). - The gigasecond is
once gigasecond = Seconds(1_000_000_000), andaddismoment + gigasecond: std/calendar’sAdd<Seconds, NaiveDateTime>does the carrying into minutes, hours, days, months and years. - Three canonical moments are dates alone. The test table holds them as
Date"2011-04-25"literals and the test turns each into midnight withNaiveDateTime.at_midnight, so the data says what the canonical data says. The expected moments areNaiveDateTime"..."literals, compared as values. - The canonical “does not mutate the input” case is a test that adds a gigasecond and asserts the moment is still what it was. Nomi values cannot change, so it can only pass; it is kept so every canonical case has a test.