Initialize model with current date

I am trying to initialize my model with the current date. I do not want to use my own module. I am trying to do this with task and effects. I am stuck in my method getCurrentTime. What is the cleanest way?

import Time exposing ( Time )
import StartApp
import Task
import Effects exposing (Never)
import Html exposing ( Html )

app = 
  StartApp.start
    { init = init
    , view = view
    , update = update
    , inputs = []}

main = 
  app.html

port tasks : Signal (Task.Task Never ())
port tasks =
  app.tasks

type alias Model = 
    { clock : Time}

init : (Model, Effects.Effects Action)
init = ((Model  ), Effects.none)

type Action = ShowClock 

update : Action -> Model -> (Model, Effects.Effects Action)
update action model = 
  case action of
    ShowClock c -> 
      ({ model | clock = c}, Effects.none)

getCurrentTime : Effects.Effects Action
getCurrentTime =
  -- stuck here
  |> Task.map ShowClock
  |> Effects.task

view : Signal.Address Action -> Model -> Html
view address model =
  Signal.map Html.text model.clock

It would also be convenient to convert the time to a string with the format "YYYY-MM-DD".

+4
source share
2 answers

There is an ELM package available at http://package.elm-lang.org/packages/evancz/task-tutorial/1.0.3/TaskTutorial , which you can use to get the current time.

elm-package.json, Windows:

elm-package install evancz/task-tutorial 1.0.3

:

getCurrentTime : Effects.Effects Action
getCurrentTime = Effects.task <| Task.map ShowClock ( TaskTutorial.getCurrentTime )

, getCurrentTime , .

- String, :

 toHRDate : Time -> String -- to human readable date
 toHRDate t =
     let
       date = Date.fromTime t
       d = toString (Date.day date) -- day
       m = toString (Date.month date) -- month
       y = toString (Date.year date) -- year
       hrd = d ++ "-" ++ m ++ "-" ++ y
    in hrd
+2

, :

, , main:

port time : Float

... html, .

var myapp = Elm.fullscreen(Elm.YourAppNameHere, 
                           {
                               time: Date.now()
                           });

- elm : http://elm-lang.org/guide/interop

foldp' Signal.Extra, . http://package.elm-lang.org/packages/Apanatshka/elm-signal-extra/5.7.0

+1

Source: https://habr.com/ru/post/1620975/


All Articles