Convert Seconds to Human Readable Duration

From Hawk Wiki
Revision as of 01:23, 28 April 2015 by Hall (Talk | contribs) (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"...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
/**
	 * 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")
	 */
	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;
		};
		//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
		var getTimeByUnit = function (secondsTmp, unit, secondsInUnit) {
			var ret = roundFunc(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) % secondsInUnit
			};
		};

		var getSeconds = function (secondsTmp) {
			var ret = roundFunc(secondsTmp);
			return {
				value: ret,
				text: numberWithUnit(ret, "S"),
				secondsLeft: 0
			};
		};

		var getMinutes = function (secondsTmp) {
			var SECONDS_IN_ONE_MINUTE = 60;
			return getTimeByUnit(secondsTmp, "M", SECONDS_IN_ONE_MINUTE);
		};

		var getHours = function (secondsTmp) {
			var SECONDS_IN_ONE_HOUR = 3600;
			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
			for (i = unitIndex; i < unitFuncMappings.length; i++) {
				if (unitFuncMappings[i].func(secondsTmp).value > 0) {
					for (var j = 0; j < unitCount; j++) {
						if (unitFuncMappings[i + j]) {
							var retTmp = unitFuncMappings[i + j].func(secondsTmp);
							if (j > 0) {
								ret.text += " "; // add a space between time units
							}
							ret.text += retTmp.text;
							ret.secondsLeft = retTmp.secondsLeft;
							secondsTmp = retTmp.secondsLeft; //refresh secondsTmp for next calculation
						}
					}
					break;
				}
			}
			return ret;
		};