Difference between revisions of "Convert Seconds to Human Readable Duration"

From Hawk Wiki
Jump to: navigation, search
(Created page with "<pre class="brush:javascript"> /** * Convert to human readable time duration * @param seconds * @param desiredUnit {string} The desired unit. can be "W", "D", "H", "M"...")
 
 
Line 1: Line 1:
 
<pre class="brush:javascript">
 
<pre class="brush:javascript">
 
/**
 
/**
* Convert to human readable time duration
+
* Convert to human readable time duration
* @param seconds
+
* @param seconds
* @param desiredUnit {string} The desired unit. can be "W", "D", "H", "M"
+
* @param desiredUnit {string} The desired unit. can be "W", "D", "H", "M"
*        unitCount {number} How many units to show
+
*        unitCount {number} How many units to show
* @returns {
+
* @returns {
*          value: {number} seconds input,
+
*          value: {number} seconds input,
*          text: {string} "Time remaining text",
+
*          text: {string} "Time remaining text",
*          secondsLeft: {number} seconds left not showing in the text
+
*          secondsLeft: {number} seconds left not showing in the text
*          }
+
*          }
* (ignore the unit smaller than the 2nd unit. if desiredUnit = "D", unitCount = 2, 1d1m show "1d")
+
* (ignore the unit smaller than the 2nd unit. if desiredUnit = "D", unitCount = 2, 1d1m show "1d")
*/
+
*/
toHumanReadableDuration: function (seconds, desiredUnit, unitCount) {
+
var toHumanReadableDuration = function (seconds, desiredUnit, unitCount) {
 
var that = this;
 
var that = this;
 
/**
 
/**
Line 22: Line 22:
 
var numberWithUnit = function (number, unitName) {
 
var numberWithUnit = function (number, unitName) {
 
return number + unitName;
 
return number + unitName;
};
 
//If the duration is <=0, just return
 
if (seconds <= 0) {
 
return {
 
value: seconds,
 
text: numberWithUnit(0, "S"),
 
secondsLeft: seconds
 
};
 
}
 
 
// round int function.
 
var roundFunc = function (val) {
 
return Math.floor(val);
 
 
};
 
};
  
 
// General function to return data based on unit and secondsInUnit
 
// General function to return data based on unit and secondsInUnit
 
var getTimeByUnit = function (secondsTmp, unit, secondsInUnit) {
 
var getTimeByUnit = function (secondsTmp, unit, secondsInUnit) {
var ret = roundFunc(secondsTmp / secondsInUnit);
+
var ret = Math.floor(secondsTmp / secondsInUnit);
 
return {
 
return {
 
value: ret,
 
value: ret,
 
text: ret > 0 ? numberWithUnit(ret, unit) : "",
 
text: ret > 0 ? numberWithUnit(ret, unit) : "",
 
// How many seconds left after Math.floor. Means how many second is omitted
 
// How many seconds left after Math.floor. Means how many second is omitted
secondsLeft: (secondsTmp - ret * secondsInUnit) % secondsInUnit
+
secondsLeft: secondsTmp - ret * secondsInUnit
 
};
 
};
 
};
 
};
  
var getSeconds = function (secondsTmp) {
+
var ret = {
var ret = roundFunc(secondsTmp);
+
value: seconds,
return {
+
text: "",
value: ret,
+
secondsLeft: 0
text: numberWithUnit(ret, "S"),
+
secondsLeft: 0
+
};
+
 
};
 
};
 +
//If the duration is <=0, just return
 +
if (seconds <= 0) {
 +
return ret;
 +
}
 +
// Call corresponding function based on the unit param
 +
var unitFuncMappings = [{
 +
"unit": "W",
 +
"secondsInUnit": 604800 //seconds in a week
 +
}, {
 +
"unit": "D",
 +
"secondsInUnit": 86400
 +
}, {
 +
"unit": "H",
 +
"secondsInUnit": 3600
 +
}, {
 +
"unit": "M",
 +
"secondsInUnit": 60
 +
}, {
 +
"unit": "S",
 +
"secondsInUnit": 1
 +
}];
  
var getMinutes = function (secondsTmp) {
+
// Find the index of desired unit
var SECONDS_IN_ONE_MINUTE = 60;
+
var unitIndex = 0,
return getTimeByUnit(secondsTmp, "M", SECONDS_IN_ONE_MINUTE);
+
i;
};
+
for (i = 0; i < unitFuncMappings.length; i++) {
 
+
if (unitFuncMappings[i].unit == unit) {
var getHours = function (secondsTmp) {
+
unitIndex = i;
var SECONDS_IN_ONE_HOUR = 3600;
+
break;
return getTimeByUnit(secondsTmp, "H", SECONDS_IN_ONE_HOUR);
+
};
+
 
+
var getDays = function (secondsTmp) {
+
var SECONDS_IN_ONE_DAY = 86400;
+
return getTimeByUnit(secondsTmp, "D", SECONDS_IN_ONE_DAY);
+
};
+
 
+
var getWeeks = function (secondsTmp) {
+
var SECONDS_IN_ONE_WEEK = 604800;
+
return getTimeByUnit(secondsTmp, "W", SECONDS_IN_ONE_WEEK);
+
};
+
 
+
/**
+
*
+
* @param secondsTmp
+
* @param unit The desired time unit, week, day, etc
+
* @param unitCount The count of units. e.g. unitCount=1: "2d", unitCount=2: "1d2h"
+
* @returns {{val: number, text: string, secondsLeft: number}}
+
*/
+
var getTimeWithFormat = function (secondsTmp, unit, unitCount) {
+
var ret = {
+
value: secondsTmp,
+
text: "",
+
secondsLeft: 0
+
};
+
// Call corresponding function based on the unit param
+
var unitFuncMappings = [{
+
"unit": "W",
+
"func": getWeeks
+
}, {
+
"unit": "D",
+
"func": getDays
+
}, {
+
"unit": "H",
+
"func": getHours
+
}, {
+
"unit": "M",
+
"func": getMinutes
+
}, {
+
"unit": "S",
+
"func": getSeconds
+
}];
+
 
+
// Find the index of desired unit
+
var unitIndex = 0,
+
i;
+
for (i = 0; i < unitFuncMappings.length; i++) {
+
if (unitFuncMappings[i].unit == unit) {
+
unitIndex = i;
+
break;
+
}
+
 
}
 
}
 +
}
  
// Get the time format for desired unitCount
+
// Get the time format for desired unitCount
for (i = unitIndex; i < unitFuncMappings.length; i++) {
+
for (i = unitIndex; i < unitFuncMappings.length; i++) {
if (unitFuncMappings[i].func(secondsTmp).value > 0) {
+
if (getTimeByUnit(seconds, unitFuncMappings[i].unit, unitFuncMappings[i].secondsInUnit).value > 0) {
for (var j = 0; j < unitCount; j++) {
+
for (var j = 0; j < unitCount; j++) {
if (unitFuncMappings[i + j]) {
+
if (unitFuncMappings[i + j]) {
var retTmp = unitFuncMappings[i + j].func(secondsTmp);
+
var retTmp = getTimeByUnit(seconds, unitFuncMappings[i].unit, unitFuncMappings[i].secondsInUnit);
if (j > 0) {
+
if (j > 0) {
ret.text += " "; // add a space between time units
+
ret.text += " "; // add a space between time units
}
+
ret.text += retTmp.text;
+
ret.secondsLeft = retTmp.secondsLeft;
+
secondsTmp = retTmp.secondsLeft; //refresh secondsTmp for next calculation
+
 
}
 
}
 +
ret.text += retTmp.text;
 +
ret.secondsLeft = retTmp.secondsLeft;
 +
seconds = retTmp.secondsLeft; //refresh seconds for next calculation
 
}
 
}
break;
 
 
}
 
}
 +
break;
 
}
 
}
return ret;
+
}
};
+
return ret;
 +
}
 
