Dark Mode

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

maxdeviant/bigben

Repository files navigation

bigben

A clock abstraction, with time travel.

Installation

gleam add bigben

Usage

clock.now |> timestamp.to_calendar(calendar.utc_offset) let month = date.month |> calendar.month_to_string let day = date.day |> int.to_string let year = date.year |> int.to_string io.println("Today is " <> month <> " " <> day <> ", " <> year <> ".") }">import bigben/clock.{type Clock}
import bigben/fake_clock
import gleam/int
import gleam/io
import gleam/time/calendar
import gleam/time/duration
import gleam/time/timestamp

pub fn main() {
// In your production code, you'll use the real `Clock` to get the time
// from the system:
let clock = clock.new()
what_day_is_it(clock)
// Today is July 26, 2025.

// In test code you can construct a `FakeClock`:
let assert Ok(now) = timestamp.parse_rfc3339("2024-04-08T02:26:31.464Z")
let fake_clock = fake_clock.new_at(now)
// and pass it off as a real `Clock`:
let clock = clock.from_fake(fake_clock)

what_day_is_it(clock)
// Today is April 8, 2024.

// We can then manipulate the clock to help us in our tests:
fake_clock.advance(fake_clock, duration.hours(4 * 24))

what_day_is_it(clock)
// Today is April 12, 2024.
}

fn what_day_is_it(clock: Clock) {
let #(date, _time) =
clock |> clock.now |> timestamp.to_calendar(calendar.utc_offset)

let month = date.month |> calendar.month_to_string
let day = date.day |> int.to_string
let year = date.year |> int.to_string

io.println("Today is " <> month <> " " <> day <> ", " <> year <> ".")
}

About

A clock abstraction, with time travel

Resources

Readme

License

MIT license

Stars

Watchers

Forks

Packages

Contributors

Languages