Part 20: Utilities
The small standard packages that almost every real program reaches for sooner or later: Time for spans, clocks and calendar dates, TimeZone for the local time in a place, Random and Entropy for numbers you cannot predict — or deliberately can — Hash for fingerprints, and Uuid and NanoId for names nobody else will pick. Each lesson is short and stands on its own, and together they show how the standard library speaks about failure: a step that may not fit returns an optional, and a request the system may refuse returns a fallible.
What you will learn
- Spans of time with
Duration, where every step that could overflow says so in its type. - Measuring elapsed time with
Instant, the clock that never goes backwards. - Calendar dates and the leap-year rule, and reading dates with errors that point at the faulty byte.
- Moments in time: a
DateTimewith aUtcOffset, RFC 3339 text, and comparing moments throughTimestamp. - Time zones built from POSIX rules, and the local times that happen twice, or never, when the clocks change.
- Seeded generators that replay the same numbers, and distributions that shape them into dice, shuffles and bell curves.
- Unpredictable bytes from the operating system, and why a failure there must never be papered over.
- Fast, non-cryptographic hashes and checksums, all at once or piece by piece.
- Creating, printing and strictly parsing UUIDs, and shorter, URL-safe Nano IDs from any alphabet.
The part at a glance
flowchart LR
u(["Utilities"]) --> t["Time"]
u --> r["Randomness"]
u --> id["Fingerprints and names"]
t --> span["How long?<br/>Duration, Stopwatch"]
t --> when["When?<br/>Date, Date and time"]
t --> where["Where?<br/>Time zone"]
r --> rep["Repeatable<br/>Random, Distribution"]
r --> unp["Unguessable<br/>Entropy"]
id --> h["From data<br/>Hash"]
id --> uu["From nothing<br/>UUID, Nano ID"]
unp -. "seeds" .-> rep
unp -. "random bits" .-> uuLessons
| Lesson | What you will learn | |
|---|---|---|
| 20.1 | Duration | lengths of time, and arithmetic on them |
| 20.2 | Stopwatch | measure how long something takes |
| 20.3 | Date | calendar dates, parsing them, and leap years |
| 20.4 | Date and time | a date with a time and an offset, in RFC 3339 |
| 20.5 | Random | a reproducible random number generator |
| 20.6 | Distribution | draw from a range, shuffle, and sample a normal distribution |
| 20.7 | Entropy | unpredictable bytes from the operating system |
| 20.8 | Hash | fast non-cryptographic hashes and checksums |
| 20.9 | UUID | create, print and parse UUIDs |
| 20.10 | Nano ID | short, URL-safe random identifiers |
| 20.11 | Time zone | turn a moment into local time in a time zone, and back |
Before you start
This part leans on the native outcomes: Part 8: Optionals for every Duration? and Date?, and Part 9: Errors for parsing and for requests the system may refuse. The randomness lessons pass generators as mutable references and call generic functions, and Entropy and Hash hand bytes over through pointers from Part 15: Memory. Each lesson's package is in the Examples repository's Utilities/ folder:
cd Examples/Utilities/Duration
rux run
A few lessons print something different on every run — a random key, a die roll, a fresh UUID or Nano ID. Their pages say which lines to expect to change.
After this part
Part 21: Data formats reads and writes JSON and TOML, the formats programs use to exchange and configure data.
Before moving on, try the checkpoint projects. Guess is a number-guessing game whose secret comes from a generator seeded with entropy; Age works out an age in years, months and days from two dates; Password draws a password straight from the system's entropy without favouring any character; and Launch counts down to a rocket launch with SleepFor.
For the language rules this part relies on, see Structs, Enums and Pointers in the Rux Reference.
19.11 Memory streams
Write binary records into memory with a ByteWriter and read them back with a ByteReader, using the same Writer and Reader code that also writes and reads a file, then test a cut-short input without damaging a file.
20.1 Duration
Build a Duration from any unit and do arithmetic on it, where FromHours, Plus and Times return Duration? because they can overflow.