榆树当前日期
Elm Current Date
在 elm 中,我有一个包含 currentDate
的模型,它是一个表示日期的字符串。我用信号更新它:
Signal.map (SetCurrentDate << timeToDateString) (Time.every Time.second)
SetCurrentDate
是更新模型的动作(SetCurrentDate date -> { model | currentDate <- date}
),而timeToDateString
是从时间转换成字符串,如"yyyy-mm-dd".
但是,这样做有两个问题:
model.currentDate
仅在一秒后才正确设置。所以在应用程序开始时有一秒钟 currentDate 设置不正确。
model.currentDate
每秒设置一次,即使它每天都在变化。
有办法解决这个问题吗?所以 currentDate 是在应用程序开始时设置的(没有一秒钟的延迟),而且它每天只更新一次?
额外发现:
如果我将信号更改为 Signal.dropRepeats <| Signal.map (SetCurrentDate << timeToDateString) (Time.every Time.second)
(因此我放弃重复),信号仅在日期更改时触发,而不是在应用程序启动时也触发一次。
编辑:如果有更好的方法了解 Elm 应用程序中的 currentDate,我很想听听。我的谷歌搜索一无所获。
谢谢!
使用信号
您使用的是 StartApp
还是您自己的 foldp
?如果您正在使用 StartApp
,最好查找 start
函数的代码并将其内联,这样您就可以访问 foldp
。
Signal.foldp
不对信号的初始值做任何事情。因此,如果您使用 Time.every second
信号,您只会在一秒钟后获得更新,如果您使用日期转换和 Signal.dropRepeats
,则只会在一天后获得更新。您可以使用 Signal.Extra.foldp'
from the 3rd party signal-extra library* 来解决这个问题。它需要一个函数来根据输入的初始值创建 foldp
的初始状态。
*完全披露:我是图书馆的作者
使用任务
task-tutorial library called getCurrentTime
. I think that can serve your needs without needing a signal that updates every second. Along with Task.sleep
中有一个非常有用的任务,你可能会得到一些每天只检查一次或两次时间的东西。
Apanatshka 的回答 ( ) 很有道理,但作为 Elm 的初学者,我无法将解释应用于实际代码。经过一番摸索之后,这里是呈现自动更新日期所需的最少代码(使用 Elm v0.16.0):
import Signal
import Html exposing (Html, text)
import Time exposing (Time, every)
import Date exposing (Date, Month, fromTime, year, month, day, hour, minute, second)
-- not used by foldp (
startTime = 0
type Action = Update Date
type alias Model = Date
showDate : Date -> String
showDate date = toString (month date) ++ " " ++
toString (day date) ++ ", " ++
toString (year date) ++ " " ++
toString (hour date) ++ ":" ++
toString (minute date) ++ ":" ++
toString (second date)
update : Action -> Model -> Model
update (Update date) _ = date
model : Signal Model
model =
Signal.foldp update (fromTime startTime) clock
timeToAction : Time -> Action
timeToAction time = Update <| fromTime time
clock : Signal Action
clock =
Signal.map timeToAction <| every Time.second
main : Signal Html
main =
Signal.map view model
view : Model -> Html
view model =
text <| showDate model
下面是一个稍微复杂一些的示例,它将时钟合并到其他信号中。不知道这与 elm 最佳实践的符合程度如何,但它确实有效,并且它可以作为一个有用的学习工具,就像它对我一样。
import Signal
import Html exposing (..)
import Html.Events exposing (..)
import Keyboard
import Char
import Time exposing (Time)
import Date exposing (Date, Month, fromTime, year, month, day, hour, minute, second)
startTime = 0
type Action =
NoOp
| Increment
| Decrement
| Update Time
type alias Model = {
count: Int,
time: Time
}
showDate : Date -> String
showDate date = toString (month date) ++ " " ++
toString (day date) ++ ", " ++
toString (year date) ++ " " ++
toString (hour date) ++ ":" ++
toString (minute date) ++ ":" ++
toString (second date)
actions : Signal.Mailbox Action
actions =
Signal.mailbox NoOp
update : Action -> Model -> Model
update action model =
case action of
NoOp -> model
Increment -> { model | count = model.count + 1 }
Decrement -> { model | count = model.count - 1 }
Update time -> { model | time = time }
model : Signal Model
model =
Signal.foldp update { count = 0, time = startTime } (Signal.mergeMany [
actions.signal,
keyPressesToAction,
clock
])
keyPressesToAction : Signal Action
keyPressesToAction =
let
keyCodeToAction keyCode =
case Char.fromCode keyCode of
'=' -> Increment
'-' -> Decrement
_ -> NoOp
in
Signal.map keyCodeToAction Keyboard.presses
timeToAction : Time -> Action
timeToAction time = Update time
clock : Signal Action
clock = Signal.map timeToAction (Time.every Time.second)
main : Signal Html
main =
Signal.map (view actions.address) model
view : Signal.Address Action -> Model -> Html
view address model =
div [] [
text <| showDate <| fromTime model.time,
div []
[ button [ onClick address Decrement ] [ text "-" ]
, text <| toString model.count
, button [ onClick address Increment ] [ text "+" ]
]
]
在 elm 中,我有一个包含 currentDate
的模型,它是一个表示日期的字符串。我用信号更新它:
Signal.map (SetCurrentDate << timeToDateString) (Time.every Time.second)
SetCurrentDate
是更新模型的动作(SetCurrentDate date -> { model | currentDate <- date}
),而timeToDateString
是从时间转换成字符串,如"yyyy-mm-dd".
但是,这样做有两个问题:
model.currentDate
仅在一秒后才正确设置。所以在应用程序开始时有一秒钟 currentDate 设置不正确。model.currentDate
每秒设置一次,即使它每天都在变化。
有办法解决这个问题吗?所以 currentDate 是在应用程序开始时设置的(没有一秒钟的延迟),而且它每天只更新一次?
额外发现:
如果我将信号更改为 Signal.dropRepeats <| Signal.map (SetCurrentDate << timeToDateString) (Time.every Time.second)
(因此我放弃重复),信号仅在日期更改时触发,而不是在应用程序启动时也触发一次。
编辑:如果有更好的方法了解 Elm 应用程序中的 currentDate,我很想听听。我的谷歌搜索一无所获。
谢谢!
使用信号
您使用的是 StartApp
还是您自己的 foldp
?如果您正在使用 StartApp
,最好查找 start
函数的代码并将其内联,这样您就可以访问 foldp
。
Signal.foldp
不对信号的初始值做任何事情。因此,如果您使用 Time.every second
信号,您只会在一秒钟后获得更新,如果您使用日期转换和 Signal.dropRepeats
,则只会在一天后获得更新。您可以使用 Signal.Extra.foldp'
from the 3rd party signal-extra library* 来解决这个问题。它需要一个函数来根据输入的初始值创建 foldp
的初始状态。
*完全披露:我是图书馆的作者
使用任务
task-tutorial library called getCurrentTime
. I think that can serve your needs without needing a signal that updates every second. Along with Task.sleep
中有一个非常有用的任务,你可能会得到一些每天只检查一次或两次时间的东西。
Apanatshka 的回答 (
import Signal
import Html exposing (Html, text)
import Time exposing (Time, every)
import Date exposing (Date, Month, fromTime, year, month, day, hour, minute, second)
-- not used by foldp (
startTime = 0
type Action = Update Date
type alias Model = Date
showDate : Date -> String
showDate date = toString (month date) ++ " " ++
toString (day date) ++ ", " ++
toString (year date) ++ " " ++
toString (hour date) ++ ":" ++
toString (minute date) ++ ":" ++
toString (second date)
update : Action -> Model -> Model
update (Update date) _ = date
model : Signal Model
model =
Signal.foldp update (fromTime startTime) clock
timeToAction : Time -> Action
timeToAction time = Update <| fromTime time
clock : Signal Action
clock =
Signal.map timeToAction <| every Time.second
main : Signal Html
main =
Signal.map view model
view : Model -> Html
view model =
text <| showDate model
下面是一个稍微复杂一些的示例,它将时钟合并到其他信号中。不知道这与 elm 最佳实践的符合程度如何,但它确实有效,并且它可以作为一个有用的学习工具,就像它对我一样。
import Signal
import Html exposing (..)
import Html.Events exposing (..)
import Keyboard
import Char
import Time exposing (Time)
import Date exposing (Date, Month, fromTime, year, month, day, hour, minute, second)
startTime = 0
type Action =
NoOp
| Increment
| Decrement
| Update Time
type alias Model = {
count: Int,
time: Time
}
showDate : Date -> String
showDate date = toString (month date) ++ " " ++
toString (day date) ++ ", " ++
toString (year date) ++ " " ++
toString (hour date) ++ ":" ++
toString (minute date) ++ ":" ++
toString (second date)
actions : Signal.Mailbox Action
actions =
Signal.mailbox NoOp
update : Action -> Model -> Model
update action model =
case action of
NoOp -> model
Increment -> { model | count = model.count + 1 }
Decrement -> { model | count = model.count - 1 }
Update time -> { model | time = time }
model : Signal Model
model =
Signal.foldp update { count = 0, time = startTime } (Signal.mergeMany [
actions.signal,
keyPressesToAction,
clock
])
keyPressesToAction : Signal Action
keyPressesToAction =
let
keyCodeToAction keyCode =
case Char.fromCode keyCode of
'=' -> Increment
'-' -> Decrement
_ -> NoOp
in
Signal.map keyCodeToAction Keyboard.presses
timeToAction : Time -> Action
timeToAction time = Update time
clock : Signal Action
clock = Signal.map timeToAction (Time.every Time.second)
main : Signal Html
main =
Signal.map (view actions.address) model
view : Signal.Address Action -> Model -> Html
view address model =
div [] [
text <| showDate <| fromTime model.time,
div []
[ button [ onClick address Decrement ] [ text "-" ]
, text <| toString model.count
, button [ onClick address Increment ] [ text "+" ]
]
]