calendar/naive_datetime

Types

A NaiveDateTime struct (without a time zone) and functions.

The NaiveDateTime struct contains the fields year, month, day, hour, minute, second, microsecond and calendar. New naive datetimes can be built with the new functions.

We call them “naive” because this datetime representation does not have a time zone. This means the datetime may not actually exist in certain areas in the world even though it is valid.

pub type NaiveDateTime {
  NaiveDateTime(
    year: Int,
    month: Int,
    day: Int,
    hour: Int,
    minute: Int,
    second: Int,
    microsecond: #(Int, Int),
    calendar: String,
  )
}

Constructors

  • NaiveDateTime(
      year: Int,
      month: Int,
      day: Int,
      hour: Int,
      minute: Int,
      second: Int,
      microsecond: #(Int, Int),
      calendar: String,
    )
pub type NaiveDateTimeError {
  InvalidNaiveDateTime
  InvalidDate
  InvalidTime
}

Constructors

  • InvalidNaiveDateTime
  • InvalidDate
  • InvalidTime

Values

pub fn add(
  ndt: NaiveDateTime,
  amount: Int,
  unit: time.TimeUnit,
) -> NaiveDateTime

Add amount to a naive datetime with specified unit.

pub fn add_days(
  ndt: NaiveDateTime,
  days: Int,
) -> Result(NaiveDateTime, NaiveDateTimeError)

Add days to a naive datetime.

pub fn add_hours(
  ndt: NaiveDateTime,
  hours: Int,
) -> Result(NaiveDateTime, NaiveDateTimeError)

Add hours to a naive datetime.

pub fn add_minutes(
  ndt: NaiveDateTime,
  minutes: Int,
) -> Result(NaiveDateTime, NaiveDateTimeError)

Add minutes to a naive datetime.

pub fn add_seconds(
  ndt: NaiveDateTime,
  seconds: Int,
) -> Result(NaiveDateTime, NaiveDateTimeError)

Add seconds to a NaiveDateTime.

pub fn add_with_validation(
  ndt: NaiveDateTime,
  amount: Int,
  unit: time.TimeUnit,
) -> Result(NaiveDateTime, NaiveDateTimeError)

Add amount to naive datetime with validation.

pub fn after(ndt1: NaiveDateTime, ndt2: NaiveDateTime) -> Bool

Check if first datetime is after second.

pub fn before(ndt1: NaiveDateTime, ndt2: NaiveDateTime) -> Bool

Check if first datetime is before second.

pub fn beginning_of_day(ndt: NaiveDateTime) -> NaiveDateTime

Get beginning of day (00:00:00).

pub fn calendar(ndt: NaiveDateTime) -> String

Get calendar from naive datetime.

pub fn compare(
  ndt1: NaiveDateTime,
  ndt2: NaiveDateTime,
) -> order.Order

Compare two NaiveDateTime structs.

pub fn convert(
  ndt: NaiveDateTime,
  target_calendar: String,
) -> Result(NaiveDateTime, NaiveDateTimeError)

Convert between calendars.

pub fn convert_unchecked(
  ndt: NaiveDateTime,
  target_calendar: String,
) -> NaiveDateTime

Convert between calendars, panicking on error.

pub fn day(ndt: NaiveDateTime) -> Int

Get day from naive datetime.

pub fn day_of_week(ndt: NaiveDateTime) -> Int

Get the day of week (1=Monday, 7=Sunday).

pub fn day_of_year(ndt: NaiveDateTime) -> Int

Get the day of year (1-366).

pub fn diff(
  ndt1: NaiveDateTime,
  ndt2: NaiveDateTime,
  unit: time.TimeUnit,
) -> Int

Calculate difference between two naive datetimes.

pub fn diff_days(ndt1: NaiveDateTime, ndt2: NaiveDateTime) -> Int

Calculate difference in days between two naive datetimes.

pub fn diff_hours(
  ndt1: NaiveDateTime,
  ndt2: NaiveDateTime,
) -> Int

Calculate difference in hours between two naive datetimes.

pub fn diff_minutes(
  ndt1: NaiveDateTime,
  ndt2: NaiveDateTime,
) -> Int

Calculate difference in minutes between two naive datetimes.

pub fn end_of_day(ndt: NaiveDateTime) -> NaiveDateTime

Get end of day (23:59:59.999999).

pub fn equal(ndt1: NaiveDateTime, ndt2: NaiveDateTime) -> Bool

Check if two naive datetimes are equal.

pub fn from_date_and_time(
  date_val: date.Date,
  time_val: time.Time,
) -> Result(NaiveDateTime, NaiveDateTimeError)

Creates a NaiveDateTime from separate Date and Time structs.

pub fn from_erl(
  erl_datetime: #(#(Int, Int, Int), #(Int, Int, Int)),
  microsecond: #(Int, Int),
  calendar: String,
) -> Result(NaiveDateTime, NaiveDateTimeError)

Create NaiveDateTime from Erlang datetime tuple.

pub fn from_erl_unchecked(
  erl_datetime: #(#(Int, Int, Int), #(Int, Int, Int)),
  microsecond: #(Int, Int),
  calendar: String,
) -> NaiveDateTime

Create NaiveDateTime from Erlang tuple, panicking on error.

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

Create from Gregorian seconds since epoch 0000-01-01 00:00:00.

