Skip to content

Instantly share code, notes, and snippets.

@zemm
Created September 5, 2016 10:49
Show Gist options
  • Save zemm/5615ef85b7414a4ff27cecba4817e04e to your computer and use it in GitHub Desktop.
Save zemm/5615ef85b7414a4ff27cecba4817e04e to your computer and use it in GitHub Desktop.
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
@zemm
Copy link
Author

zemm commented Sep 5, 2016

Using justinmimbs/elm-date-extra

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment