rluiten/elm-date-extra/7.2.0

NOTE: this package is not compatible with Elm 0.19.1

Date.Extra.Duration

A Duration is a length of time that may vary with with calendar date and time. It can be used to modify a date.

When modify dates using Durations (Day | Month | Week | Year) this module compensates for day light saving hour variations to minimise the scenarios that cause the Hour field in the result to be different to the input date. It can't completely avoid the hour changing as some hours are not a real world date and hence will modify the hour more than the Duration modified.

This behaviour is modelled on momentjs so any observed behaviour that is not the same as momentjs should be raised as in issue.

Note adding or subtracting 24 * Hour units from a date may produce a different answer to adding or subtracting a Day if day light saving transitions occur as part of the date change.

Warning

Be careful if you add Duration Delta to a Date as Duration contains months and Years which are not fixed elapsed times like Period Delta, however if you really need a relative number of months or years then it may meet your needs.

add : Duration -> Int -> Date -> Date

Add duration * count to date.

= Millisecond
| Second
| Minute
| Hour
| Day
| Week
| Month
| Year
| Delta DeltaRecord

A Duration is time period that may vary with with calendar and time.

Using Duration adding 24 hours can produce different result to adding 1 day.

type alias DeltaRecord =
{ year : Int
, month : Int
, day : Int
, hour : Int
, minute : Int
, second : Int
, millisecond : Int
}

A multi granularity duration delta.

This does not contain week like Period.DeltaRecord. It does contain month and year.

zeroDelta : DeltaRecord

All zero delta. Useful as a starting point if you want to set a few fields only.

diff : Date -> Date -> DeltaRecord

Return a Period representing date difference. date1 - date2.

If you add the result of this function to date2 with addend of 1 will return date1.

Differences to Period.diff

  • Duration DeltaRecord excludes week field
  • Duration DeltaRecord includes month field
  • Duration DeltaRecord includes year field
  • Day is number of days difference between months.

When adding a Duration DeltaRecord to a date. The larger granularity fields are added before lower granularity fields so Years are added before Months before Days etc.

  • Very different behaviour to Period diff * If date1 > date2 then all fields in DeltaRecord will be positive or zero. * If date1 < date2 then all fields in DeltaRecord will be negative or zero.
  • Because it deals with non fixed length periods of time

Example 1. days in 2016/05 (May) = 31 days in 2016/04 (Apr) = 30 days in 2016/03 (Mar) = 31

days in 2015/03 (Mar) = 31

diff of "2016/05/15" "2015/03/20" result naive field diff. year 1, month 2, day -5

days "2015/03/20" to "2015/04/01" (31 - 20) = 11 days (12). still in march with 11. days "2015/04/01" to "2016/04/15" (15 - 1) = 14 days months "2016/04/15" to "2016/05/15" 1 months result field diff year 1, month 1, day 26

This logic applies all the way down to milliseconds.

Copyright (c) 2016 Robin Luiten