Skip to content

Instantly share code, notes, and snippets.

@r-k-b
Created February 13, 2017 11:17
Show Gist options
  • Save r-k-b/e589b02d68cab07af63347507c8d0a2d to your computer and use it in GitHub Desktop.
Save r-k-b/e589b02d68cab07af63347507c8d0a2d to your computer and use it in GitHub Desktop.
How do I get the current time in Elm 0.18?
{-|
Based on [robertjlooby's answer](http://stackoverflow.com/a/38022835/2014893), updated for Elm 0.18.
-}
import Html exposing (div, button, text, program)
import Html.Events exposing (onClick)
import Task
import Time exposing (Time)
main =
program
{ init = ( Model 0 0, Cmd.none )
, view = view
, update = update
, subscriptions = (\_ -> Sub.none)
}
type alias Model =
{ count : Int
, updateTime : Time
}
view model =
Html.map GetTimeAndThen (modelView model)
type Msg
= GetTimeAndThen ModelMsg
| GotTime ModelMsg Time
update msg model =
case msg of
GetTimeAndThen wrappedMsg ->
( model, Task.perform (GotTime wrappedMsg) Time.now )
GotTime wrappedMsg time ->
let
( newModel, cmd ) =
modelUpdate wrappedMsg time model
in
( newModel, Cmd.map GetTimeAndThen cmd )
type ModelMsg
= Increment
| Decrement
modelUpdate msg time model =
case msg of
Increment ->
( { model | count = model.count + 1, updateTime = time }, Cmd.none )
Decrement ->
( { model | count = model.count - 1, updateTime = time }, Cmd.none )
modelView model =
div []
[ button [ onClick Decrement ] [ text "-" ]
, div [] [ text (toString model.count) ]
, button [ onClick Increment ] [ text "+" ]
, div [] [ text (toString model.updateTime) ]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment