按时区获取 Person 榆树 中的本地时间

发布于 2025-01-15 10:34:53 字数 2226 浏览 1 评论 0原文

我是 elm 新手,我正在尝试创建一个管理面板,根据当地时间和工作时间显示技术支持列表及其可用时间。 支持者名单:

init flags =
    ( { supportUserList =
            [ { supportName = "Maynard Kaminski"
              , numberOfClient = 12
              , zone = "Europe/Moscow"
              , startTime = "9 am"
              , endTime = "1 pm"
              }
            , { supportName = "Belle Willett"
                , numberOfClient = 8
              , zone = "Canada/Eastern"
              , startTime = "2 pm"
              , endTime = "6 pm"
              }
            , { supportName = "Gaylene Hickson"
              , numberOfClient = 7
              , zone = "Africa/Nairobi"
              , startTime = "6 pm"
              , endTime = "10 pm"
              }
            , { supportName = "Cinthia Talbert"
              , numberOfClient = 4
              , zone = "Asia/Tokyo"
              , startTime = "2 pm"
              , endTime = "6 pm"
              }
            , { supportName = "Sydney Crenshaw"
              , numberOfClient = 7
              , zone = "Pacific/Honolulu"
              , startTime = "6 am"
              , endTime = "10 am"
              }
            ]
      }
    , Cmd.none
    )
viewSupporter : Supporter -> Html msg
viewSupporter supporter =
    li []
        [ text supporter.supportName
        , text " "
        , text " ("
        , text (String.fromInt (supporter.numberOfClient))
        , text ")"
        , text " "
        , text supporter.startTime
        , text " - "
        , text supporter.endTime
        , text " ("
        , text "local time ?"
        , text ")"
        ]

预期结果 在此处输入图片描述

当前结果 在此处输入图像描述

我试图检查https://guide.elm-lang.org/effects/time.html如何获取 Elm 的当前时间?

但仍然找不到解决方案。 这是我当前的代码位于 Eillie 中。 花了将近9个小时,没有找到解决方案。我如何通过时区值获取一个人的当地时间。谢谢

I am quite new in elm and I am trying to create an admin panel that shows the technical support list and their available time according to their local time and their working hours.
Supporter List:

init flags =
    ( { supportUserList =
            [ { supportName = "Maynard Kaminski"
              , numberOfClient = 12
              , zone = "Europe/Moscow"
              , startTime = "9 am"
              , endTime = "1 pm"
              }
            , { supportName = "Belle Willett"
                , numberOfClient = 8
              , zone = "Canada/Eastern"
              , startTime = "2 pm"
              , endTime = "6 pm"
              }
            , { supportName = "Gaylene Hickson"
              , numberOfClient = 7
              , zone = "Africa/Nairobi"
              , startTime = "6 pm"
              , endTime = "10 pm"
              }
            , { supportName = "Cinthia Talbert"
              , numberOfClient = 4
              , zone = "Asia/Tokyo"
              , startTime = "2 pm"
              , endTime = "6 pm"
              }
            , { supportName = "Sydney Crenshaw"
              , numberOfClient = 7
              , zone = "Pacific/Honolulu"
              , startTime = "6 am"
              , endTime = "10 am"
              }
            ]
      }
    , Cmd.none
    )
viewSupporter : Supporter -> Html msg
viewSupporter supporter =
    li []
        [ text supporter.supportName
        , text " "
        , text " ("
        , text (String.fromInt (supporter.numberOfClient))
        , text ")"
        , text " "
        , text supporter.startTime
        , text " - "
        , text supporter.endTime
        , text " ("
        , text "local time ?"
        , text ")"
        ]

Expected Result
enter image description here

Current Result
enter image description here

I was trying to check https://guide.elm-lang.org/effects/time.html and How do I get the current time in Elm?

But still could not find a solution.
Here is my current code is in Eillie.
Spent almost 9 hours and could not find a solution. How could I get a person's local time by their time zone value. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迟到的我 2025-01-22 10:34:53

您需要将这些 zone 字符串转换为实际的 Time.Zone,这可以使用 justinmimbs 的 时区库

