Skip to content

Commit

Permalink
Merge pull request #15 from felipehummel/fix-months-30
Browse files Browse the repository at this point in the history
Fix relative months calculation
  • Loading branch information
ryan-haskell authored Nov 23, 2018
2 parents 10a89c9 + e62efaf commit f2f9622
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/DateFormat/Relative.elm
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ relativeTimeWithFunctions zone millis functions =
functions.days <| days

else if days < 365 then
functions.months <| (days // 12)
functions.months <| (days // 30)

else
functions.years <| (days // 365)
Expand Down
44 changes: 44 additions & 0 deletions tests/RelativeTests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ addDays days =
addHours (days * 24)


addMonths months =
addDays (months * 30)


suite : Test
suite =
describe "The DateFormat.Relative module"
Expand Down Expand Up @@ -68,6 +72,26 @@ suite =
\days ->
relativeTime time (addDays days)
|> Expect.equal (String.fromInt (abs days) ++ " days ago")
, fuzz (Fuzz.intRange -11 -2) "2-11 months ago" <|
\days ->
relativeTime time (addMonths days)
|> Expect.equal (String.fromInt (abs days) ++ " months ago")
, test "12 months ago" <|
\_ ->
relativeTime time (addMonths -12)
|> Expect.equal "12 months ago"
, test "last year" <|
\_ ->
relativeTime time (addMonths -13)
|> Expect.equal "last year"
, test "last year 2" <|
\_ ->
relativeTime time (addMonths -23)
|> Expect.equal "last year"
, test "2 years ago" <|
\_ ->
relativeTime time (addMonths -25)
|> Expect.equal "2 years ago"
, fuzz (Fuzz.intRange 1 29) "next 29s" <|
\secs ->
relativeTime time (addSeconds secs)
Expand Down Expand Up @@ -100,4 +124,24 @@ suite =
\days ->
relativeTime time (addDays days)
|> Expect.equal ("in " ++ String.fromInt (abs days) ++ " days")
, fuzz (Fuzz.intRange 2 11) "in 2-11 months" <|
\days ->
relativeTime time (addMonths days)
|> Expect.equal ("in " ++ String.fromInt (abs days) ++ " months")
, test "in 12 months" <|
\_ ->
relativeTime time (addMonths 12)
|> Expect.equal "in 12 months"
, test "in a year" <|
\_ ->
relativeTime time (addMonths 13)
|> Expect.equal "in a year"
, test "in a year 2" <|
\_ ->
relativeTime time (addMonths 23)
|> Expect.equal "in a year"
, test "in 2 years" <|
\_ ->
relativeTime time (addMonths 25)
|> Expect.equal "in 2 years"
]

0 comments on commit f2f9622

Please sign in to comment.