mirror of
https://github.com/samsonjs/strftime.git
synced 2026-04-27 14:57:37 +00:00
commit
09b7e60581
2 changed files with 57 additions and 2 deletions
20
lib/index.js
20
lib/index.js
|
|
@ -42,6 +42,19 @@
|
||||||
return n < 10 ? (padding + n) : n;
|
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) {
|
function hours12(d) {
|
||||||
var hour = d.getHours();
|
var hour = d.getHours();
|
||||||
if (hour == 0) hour = 12;
|
if (hour == 0) hour = 12;
|
||||||
|
|
@ -73,8 +86,10 @@
|
||||||
d = d || new Date();
|
d = d || new Date();
|
||||||
locale = locale || DefaultLocale;
|
locale = locale || DefaultLocale;
|
||||||
locale.formats = locale.formats || {}
|
locale.formats = locale.formats || {}
|
||||||
|
var msDelta = 0;
|
||||||
if (_useUTC) {
|
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
|
// 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 || '%H:%M', d, locale);
|
||||||
case 'r': return strftime(locale.formats.r || '%I:%M:%S %p', d, locale);
|
case 'r': return strftime(locale.formats.r || '%I:%M:%S %p', d, locale);
|
||||||
case 'S': return pad(d.getSeconds());
|
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 strftime(locale.formats.T || '%H:%M:%S', d, locale);
|
||||||
case 't': return '\t';
|
case 't': return '\t';
|
||||||
case 'u':
|
case 'u':
|
||||||
|
|
|
||||||
39
test/test.coffee
Normal file
39
test/test.coffee
Normal 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"
|
||||||
Loading…
Reference in a new issue