Skip to content

Commit

Permalink
Catch invalid time values, set default value for decimal places
Browse files Browse the repository at this point in the history
differently only when the min. time unit are seconds and remove trailing
spaces
  • Loading branch information
SebastianZ committed Jun 25, 2013
1 parent dfefaf3 commit d719652
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions extension/content/firebug/lib/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,11 @@ Str.formatSize = function(bytes)
*/
Str.formatTime = function(time, minTimeUnit, maxTimeUnit, decimalPlaces)
{
var time = parseInt(time);

if (isNaN(time))
return "";

var timeUnits = [
{
unit: "ms",
Expand Down Expand Up @@ -745,8 +750,6 @@ Str.formatTime = function(time, minTimeUnit, maxTimeUnit, decimalPlaces)
// Get the index of the min. and max. time unit and the decimal places
var minTimeUnitIndex = (Math.abs(time) < 1000) ? 0 : 1;
var maxTimeUnitIndex = timeUnits.length - 1;
if (!decimalPlaces)
decimalPlaces = (Math.abs(time) >= 60000) ? 0 : 2;

for (var i=0, len=timeUnits.length; i<len; ++i)
{
Expand All @@ -756,6 +759,9 @@ Str.formatTime = function(time, minTimeUnit, maxTimeUnit, decimalPlaces)
maxTimeUnitIndex = i;
}

if (!decimalPlaces)
decimalPlaces = (Math.abs(time) >= 60000 && minTimeUnitIndex == 1 ? 0 : 2);

// Calculate the maximal time interval
var timeUnitInterval = 1;
for (var i=0; i<maxTimeUnitIndex; ++i)
Expand All @@ -780,17 +786,13 @@ Str.formatTime = function(time, minTimeUnit, maxTimeUnit, decimalPlaces)
}

if (value != 0 || (i == minTimeUnitIndex && formattedString == ""))
{
formattedString += value.toLocaleString() + timeUnits[i].unit;
if (i != minTimeUnitIndex)
formattedString += " ";
}
formattedString += value.toLocaleString() + timeUnits[i].unit + " ";
time %= timeUnitInterval;
if (i != 0)
timeUnitInterval /= timeUnits[i - 1].interval;
}

return formattedString;
return formattedString.trim();
}
};

Expand Down

0 comments on commit d719652

Please sign in to comment.