BitcrowdEcto.DateTime (bitcrowd_ecto v1.2.0)

View Source

Functions to work with date and time values.

Summary

Functions

Works similar to Timex.beginning_of_day/3, but way more simple.

Calculates the beginning of last month.

Works similar to Timex.beginning_of_day/3, but way more simple.

Calculates the beginning of next month.

Calculates the beginning of tomorrow, equalizing day length differences due to DST.

Calculates the beginning of yesterday, equalizing day length differences due to DST.

Converts a {<value>, <unit>} tuple into seconds.

Works similar to Timex.shift/3, but way more simple.

Types

period()

(since 0.2.0)
@type period() :: {integer(), unit()}

unit()

(since 0.2.0)
@type unit() :: :second | :minute | :hour | :day | :week

Functions

beginning_of_day(datetime)

(since 0.10.0)
@spec beginning_of_day(DateTime.t()) :: DateTime.t()

Works similar to Timex.beginning_of_day/3, but way more simple.

Behaviour

Nulls the time fields of the DateTime and keeps the rest.

Examples

iex> beginning_of_day(~U[2022-04-07 07:21:22.036Z])
~U[2022-04-07 00:00:00.000000Z]

beginning_of_last_month(datetime)

(since 0.12.0)
@spec beginning_of_last_month(DateTime.t()) :: DateTime.t()

Calculates the beginning of last month.

Behaviour

Goes to this month's beginning, subtracts 15 days, and goes back to the month's beginning.

Should be relatively safe.

Examples

iex> beginning_of_last_month(~U[2022-04-07 07:21:22.036Z])
~U[2022-03-01 00:00:00.000000Z]

iex> beginning_of_last_month(~U[2022-02-07 07:21:22.036Z])
~U[2022-01-01 00:00:00.000000Z]

beginning_of_month(datetime)

(since 0.12.0)
@spec beginning_of_month(DateTime.t()) :: DateTime.t()

Works similar to Timex.beginning_of_day/3, but way more simple.

Behaviour

Sets day to 1 and nulls the time fields.

Examples

iex> beginning_of_month(~U[2022-04-07 07:21:22.036Z])
~U[2022-04-01 00:00:00.000000Z]

beginning_of_next_month(datetime)

(since 0.12.0)
@spec beginning_of_next_month(DateTime.t()) :: DateTime.t()

Calculates the beginning of next month.

Behaviour

Goes to this month's beginning, adds 45 days, and goes back to the month's beginning.

Should be relatively safe.

Examples

iex> beginning_of_next_month(~U[2022-04-07 07:21:22.036Z])
~U[2022-05-01 00:00:00.000000Z]

iex> beginning_of_next_month(~U[2022-02-07 07:21:22.036Z])
~U[2022-03-01 00:00:00.000000Z]

beginning_of_tomorrow(datetime)

(since 0.12.0)
@spec beginning_of_tomorrow(DateTime.t()) :: DateTime.t()

Calculates the beginning of tomorrow, equalizing day length differences due to DST.

Behaviour

Adds 1.5d to today's midnight and goes back to midnight. Should be relatively safe.

Examples

iex> beginning_of_tomorrow(~U[2022-04-07 07:21:22.036Z])
~U[2022-04-08 00:00:00.000000Z]

iex> ~U[2020-03-29 12:00:00.000Z]
...> |> DateTime.shift_zone!("Europe/Berlin")
...> |> beginning_of_tomorrow()
...> |> DateTime.to_iso8601()
"2020-03-30T00:00:00.000000+02:00"

beginning_of_yesterday(datetime)

(since 0.12.0)
@spec beginning_of_yesterday(DateTime.t()) :: DateTime.t()

Calculates the beginning of yesterday, equalizing day length differences due to DST.

Behaviour

Subtracts 0.5d from today's midnight and goes back to midnight. Should be relatively safe.

Examples

iex> beginning_of_yesterday(~U[2022-04-07 07:21:22.036Z])
~U[2022-04-06 00:00:00.000000Z]

iex> ~U[2020-03-29 12:00:00.000Z]
...> |> DateTime.shift_zone!("Europe/Berlin")
...> |> beginning_of_yesterday()
...> |> DateTime.to_iso8601()
"2020-03-28T00:00:00.000000+01:00"

in_seconds(arg)

(since 0.2.0)
@spec in_seconds(period()) :: integer()

Converts a {<value>, <unit>} tuple into seconds.

#Examples

iex> in_seconds({99, :second})
99

iex> in_seconds({1, :minute})
60

iex> in_seconds({1, :hour})
3600

iex> in_seconds({1, :day})
86400

iex> in_seconds({1, :week})
604800

shift(datetime, period)

(since 0.10.0)
@spec shift(DateTime.t(), integer() | period()) :: DateTime.t()

Works similar to Timex.shift/3, but way more simple.

Behaviour

Semantics are like DateTime.add/3. TimeZone-awareness when using tzdata. DateTime, e.g. "2020-03-29 14:00 Europe/Berlin" - 1 day = "2020-03-28 13:00" as March 29th only had 23 hours due to DST.

Examples

iex> shift(~U[2022-04-07 07:21:22.036Z], 15)
~U[2022-04-07 07:21:37.036Z]

iex> shift(~U[2022-04-07 07:21:22.036Z], -3600)
~U[2022-04-07 06:21:22.036Z]

iex> shift(~U[2022-04-07 07:21:22.036Z], {1, :day})
~U[2022-04-08 07:21:22.036Z]

iex> ~U[2020-03-29 12:00:00.000Z]
...> |> DateTime.shift_zone!("Europe/Berlin")
...> |> shift({-1, :day})
...> |> DateTime.to_iso8601()
"2020-03-28T13:00:00.000+01:00"