View Source MoreTime (more_time v0.1.0)

MoreTime contains some helpful functions for operating on DateTime-like maps. These are useful for rounding and bucketing times in a stable way.

Like the standard library, it operates on maps that have the set of keys that are used by DateTime. Any other map/struct that has the same keys should work with these functions.

Link to this section Summary

Functions

Returns a DateTime struct that carries over the year, month and day information from the passed-in DateTime-like map. It preserves time zone and calendar information.

Accepts a DateTime-like map and returns a DateTime pointing to the start of the month in which that input DateTime falls. It uses the original Calendar to do that, so it should work for different calendars.

Accepts a DateTime-like map and returns a DateTime pointing to the start of the week in which that input DateTime falls. It uses the original Calendar to do that, so it should work for different calendars.

Accepts a DateTime-like map and returns a DateTime pointing to the start of the year in which that input DateTime falls. It uses the original Calendar to do that, so it should work for different calendars.

Takes a DateTime-like map and buckets it by the time unit supplied, starting from the beginning of time (unix epoch). It returns a DateTime. This uses common expectations about what an minute, hour, day, etc are in seconds. It preserves any calender and time zone information.

Takes a DateTime-like map, and returns a DateTime set to the last microsecond of the specified day.

Accepts a DateTime-like map and returns a DateTime pointing to the end of the month in which that input DateTime falls. It uses the original Calendar to do that, so it should work for different calendars.

Accepts a DateTime-like map and returns a DateTime pointing to the end of the week in which that input DateTime falls. It uses the original Calendar to do that, so it should work for different calendars.

Accepts a DateTime-like map and returns a DateTime pointing to the end of the year in which that input DateTime falls. It uses the original Calendar to do that, so it should work for different calendars.

Link to this section Functions

Returns a DateTime struct that carries over the year, month and day information from the passed-in DateTime-like map. It preserves time zone and calendar information.

Examples

iex> MoreTime.beginning_of_day(~U[2022-01-13 14:07:06.098765Z])
~U[2022-01-13 00:00:00Z]

Accepts a DateTime-like map and returns a DateTime pointing to the start of the month in which that input DateTime falls. It uses the original Calendar to do that, so it should work for different calendars.

Examples

iex> MoreTime.beginning_of_month(~U[2022-01-13 14:07:06.098765Z])
~U[2022-01-01 00:00:00Z]

Accepts a DateTime-like map and returns a DateTime pointing to the start of the week in which that input DateTime falls. It uses the original Calendar to do that, so it should work for different calendars.

Examples

iex> MoreTime.beginning_of_week(~U[2022-01-13 14:07:06.098765Z])
~U[2022-01-10 00:00:00Z]

Accepts a DateTime-like map and returns a DateTime pointing to the start of the year in which that input DateTime falls. It uses the original Calendar to do that, so it should work for different calendars.

Examples

iex> MoreTime.beginning_of_year(~U[2022-01-13 14:07:06.098765Z])
~U[2022-01-01 00:00:00Z]

Takes a DateTime-like map and buckets it by the time unit supplied, starting from the beginning of time (unix epoch). It returns a DateTime. This uses common expectations about what an minute, hour, day, etc are in seconds. It preserves any calender and time zone information.

Note that the bucketing algorithm is hard buckets since the beginning of the epoch. That is desirable because it means you will always have stable bucket sizes.

However, it might produce somewhat different results that you expect. For example, 7 minute buckets begin from the start of the epoch, not from the start of the day. This means the first 7 minute bucket of any particular day might be the 3rd minute of the day. The same principle applies to the other bucket sizes. Any bucket size by which a day can be evenly divided will, however, always line up with the day.

Valid bucket sizes are one of:

:day, :hour, :minute, :second

Examples

iex> MoreTime.bucket(~U[2021-01-13 14:07:06.098765Z], 1, :day)
~U[2021-01-13 00:00:00Z]

iex> MoreTime.bucket(~U[2021-02-17 14:53:06.098765Z], 23, :day)
~U[2021-01-26 00:00:00Z]

iex> MoreTime.bucket(~U[2021-01-13 14:07:06.098765Z], 1, :hour)
~U[2021-01-13 14:00:00Z]

Takes a DateTime-like map, and returns a DateTime set to the last microsecond of the specified day.

Examples

iex> MoreTime.end_of_day(~U[2022-01-13 14:07:06.098765Z])
~U[2022-01-13 23:59:59.999999Z]

Accepts a DateTime-like map and returns a DateTime pointing to the end of the month in which that input DateTime falls. It uses the original Calendar to do that, so it should work for different calendars.

Examples

iex> MoreTime.end_of_month(~U[2022-01-13 14:07:06.098765Z])
~U[2022-01-31 23:59:59.999999Z]

Accepts a DateTime-like map and returns a DateTime pointing to the end of the week in which that input DateTime falls. It uses the original Calendar to do that, so it should work for different calendars.

Examples

iex> MoreTime.end_of_week(~U[2022-01-13 14:07:06.098765Z])
~U[2022-01-16 23:59:59.999999Z]

Accepts a DateTime-like map and returns a DateTime pointing to the end of the year in which that input DateTime falls. It uses the original Calendar to do that, so it should work for different calendars.

Examples

iex> MoreTime.end_of_year(~U[2022-01-13 14:07:06.098765Z])
~U[2022-12-31 23:59:59.999999Z]