pub fn from_iso8601(
  iso_string: String,
) -> Result(NaiveDateTime, NaiveDateTimeError)

Parse ISO8601 string to NaiveDateTime.

pub fn from_iso8601_unchecked(
  iso_string: String,
) -> NaiveDateTime

Parse ISO8601 string to NaiveDateTime, panicking on error.

pub fn from_timestamp(
  timestamp: Int,
) -> Result(NaiveDateTime, NaiveDateTimeError)

Create NaiveDateTime from Unix timestamp.

pub fn hour(ndt: NaiveDateTime) -> Int

Get hour from naive datetime.

pub fn inspect(ndt: NaiveDateTime) -> String

Get string representation for inspection.

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

Validate if components make a valid naive datetime.

pub fn local_now() -> Result(NaiveDateTime, NaiveDateTimeError)

Returns the current local naive datetime.

pub fn local_now_with_calendar(
  calendar: String,
) -> Result(NaiveDateTime, NaiveDateTimeError)

Returns the current local naive datetime with calendar.

pub fn microsecond(ndt: NaiveDateTime) -> #(Int, Int)

Get microsecond from naive datetime.

pub fn minute(ndt: NaiveDateTime) -> Int

Get minute from naive datetime.

pub fn month(ndt: NaiveDateTime) -> Int

Get month from naive datetime.

pub fn new(
  year: Int,
  month: Int,
  day: Int,
  hour: Int,
  minute: Int,
  second: Int,
  microsecond: #(Int, Int),
  calendar: String,
) -> Result(NaiveDateTime, NaiveDateTimeError)

Creates a new NaiveDateTime struct.

pub fn new_simple(
  year: Int,
  month: Int,
  day: Int,
  hour: Int,
  minute: Int,
  second: Int,
) -> Result(NaiveDateTime, NaiveDateTimeError)

Creates a new NaiveDateTime with simple parameters.

pub fn range(
  start: NaiveDateTime,
  end: NaiveDateTime,
  step_seconds: Int,
) -> List(NaiveDateTime)

Create naive datetime range.

pub fn replace(
  ndt: NaiveDateTime,
  year: Int,
  month: Int,
  day: Int,
  hour: Int,
  minute: Int,
  second: Int,
  microsecond: #(Int, Int),
) -> Result(NaiveDateTime, NaiveDateTimeError)

Create a new naive datetime with specified components replaced.

pub fn replace_partial(
  ndt: NaiveDateTime,
  year: Int,
  month: Int,
  day: Int,
) -> Result(NaiveDateTime, NaiveDateTimeError)

Create a new naive datetime with only some components replaced.

pub fn second(ndt: NaiveDateTime) -> Int

Get second from naive datetime.

pub fn shift(
  ndt: NaiveDateTime,
  duration: duration.Duration,
) -> Result(NaiveDateTime, NaiveDateTimeError)

Shift naive datetime by duration.

pub fn to_date(ndt: NaiveDateTime) -> date.Date

Extracts the Date part from a NaiveDateTime.

pub fn to_erl(
  ndt: NaiveDateTime,
) -> #(#(Int, Int, Int), #(Int, Int, Int))

Convert NaiveDateTime to Erlang datetime tuple.

pub fn to_gregorian_seconds(ndt: NaiveDateTime) -> #(Int, Int)

Convert to Gregorian seconds since epoch 0000-01-01 00:00:00.

pub fn to_iso8601(ndt: NaiveDateTime) -> String

Converts a NaiveDateTime to ISO8601 extended format.

pub fn to_iso8601_with_format(
  ndt: NaiveDateTime,
  format: iso.Format,
) -> String

Converts a NaiveDateTime to ISO8601 with format option.

pub fn to_string(ndt: NaiveDateTime) -> String

Converts a NaiveDateTime to a string in ISO8601 format.

pub fn to_time(ndt: NaiveDateTime) -> time.Time

Extracts the Time part from a NaiveDateTime.

pub fn to_timestamp(ndt: NaiveDateTime) -> Int

Convert NaiveDateTime to Unix timestamp (seconds since epoch).

pub fn today_with_time(
  hour: Int,
  minute: Int,
  second: Int,
) -> Result(NaiveDateTime, NaiveDateTimeError)

Create NaiveDateTime with current date and specified time.

pub fn today_with_time_and_calendar(
  hour: Int,
  minute: Int,
  second: Int,
  microsecond: #(Int, Int),
  calendar: String,
) -> Result(NaiveDateTime, NaiveDateTimeError)

Create NaiveDateTime with current date and specified time and calendar.

pub fn truncate(
  ndt: NaiveDateTime,
  precision: Int,
) -> Result(NaiveDateTime, NaiveDateTimeError)

Truncate naive datetime to specified precision.

pub fn utc_now() -> Result(NaiveDateTime, NaiveDateTimeError)

Returns the current naive datetime in UTC.

pub fn utc_now_with_precision(
  precision: time.TimeUnit,
  calendar: String,
) -> Result(NaiveDateTime, NaiveDateTimeError)

Returns the current naive datetime in UTC with precision.

pub fn week_of_year(ndt: NaiveDateTime) -> Int

Get the week of year (1-53).

pub fn year(ndt: NaiveDateTime) -> Int

Get year from naive datetime.

Search Document