Timex.Protocol protocol (timex v4.0.0)

Copy Markdown View Source

This protocol defines the API for functions which take a Date, NaiveDateTime, or DateTime as input.

Built-in implementations assume Calendar.ISO inputs with years from 0 through 9999. The public Timex entrypoints enforce these preconditions and return explicit unsupported-calendar or unsupported-year errors. Direct protocol callers must validate inputs themselves.

Summary

Types

t()

All the types that implement this protocol.

Functions

Get a new version of the date/time value representing the beginning of the day

Get a new version of the date/time value representing the beginning of it's month

Get a new version of the date/time value representing the beginning of it's quarter

Get a new version of the date/time value representing the beginning of it's week, providing a weekday name (as an atom) for the day which starts the week, i.e. :mon.

Get a new version of the date/time value representing the beginning of it's year

Get the century a date/time value is in

Get the ordinal day number of the given date/time value

Get the number of days in the month for the given date/time value

Get a new version of the date/time value representing the end of the day

Get a new version of the date/time value representing the ending of it's month

Get a new version of the date/time value representing the ending of it's quarter

Get a new version of the date/time value representing the ending of it's week, providing a weekday name (as an atom) for the day which starts the week, i.e. :mon.

Get a new version of the date/time value representing the ending of it's year

Shifts the given date/time value to the ISO day given

Return a boolean indicating whether the date/time value is in a leap year

Determine if the provided date/time value is valid.

Return a pair {year, week number} (as defined by ISO 8601) that the given date/time value falls on.

Get the quarter for the given date/time value

Set fields on a date/time value using a list of unit/value pairs

Shift a date/time value using a list of shift unit/value pairs

Convert a date/time value to a Date

Convert a date/time value to a DateTime. A timezone must be provided. Use Timex.to_datetime/1 for the default timezone.

Convert a date/time value to it's Erlang tuple variant i.e. Date becomes {y,m,d}, DateTime/NaiveDateTime become {{y,m,d},{h,mm,s}}

Convert a date/time value to gregorian microseconds (microseconds since the start of year zero)

Convert a date/time value to gregorian seconds (seconds since start of year zero)

Convert a date/time value to a Julian calendar date number

Convert a date/time value to a NaiveDateTime

Convert a date/time value to seconds since the UNIX Epoch

Get the week number within the month of the given date/time value, starting at 1.

Get the ordinal weekday number of the given date/time value

Get the ordinal weekday number of the given date/time value and relative to the given weekstart

Types

t()

@type t() :: term()

All the types that implement this protocol.

Functions

beginning_of_day(datetime)

@spec beginning_of_day(Timex.Types.valid_datetime()) ::
  Timex.Types.valid_datetime() | {:error, term()}

Get a new version of the date/time value representing the beginning of the day

beginning_of_month(datetime)

@spec beginning_of_month(Timex.Types.valid_datetime()) ::
  Timex.Types.valid_datetime() | {:error, term()}

Get a new version of the date/time value representing the beginning of it's month

beginning_of_quarter(datetime)

@spec beginning_of_quarter(Timex.Types.valid_datetime()) ::
  Timex.Types.valid_datetime() | {:error, term()}

Get a new version of the date/time value representing the beginning of it's quarter

beginning_of_week(datetime, weekstart)

@spec beginning_of_week(Timex.Types.valid_datetime(), Timex.Types.weekstart()) ::
  Timex.Types.valid_datetime() | {:error, term()}

Get a new version of the date/time value representing the beginning of it's week, providing a weekday name (as an atom) for the day which starts the week, i.e. :mon.

beginning_of_year(datetime)

@spec beginning_of_year(Timex.Types.year() | Timex.Types.valid_datetime()) ::
  Timex.Types.valid_datetime() | {:error, term()}

Get a new version of the date/time value representing the beginning of it's year

century(datetime)

@spec century(Timex.Types.year() | Timex.Types.valid_datetime()) ::
  non_neg_integer() | {:error, term()}

Get the century a date/time value is in

day(datetime)

@spec day(Timex.Types.valid_datetime()) :: Timex.Types.daynum() | {:error, term()}

Get the ordinal day number of the given date/time value

days_in_month(datetime)

@spec days_in_month(Timex.Types.valid_datetime()) ::
  Timex.Types.num_of_days() | {:error, term()}

Get the number of days in the month for the given date/time value

end_of_day(datetime)

@spec end_of_day(Timex.Types.valid_datetime()) ::
  Timex.Types.valid_datetime() | {:error, term()}

