Merge branch 'long_form' into development

# Conflicts:
#	docs/changes.txt
This commit is contained in:
Tom Keffer
2022-11-08 05:49:45 -08:00
8 changed files with 224 additions and 24 deletions

View File

@@ -595,15 +595,27 @@ FALSE#slurp
<td>$day.foobar.min</td>
</tr>
</tr>
<tr>
<tr>
<td>Test for a bad aggregation type on a \$day tag: \$day.outTemp.foo</td>
<td>$day.outTemp.foo</td>
</tr>
</tr>
<tr>
<tr>
<td>Test for sunshineDur: \$day.sunshineDur.sum</td>
<td>$day.sunshineDur.sum</td>
</tr>
<tr>
<td>Test for sunshineDur: \$day.sunshineDur.sum.long_form</td>
<td>$day.sunshineDur.sum.long_form</td>
</tr>
<tr>
<td>Test for sunshineDur, custom format:</td>
<td>$day.sunshineDur.sum.long_form("%(hour)d%(hour_label)s, %(minute)d%(minute_label)s")</td>
</tr>
<tr>
<td>sunshineDur in hours:</td>
<td>$day.sunshineDur.sum.hour.format("%.2f")</td>
</tr>
</table>
<hr/>
@@ -746,6 +758,14 @@ FALSE#slurp
<td align="right">Moon:</td>
<td align="left">$almanac.moon_phase ($almanac.moon_fullness%full)</td>
</tr>
<tr>
<td align="right">\$almanac.sun.visible:</td>
<td align="left">$almanac.sun.visible.long_form <br/>($almanac.sun.visible)</td>
</tr>
<tr>
<td align="right">\$almanac.sun.visible_change:</td>
<td align="left">$almanac.sun.visible_change.long_form ($almanac.sun.visible_change)</td>
</tr>
</table>
<hr/>

View File

@@ -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)

View File

@@ -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.
Add suffix ".length" to class TimespanBinder. This allows expressions such as
$month.length. PR #809. Thanks to user Karen!

View File

@@ -2331,6 +2331,16 @@ or in foobar units: $day.barometer.min.foobar
configuration file <span class="code">skin.conf</span>.
</td>
</tr>
<tr>
<td class="code text_highlight">.long_form</td>
<td>
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 (<em>e.g.,</em> <span
class="code">45000 seconds</span>). The "long form" breaks the time down into constituent time
elements (<em>e.g.,</em> <span class="code">12 hours, 30 minutes, 0 seconds</span>).
</td>
</tr>
<tr>
<td class="code text_highlight">.json</td>
<td>

View File