然后,您需要使用 Time.every 之类的方法获取当前时间,并使用 Time.Extra.posixToParts

一些示例代码:

module Main exposing (main)

import Browser
import Dict
import Html exposing (Html, li, text, ul)
import Time exposing (Month(..))
import Time.Extra
import TimeZone


type Msg
    = Tick Time.Posix


type alias Model =
    { now : Time.Posix }


type alias RawSupporter =
    { supportName : String
    , numberOfClient : Int
    , zone : String
    , startTime : String
    , endTime : String
    }


type alias Supporter =
    { supportName : String
    , numberOfClient : Int
    , zone : Maybe Time.Zone
    , rawZone : String
    , startTime : String
    , endTime : String
    }


rawSupporters : List RawSupporter
rawSupporters =
    [ { supportName = "Maynard Kaminski"
      , numberOfClient = 12
      , zone = "Europe/Moscow"
      , startTime = "9 am"
      , endTime = "1 pm"
      }
    , { supportName = "Belle Willett"
      , numberOfClient = 8
      , zone = "Canada/Eastern"
      , startTime = "2 pm"
      , endTime = "6 pm"
      }
    , { supportName = "Gaylene Hickson"
      , numberOfClient = 7
      , zone = "Africa/Nairobi"
      , startTime = "6 pm"
      , endTime = "10 pm"
      }
    , { supportName = "Cinthia Talbert"
      , numberOfClient = 4
      , zone = "Asia/Tokyo"
      , startTime = "2 pm"
      , endTime = "6 pm"
      }
    , { supportName = "Sydney Crenshaw"
      , numberOfClient = 7
      , zone = "Pacific/Honolulu"
      , startTime = "6 am"
      , endTime = "10 am"
      }
    ]


supporters : List Supporter
supporters =
    rawSupporters
        |> List.map
            (\raw ->
                { supportName = raw.supportName
                , numberOfClient = raw.numberOfClient
                , zone =
                    TimeZone.zones
                        |> Dict.get raw.zone
                        |> Maybe.map (\evaluate -> evaluate ())
                , rawZone = raw.zone
                , startTime = raw.startTime
                , endTime = raw.endTime
                }
            )


viewSupporter : Time.Posix -> Supporter -> Html msg
viewSupporter now supporter =
    let
        parts =
            supporter.zone
                |> Maybe.map
                    (\zone ->
                        Time.Extra.posixToParts zone now
                    )

        localTimeDisplay =
            case parts of
                Just { year, month, day, hour, minute, second, millisecond } ->
                    ([ [ year, monthNum month, day ]
                        |> List.map String.fromInt
                        |> String.join "-"
                     , [ hour, minute, second ]
                        |> List.map String.fromInt
                        |> String.join ":"
                     ]
                        |> String.join " "
                    )
                        ++ " (local time)"

                Nothing ->
                    "<Not a valid timezone: '" ++ supporter.rawZone ++ "'>"
    in
    [ supporter.supportName
    , " ("
    , String.fromInt supporter.numberOfClient
    , ") "
    , supporter.startTime
    , " - "
    , supporter.endTime
    , " "
    , localTimeDisplay
    ]
        |> List.map text
        |> li []


monthNum : Month -> Int
monthNum m =
    case m of
        Jan ->
            1

        Feb ->
            2

        Mar ->
            3

        Apr ->
            4

        May ->
            5

        Jun ->
            6

        Jul ->
            7

        Aug ->
            8

        Sep ->
            9

        Oct ->
            10

        Nov ->
            11

        Dec ->
            12


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        Tick newTime ->
            ( { model | now = newTime }, Cmd.none )


view : Model -> Html Msg
view model =
    supporters
        |> List.map (viewSupporter model.now)
        |> ul []


main : Program () Model Msg
main =
    Browser.element
        { init = \() -> ( { now = Time.millisToPosix 0 }, Cmd.none )
        , view = view
        , update = update
        , subscriptions = \_ -> Time.every 1000 Tick
        }

这是在 ellie-app.com 上运行的相同代码。

