From 644bb655773c033113bd65a0c2c32808dd03d11a Mon Sep 17 00:00:00 2001 From: Andrew Schaaf Date: Tue, 7 Jun 2011 15:09:19 -0400 Subject: [PATCH 1/4] add some tests --- test/test.coffee | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 test/test.coffee diff --git a/test/test.coffee b/test/test.coffee new file mode 100644 index 0000000..f29abdb --- /dev/null +++ b/test/test.coffee @@ -0,0 +1,36 @@ + +assert = require 'assert' + +lib = require './../lib' + + +# Tue, 07 Jun 2011 18:51:45 GMT +t = new Date 1307472705867 + + +TESTS = [ + + ["%Y", "2011", "2011"] + + ["%m", "06"] + ["%b", "Jun", "Jun"] + ["%B", "June", "June"] + + ["%d", null, "07"] + + ["%H", null, "18"] + + ["%M", "51", "51"] + + ["%S", "45", "45"] + + ["%s", "1307472705"] +] + +for [format, expectedNonUTC, expectedUTC] in TESTS + expectedUTC or= expectedNonUTC + for [name, expected] in [['strftime', expectedNonUTC], ['strftimeUTC', expectedUTC]] + if expected + got = lib[name] format, t + assert.equal expected, got, "Error for #{name}(#{JSON.stringify(format)}, t): expected #{JSON.stringify(expected)}, got #{JSON.stringify(got)}" + From 2644ab016a3c0e400ea4acdcfd02bdc9a908b917 Mon Sep 17 00:00:00 2001 From: Andrew Schaaf Date: Tue, 7 Jun 2011 15:11:57 -0400 Subject: [PATCH 2/4] %s: seconds, not ms (closes #6) --- lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 614b8f2..1772807 100644 --- a/lib/index.js +++ b/lib/index.js @@ -100,7 +100,7 @@ case 'R': return strftime(locale.formats.R || '%H:%M', d, locale); case 'r': return strftime(locale.formats.r || '%I:%M:%S %p', d, locale); case 'S': return pad(d.getSeconds()); - case 's': return d.getTime(); + case 's': return Math.floor(d.getTime() / 1000); case 'T': return strftime(locale.formats.T || '%H:%M:%S', d, locale); case 't': return '\t'; case 'u': From c5362e748c43c6673be83cec92e8887bf92cb60b Mon Sep 17 00:00:00 2001 From: Andrew Schaaf Date: Tue, 7 Jun 2011 15:18:33 -0400 Subject: [PATCH 3/4] add %L for 3-digit milliseconds --- lib/index.js | 14 ++++++++++++++ test/test.coffee | 12 +++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/index.js b/lib/index.js index 1772807..347d2fa 100644 --- a/lib/index.js +++ b/lib/index.js @@ -42,6 +42,19 @@ return n < 10 ? (padding + n) : n; } + function pad3(n, padding) { + padding = padding || '0'; + if (n < 10) { + return padding + padding + n; + } + else if (n < 100) { + return padding + n; + } + else { + return n; + } + } + function hours12(d) { var hour = d.getHours(); if (hour == 0) hour = 12; @@ -101,6 +114,7 @@ case 'r': return strftime(locale.formats.r || '%I:%M:%S %p', d, locale); case 'S': return pad(d.getSeconds()); case 's': return Math.floor(d.getTime() / 1000); + case 'L': return pad3(Math.floor(d.getTime() % 1000)); case 'T': return strftime(locale.formats.T || '%H:%M:%S', d, locale); case 't': return '\t'; case 'u': diff --git a/test/test.coffee b/test/test.coffee index f29abdb..9ab9f0e 100644 --- a/test/test.coffee +++ b/test/test.coffee @@ -10,19 +10,21 @@ t = new Date 1307472705867 TESTS = [ - ["%Y", "2011", "2011"] + ["%L", "867"] + + ["%Y", "2011"] ["%m", "06"] - ["%b", "Jun", "Jun"] - ["%B", "June", "June"] + ["%b", "Jun"] + ["%B", "June"] ["%d", null, "07"] ["%H", null, "18"] - ["%M", "51", "51"] + ["%M", "51"] - ["%S", "45", "45"] + ["%S", "45"] ["%s", "1307472705"] ] From 314603eefee669c55303f37f9ef170df15d640a4 Mon Sep 17 00:00:00 2001 From: Andrew Schaaf Date: Tue, 7 Jun 2011 15:22:00 -0400 Subject: [PATCH 4/4] fix strftimeUTC("%s") (closes #7) --- lib/index.js | 6 ++++-- test/test.coffee | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index 347d2fa..9c2c172 100644 --- a/lib/index.js +++ b/lib/index.js @@ -86,8 +86,10 @@ d = d || new Date(); locale = locale || DefaultLocale; locale.formats = locale.formats || {} + var msDelta = 0; if (_useUTC) { - d = new Date(d.getTime() + ((d.getTimezoneOffset() || 0) * 60000)); + msDelta = (d.getTimezoneOffset() || 0) * 60000; + d = new Date(d.getTime() + msDelta); } // Most of the specifiers supported by C's strftime @@ -113,7 +115,7 @@ case 'R': return strftime(locale.formats.R || '%H:%M', d, locale); case 'r': return strftime(locale.formats.r || '%I:%M:%S %p', d, locale); case 'S': return pad(d.getSeconds()); - case 's': return Math.floor(d.getTime() / 1000); + case 's': return Math.floor((d.getTime() - msDelta) / 1000); case 'L': return pad3(Math.floor(d.getTime() % 1000)); case 'T': return strftime(locale.formats.T || '%H:%M:%S', d, locale); case 't': return '\t'; diff --git a/test/test.coffee b/test/test.coffee index 9ab9f0e..aaed772 100644 --- a/test/test.coffee +++ b/test/test.coffee @@ -36,3 +36,4 @@ for [format, expectedNonUTC, expectedUTC] in TESTS got = lib[name] format, t assert.equal expected, got, "Error for #{name}(#{JSON.stringify(format)}, t): expected #{JSON.stringify(expected)}, got #{JSON.stringify(got)}" +console.log "OK"