From 0130c429761433eb96d8f2de9aac588b6126e9f0 Mon Sep 17 00:00:00 2001
From: Tom Keffer
+ A "delta time" is the difference between two times, for example the amount of uptime (difference between
+ start and current time). Before, delta times were treated as a special case, which limited their
+ flexibility. Now, a delta time is treated like any other scalar. However, this means that if you want to
+ format the time in the "long form", that is, so the results look like 4 hours, 15 minutes, rather than 15300 seconds, then you will
+ have to say so explicitly.
+
+ If you use the Seasons skin, you will have to make these three small changes, where you add
+ the text in green:
+
+ In a similar manner, if you use the Standard skin, you will have to make these two changes.
+
+ The advantage of this approach is that the delta time can be treated like any other scalar. Here
+ are some examples:
+
- The bad news is that fixing issue #808 required
+ Fixing issue #808 required
introducing a separate dictionary for members of group group_deltatime. This
means that if you specified custom formats under [Units]/[[TimeFormats]], then
you will have to move them to the new section [Units]/[[DeltaTimeFormats]].
- Very few users should be affected as being able to set custom delta time formats is an undocumented feature.
+ Very few users should be affected by this change because the ability to set custom delta time formats is an
+ undocumented feature.
The good news is that the contexts they now use have more standard names. The table below summarizes:
@@ -298,7 +442,6 @@ sudo apt-get install weewx
week = "%(day)d%(day_label)s, %(hour)d%(hour_label)s, %(minute)d%(minute_label)s"
month = "%(day)d%(day_label)s, %(hour)d%(hour_label)s, %(minute)d%(minute_label)s"
year = "%(day)d%(day_label)s, %(hour)d%(hour_label)s, %(minute)d%(minute_label)s"
-
$day.foobar.min
-
+
- Test for a bad aggregation type on a \$day tag: \$day.outTemp.foo
$day.outTemp.foo
+
+ Test for sunshineDur: \$day.sunshineDur.sum
$day.sunshineDur.sum
+
+ Test for sunshineDur: \$day.sunshineDur.sum.long_form
+ $day.sunshineDur.sum.long_form
+
+
+ Test for sunshineDur, custom format:
+ $day.sunshineDur.sum.long_form("%(hour)d%(hour_label)s, %(minute)d%(minute_label)s")
+
+
sunshineDur in hours:
+ $day.sunshineDur.sum.hour.format("%.2f")
+
@@ -722,6 +734,14 @@ FALSE#slurp
Moon:
$almanac.moon_phase ($almanac.moon_fullness%full)
+
+
+ \$almanac.sun.visible:
+ $almanac.sun.visible.long_form
+
($almanac.sun.visible)
+
\$almanac.sun.visible_change:
+ $almanac.sun.visible_change.long_form ($almanac.sun.visible_change)
+
diff --git a/bin/weewx/units.py b/bin/weewx/units.py
index 06886dae..1e165e29 100644
--- a/bin/weewx/units.py
+++ b/bin/weewx/units.py
@@ -755,15 +755,6 @@ class Formatter(object):
else:
val_str = time.strftime(useThisFormat, time.localtime(t))
addLabel = False
- elif val_t[2] == "group_deltatime":
- # Get a delta-time format string. Use a default if the user did not supply one:
- if useThisFormat:
- format_string = useThisFormat
- else:
- format_string = self.deltatime_format_dict.get(context, DEFAULT_DELTATIME_FORMAT)
- # Now format the delta time, using the function delta_time_to_string:
- val_str = self.delta_time_to_string(val_t, format_string)
- addLabel = False
else:
# It's not a time. It's a regular value. Get a suitable format string:
if useThisFormat is None:
@@ -798,9 +789,36 @@ class Formatter(object):
_sector = int(_degree / _sector_size)
return self.ordinate_names[_sector]
+ def long_form(self, val_t, context, format_string=None):
+ """Format a delta time using the long-form.
+
+ Args:
+ val_t (ValueTuple): a ValueTuple holding the delta time.
+ context (str): The time context. Something like 'day', 'current', etc.
+ format_string (str|None): An optional custom format string. Otherwise, an appropriate
+ string will be looked up in deltatime_format_dict.
+ Returns
+ str: The results formatted in a "long-form" time. This is something like
+ "2 hours, 14 minutes, 21 seconds".
+ """
+ # Get a delta-time format string. Use a default if the user did not supply one:
+ if not format_string:
+ format_string = self.deltatime_format_dict.get(context, DEFAULT_DELTATIME_FORMAT)
+ # Now format the delta time, using the function delta_time_to_string:
+ val_str = self.delta_time_to_string(val_t, format_string)
+ return val_str
+
def delta_time_to_string(self, val_t, label_format):
- """Convert elapsed time to a string """
- secs = convert(val_t, 'second').value
+ """Format elapsed time as a string
+
+ Args:
+ val_t (ValueTuple): A ValueTuple containing the elapsed time.
+ label_format (str): The formatting string.
+
+ Returns:
+ str: The formatted time as a string.
+ """
+ secs = convert(val_t, 'second')[0]
etime_dict = {}
secs = abs(secs)
for (label, interval) in (('day', 86400), ('hour', 3600), ('minute', 60), ('second', 1)):
@@ -1038,6 +1056,12 @@ class ValueHelper(object):
# appropriate ordinate:
return self.formatter.to_ordinal_compass(self.value_t)
+ def long_form(self, format_string=None):
+ """Format a delta time"""
+ return self.formatter.long_form(self.value_t,
+ context=self.context,
+ format_string=format_string)
+
def json(self, **kwargs):
return json.dumps(self.raw, cls=ComplexEncoder, **kwargs)
diff --git a/docs/changes.txt b/docs/changes.txt
index 85b35dce..1f07df9d 100644
--- a/docs/changes.txt
+++ b/docs/changes.txt
@@ -15,6 +15,9 @@ Fix bug that prevents group_deltatime from being used by timespans. Users
who used custom formatting for delta times will be affected. See the Upgrade
Guide. Fixes issue #808.
+Allow more flexible formatting for delta times. This can break old skins.
+See Upgrade Guide. PR #807.
+
4.9.1 10/25/2022
Fix problem with `wind` for older versions of sqlite.
diff --git a/docs/customizing.htm b/docs/customizing.htm
index 6a225f10..96960ef4 100644
--- a/docs/customizing.htm
+++ b/docs/customizing.htm
@@ -2331,6 +2331,16 @@ or in foobar units: $day.barometer.min.foobar
configuration file skin.conf.
+
+
.long_form
+
+ Format delta times in the "long form". A "delta time" is the difference between two times. An
+ example is the amount of uptime (difference between start and current time). By default, this will
+ be formatted as the number of elapsed seconds (e.g., 45000 seconds). The "long form" breaks the time down into constituent time
+ elements (e.g., 12 hours, 30 minutes, 0 seconds).
+
+
diff --git a/skins/Seasons/about.inc b/skins/Seasons/about.inc
index 41e59006..9629aff7 100644
--- a/skins/Seasons/about.inc
+++ b/skins/Seasons/about.inc
@@ -30,11 +30,11 @@
.json
diff --git a/docs/upgrading.htm b/docs/upgrading.htm
index e67f1311..5460689f 100644
--- a/docs/upgrading.htm
+++ b/docs/upgrading.htm
@@ -251,13 +251,157 @@ sudo apt-get install weewx
Instructions for specific versions
Upgrading to V4.10
+
+ Breaking changes for skins that use delta times
+
+
+
+
+
+ V4.9 and earlier Seasons skin
+ 4.10 Seasons skin
+
+
+ Seasons/about.inc starting at line 31
+
+
+
+
+
+ <tr>
+ <td class="label">$gettext("Server uptime")</td>
+ <td class="data">$station.os_uptime</td>
+ </tr>
+ <tr>
+ <td class="label">$gettext("WeeWX uptime")</td>
+ <td class="data">$station.uptime</td>
+ </tr>
+
+
+
+
+ <tr>
+ <td class="label">$gettext("Server uptime")</td>
+ <td class="data">$station.os_uptime.long_form</td>
+ </tr>
+ <tr>
+ <td class="label">$gettext("WeeWX uptime")</td>
+ <td class="data">$station.uptime.long_form</td>
+ </tr>
+
+
+
+ Seasons/celestial.inc starting at line 94
+
+
+
+
+
+
+ <tr>
+ <td class="label">$gettext("Total daylight")</td>
+ <td class="data">$almanac.sun.visible<br/>$sun_visible_change.long_form $change_str</td>
+ </tr>
+
+
+
+
+ <tr>
+ <td class="label">$gettext("Total daylight")</td>
+ <td class="data">$almanac.sun.visible<br/>$sun_visible_change.long_form $change_str</td>
+ </tr>
+
+
+
+
+
+
+ V4.9 and earlier Standard skin
+ 4.10 Standard skin
+
+
+ Standard/index.html.tmpl starting at line 309
+
+
+
+
+
+ <p>$gettext("WeeWX uptime"): $station.uptime<br/>
+ $gettext("Server uptime"): $station.os_uptime<br/>
+
+
+
+
+ <p>$gettext("WeeWX uptime"): $station.uptime.long_form<br/>
+ $gettext("Server uptime"): $station.os_uptime.long_form<br/>
+
+
+
+
+
+ Tag
+ Results
+
+
+
+$almanac.sun.visible
+
+
+45000 seconds
+
+
+
+
+$almanac.sun.visible.hour
+
+
+12.5 hours
+
+
+
+
+$almanac.sun.visible.hour.format("%.2f")
+
+
+12.50 hours
+
+
+
+
+$almanac.sun.visible.long_form
+
+
+12 hours, 30 minutes, 0 seconds
+
+ Changes for custom delta time formats
$gettext("Server uptime")
- $station.os_uptime
+ $station.os_uptime.long_form
$gettext("WeeWX uptime")
- $station.uptime
+ $station.uptime.long_form
$gettext("WeeWX version")
diff --git a/skins/Seasons/celestial.inc b/skins/Seasons/celestial.inc
index 6980644f..25c409fa 100644
--- a/skins/Seasons/celestial.inc
+++ b/skins/Seasons/celestial.inc
@@ -93,7 +93,7 @@
#end if
diff --git a/skins/Standard/index.html.tmpl b/skins/Standard/index.html.tmpl
index 2b249ef9..a8251067 100644
--- a/skins/Standard/index.html.tmpl
+++ b/skins/Standard/index.html.tmpl
@@ -1,4 +1,4 @@
-## Copyright 2009-2021 Tom Keffer
+## Copyright 2009-2022 Tom Keffer
## Distributed under terms of GPLv3. See LICENSE.txt for your rights.
#errorCatcher Echo
#encoding UTF-8
@@ -306,8 +306,8 @@
$gettext("Total daylight")
- $almanac.sun.visible
+
$sun_visible_change $change_str$almanac.sun.visible
$sun_visible_change.long_form $change_str
$gettext("Smartphone formatted")
-$gettext("WeeWX uptime"): $station.uptime
- $gettext("Server uptime"): $station.os_uptime
+
$gettext("WeeWX uptime"): $station.uptime.long_form
+ $gettext("Server uptime"): $station.os_uptime.long_form
weewx v$station.version