From 93bfa89a9d3a01b22e5ebac4171280bc72760042 Mon Sep 17 00:00:00 2001
From: Adrian Cochrane
Date: Mon, 20 Nov 2023 16:52:52 +1300
Subject: [PATCH] Add a timezone menu!
---
bureaucromancy.cabal | 4 +--
src/Text/HTML/Form/WebApp.hs | 9 +++++++
src/Text/HTML/Form/WebApp/Ginger/TZ.hs | 20 +++++++++++++++
tpl/cal/timezone.html | 8 ++++++
tpl/cal/year-numpad.html | 28 +++++++++++++++++++++
tpl/cal/year-numpad.html~ | 34 ++++++++++++++++++++++++++
tpl/gregorian.html | 2 +-
7 files changed, 102 insertions(+), 3 deletions(-)
create mode 100644 src/Text/HTML/Form/WebApp/Ginger/TZ.hs
create mode 100644 tpl/cal/timezone.html
create mode 100644 tpl/cal/year-numpad.html
create mode 100644 tpl/cal/year-numpad.html~
diff --git a/bureaucromancy.cabal b/bureaucromancy.cabal
index 7222203..2f5e550 100644
--- a/bureaucromancy.cabal
+++ b/bureaucromancy.cabal
@@ -63,7 +63,7 @@ library
-- Modules exported by the library.
exposed-modules: Text.HTML.Form, Text.HTML.Form.Query,
- Text.HTML.Form.WebApp, Text.HTML.Form.WebApp.Ginger, Text.HTML.Form.WebApp.Ginger.Hourglass
+ Text.HTML.Form.WebApp, Text.HTML.Form.WebApp.Ginger, Text.HTML.Form.WebApp.Ginger.Hourglass, Text.HTML.Form.WebApp.Ginger.TZ
-- Modules included in this library but not exported.
-- other-modules:
@@ -74,7 +74,7 @@ library
-- Other library packages from which modules are imported.
build-depends: base ^>=4.16.4.0, ginger, file-embed-lzma, file-embed, mtl,
bytestring, text, xml-conduit, network-uri, regex-tdfa, containers,
- filepath, directory, hourglass >= 0.2.12 && < 0.3
+ filepath, directory, hourglass >= 0.2.12 && < 0.3, tz >= 0.1 && < 0.2
-- Directories containing source files.
hs-source-dirs: src
diff --git a/src/Text/HTML/Form/WebApp.hs b/src/Text/HTML/Form/WebApp.hs
index 5445488..5cc31ab 100644
--- a/src/Text/HTML/Form/WebApp.hs
+++ b/src/Text/HTML/Form/WebApp.hs
@@ -17,9 +17,11 @@ import Text.HTML.Form.WebApp.Ginger (template, template', resolveSource, list')
import Text.HTML.Form.Query (renderQueryString, renderQuery')
import Text.HTML.Form.WebApp.Ginger.Hourglass (timeData, modifyTime', timeParseOrNow,
gSeqTo, gPad2)
+import Text.HTML.Form.WebApp.Ginger.TZ (tzdata)
import Text.Ginger.GVal as V (GVal(..), toGVal, orderedDict, (~>), fromFunction)
import Text.Ginger.Html (html)
+import Data.Hourglass (Elapsed(..), Seconds(..), timeGetElapsed, localTimeToGlobal)
type Query = [(ByteString, Maybe ByteString)]
renderPage :: Form -> [Text] -> Query -> IO (Maybe (Either Query Text))
@@ -40,6 +42,13 @@ renderInput form ix input@Input { inputType = "date", inputName = name } ["year"
\prop -> case prop of
"T" -> timeData t'
_ -> toGVal ()
+renderInput form ix input@Input { inputType = "date", inputName = name } ["zone", ""] qs = do
+ t <- timeParseOrNow $ get name qs
+ let Elapsed (Seconds t') = timeGetElapsed $ localTimeToGlobal t
+ template' "cal/year-numpad.html" form ix input qs $ \prop -> case prop of
+ "T" -> timeData t
+ "zones" -> tzdata t'
+ _ -> toGVal ()
renderInput form ix input@Input { multiple = True } [p] qs
| '=':v' <- Txt.unpack p,
(utf8 $ inputName input, Just $ utf8' v') `Prelude.elem` qs =
diff --git a/src/Text/HTML/Form/WebApp/Ginger/TZ.hs b/src/Text/HTML/Form/WebApp/Ginger/TZ.hs
new file mode 100644
index 0000000..5b68291
--- /dev/null
+++ b/src/Text/HTML/Form/WebApp/Ginger/TZ.hs
@@ -0,0 +1,20 @@
+{-# LANGUAGE OverloadedStrings #-}
+module Text.HTML.Form.WebApp.Ginger.TZ(tzdata) where
+
+import Text.Ginger.GVal as V (GVal, orderedDict, (~>), list)
+import qualified Data.Map.Strict as M
+import Data.Time.Zones.All (tzNameLabelMap, tzByLabel)
+import Data.Time.Zones (diffForPOSIX)
+import Data.Int (Int64)
+
+tzdata :: Int64 -> GVal m
+tzdata now = list [orderedDict [
+ "label" ~> label,
+ "value" ~> (diffForPOSIX tz' now `div` 60),
+ "offset" ~> formatOffset (diffForPOSIX tz' now `div` 60)
+ ] | (label, tz) <- M.toList tzNameLabelMap, let tz' = tzByLabel tz]
+formatOffset :: (Show a, Integral a) => a -> [Char]
+formatOffset offset = (show hours ++ ':': show minutes)
+ where
+ hours = offset `div` 60
+ minutes = abs $ offset `rem` 60
diff --git a/tpl/cal/timezone.html b/tpl/cal/timezone.html
new file mode 100644
index 0000000..a1cf1af
--- /dev/null
+++ b/tpl/cal/timezone.html
@@ -0,0 +1,8 @@
+{% extends "/base.html" %}
+
+{%- block main -%}
+
+ {% for tz in timezones %}
+ - {{ tz.label }} ({{ tz.offset }})
+ {% endfor %}
+{% - endblock %-}
diff --git a/tpl/cal/year-numpad.html b/tpl/cal/year-numpad.html
new file mode 100644
index 0000000..c51d6cf
--- /dev/null
+++ b/tpl/cal/year-numpad.html
@@ -0,0 +1,28 @@
+{% extends "/base.html" %}
+
+{%- block main -%}{%- endblock -%}
diff --git a/tpl/cal/year-numpad.html~ b/tpl/cal/year-numpad.html~
new file mode 100644
index 0000000..15f7d53
--- /dev/null
+++ b/tpl/cal/year-numpad.html~
@@ -0,0 +1,34 @@
+{% extends "base.html" %}
+
+{%- block main -%}
+ ↓
+ {{ input.value }}
+ ↑
+
+
+{%- endblock -%}
diff --git a/tpl/gregorian.html b/tpl/gregorian.html
index 516d33f..9b9a0a1 100644
--- a/tpl/gregorian.html
+++ b/tpl/gregorian.html
@@ -33,7 +33,7 @@
{{ T.meridiem }}
↓
- {{ T.zone }}
+ {{ T.zone }}
↑
Now
--
2.30.2