@@ -251,13 +251,157 @@ sudo apt-get install weewx</pre>
<h1><a id="Instructions_for_specific_versions">Instructions for specific versions</a></h1>
<h2>Upgrading to V4.10</h2>
<h3>Breaking changes for skins that use delta times</h3>
<p>
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 <span
class="code">4 hours, 15 minutes</span>, rather than <span class="code">15300 seconds</span>, then you will
have to say so explicitly.
</p>
<p>
If you use the <em>Seasons</em> skin, you will have to make these three small changes, where you add
the text in <span class="added">green</span>:
</p>
<table>
<tr class="first_row">
<td style='width:50%'>V4.9 and earlier Seasons skin</td>
<td>4.10 Seasons skin</td>
</tr>
<tr>
<td colspan="2" style="text-align: center"><span class="code">Seasons/about.inc</span> starting at line 31</td>
</tr>
<tr>
<td>
<pre class="tty">
&lt;tr&gt;
&lt;td class="label"&gt;$gettext("Server uptime")&lt;/td&gt;
&lt;td class="data"&gt;$station.os_uptime&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class="label"&gt;$gettext("WeeWX uptime")&lt;/td&gt;
&lt;td class="data"&gt;$station.uptime&lt;/td&gt;
&lt;/tr&gt;
</pre>
</td>
<td>
<pre class="tty">
&lt;tr&gt;
&lt;td class="label"&gt;$gettext("Server uptime")&lt;/td&gt;
&lt;td class="data"&gt;$station.os_uptime<span class="added">.long_form</span>&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td class="label"&gt;$gettext("WeeWX uptime")&lt;/td&gt;
&lt;td class="data"&gt;$station.uptime<span class="added">.long_form</span>&lt;/td&gt;
&lt;/tr&gt;
</pre>
</td>
</tr>
<tr>
<td colspan="2" style="text-align: center"><span class="code">Seasons/celestial.inc</span> starting at line 94</td>
</tr>
<tr>
<td>
<pre class="tty">
&lt;tr&gt;
&lt;td class="label"&gt;$gettext("Total daylight")&lt;/td&gt;
&lt;td class="data"&gt;$almanac.sun.visible&lt;br/&gt;$sun_visible_change.long_form $change_str&lt;/td&gt;
&lt;/tr&gt;
</pre>
</td>
<td>
<pre class="tty">
&lt;tr&gt;
&lt;td class="label"&gt;$gettext("Total daylight")&lt;/td&gt;
&lt;td class="data"&gt;$almanac.sun.visible&lt;br/&gt;$sun_visible_change<span class="added">.long_form</span> $change_str&lt;/td&gt;
&lt;/tr&gt;
</pre>
</td>
</tr>
</table>
<p>
In a similar manner, if you use the <em>Standard</em> skin, you will have to make these two changes.
</p>
<table>
<tr class="first_row">
<td style='width:50%'>V4.9 and earlier Standard skin</td>
<td>4.10 Standard skin</td>
</tr>
<tr>
<td colspan="2" style="text-align: center"><span class="code">Standard/index.html.tmpl</span> starting at line 309</td>
</tr>
<tr>
<td>
<pre class="tty">
&lt;p&gt;$gettext("WeeWX uptime"): $station.uptime&lt;br/&gt;
$gettext("Server uptime"): $station.os_uptime&lt;br/&gt;
</pre>
</td>
<td>
<pre class="tty">
&lt;p&gt;$gettext("WeeWX uptime"): $station.uptime<span class="added">.long_form</span>&lt;br/&gt;
$gettext("Server uptime"): $station.os_uptime<span class="added">.long_form</span>&lt;br/&gt;
</pre>
</td>
</tr>
</table>
<p>
The advantage of this approach is that the delta time can be treated like any other scalar. Here
are some examples:
</p>
<table>
<tr class="first_row">
<td style='width:50%'>Tag</td>
<td>Results</td>
</tr>
<tr>
<td class="tty">
$almanac.sun.visible
</td>
<td class="tty">
45000 seconds
</td>
</tr>
<tr>
<td class="tty">
$almanac.sun.visible.hour
</td>
<td class="tty">
12.5 hours
</td>
</tr>
<tr>
<td class="tty">
$almanac.sun.visible.hour.format("%.2f")
</td>
<td class="tty">
12.50 hours
</td>
</tr>
<tr>
<td class="tty">
$almanac.sun.visible.long_form
</td>
<td class="tty">
12 hours, 30 minutes, 0 seconds
</td>
</tr>
</table>
<h3>Changes for custom delta time formats</h3>
<p>
The bad news is that fixing issue <a href="https://github.com/weewx/weewx/issues/808">#808</a> required
Fixing issue <a href="https://github.com/weewx/weewx/issues/808">#808</a> required
introducing a separate dictionary for members of group <span class="code">group_deltatime</span>. This
means that if you specified custom formats under <span class="code">[Units]/[[TimeFormats]]</span>, then
you will have to move them to the new section <span class="code">[Units]/[[DeltaTimeFormats]]</span>.
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.
</p>
<p>
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</pre>
<span class="added">week = "%(day)d%(day_label)s, %(hour)d%(hour_label)s, %(minute)d%(minute_label)s"</span>
<span class="added">month = "%(day)d%(day_label)s, %(hour)d%(hour_label)s, %(minute)d%(minute_label)s"</span>
<span class="added">year = "%(day)d%(day_label)s, %(hour)d%(hour_label)s, %(minute)d%(minute_label)s"</span>
</pre>
</td>
</tr>

View File

@@ -30,11 +30,11 @@
</tr>
<tr>
<td class="label">$gettext("Server uptime")</td>
<td class="data">$station.os_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</td>
<td class="data">$station.uptime.long_form</td>
</tr>
<tr>
<td class="label">$gettext("WeeWX version")</td>

View File

@@ -93,7 +93,7 @@
#end if
<tr>
<td class="label">$gettext("Total daylight")</td>
<td class="data">$almanac.sun.visible<br/>$sun_visible_change $change_str</td>
<td class="data">$almanac.sun.visible<br/>$sun_visible_change.long_form $change_str</td>
</tr>
</table>
</div>

View File

@@ -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 @@
</p>
<p><a href="RSS/weewx_rss.xml">$gettext("RSS feed")</a></p>
<p><a href="smartphone/index.html">$gettext("Smartphone formatted")</a></p>
<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/>
weewx v$station.version</p>
</div> <!-- End id "about" -->
@@ -494,7 +494,7 @@
#for $monthYear in $SummaryByMonth
<option value="$monthYear">$monthYear</option>
#end for
<option selected>-$gettext("Select month")-</option>
<option selected>-$gettext("Select month")-</option>
</select>
<br/>
$gettext("Yearly summary"):&nbsp;
@@ -502,7 +502,7 @@
#for $yr in $SummaryByYear
<option value="$yr">$yr</option>
#end for
<option selected>-$gettext("Select year")-</option>
<option selected>-$gettext("Select year")-</option>
</select>
</p>
</div>