LEARN VOID

Dates and clocks

Validate a calendar date and choose between wall time and elapsed time.

calendar-time.voidDownload source
use time

func entry(february_day: i32) -> i32:
    if time.make_date(2024, 2, february_day as i64) is Ok(var valid):
        return february_day
    return 0
Input 29Expected result 29

Check a calendar day

February 2024 has a leap day. time.make_date returns a result, so the caller can handle an invalid day without constructing an invalid date. Try 29, then 30.

Choose the right clock

time.now_utc() asks for an instant on the wall clock. Use time.monotonic_now() to measure elapsed time within a process. Both queries can fail.

Resolve local time

A local date and clock reading do not uniquely identify an instant across daylight-saving transitions. time.resolve_local takes an explicit choice with a rejecting default. Handle an ambiguous or missing local time at the boundary.

Continue learning with Recoverable errors.