From 652888944bfedb286b4e9b13936aedfcf5faec5c Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Mon, 17 Jun 2013 16:00:42 +1000 Subject: [PATCH] improved coverage for date_format --- lib/date_format.js | 61 ++++++++++++++++++++-------------------- test/date_format-test.js | 15 ++++++++++ 2 files changed, 46 insertions(+), 30 deletions(-) diff --git a/lib/date_format.js b/lib/date_format.js index a41ec4c..bf36318 100644 --- a/lib/date_format.js +++ b/lib/date_format.js @@ -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.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) { var format = exports.ISO8601_FORMAT; if (typeof(date) === "string") { @@ -32,34 +63,4 @@ exports.asString = function(/*format,*/ date) { .replace(/O/g, vTimeZone); 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; - } }; diff --git a/test/date_format-test.js b/test/date_format-test.js index 5f7f2ff..5026991 100644 --- a/test/date_format-test.js +++ b/test/date_format-test.js @@ -19,6 +19,21 @@ vows.describe('date_format').addBatch({ dateFormat.asString(date), '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);