Quellcode durchsuchen

weather-darksky: Always show today's forecast

We could skip it as forecast time can be 00:00.
Will Storey vor 8 Jahren
Ursprung
Commit
9fc434691d
1 geänderte Dateien mit 24 neuen und 2 gelöschten Zeilen
  1. 24 2
      weather-darksky.tcl

+ 24 - 2
weather-darksky.tcl

@@ -221,7 +221,7 @@ proc ::wds::output_current {chan geonames darksky} {
 
 	append output " \002Local time\002: "
 	append output [clock format [dict get $darksky time] \
-		-format "%H:%M" \
+		-format   "%H:%M" \
 		-timezone :[dict get $darksky timezone] \
 	]
 
@@ -280,9 +280,10 @@ proc ::wds::output_forecast {chan geonames darksky} {
 		if {$count == 5} {
 			break
 		}
-		if {[dict get $forecast time] < [clock seconds]} {
+		if {![wds::should_show_forecast [dict get $darksky timezone] $forecast]} {
 			continue
 		}
+
 		if {$output != ""} {
 			append output " "
 		}
@@ -311,6 +312,27 @@ proc ::wds::output_forecast {chan geonames darksky} {
 	$::wds::output_cmd "PRIVMSG $chan :$output"
 }
 
+# We can get old forecast info. For example if it's Sunday we could get
+# Saturday's. We don't want to show that.
+#
+# We do want to show today's though. The time given in the forecast is at
+# 00:00:00 of the day.
+proc ::wds::should_show_forecast {timezone forecast} {
+	set now [clock seconds]
+
+	set today        [::wds::format_ymd $timezone $now]
+	set forecast_day [::wds::format_ymd $timezone [dict get $forecast time]]
+
+	return $today == $forecast_day || [dict get $forecast time] >= $now
+}
+
+proc ::wds::format_ymd {timezone time} {
+	return [clock format $time \
+		-format   "%Y-%m-%d" \
+		-timezone :$timezone \
+	]
+}
+
 proc ::wds::celsius_to_fahrenheit {celsius} {
 	set fahrenheit [expr $celsius*9.0/5.0+32.0]
 	return [::wds::format_decimal $fahrenheit]