Get a new version of the date/time value representing the end of the day

end_of_month(datetime)

@spec end_of_month(Timex.Types.valid_datetime()) ::
  Timex.Types.valid_datetime() | {:error, term()}

Get a new version of the date/time value representing the ending of it's month

end_of_quarter(datetime)

@spec end_of_quarter(Timex.Types.valid_datetime()) ::
  Timex.Types.valid_datetime() | {:error, term()}

Get a new version of the date/time value representing the ending of it's quarter

end_of_week(datetime, weekstart)

Get a new version of the date/time value representing the ending of it's week, providing a weekday name (as an atom) for the day which starts the week, i.e. :mon.

end_of_year(datetime)

@spec end_of_year(Timex.Types.year() | Timex.Types.valid_datetime()) ::
  Timex.Types.valid_datetime() | {:error, term()}

Get a new version of the date/time value representing the ending of it's year

from_iso_day(datetime, day)

@spec from_iso_day(Timex.Types.valid_datetime(), non_neg_integer()) ::
  Timex.Types.valid_datetime() | {:error, term()}

Shifts the given date/time value to the ISO day given

is_leap?(datetime)

@spec is_leap?(Timex.Types.valid_datetime() | Timex.Types.year()) ::
  boolean() | {:error, term()}

Return a boolean indicating whether the date/time value is in a leap year

is_valid?(datetime)

@spec is_valid?(Timex.Types.valid_datetime()) :: boolean() | {:error, term()}

Determine if the provided date/time value is valid.

iso_week(datetime)

@spec iso_week(Timex.Types.valid_datetime()) ::
  {Timex.Types.year(), Timex.Types.weeknum()} | {:error, term()}

Return a pair {year, week number} (as defined by ISO 8601) that the given date/time value falls on.

quarter(datetime)

@spec quarter(Timex.Types.month() | Timex.Types.valid_datetime()) ::
  1..4 | {:error, term()}

Get the quarter for the given date/time value

set(datetime, options)

Set fields on a date/time value using a list of unit/value pairs

shift(datetime, options)

Shift a date/time value using a list of shift unit/value pairs

to_date(datetime)

@spec to_date(Timex.Types.valid_datetime()) :: Date.t() | {:error, term()}

Convert a date/time value to a Date

to_datetime(datetime, timezone)

Convert a date/time value to a DateTime. A timezone must be provided. Use Timex.to_datetime/1 for the default timezone.

to_erl(datetime)

@spec to_erl(Timex.Types.valid_datetime()) ::
  Timex.Types.date() | Timex.Types.datetime() | {:error, term()}

Convert a date/time value to it's Erlang tuple variant i.e. Date becomes {y,m,d}, DateTime/NaiveDateTime become {{y,m,d},{h,mm,s}}

to_gregorian_microseconds(datetime)

@spec to_gregorian_microseconds(Timex.Types.valid_datetime()) ::
  non_neg_integer() | {:error, term()}

Convert a date/time value to gregorian microseconds (microseconds since the start of year zero)

to_gregorian_seconds(datetime)

@spec to_gregorian_seconds(Timex.Types.valid_datetime()) ::
  non_neg_integer() | {:error, term()}

Convert a date/time value to gregorian seconds (seconds since start of year zero)

to_julian(datetime)

@spec to_julian(Timex.Types.valid_datetime()) :: integer() | {:error, term()}

Convert a date/time value to a Julian calendar date number

to_naive_datetime(datetime)

@spec to_naive_datetime(Timex.Types.valid_datetime()) ::
  NaiveDateTime.t() | {:error, term()}

Convert a date/time value to a NaiveDateTime

to_unix(datetime)

@spec to_unix(Timex.Types.valid_datetime()) :: non_neg_integer() | {:error, term()}

Convert a date/time value to seconds since the UNIX Epoch

week_of_month(datetime)

@spec week_of_month(Timex.Types.valid_datetime()) ::
  Timex.Types.week_of_month() | {:error, term()}

Get the week number within the month of the given date/time value, starting at 1.

Weeks begin on Monday. The partial week containing the first day of the month is week 1, even when it begins in the preceding month or ISO year. A month can contain up to six weeks.

weekday(datetime)

@spec weekday(Timex.Types.valid_datetime()) ::
  Timex.Types.weekday() | {:error, term()}

Get the ordinal weekday number of the given date/time value

weekday(datetime, weekstart)

Get the ordinal weekday number of the given date/time value and relative to the given weekstart