You'll want to convert those zone strings into actual Time.Zones, which can be done with justinmimbs' TimeZone library.

Then, you'll need to get the current time with something like Time.every, and convert it to local parts with Time.Extra.posixToParts.

Some example code:

module Main exposing (main)

import Browser
import Dict
import Html exposing (Html, li, text, ul)
import Time exposing (Month(..))
import Time.Extra
import TimeZone


type Msg
    = Tick Time.Posix


type alias Model =
    { now : Time.Posix }


type alias RawSupporter =
    { supportName : String
    , numberOfClient : Int
    , zone : String
    , startTime : String
    , endTime : String
    }


type alias Supporter =
    { supportName : String
    , numberOfClient : Int
    , zone : Maybe Time.Zone
    , rawZone : String
    , startTime : String
    , endTime : String
    }


rawSupporters : List RawSupporter
rawSupporters =
    [ { supportName = "Maynard Kaminski"
      , numberOfClient = 12
      , zone = "Europe/Moscow"
      , startTime = "9 am"
      , endTime = "1 pm"
      }
    , { supportName = "Belle Willett"
      , numberOfClient = 8
      , zone = "Canada/Eastern"
      , startTime = "2 pm"
      , endTime = "6 pm"
      }
    , { supportName = "Gaylene Hickson"
      , numberOfClient = 7
      , zone = "Africa/Nairobi"
      , startTime = "6 pm"
      , endTime = "10 pm"
      }
    , { supportName = "Cinthia Talbert"
      , numberOfClient = 4
      , zone = "Asia/Tokyo"
      , startTime = "2 pm"
      , endTime = "6 pm"
      }
    , { supportName = "Sydney Crenshaw"
      , numberOfClient = 7
      , zone = "Pacific/Honolulu"
      , startTime = "6 am"
      , endTime = "10 am"
      }
    ]


supporters : List Supporter
supporters =
    rawSupporters
        |> List.map
            (\raw ->
                { supportName = raw.supportName
                , numberOfClient = raw.numberOfClient
                , zone =
                    TimeZone.zones
                        |> Dict.get raw.zone
                        |> Maybe.map (\evaluate -> evaluate ())
                , rawZone = raw.zone
                , startTime = raw.startTime
                , endTime = raw.endTime
                }
            )


viewSupporter : Time.Posix -> Supporter -> Html msg
viewSupporter now supporter =
    let
        parts =
            supporter.zone
                |> Maybe.map
                    (\zone ->
                        Time.Extra.posixToParts zone now
                    )

        localTimeDisplay =
            case parts of
                Just { year, month, day, hour, minute, second, millisecond } ->
                    ([ [ year, monthNum month, day ]
                        |> List.map String.fromInt
                        |> String.join "-"
                     , [ hour, minute, second ]
                        |> List.map String.fromInt
                        |> String.join ":"
                     ]
                        |> String.join " "
                    )
                        ++ " (local time)"

                Nothing ->
                    "<Not a valid timezone: '" ++ supporter.rawZone ++ "'>"
    in
    [ supporter.supportName
    , " ("
    , String.fromInt supporter.numberOfClient
    , ") "
    , supporter.startTime
    , " - "
    , supporter.endTime
    , " "
    , localTimeDisplay
    ]
        |> List.map text
        |> li []


monthNum : Month -> Int
monthNum m =
    case m of
        Jan ->
            1

        Feb ->
            2

        Mar ->
            3

        Apr ->
            4

        May ->
            5

        Jun ->
            6

        Jul ->
            7

        Aug ->
            8

        Sep ->
            9

        Oct ->
            10

        Nov ->
            11

        Dec ->
            12


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        Tick newTime ->
            ( { model | now = newTime }, Cmd.none )


view : Model -> Html Msg
view model =
    supporters
        |> List.map (viewSupporter model.now)
        |> ul []


main : Program () Model Msg
main =
    Browser.element
        { init = \() -> ( { now = Time.millisToPosix 0 }, Cmd.none )
        , view = view
        , update = update
        , subscriptions = \_ -> Time.every 1000 Tick
        }

Here's the same code running on ellie-app.com.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文