gtz

Functions to provide simple timezone support for other Gleam datetime libraries.

Values

pub fn calculate_offset(
  timestamp: timestamp.Timestamp,
  in time_zone: String,
) -> Result(duration.Duration, Nil)

Calculates the offset of a given timestamp in a specific time zone. Returns an error if the time zone is invalid.

This can be combined with the gleam_time package to convert timestamps to calendar dates in a given time zone.

Example

import gtz
import gleam/time/timestamp

let my_ts =
  1_729_257_776
  |> timestamp.from_unix_seconds

let assert Ok(offset) =
  gtz.calculate_offset(my_ts, in: "America/New_York")

timestamp.to_calendar(my_ts, offset)
// -> #(
//   calendar.Date(2024, calendar.October, 18),
//   calendar.TimeOfDay(9, 22, 56, 0)
// )
pub fn local_name() -> String

Returns the name of the host system’s timezone.

Examples

gtz.local_name()
// -> "Europe/London"
pub fn timezone(
  name: String,
) -> Result(tempo.TimeZoneProvider, Nil)

Constructs a TimeZoneProvider type to be used with the Tempo package. Returns an error if the timezone is not valid.

Examples

import tempo/datetime

let assert Ok(tz) = gtz.timezone("America/New_York")

datetime.literal("2024-06-21T06:30:02.334Z")
|> datetime.to_timezone(tz)
|> datetime.to_string
// -> "2024-01-03T02:30:02.334-04:00"
Search Document