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 41d702b0..c0334853 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.
+
Add suffix ".length" to class TimespanBinder. This allows expressions such as
$month.length. PR #809. Thanks to user Karen!
diff --git a/docs/customizing.htm b/docs/customizing.htm
index 5cd63e79..fdefd0ad 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).
+
+ 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:
+
+ The advantage of this approach is that the delta time can be treated like any other scalar. Here
+ are some examples:
+
+
+
+
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
- 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"
-