tempo/offset

Functions to use with the Offset type in Tempo. The offset values represents the time difference between the current time and UTC time.

Example

import tempo/offset

pub fn get_system_offset() {
  offset.local()
  |> offset.to_string
  // -> "+05:00"
}

Constants

pub const utc: Offset

The Tempo representation of the UTC offset.

Functions

pub fn describe_parse_error(error: OffsetParseError) -> String

Converts an offset parse error to a human readable error message.

Example

offset.from_string("bad offset")
|> snag.map_error(with: offset.describe_parse_error)
// -> snag.error("Invalid offset format: "bad offset"")
pub fn from_duration(duration: Duration) -> Result(Offset, Nil)

Creates a new validated offset from a duration. Offsets are most commonly expressed as a number of minutes or hours.

Example

offset.new(duration.minutes(-65))
|> result.map(offset.to_string)
// -> Ok("-01:05")
offset.new(duration.hours(36))
// -> Error(Nil)
pub fn from_string(
  offset: String,
) -> Result(Offset, OffsetParseError)

Tries to create a new offset from a string. Accepted formats are (+-)hh:mm, (+-)hhmm, (+-)hh, and (+-)h.

Example

offset.from_string("-04")
|> result.map(offset.to_string)
// -> Ok("-04:00")
pub fn literal(offset: String) -> Offset

Creates a new offset from a string literal, but will panic if the string is invalid. Accepted formats are (+-)hh:mm, (+-)hhmm, (+-)hh, and (+-)h.

Useful for declaring offset literals that you know are valid within your program.

Example

offset.literal("-04:00")
|> offset.to_string
// -> "-04:00"
pub fn local() -> Offset

Returns the local offset of the host.

Example

offset.local()
|> offset.to_string
// -> "+05:00"
pub fn new(from duration: Duration) -> Result(Offset, Nil)

Deprecated: Use the more explicitly named `from_duration` function instead

pub fn to_duration(offset: Offset) -> Duration

Converts an offset to a duration.

Example

offset.literal("-04:00")
|> offset.to_duration
// -> duration.hours(4)
pub fn to_string(offset: Offset) -> String

Converts an offset to a string representation.

Will not return “Z” for a zero offset because it is probably not what the user wants without the context of a full datetime. Datetime modules building on this should cover formatting for Z themselves.

Example

offset.literal("-00")
|> offset.to_string
// -> "-00:00"
Search Document