Created
September 5, 2016 10:49
-
-
Save zemm/5615ef85b7414a4ff27cecba4817e04e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main exposing (..) | |
import Html exposing (Html) | |
import Html.App as App | |
import Time exposing (Time, second) | |
import Date exposing (Date) | |
import Date.Extra as Dx | |
main : Program Never | |
main = | |
App.program | |
{ init = init | |
, view = view | |
, update = update | |
, subscriptions = subscriptions | |
} | |
-- MODEL | |
type alias Model = | |
Time | |
init : ( Model, Cmd Msg ) | |
init = | |
( 0, Cmd.none ) | |
-- UPDATE | |
type Msg | |
= Tick Time | |
update : Msg -> Model -> ( Model, Cmd Msg ) | |
update msg model = | |
case msg of | |
Tick newTime -> | |
( newTime, Cmd.none ) | |
-- SUBSCRIPTIONS | |
subscriptions : Model -> Sub Msg | |
subscriptions model = | |
Time.every second Tick | |
-- VIEW | |
view : Model -> Html Msg | |
view model = | |
let | |
now = | |
Date.fromTime model | |
target = | |
nextMomentFrom 13 37 now | |
diff = | |
Dx.diff Dx.Second now target | |
in | |
Html.div [] | |
[ Html.div [] [ Html.text <| toString now ] | |
, Html.div [] [ Html.text <| toString target ] | |
, Html.div [] [ Html.text <| toString diff ] | |
] | |
nextMomentFrom : Int -> Int -> Date -> Date | |
nextMomentFrom h m from = | |
let | |
sameDayTarget = | |
Dx.floor Dx.Day from |> Dx.add Dx.Hour h |> Dx.add Dx.Minute m | |
in | |
if Dx.compare sameDayTarget from == LT then | |
sameDayTarget |> Dx.add Dx.Day 1 | |
else | |
sameDayTarget |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using
justinmimbs/elm-date-extra