在榆树中按时区获取人当地时间

Get Person Local time by timezone in elm

我是 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 ")"
        ]

预期结果 enter image description here

当前结果 enter image description here

我正在尝试检查 https://guide.elm-lang.org/effects/time.html and

但是还是找不到解决办法。 这是我当前的代码在 Eillie 中。 花了将近 9 个小时,找不到解决方案。我怎么能通过他们的时区值得到一个人的当地时间。谢谢

您需要将那些 zone 字符串转换为实际的 Time.Zone,这可以通过 justinmimbs 的 TimeZone library.

完成

然后,您需要使用 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
        }

这里是the same code running on ellie-app.com.