calendar/datetime

Types

A datetime implementation with a time zone.

This datetime can be seen as a snapshot of a date and time at a given time zone. For such purposes, it also includes both UTC and Standard offsets, as well as the zone abbreviation field used exclusively for formatting purposes.

pub type DateTime {
  DateTime(
    year: Int,
    month: Int,
    day: Int,
    hour: Int,
    minute: Int,
    second: Int,
    time_zone: String,
    zone_abbr: String,
    utc_offset: Int,
    std_offset: Int,
    microsecond: #(Int, Int),
    calendar: String,
  )
}

Constructors

  • DateTime(
      year: Int,
      month: Int,
      day: Int,
      hour: Int,
      minute: Int,
      second: Int,
      time_zone: String,
      zone_abbr: String,
      utc_offset: Int,
      std_offset: Int,
      microsecond: #(Int, Int),
      calendar: String,
    )
pub type DateTimeError {
  InvalidDateTime
  InvalidDate
  InvalidTime
  InvalidFormat
  TimeZoneNotFound
  UtcOnlyTimeZoneDatabase
  AmbiguousTime
  GapInTime
}

Constructors

  • InvalidDateTime
  • InvalidDate
  • InvalidTime
  • InvalidFormat
  • TimeZoneNotFound
  • UtcOnlyTimeZoneDatabase
  • AmbiguousTime
  • GapInTime
pub type DateTimeFormat {
  Extended
  Basic
}

Constructors

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

Constructors

  • Native
  • Second
  • Millisecond
  • Microsecond
  • Nanosecond

Values

pub fn add(
  dt: DateTime,
  amount: Int,
  unit: TimeUnit,
) -> Result(DateTime, DateTimeError)

Adds an amount of time to a DateTime in the given unit.

pub fn add_seconds(
  dt: DateTime,
  seconds: Int,
) -> Result(DateTime, DateTimeError)

Add seconds to a DateTime (adjusts UTC time).

pub fn after(dt1: DateTime, dt2: DateTime) -> Bool

Returns true if the first datetime is strictly later than the second.

pub fn before(dt1: DateTime, dt2: DateTime) -> Bool

Returns true if the first datetime is strictly earlier than the second.

pub fn compare(dt1: DateTime, dt2: DateTime) -> order.Order
pub fn convert(
  dt: DateTime,
  target_calendar: String,
) -> Result(DateTime, DateTimeError)

Converts the given datetime from its calendar to the given calendar.

pub fn diff(dt1: DateTime, dt2: DateTime, unit: TimeUnit) -> Int

Calculates the difference between two datetimes in the specified unit.

pub fn from_erl(
  erl_datetime: #(#(Int, Int, Int), #(Int, Int, Int)),
  time_zone: String,
  zone_abbr: String,
  utc_offset: Int,
  std_offset: Int,
) -> Result(DateTime, DateTimeError)

Creates DateTime from Erlang datetime tuple.

pub fn from_gregorian_seconds(
  seconds: Int,
  microsecond: #(Int, Int),
  calendar: String,
) -> Result(DateTime, DateTimeError)

Creates a DateTime from Gregorian seconds (seconds since 0000-01-01 00:00:00 UTC).

pub fn from_iso8601(
  string: String,
) -> Result(DateTime, DateTimeError)

Parses an ISO 8601 datetime string.

pub fn from_iso8601_with_calendar(
  string: String,
  calendar: String,
) -> Result(DateTime, DateTimeError)

Parses an ISO 8601 datetime string with specified calendar.

pub fn from_naive_datetime(
  ndt: naive_datetime.NaiveDateTime,
  time_zone: String,
  zone_abbr: String,
  utc_offset: Int,
  std_offset: Int,
) -> DateTime

Creates a DateTime from a NaiveDateTime and timezone information.

pub fn from_naive_utc(
  ndt: naive_datetime.NaiveDateTime,
) -> DateTime

Creates a DateTime from NaiveDateTime with UTC timezone.

pub fn from_naive_with_timezone(
  ndt: naive_datetime.NaiveDateTime,
  time_zone: String,
  zone_abbr: String,
  utc_offset: Int,
  std_offset: Int,
) -> Result(DateTime, DateTimeError)

Creates a DateTime from NaiveDateTime with specified timezone.

