From bb4b15ce0ca82dc0206cb3788c4243263e7cbc6a Mon Sep 17 00:00:00 2001 From: Joshua Stoutenburg Date: Sun, 28 Aug 2016 22:01:33 -0600 Subject: [PATCH] Refactor Gigasecond.add to Speak Date Instead of String --- exercises/gigasecond/Gigasecond.example | 113 +---------------------- exercises/gigasecond/GigasecondTests.elm | 21 ++++- 2 files changed, 19 insertions(+), 115 deletions(-) diff --git a/exercises/gigasecond/Gigasecond.example b/exercises/gigasecond/Gigasecond.example index 8ee223c..c6526b8 100644 --- a/exercises/gigasecond/Gigasecond.example +++ b/exercises/gigasecond/Gigasecond.example @@ -2,120 +2,13 @@ module Gigasecond exposing (add) import Date import Time -import String -add : String -> Result String String -add x = - x - |> Date.fromString - |> Result.map (add' gigasecond) - |> Result.map format - - -add' : Time.Time -> Date.Date -> Date.Date -add' t d = - d - |> Date.toTime - |> flip (+) t - |> Date.fromTime +add : Date.Date -> Date.Date +add = + Date.toTime >> (+) gigasecond >> Date.fromTime gigasecond : Time.Time gigasecond = 10 ^ 12 - - -format : Date.Date -> String -format date = - let - date = - toGMT date - in - --toString date - [ Date.year date - |> toString - , "-" - , Date.month date - |> monthToInt - |> Maybe.withDefault 1 - |> toString - |> String.pad 2 '0' - , "-" - , Date.day date - |> toString - |> String.pad 2 '0' - , "T" - , Date.hour date - |> toString - |> String.pad 2 '0' - , ":" - , Date.minute date - |> toString - |> String.pad 2 '0' - , ":" - , Date.second date - |> toString - |> String.pad 2 '0' - ] - |> String.concat - - -monthToInt : Date.Month -> Maybe Int -monthToInt n = - case n of - Date.Jan -> - Just 1 - - Date.Feb -> - Just 2 - - Date.Mar -> - Just 3 - - Date.Apr -> - Just 4 - - Date.May -> - Just 5 - - Date.Jun -> - Just 6 - - Date.Jul -> - Just 7 - - Date.Aug -> - Just 8 - - Date.Sep -> - Just 9 - - Date.Oct -> - Just 10 - - Date.Nov -> - Just 11 - - Date.Dec -> - Just 12 - - -toGMT : Date.Date -> Date.Date -toGMT date = - let - tzOffset = - date - |> toString - |> String.split " " - |> List.drop 5 - |> List.head - |> Maybe.withDefault "GMT-0000" - |> String.dropLeft 3 - |> String.toInt - |> Result.withDefault 0 - |> flip (//) 100 - |> toFloat - |> (*) -Time.hour - in - add' tzOffset date diff --git a/exercises/gigasecond/GigasecondTests.elm b/exercises/gigasecond/GigasecondTests.elm index 6880402..6bcf89c 100644 --- a/exercises/gigasecond/GigasecondTests.elm +++ b/exercises/gigasecond/GigasecondTests.elm @@ -4,6 +4,7 @@ import Test.Runner.Node exposing (run) import Json.Encode exposing (Value) import Test exposing (..) import Expect +import Date import Gigasecond exposing (add) @@ -13,23 +14,33 @@ tests = [ describe "add" [ test "2011-04-25" <| \() -> - Expect.equal (Ok "2043-01-01T01:46:40") (Gigasecond.add "2011-04-25") + Expect.equal (actual "2011-04-25") (expected "2043-01-01T01:46:40") , test "1977-06-13" <| \() -> - Expect.equal (Ok "2009-02-19T01:46:40") (Gigasecond.add "1977-06-13") + Expect.equal (actual "1977-06-13") (expected "2009-02-19T01:46:40") , test "1959-07-19" <| \() -> - Expect.equal (Ok "1991-03-27T01:46:40") (Gigasecond.add "1959-07-19") + Expect.equal (actual "1959-07-19") (expected "1991-03-27T01:46:40") , test "full time specified" <| \() -> - Expect.equal (Ok "2046-10-02T23:46:40") (Gigasecond.add "2015-01-24T22:00:00") + Expect.equal (actual "2015-01-24T22:00:00") (expected "2046-10-02T23:46:40") , test "full time with day roll-over" <| \() -> - Expect.equal (Ok "2046-10-03T01:46:39") (Gigasecond.add "2015-01-24T23:59:59") + Expect.equal (actual "2015-01-24T23:59:59") (expected "2046-10-03T01:46:39") ] ] +actual : String -> Result String Date.Date +actual = + Date.fromString >> Result.map Gigasecond.add + + +expected : String -> Result String Date.Date +expected = + Date.fromString + + main : Program Value main = run emit tests