improved coverage for date_format

This commit is contained in:
Gareth Jones 2013-06-17 16:00:42 +10:00
parent efc4e36317
commit 652888944b
2 changed files with 46 additions and 30 deletions

View File

@ -4,6 +4,37 @@ exports.ISO8601_WITH_TZ_OFFSET_FORMAT = "yyyy-MM-ddThh:mm:ssO";
exports.DATETIME_FORMAT = "dd MM yyyy hh:mm:ss.SSS"; exports.DATETIME_FORMAT = "dd MM yyyy hh:mm:ss.SSS";
exports.ABSOLUTETIME_FORMAT = "hh:mm:ss.SSS"; exports.ABSOLUTETIME_FORMAT = "hh:mm:ss.SSS";
function padWithZeros(vNumber, width) {
var numAsString = vNumber + "";
while (numAsString.length < width) {
numAsString = "0" + numAsString;
}
return numAsString;
}
function addZero(vNumber) {
return padWithZeros(vNumber, 2);
}
/**
* Formats the TimeOffest
* Thanks to http://www.svendtofte.com/code/date_format/
* @private
*/
function offset(date) {
// Difference to Greenwich time (GMT) in hours
var os = Math.abs(date.getTimezoneOffset());
var h = String(Math.floor(os/60));
var m = String(os%60);
if (h.length == 1) {
h = "0" + h;
}
if (m.length == 1) {
m = "0" + m;
}
return date.getTimezoneOffset() < 0 ? "+"+h+m : "-"+h+m;
}
exports.asString = function(/*format,*/ date) { exports.asString = function(/*format,*/ date) {
var format = exports.ISO8601_FORMAT; var format = exports.ISO8601_FORMAT;
if (typeof(date) === "string") { if (typeof(date) === "string") {
@ -32,34 +63,4 @@ exports.asString = function(/*format,*/ date) {
.replace(/O/g, vTimeZone); .replace(/O/g, vTimeZone);
return formatted; return formatted;
function padWithZeros(vNumber, width) {
var numAsString = vNumber + "";
while (numAsString.length < width) {
numAsString = "0" + numAsString;
}
return numAsString;
}
function addZero(vNumber) {
return padWithZeros(vNumber, 2);
}
/**
* Formats the TimeOffest
* Thanks to http://www.svendtofte.com/code/date_format/
* @private
*/
function offset(date) {
// Difference to Greenwich time (GMT) in hours
var os = Math.abs(date.getTimezoneOffset());
var h = String(Math.floor(os/60));
var m = String(os%60);
if (h.length == 1) {
h = "0" + h;
}
if (m.length == 1) {
m = "0" + m;
}
return date.getTimezoneOffset() < 0 ? "+"+h+m : "-"+h+m;
}
}; };

View File

@ -19,6 +19,21 @@ vows.describe('date_format').addBatch({
dateFormat.asString(date), dateFormat.asString(date),
'2010-01-11 14:31:30.005' '2010-01-11 14:31:30.005'
); );
},
/* this test will only pass in Australia (GMT+11) - javascript doesn't
provide a way to change or specify a Date's timezone
'should provide a ISO8601 with timezone offset format': function(date) {
assert.equal(
dateFormat.asString(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, date),
"2010-01-11T14:31:30+1100"
);
},
*/
'should provide a just-the-time format': function(date) {
assert.equal(
dateFormat.asString(dateFormat.ABSOLUTETIME_FORMAT, date),
'14:31:30.005'
);
} }
} }
}).export(module); }).export(module);