Skip to content

Commit 967fbdd

Browse files
authored
Merge pull request #2 from jtimberman/f-for-stupid-americans
Add configurable temperature unit support for people from Liberia, Puerto Rico, Guam, Micronesia, United States Virgin Islands, Cayman Islands, American Samoa, Northern Mariana Islands, Marshall Islands and Palau. Oh, and the USA. https://worldpopulationreview.com/country-rankings/countries-that-use-fahrenheit
2 parents eb73014 + d6c44e0 commit 967fbdd

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

sparkweather-test.el

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,47 @@
390390
(should (= (length overlaps) 1))
391391
(should (equal (car overlaps) '("All Day" "Lunch" 12 14))))))
392392

393+
(ert-deftest sparkweather-test-temperature-unit-symbol-celsius ()
394+
"Temperature unit symbol returns °C for celsius."
395+
(let ((sparkweather-temperature-unit 'celsius))
396+
(should (equal (sparkweather--temperature-unit-symbol) "°C"))))
397+
398+
(ert-deftest sparkweather-test-temperature-unit-symbol-fahrenheit ()
399+
"Temperature unit symbol returns °F for fahrenheit."
400+
(let ((sparkweather-temperature-unit 'fahrenheit))
401+
(should (equal (sparkweather--temperature-unit-symbol) "°F"))))
402+
403+
(ert-deftest sparkweather-test-temperature-unit-symbol-c ()
404+
"Temperature unit symbol returns °C for \"C\" string."
405+
(let ((sparkweather-temperature-unit "C"))
406+
(should (equal (sparkweather--temperature-unit-symbol) "°C"))))
407+
408+
(ert-deftest sparkweather-test-temperature-unit-symbol-f ()
409+
"Temperature unit symbol returns °F for \"F\" string."
410+
(let ((sparkweather-temperature-unit "F"))
411+
(should (equal (sparkweather--temperature-unit-symbol) "°F"))))
412+
413+
(ert-deftest sparkweather-test-convert-temperature-celsius ()
414+
"Temperature conversion with celsius returns value unchanged."
415+
(let ((sparkweather-temperature-unit 'celsius))
416+
(should (= (sparkweather--convert-temperature 0) 0))
417+
(should (= (sparkweather--convert-temperature 20) 20))
418+
(should (= (sparkweather--convert-temperature -10) -10))))
419+
420+
(ert-deftest sparkweather-test-convert-temperature-fahrenheit ()
421+
"Temperature conversion with fahrenheit converts correctly."
422+
(let ((sparkweather-temperature-unit 'fahrenheit))
423+
(should (= (sparkweather--convert-temperature 0) 32))
424+
(should (= (sparkweather--convert-temperature 100) 212))
425+
(should (= (sparkweather--convert-temperature -40) -40))
426+
(should (= (sparkweather--convert-temperature 20) 68))))
427+
428+
(ert-deftest sparkweather-test-convert-temperature-f-string ()
429+
"Temperature conversion with \"F\" string converts correctly."
430+
(let ((sparkweather-temperature-unit "F"))
431+
(should (= (sparkweather--convert-temperature 0) 32))
432+
(should (= (sparkweather--convert-temperature 20) 68))))
433+
393434
(provide 'sparkweather-test)
394435

395436
;;; sparkweather-test.el ends here

sparkweather.el

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ Has no effect if `sparkweather-add-footer' is nil."
111111
:type 'boolean
112112
:group 'sparkweather)
113113

114+
(defcustom sparkweather-temperature-unit 'celsius
115+
"Temperature unit for display and API requests.
116+
Valid values are \\='celsius, \\='fahrenheit, \"C\", or \"F\"."
117+
:type '(choice (const :tag "Celsius" celsius)
118+
(const :tag "Fahrenheit" fahrenheit)
119+
(const :tag "C" "C")
120+
(const :tag "F" "F"))
121+
:group 'sparkweather)
122+
114123
(defcustom sparkweather-lunch-start-hour 12
115124
"Start hour for lunch time window (24-hour format, 0-23).
116125
@@ -151,6 +160,20 @@ DEPRECATED: Use `sparkweather-time-windows' instead."
151160
'sparkweather-time-windows
152161
"0.2.0")
153162

163+
(defun sparkweather--temperature-unit-symbol ()
164+
"Return the display symbol (°C or °F) for the configured temperature unit."
165+
(pcase sparkweather-temperature-unit
166+
((or 'fahrenheit "F") "°F")
167+
(_ "°C")))
168+
169+
(defun sparkweather--convert-temperature (celsius)
170+
"Convert CELSIUS temperature to the configured unit.
171+
Returns temperature in Fahrenheit if unit is set to fahrenheit or \"F\",
172+
otherwise returns CELSIUS unchanged."
173+
(pcase sparkweather-temperature-unit
174+
((or 'fahrenheit "F") (+ (* celsius 1.8) 32))
175+
(_ celsius)))
176+
154177
(defun sparkweather--detect-invalid-windows (windows)
155178
"Detect windows with invalid hour ranges in WINDOWS list.
156179
Returns list of invalid windows as (NAME START END REASON), or nil if all valid.
@@ -548,15 +571,19 @@ Returns (temps precip-probs temp-min temp-max precip-max rainy-codes)."
548571
Returns list of entries for `tabulated-list-mode'."
549572
(pcase-let* ((`(,temps ,precip-probs ,temp-min ,temp-max ,precip-max ,rainy-codes)
550573
(sparkweather--calculate-ranges data))
574+
(converted-temps (mapcar #'sparkweather--convert-temperature temps))
551575
(`(,window-data ,highlights)
552576
(sparkweather--prepare-windows data windows))
553-
(temp-sparkline (sparkweather--sparkline temps highlights current-hour))
577+
(temp-sparkline (sparkweather--sparkline converted-temps highlights current-hour))
554578
(precip-sparkline (sparkweather--sparkline precip-probs highlights current-hour))
555579
(worst-weather-code (and rainy-codes (apply #'max rainy-codes)))
556580
(worst-weather-info (when worst-weather-code
557581
(sparkweather--wmo-code-info worst-weather-code))))
558582
(append
559-
(list (list 'temp (vector (format "%d%d°C" (round temp-min) (round temp-max))
583+
(list (list 'temp (vector (format "%d%d%s"
584+
(round (sparkweather--convert-temperature temp-min))
585+
(round (sparkweather--convert-temperature temp-max))
586+
(sparkweather--temperature-unit-symbol))
560587
temp-sparkline)))
561588
(when worst-weather-info
562589
(list (list 'precip (vector (format "%d%% %s"

0 commit comments

Comments
 (0)