Skip to content

Instantly share code, notes, and snippets.

@strokyl
Last active October 28, 2025 14:49
Show Gist options
  • Select an option

  • Save strokyl/63288e48d40e392004eaa6b2fceeb65a to your computer and use it in GitHub Desktop.

Select an option

Save strokyl/63288e48d40e392004eaa6b2fceeb65a to your computer and use it in GitHub Desktop.
module Main exposing (main)
import Browser
import Time
import Task
import Html exposing (Html, text, h1)
main = Browser.element { init = init, view = view, update = update, subscriptions = subscriptions}
type Model = Loading { zone: Maybe Time.Zone , time: Maybe Time.Posix} | Loaded { zone: Time.Zone , time: Time.Posix}
init: () -> (Model, Cmd Msg)
init () = (Loading { zone = Nothing, time = Nothing}, Task.perform GetLocalTimeZone Time.here)
type Msg = Tick Time.Posix | GetLocalTimeZone Time.Zone
update: Msg -> Model -> (Model, Cmd Msg)
update msg model =
let newModel = case (msg, model) of
(Tick time, Loaded loadedModel) -> Loaded { loadedModel | time = time}
(Tick newTime, Loading ({ zone , time} as loadingModel)) -> case (zone) of
(Just zone_) -> Loaded {zone = zone_, time = newTime}
_ -> Loading { loadingModel | time = Just newTime}
(GetLocalTimeZone zone, Loaded loadedModel) -> Loaded {loadedModel | zone = zone}
(GetLocalTimeZone newZone, Loading ({zone, time} as loadingModel)) -> case (time) of
(Just time_) -> Loaded {zone = newZone, time = time_}
_ -> Loading {loadingModel | zone = Just newZone}
in
(newModel, Cmd.none)
subscriptions: Model -> Sub Msg
subscriptions _ = Time.every 1000 Tick
int_to_string_for_hour n = n |> String.fromInt |> String.padLeft 2 '0'
view model = case model of
Loaded { zone, time} ->
let
hour = int_to_string_for_hour (Time.toHour zone time)
minute = int_to_string_for_hour (Time.toMinute zone time)
second = int_to_string_for_hour (Time.toSecond zone time)
in
h1 [] [ text (hour ++ ":" ++ minute ++ ":" ++ second) ]
Loading _ ->
h1 [] [ text "Loading"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment