timex v2.1.5 Timex.Comparable protocol

This protocol is used for comparing and diffing different date/time representations

Summary

Functions

Compare two date or datetime types

Get the difference between two date or datetime types

Types

comparable ::
  DateTime.t |
  Date.t |
  Timex.Types.date |
  Timex.Types.datetime |
  Timex.Types.gregorian |
  constants
compare_result :: -1 | 0 | 1 | {:error, term}
constants ::
  :epoch |
  :zero |
  :distant_past |
  :distant_future
granularity ::
  :years |
  :months |
  :weeks |
  :calendar_weeks |
  :days |
  :hours |
  :minutes |
  :seconds |
  :timestamp
t :: term

Functions

compare(a, b, granularity \\ :seconds)

Compare two date or datetime types.

You can optionally specify a comparison granularity, any of the following:

  • :years
  • :months
  • :weeks
  • :calendar_weeks (weeks of the calendar as opposed to actual weeks in terms of days)
  • :days
  • :hours
  • :minutes
  • :seconds
  • :timestamp

and the dates will be compared with the cooresponding accuracy. The default granularity is :seconds.

  • 0: when equal
  • -1: when the first date/time comes before the second
  • 1: when the first date/time comes after the second
  • {:error, reason}: when there was a problem comparing, perhaps due to a value being passed which is not a valid date/datetime
diff(a, b, granularity \\ :seconds)

Specs

diff(comparable, comparable, granularity) ::
  Timex.Types.timestamp |
  non_neg_integer |
  {:error, term}

Get the difference between two date or datetime types.

You can optionally specify a diff granularity, any of the following:

  • :years
  • :months
  • :calendar_weeks (weeks of the calendar as opposed to actual weeks in terms of days)
  • :weeks
  • :days
  • :hours
  • :minutes
  • :seconds
  • :timestamp

and the result will be a non-negative integer value of those units or a timestamp.