Hare by Example: Time

Hare offers support for time and durations. The `time` packages play well together in order to use timestamps, dates, and durations however we see fit.

use fmt;
use os;
use time;
use time::date;

export fn main() void = {
	// Let's get the current time instant based on the realtime
	// wall-clock time.
	const wall = time::now(time::clock::REALTIME);

	// We can even get the current monotonic clock time.
	const mono = time::now(time::clock::MONOTONIC);

	// We can compare these too instants
	fmt::println(time::compare(wall, mono))!;

	// We can also get the duration that is between the two.
	fmt::println(time::diff(wall, mono))!;

	// We can adjust any instant based on some duration.
	const forward = time::add(wall, 12 * time::HOUR);

	// We can even go backwards by adding a negative duration.
	const backward = time::add(wall, -10 * time::MINUTE);

	// We can get the current date and get a formatted version of
	// it for display.
	const curr_date = date::now();
	date::format(os::stdout, date::RFC3339, &curr_date)!;
	fmt::println()!;

	// We can parse a date from a string format.
	const parsed = date::from_str(date::RFC3339,
		"2023-01-13T13:13:13+0000", date::UTC)!;
	date::format(os::stdout, date::RFC3339, &parsed)!;
};
$ hare time.ha
4/4 tasks completed (100%)
1
-1751547149549043369
2025-07-07T14:19:17+0000
2023-01-13T13:13:13+0000