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 current library. Why is that?
EDIT: I made a package to help with this. http://package.elm-lang.org/packages/z5h/time-app
This was asked around elm 0.15 - things are different in elm 0.17 & 0.18: see How do I get the current time in Elm 0.17/0.18?
Update for 0.19 It is not possible to get the current time using the standard library.. You need to use elm/time
. As with 0.18, all you need is a command and Msg to handle the result
type Msg
= OnTime Time.Posix
getTime : Cmd Msg
getTime =
Task.perform OnTime Time.now
Update for 0.18 This has got simpler again. Now all you need is a command and Msg to handle the result
type Msg
= OnTime Time
getTime : Cmd Msg
getTime =
Task.perform OnTime Time.now
See this Ellie
Original answer
With 0.17, this got a whole lot easier. There is now a Task in the Time library. So for example, we now have:
Time.now
|> Task.Perform NoOp CurrentTime
You can use the Time package and/or the Date package.
Here's a contrived example which uses both:
import Signal
import Time exposing (every, second)
import Date exposing (year, hour, minute, second, fromTime)
import Graphics.Element exposing (show)
main =
Signal.map currentTime (Time.every Time.second)
currentTime t =
let date' = fromTime t
hour' = toString (Date.hour date')
minute' = toString (Date.minute date')
second' = toString (Date.second date')
year' = toString (year date')
now = "The current time is: " ++ hour' ++ ":" ++ minute' ++ ":" ++ second'
in
show now
To resolve my own question, I've created a variant of StartApp that includes a timestamp on each action.
So the update function has signature:update : action -> Time -> model -> (model, Effects action)
The Gist is here. https://gist.github.com/z5h/41ca436679591b6c3e51
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
]
If you want the time as of program start you can do the following:
Now.elm
module Now where
import Native.Now
loadTime : Float
loadTime = Native.Now.loadTime
Native/Now.js
Elm.Native.Now = {};
Elm.Native.Now.make = function(localRuntime) {
localRuntime.Native = localRuntime.Native || {};
localRuntime.Native.Now = localRuntime.Native.Now || {};
if (localRuntime.Native.Now.values) {
return localRuntime.Native.Now.values;
}
var Result = Elm.Result.make(localRuntime);
return localRuntime.Native.Now.values = {
loadTime: (new window.Date).getTime()
};
};
your code
programStart = Now.loadTime
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With