Problem
Functions that convert or work
with time units, i.e. `System.convert_time_unit/1`, `DateTime.add/3` and
`Time.add/3` are limited to working with a subset of possible time
units. The current subset of named units includes `:second`,
`:millisecond`, `:microsecond`, and `:nanosecond`. It is extremely
common to manipulate dates and times by larger units such as minutes,
hours or days. Currently we have to convert any values to seconds and
then use that to change the value, which isn't particularly explicit or
expressive. For example, to get the date for one week from now I need to
write:
DateTime.add(DateTime.utc_now(), 7 * 24 * 60 * 60, :second)
Proposal
I
propose the addition of larger pre-defined units to make conversion
more expressive. Units could include some or all of the following:
- `:minute`
- `:hour`
- `:day`
- `:week`
Some larger units such as `month` or `year` are not fixed or standard units and could be omitted.
This enables more expressive time calculations such as "one hour from now", "one day from now", etc.:
utc_now = DateTime.utc_now()
DateTime.add(utc_now, 30, :minute)
DateTime.add(utc_now, 1, :hour)DateTime.add(utc_now, 1, :day)
In
my experience it is extremely common to find module attributes or
variables that declar a unit of time to make larger values
comprehensible. It is much clearer to specify `1, :day` instead of
`@one_day_in_seconds, :second` or something similar.