pub fn from_unix(
  timestamp: Int,
  unit: TimeUnit,
  calendar: String,
) -> Result(DateTime, DateTimeError)

Converts a Unix timestamp to a DateTime.

pub fn from_utc_timestamp(
  timestamp: Int,
  time_zone: String,
) -> Result(DateTime, DateTimeError)

Create DateTime from UTC timestamp.

pub fn new(
  year: Int,
  month: Int,
  day: Int,
  hour: Int,
  minute: Int,
  second: Int,
  time_zone: String,
  zone_abbr: String,
  utc_offset: Int,
  std_offset: Int,
  microsecond: #(Int, Int),
  calendar: String,
) -> Result(DateTime, DateTimeError)

Creates a new DateTime struct.

pub fn new_from_date_and_time(
  date_val: date.Date,
  time_val: time.Time,
) -> Result(DateTime, DateTimeError)

Creates a DateTime from Date and Time structs with UTC timezone.

pub fn new_from_date_and_time_with_tz(
  date_val: date.Date,
  time_val: time.Time,
  time_zone: String,
  zone_abbr: String,
  utc_offset: Int,
  std_offset: Int,
) -> Result(DateTime, DateTimeError)

Creates a DateTime from Date and Time structs with specified timezone.

pub fn new_utc(
  year: Int,
  month: Int,
  day: Int,
  hour: Int,
  minute: Int,
  second: Int,
  microsecond: #(Int, Int),
  calendar: String,
) -> Result(DateTime, DateTimeError)

Creates a UTC DateTime from date and time components.

pub fn new_utc_simple(
  year: Int,
  month: Int,
  day: Int,
  hour: Int,
  minute: Int,
  second: Int,
) -> Result(DateTime, DateTimeError)

Creates a simple UTC DateTime with default values.

pub fn now(
  time_zone: String,
  zone_abbr: String,
  utc_offset: Int,
  std_offset: Int,
) -> Result(DateTime, DateTimeError)

Returns the current datetime in a specific timezone.

pub fn shift(
  dt: DateTime,
  dur: duration.Duration,
) -> Result(DateTime, DateTimeError)

Adds time to a DateTime using Duration.

pub fn shift_zone(
  dt: DateTime,
  time_zone: String,
  zone_abbr: String,
  utc_offset: Int,
  std_offset: Int,
) -> Result(DateTime, DateTimeError)

Shifts a DateTime to a different timezone.

pub fn to_date(dt: DateTime) -> date.Date

Extracts the Date part from a DateTime.

pub fn to_erl(
  dt: DateTime,
) -> #(#(Int, Int, Int), #(Int, Int, Int))

Converts DateTime to Erlang datetime tuple.

pub fn to_gregorian_seconds(dt: DateTime) -> #(Int, Int)

Converts a DateTime to Gregorian seconds (seconds since 0000-01-01 00:00:00 UTC). Returns #(gregorian_seconds, microseconds).

pub fn to_iso8601(dt: DateTime) -> String

Converts a DateTime to ISO8601 format with timezone.

pub fn to_iso8601_basic(dt: DateTime) -> String

Converts DateTime to basic ISO 8601 format.

pub fn to_iso8601_with_format(
  dt: DateTime,
  format: DateTimeFormat,
) -> String

Converts DateTime to ISO 8601 with format option.

pub fn to_local_timestamp(dt: DateTime) -> Int

Convert DateTime to local timestamp.

pub fn to_naive_datetime(
  dt: DateTime,
) -> naive_datetime.NaiveDateTime

Extracts the NaiveDateTime part from a DateTime.

pub fn to_string(dt: DateTime) -> String

Converts a DateTime to a string with timezone information.

pub fn to_time(dt: DateTime) -> time.Time

Extracts the Time part from a DateTime.

pub fn to_unix(dt: DateTime, unit: TimeUnit) -> Int

Converts a DateTime to Unix timestamp.

pub fn to_utc_timestamp(dt: DateTime) -> Int

Convert DateTime to UTC timestamp.

pub fn truncate(dt: DateTime, precision: Int) -> DateTime

Returns the given datetime with the microsecond field truncated to the given precision.

pub fn utc_now() -> Result(DateTime, DateTimeError)

Returns the current datetime in UTC.

pub fn utc_now_with_precision(
  time_unit: TimeUnit,
  calendar: String,
) -> Result(DateTime, DateTimeError)

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

Search Document