biome-grid: add formatting for iso8601

This commit is contained in:
Pavel Korytov 2023-08-17 18:56:43 +03:00
parent 541bd98427
commit 080be34883

View file

@ -280,6 +280,7 @@ FORMAT is an expression as defined by `biome-grid-format'."
(240 . "#b13c74"))) (240 . "#b13c74")))
(nil "is_day" is-day) (nil "is_day" is-day)
("wmo code" nil wmo-code) ("wmo code" nil wmo-code)
("iso8601" nil date)
("°" nil direction))) ("°" nil direction)))
"Format units in the grid. "Format units in the grid.
@ -303,6 +304,7 @@ The format definition is one of the following:
:value-type (string :tag "HEX color"))) :value-type (string :tag "HEX color")))
(const :tag "Is day" is-day) (const :tag "Is day" is-day)
(const :tag "WMO code" wmo-code) (const :tag "WMO code" wmo-code)
(const :tag "Date" date)
(const :tag "Direction" direction))))) (const :tag "Direction" direction)))))
(defcustom biome-grid-wmo-codes (defcustom biome-grid-wmo-codes
@ -373,6 +375,20 @@ The defaults values are taken from open-meteo docs."
:type 'boolean :type 'boolean
:group 'biome) :group 'biome)
(defcustom biome-grid-date-format "%Y-%m-%d"
"Format for date strings.
See `format-time-string' for possible format values."
:type 'string
:group 'biome)
(defcustom biome-grid-datetime-format "%Y-%m-%d %H:%M"
"Format for datetime strings.
See `format-time-string' for possible format values."
:type 'string
:group 'biome)
(defvar-local biome-grid--columns-display nil (defvar-local biome-grid--columns-display nil
"Which columns to display in the grid. "Which columns to display in the grid.
@ -451,6 +467,17 @@ VALUE is 0 or 1."
(symbol-name col-key)))) (symbol-name col-key))))
return format-def)) return format-def))
(defun biome-grid--format-date (value)
"Format VALUE as iso8601 date or datetime."
(let* ((decoded-time (iso8601-parse value))
(has-time (fixnump (car decoded-time))))
(if has-time
(format-time-string biome-grid-datetime-format (encode-time decoded-time))
(setf (nth 0 decoded-time) 0
(nth 1 decoded-time) 0
(nth 2 decoded-time) 0)
(format-time-string biome-grid-date-format (encode-time decoded-time)))))
(defun biome-grid--prepare-entries (entries col-key unit) (defun biome-grid--prepare-entries (entries col-key unit)
"Prepare entries for display. "Prepare entries for display.
@ -470,6 +497,8 @@ the API. UNIT is the unit of the column."
(biome-grid--format-direction entry)) (biome-grid--format-direction entry))
((eq format-def 'is-day) ((eq format-def 'is-day)
(biome-grid--format-is-day entry)) (biome-grid--format-is-day entry))
((eq format-def 'date)
(biome-grid--format-date entry))
(t entry))) (t entry)))
entries))) entries)))