I\'m running elm-repl to play around with the language.
I\'d like to see what the current time is. How would I do that? It doesn\'t appear to be possible with the c
The answer (for 0.18) from Simon H got me started in the right direction, but I did have some trouble working out how to actually do something with that time. (user2167582 adds a comment to Simon's answer which asks the same thing: how do you 'get the time out?').
My specific problem was that I wanted to include the current time in the body of a POST to the server.
I eventually solved that and was quite pleased with the end result -- the use of Task.andThen meant that I in my postTime function I can just use timestamp as a 'regular' float-valued parameter (which it is when the task gets run, I suppose).
My full SO answer is here.
Below is the solution I came up with and here it is in Ellie:
module Main exposing (..)
import Html exposing (..)
import Html.Events exposing (..)
import Http
import Json.Decode as JD
import Json.Encode as JE
import Task
import Time
type alias Model =
{ url : String
}
type Msg
= PostTimeToServer
| PostDone (Result Http.Error String)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
PostTimeToServer ->
( model, postTimeToServer model.url )
PostDone _ ->
( model, Cmd.none )
view : Model -> Html Msg
view model =
div []
[ div []
[ button [ onClick PostTimeToServer ] [ Html.text "POST the current time." ]
]
]
postTimeToServer : String -> Cmd Msg
postTimeToServer url =
let
getTime =
Time.now
postTime t =
JD.string
|> Http.post url (JE.float t |> Http.jsonBody)
|> Http.toTask
request =
getTime <<-- Here is
|> Task.andThen postTime <<-- the key bit.
in
Task.attempt PostDone request
main =
Html.program
{ init = ( Model "url_here", Cmd.none )
, update = update
, view = view
, subscriptions = always Sub.none
}
Elm 0.19
Below I set inital time as unix time start Time.millisToPosix 0, but you can set it to Nothing and later to Just time or pass it with Flag.
module Main exposing (main)
import Browser
import Html exposing (Html)
import Task
import Time exposing (Posix)
main : Program () Model Msg
main =
Browser.element
{ init = \_ -> init
, view = view
, update = update
, subscriptions = \_ -> Sub.none
}
-- MODEL
type alias Model =
{ zone : Time.Zone
, now : Posix
}
init : ( Model, Cmd Msg )
init =
( Model Time.utc (Time.millisToPosix 0), Task.perform Zone Time.here )
-- UPDATE
type Msg
= Zone Time.Zone
| Now Posix
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Zone zone ->
( { model | zone = zone }, Task.perform Now Time.now )
Now now ->
( { model | now = now }, Cmd.none )
-- VIEW
formatTime zone posix =
(String.padLeft 2 '0' <| String.fromInt <| Time.toHour zone posix)
++ ":"
++ (String.padLeft 2 '0' <| String.fromInt <| Time.toMinute zone posix)
++ ":"
++ (String.padLeft 2 '0' <| String.fromInt <| Time.toSecond zone posix)
view : Model -> Html Msg
view model =
Html.div []
[ Html.text <| formatTime model.zone model.now
]