我正在使用 elm-repl 玩弄这门语言。
我想查看当前时间。我该怎么做?但是目前的库似乎不支持此功能。为什么会这样呢?
编辑:我制作了一个包来帮助解决这个问题。http://package.elm-lang.org/packages/z5h/time-app
这个问题是关于 elm 0.15 的 - 在 elm 0.17 & 0.18 中有所不同,请参见如何在 Elm 0.17/0.18 中获取当前时间?
我正在使用 elm-repl 玩弄这门语言。
我想查看当前时间。我该怎么做?但是目前的库似乎不支持此功能。为什么会这样呢?
编辑:我制作了一个包来帮助解决这个问题。http://package.elm-lang.org/packages/z5h/time-app
这个问题是关于 elm 0.15 的 - 在 elm 0.17 & 0.18 中有所不同,请参见如何在 Elm 0.17/0.18 中获取当前时间?
elm/time
。与0.18一样,您只需要一个命令和消息处理结果type Msg
= OnTime Time.Posix
getTime : Cmd Msg
getTime =
Task.perform OnTime Time.now
0.18版本更新。这变得更简单了。现在所有你需要的是一个命令和一条消息来处理结果。
type Msg
= OnTime Time
getTime : Cmd Msg
getTime =
Task.perform OnTime Time.now
查看此Ellie
使用0.17版本,现在变得更加简单了。 Time库中现已有一个任务。因此,例如,我们现在拥有:
Time.now
|> Task.Perform NoOp CurrentTime
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
Task
并传递回调,该回调将在系统调用后接收时间,或者使用 Task
和 Mailbox
,这将导致值在可用后发送回您的程序。请参见链接和链接以了解有关链接任务和与邮箱通信的信息。 - pdoherty926update : action -> Time -> model -> (model, Effects action)
Elm 0.19
下面的代码将初始时间设置为Unix时间戳起点Time.millisToPosix 0
,但你可以将其设置为Nothing
,然后再将其设置为Just time
或用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
]
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()
};
};
你的代码
programStart = Now.loadTime
"native-modules": true
- Tom Kludyelm-repl
不能处理 Signal
,而时间会随着时间的推移而改变,因此它是一个信号。
目前我不知道有没有获取时间的 Task
。也没有一种在 repl 中执行任务的方法,尽管我希望这将成为未来的特性。getCurrentTime
函数)。type Action = Tick Time | ...
并在更新中使用:Tick time -> {model | time = time}
。 - mgoldSimon H的答案(0.18)让我朝着正确的方向开始了,但是我确实遇到了一些麻烦,不知道如何实际操作这个时间。(user2167582
在评论中问了同样的问题:如何“取出时间”?)。
我的具体问题是,我想在POST请求的正文中包含当前时间。
最终我解决了这个问题,并对结果感到非常满意——使用Task.andThen
意味着在我的postTime
函数中,我可以将timestamp
作为“常规”的浮点值参数使用(当任务运行时,我想它就是这样)。
我的完整SO答案在这里。
下面是我想出的解决方案,在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
}