Skip to content

Commit

Permalink
Merge pull request #96 from garemoko/duration_functions_squashed
Browse files Browse the repository at this point in the history
Add duration functions to Utils
  • Loading branch information
bscSCORM committed Jan 1, 2015
2 parents 0b7228d + c4f8e33 commit 2a204f3
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
89 changes: 89 additions & 0 deletions src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,95 @@ TinCan client library
pad(d.getUTCMilliseconds(), 3) + "Z";
},

/**
@method convertISO8601DurationToMilliseconds
@static
@param {String} ISO8601Duration Duration in ISO8601 format
@return {Int} Duration in milliseconds
Note: does not handle input strings with years, months and days
*/
convertISO8601DurationToMilliseconds: function (ISO8601Duration) {
var isValueNegative = (ISO8601Duration.indexOf("-") >= 0),
indexOfT = ISO8601Duration.indexOf("T"),
indexOfH = ISO8601Duration.indexOf("H"),
indexOfM = ISO8601Duration.indexOf("M"),
indexOfS = ISO8601Duration.indexOf("S"),
hours,
minutes,
seconds,
durationInMilliseconds;

if ((indexOfT === -1) || ((indexOfM !== -1) && (indexOfM < indexOfT)) || (ISO8601Duration.indexOf("D") !== -1) || (ISO8601Duration.indexOf("Y") !== -1)) {
throw new Error("ISO 8601 timestamps including years, months and/or days are not currently supported");
}

if (indexOfH === -1) {
indexOfH = indexOfT;
hours = 0;
}
else {
hours = parseInt(ISO8601Duration.slice(indexOfT + 1, indexOfH), 10);
}

if (indexOfM === -1) {
indexOfM = indexOfT;
minutes = 0;
}
else {
minutes = parseInt(ISO8601Duration.slice(indexOfH + 1, indexOfM), 10);
}

seconds = parseFloat(ISO8601Duration.slice(indexOfM + 1, indexOfS));

durationInMilliseconds = parseInt((((((hours * 60) + minutes) * 60) + seconds) * 1000), 10);
if (isNaN(durationInMilliseconds)){
durationInMilliseconds = 0;
}
if (isValueNegative) {
durationInMilliseconds = durationInMilliseconds * -1;
}

return durationInMilliseconds;
},

/**
@method convertMillisecondsToISO8601Duration
@static
@param {Int} inputMilliseconds Duration in milliseconds
@return {String} Duration in ISO8601 format
*/
convertMillisecondsToISO8601Duration: function (inputMilliseconds) {
var hours,
minutes,
seconds,
i_inputMilliseconds = parseInt(inputMilliseconds, 10),
inputIsNegative = "",
rtnStr = "";

if (i_inputMilliseconds < 0) {
inputIsNegative = "-";
i_inputMilliseconds = i_inputMilliseconds * -1;
}

hours = parseInt(((i_inputMilliseconds) / 3600000), 10);
minutes = parseInt((((i_inputMilliseconds) % 3600000) / 60000), 10);
seconds = (((i_inputMilliseconds) % 3600000) % 60000) / 1000;

rtnStr = inputIsNegative + "PT";
if (hours > 0) {
rtnStr += hours + "H";
}

if (minutes > 0) {
rtnStr += minutes + "M";
}

rtnStr += seconds + "S";

return rtnStr;
},

/**
@method getSHA1String
@static
Expand Down
12 changes: 12 additions & 0 deletions test/js/unit/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@
ok(result === "2013-03-01T08:04:06.003Z", "return value");
}
);
test(
"convertISO8601DurationToMilliseconds",
function () {
ok(TinCan.Utils.convertISO8601DurationToMilliseconds("PT1H34M42.475S") === 5682475, "return value");
}
);
test(
"convertMillisecondsToISO8601Duration",
function () {
ok(TinCan.Utils.convertMillisecondsToISO8601Duration(5682475) === "PT1H34M42.475S", "return value");
}
);
test(
"getSHA1String",
function () {
Expand Down

0 comments on commit 2a204f3

Please sign in to comment.