How can I get the current time in gleam?

47 Views Asked by At

How can I get the current date/time in Gleam? I don't see any relevant packages in the Gleam standard library.

1

There are 1 best solutions below

0
Andy Fleming On BEST ANSWER

There is no time functionality built into the standard library as of 1.0.0.

Option 1: Use @external to get it via erlang.

import gleam/io
pub type DateTime
// An external function that creates an instance of the type
@external(erlang, "os", "timestamp")
pub fn now() -> DateTime

pub fn main() {
  io.debug(now())
}

Option 2: Use a library like birl

import birl
import birl/duration

pub fn main() {
    let now = birl.now()
    let two_weeks_later = birl.add(now, duration.weeks(2))
    birl.to_iso8601(two_weeks_later)
}