Created
March 19, 2024 21:31
-
-
Save mostlyobvious/8d4da4beb6b77cf4f4aece61a4ed9105 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module KakaDudu exposing (..) | |
import Browser | |
import Dict | |
import Html exposing (..) | |
import Task | |
import Time | |
import TimeZone | |
main = | |
Browser.element | |
{ init = init | |
, view = view | |
, update = update | |
, subscriptions = subscriptions | |
} | |
type alias Model = | |
( String, Time.Zone ) | |
deprecatedTimeZones : String -> String | |
deprecatedTimeZones oldName = | |
case oldName of | |
"Europe/Copenhagen" -> | |
"Europe/Berlin" | |
_ -> | |
oldName | |
requestBrowserTimeZone : Cmd Msg | |
requestBrowserTimeZone = | |
Task.attempt ReceiveTimeZone Time.getZoneName | |
initialTime = | |
( "UTC", Time.utc ) | |
init : () -> ( Model, Cmd Msg ) | |
init _ = | |
( initialTime | |
, requestBrowserTimeZone | |
) | |
type Msg | |
= ReceiveTimeZone (Result String Time.ZoneName) | |
update : Msg -> Model -> ( Model, Cmd Msg ) | |
update msg ( modelZoneName, modelZone ) = | |
case msg of | |
ReceiveTimeZone result -> | |
case result of | |
Ok zoneName -> | |
case zoneName of | |
Time.Name newZoneName -> | |
let | |
betterZoneName = | |
deprecatedTimeZones newZoneName | |
in | |
case Dict.get betterZoneName TimeZone.zones of | |
Just zone -> | |
( ( newZoneName, zone () ), Cmd.none ) | |
Nothing -> | |
( initialTime, Cmd.none ) | |
Time.Offset _ -> | |
( initialTime, Cmd.none ) | |
Err _ -> | |
( initialTime, Cmd.none ) | |
subscriptions : Model -> Sub Msg | |
subscriptions _ = | |
Sub.none | |
view : Model -> Html Msg | |
view ( zoneName, zone ) = | |
h1 [] [ text zoneName ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment