View Source BitcrowdEcto.DateTime (bitcrowd_ecto v0.16.0)

Functions to work with date and time values.

Link to this section 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.

Link to this section Types

@type period() :: {integer(), unit()}
@type unit() :: :second | :minute | :hour | :day | :week

Link to this section Functions

Link to this function

beginning_of_day(datetime)

View Source (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

Behaviour

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

examples

Examples

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

beginning_of_last_month(datetime)

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

Calculates the beginning of last month.

behaviour

Behaviour

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

Should be relatively safe.

examples

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]
Link to this function

beginning_of_month(datetime)

View Source (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

Behaviour

Sets day to 1 and nulls the time fields.

examples

Examples

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

beginning_of_next_month(datetime)

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

Calculates the beginning of next month.

behaviour

Behaviour

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

Should be relatively safe.

examples

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]
Link to this function

beginning_of_tomorrow(datetime)

View Source (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

Behaviour

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

examples

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"
Link to this function

beginning_of_yesterday(datetime)

View Source (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

Behaviour

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

examples

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"
Link to this function

in_seconds(arg)

View Source (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
Link to this function

shift(datetime, period)

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

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

behaviour

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

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"