</pre>
 
</pre>

Latest revision as of 01:31, 28 April 2015

/**
 * Convert to human readable time duration
 * @param seconds
 * @param desiredUnit {string} The desired unit. can be "W", "D", "H", "M"
 *        unitCount {number} How many units to show
 * @returns {
 *           value: {number} seconds input,
 *           text: {string} "Time remaining text",
 *           secondsLeft: {number} seconds left not showing in the text
 *          }
 *	(ignore the unit smaller than the 2nd unit. if desiredUnit = "D", unitCount = 2, 1d1m show "1d")
 */
var toHumanReadableDuration = function (seconds, desiredUnit, unitCount) {
		var that = this;
		/**
		 * Return a text with number and unit
		 * @param number the number
		 * @param unitName the unit text
		 * @returns {*}
		 */
		var numberWithUnit = function (number, unitName) {
			return number + unitName;
		};

		// General function to return data based on unit and secondsInUnit
		var getTimeByUnit = function (secondsTmp, unit, secondsInUnit) {
			var ret = Math.floor(secondsTmp / secondsInUnit);
			return {
				value: ret,
				text: ret > 0 ? numberWithUnit(ret, unit) : "",
				// How many seconds left after Math.floor. Means how many second is omitted
				secondsLeft: secondsTmp - ret * secondsInUnit
			};
		};

		var ret = {
			value: seconds,
			text: "",
			secondsLeft: 0
		};
		//If the duration is <=0, just return
		if (seconds <= 0) {
			return ret;
		}
		// Call corresponding function based on the unit param
		var unitFuncMappings = [{
			"unit": "W",
			"secondsInUnit": 604800 //seconds in a week
		}, {
			"unit": "D",
			"secondsInUnit": 86400
		}, {
			"unit": "H",
			"secondsInUnit": 3600
		}, {
			"unit": "M",
			"secondsInUnit": 60
		}, {
			"unit": "S",
			"secondsInUnit": 1
		}];

		// Find the index of desired unit
		var unitIndex = 0,
			i;
		for (i = 0; i < unitFuncMappings.length; i++) {
			if (unitFuncMappings[i].unit == unit) {
				unitIndex = i;
				break;
			}
		}

		// Get the time format for desired unitCount
		for (i = unitIndex; i < unitFuncMappings.length; i++) {
			if (getTimeByUnit(seconds, unitFuncMappings[i].unit, unitFuncMappings[i].secondsInUnit).value > 0) {
				for (var j = 0; j < unitCount; j++) {
					if (unitFuncMappings[i + j]) {
						var retTmp = getTimeByUnit(seconds, unitFuncMappings[i].unit, unitFuncMappings[i].secondsInUnit);
						if (j > 0) {
							ret.text += " "; // add a space between time units
						}
						ret.text += retTmp.text;
						ret.secondsLeft = retTmp.secondsLeft;
						seconds = retTmp.secondsLeft; //refresh seconds for next calculation
					}
				}
				break;
			}
		}
		return ret;
	}