Refactor Gigasecond.add to Speak Date Instead of String

This commit is contained in:
Joshua Stoutenburg 2016-08-28 22:01:33 -06:00
parent 934118e5c7
commit bb4b15ce0c
2 changed files with 19 additions and 115 deletions

View file

@ -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

View file

@ -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