Merge pull request #8 from andrewschaaf/master

Tests, fixes, features
This commit is contained in:
Sami Samhuri 2011-06-07 18:26:58 -07:00
commit 09b7e60581
2 changed files with 57 additions and 2 deletions

View file

@ -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;
@ -73,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
@ -100,7 +115,8 @@
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() - 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';
case 'u':

39
test/test.coffee Normal file
View file

@ -0,0 +1,39 @@
assert = require 'assert'
lib = require './../lib'
# Tue, 07 Jun 2011 18:51:45 GMT
t = new Date 1307472705867
TESTS = [
["%L", "867"]
["%Y", "2011"]
["%m", "06"]
["%b", "Jun"]
["%B", "June"]
["%d", null, "07"]
["%H", null, "18"]
["%M", "51"]
["%S", "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)}"
console.log "OK"