calendar/time

Types

A Time struct and functions.

The Time struct contains the fields hour, minute, second and microseconds. New times can be built with the new function.

The functions on this module work with the Time struct as well as any struct that contains the same fields as the Time struct, such as NaiveDateTime and DateTime.

pub type Time {
  Time(
    hour: Int,
    minute: Int,
    second: Int,
    microsecond: #(Int, Int),
    calendar: String,
  )
}

Constructors

  • Time(
      hour: Int,
      minute: Int,
      second: Int,
      microsecond: #(Int, Int),
      calendar: String,
    )

Compare two times.

pub type TimeComparison {
  Lt
  Eq
  Gt
}

Constructors

  • Lt
  • Eq
  • Gt
pub type TimeError {
  InvalidTime
  InvalidHour
  InvalidMinute
  InvalidSecond
  InvalidMicrosecond
}

Constructors

  • InvalidTime
  • InvalidHour
  • InvalidMinute
  • InvalidSecond
  • InvalidMicrosecond
pub type TimeFormat {
  Extended
  Basic
}

Constructors

  • Extended
  • Basic
pub type TimeUnit {
  Second
  Millisecond
  Microsecond
  Native
}

Constructors

  • Second
  • Millisecond
  • Microsecond
  • Native

Values

pub fn add(
  time: Time,
  amount: Int,
  unit: TimeUnit,
) -> Result(Time, TimeError)

Adds a duration to a time.

pub fn after(time1: Time, time2: Time) -> Bool

Check if first time is after second time.

pub fn before(time1: Time, time2: Time) -> Bool

Check if first time is before second time.

pub fn compare(time1: Time, time2: Time) -> TimeComparison
pub fn convert(
  time: Time,
  target_calendar: String,
) -> Result(Time, TimeError)

Convert time between calendars.

pub fn convert_unchecked(
  time: Time,
  target_calendar: String,
) -> Time

Convert time between calendars, panicking on error.

pub fn convert_unit(
  amount: Int,
  from_unit: TimeUnit,
  to_unit: TimeUnit,
) -> Int

Convert between time units.

pub fn diff(time1: Time, time2: Time, unit: TimeUnit) -> Int

Calculate difference between two times.

pub fn from_erl(
  erl_time: #(Int, Int, Int),
  microsecond: #(Int, Int),
  calendar: String,
) -> Result(Time, TimeError)

Creates time from Erlang time tuple.

pub fn from_erl_unchecked(
  erl_time: #(Int, Int, Int),
  microsecond: #(Int, Int),
  calendar: String,
) -> Time

Creates time from Erlang time tuple, panicking on error.

pub fn from_iso8601(
  time_string: String,
) -> Result(Time, TimeError)

Parses an ISO8601 time string.

pub fn from_iso8601_unchecked(time_string: String) -> Time

Parses an ISO8601 time string, panicking on error.

pub fn from_seconds_after_midnight(
  seconds: Int,
) -> Result(Time, TimeError)

Creates a Time from seconds since midnight.

pub fn from_seconds_after_midnight_with_microsecond(
  seconds: Int,
  microsecond: #(Int, Int),
  calendar: String,
) -> Result(Time, TimeError)

Creates a Time from seconds since midnight with microsecond precision.

pub fn from_timestamp(timestamp: Int) -> Result(Time, TimeError)

Creates a Time from a Unix timestamp. Note: This extracts only the time portion, ignoring the date.

pub fn inspect(time: Time) -> String

Get string representation for inspection.

pub fn is_midnight(time: Time) -> Bool

Check if a time is at midnight.

pub fn is_valid(
  hour: Int,
  minute: Int,
  second: Int,
  microsecond: #(Int, Int),
) -> Bool

Check if a time is valid.

pub fn max_precision(unit: TimeUnit) -> Int

Get the maximum precision for a time unit.

pub fn midnight() -> Result(Time, TimeError)

Get the time at midnight (00:00:00).

pub fn new(
  hour: Int,
  minute: Int,
  second: Int,
  microsecond: #(Int, Int),
  calendar: String,
) -> Result(Time, TimeError)

Creates a new Time struct.

Returns an error if the time is invalid.

pub fn new_simple(
  hour: Int,
  minute: Int,
  second: Int,
) -> Result(Time, TimeError)

Creates a new Time struct with default values. Uses microsecond #(0, 0) and Calendar.ISO as defaults.

pub fn new_unchecked_bang(
  hour: Int,
  minute: Int,
  second: Int,
  microsecond: #(Int, Int),
  calendar: String,
) -> Time

Creates a new Time, panicking if invalid.

pub fn noon() -> Result(Time, TimeError)

Get the time at noon (12:00:00).

pub fn normalize_precision(
  microsecond: #(Int, Int),
  target_precision: Int,
) -> #(Int, Int)

Normalize microseconds to a specific precision.

pub fn second_of_day(time: Time) -> Int

Get the second of the day (0-86399).

pub fn shift(
  time: Time,
  duration: duration.Duration,
) -> Result(Time, TimeError)

Shift time by a duration.

pub fn to_erl(time: Time) -> #(Int, Int, Int)

Converts time to Erlang time tuple.

pub fn to_iso8601(time: Time) -> String

Converts a Time to ISO8601 format.

pub fn to_iso8601_with_format(
  time: Time,
  format: TimeFormat,
) -> String

Converts a Time to ISO8601 format with specified format.

pub fn to_microseconds_after_midnight(time: Time) -> Int

Converts time to total microseconds since midnight.

pub fn to_seconds_after_midnight(time: Time) -> Int

Converts time to total seconds since midnight.

pub fn to_seconds_after_midnight_tuple(time: Time) -> #(Int, Int)

Converts time to total seconds since midnight as a tuple with microseconds.

pub fn to_string(time: Time) -> String

Converts a Time to a string in ISO8601 format.

pub fn to_timestamp(time: Time) -> Int

Converts a Time to a Unix timestamp (seconds since epoch). Note: This requires a date to be meaningful. This function assumes the Unix epoch date (1970-01-01) for demonstration purposes.

pub fn truncate(time: Time, precision: Int) -> Time

Truncate time precision.

pub fn utc_now() -> Result(Time, TimeError)

Returns the current time in UTC.

pub fn utc_now_with_precision(
  precision: TimeUnit,
  calendar: String,
) -> Result(Time, TimeError)

Returns the current time in UTC with specified precision and calendar.

Search Document