Skip to content

Commit

Permalink
test locales, s/getLocalizedStrftime/localizedStrftime/
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonjs committed Jun 14, 2011
1 parent 2d20f26 commit f7e89bd
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 16 deletions.
4 changes: 3 additions & 1 deletion Readme.md
Expand Up @@ -37,11 +37,13 @@ And if you don't want to pass a localization object every time you can get a loc

var strftime = require('strftime')
var it_IT = { /* same as above */ }
var strftime_IT = strftime.getLocalizedStrftime(it_IT)
var strftime_IT = strftime.localizedStrftime(it_IT)
console.log(strftime_IT('%B %d, %y %H:%M:%S')) // aprile 28, 2011 18:21:08

For details just see `man 3 strftime` as the format specifiers are identical.

**NOTE:** `getLocalizedStrftime` is deprecated, use `localizedStrftime` instead. `getLocalizedStrftime` will be removed in 0.5 or 0.6.

License
=======

Expand Down
10 changes: 7 additions & 3 deletions lib/index.js
Expand Up @@ -23,7 +23,11 @@

namespace.strftime = strftime;
namespace.strftimeUTC = strftimeUTC;
namespace.getLocalizedStrftime = getLocalizedStrftime;
namespace.localizedStrftime = localizedStrftime;
namespace.getLocalizedStrftime = function(locale) {
console.log('[strftime] DEPRECATION NOTICE: getLocalizedStrftime is deprecated, use localizedStrftime instead');
return (namespace.getLocalizedStrftime = localizedStrftime)(locale);
};
////

function words(s) { return (s || '').split(' '); }
Expand All @@ -45,7 +49,7 @@
return _strftime(fmt, d, locale, true);
}

function getLocalizedStrftime(locale) {
function localizedStrftime(locale) {
return function(fmt, d) {
return strftime(fmt, d, locale);
};
Expand All @@ -60,7 +64,7 @@
}
d = d || new Date();
locale = locale || DefaultLocale;
locale.formats = locale.formats || {}
locale.formats = locale.formats || {};
var msDelta = 0;
if (_useUTC) {
msDelta = (d.getTimezoneOffset() || 0) * 60000;
Expand Down
72 changes: 60 additions & 12 deletions test/test.js
Expand Up @@ -11,19 +11,10 @@ var assert = require('assert')
// Tue, 07 Jun 2011 18:51:45 GMT
, Time = new Date(1307472705067)

/// check exports

assert.fn = function(value, msg) {
assert.equal('function', typeof value, msg)
}

assert.fn(lib.strftime)
assert.fn(lib.strftimeUTC)
assert.fn(lib.getLocalizedStrftime)


/// check formats

assert.format = function(format, expected, expectedUTC) {
function _assertFmt(expected, name) {
name = name || 'strftime'
Expand All @@ -37,6 +28,15 @@ assert.format = function(format, expected, expectedUTC) {
_assertFmt(expectedUTC || expected, 'strftimeUTC')
}

/// check exports
assert.fn(lib.strftime)
assert.fn(lib.strftimeUTC)
assert.fn(lib.getLocalizedStrftime)

/// time zones
testTimezone('P[DS]T')

/// check all formats in GMT, most coverage
assert.format('%A', 'Tuesday')
assert.format('%a', 'Tue')
assert.format('%B', 'June')
Expand Down Expand Up @@ -71,9 +71,57 @@ assert.format('%%', '%') // any other char

ok('GMT')

// PST/PDT
testTimezone('P[DS]T')
// test other time zones you care about here

/// locales

var it_IT =
{ days: words('domenica lunedi martedi mercoledi giovedi venerdi sabato')
, shortDays: words('dom lun mar mer gio ven sab')
, months: words('gennaio febbraio marzo aprile maggio giugno luglio agosto settembre ottobre novembre dicembre')
, shortMonths: words('gen feb mar apr mag giu lug ago set ott nov dic')
, AM: 'it$AM'
, PM: 'it$PM'
, formats: {
D: 'it$%m/%d/%y'
, F: 'it$%Y-%m-%d'
, R: 'it$%H:%M'
, r: 'it$%I:%M:%S %p'
, T: 'it$%H:%M:%S'
, v: 'it$%e-%b-%Y'
}
}

assert.format_it = function(format, expected, expectedUTC) {
function _assertFmt(expected, name) {
name = name || 'strftime'
var actual = lib[name](format, Time, it_IT)
assert.equal(expected, actual,
name + '("' + format + '", Time) is ' + JSON.stringify(actual)
+ ', expected ' + JSON.stringify(expected))
}

if (expected) _assertFmt(expected)
_assertFmt(expectedUTC || expected, 'strftimeUTC')
}

assert.format_it('%A', 'martedi')
assert.format_it('%a', 'mar')
assert.format_it('%B', 'giugno')
assert.format_it('%b', 'giu')
assert.format_it('%D', 'it$06/07/11')
assert.format_it('%F', 'it$2011-06-07')
assert.format_it('%p', null, 'it$PM')
assert.format_it('%R', null, 'it$18:51')
assert.format_it('%r', null, 'it$06:51:45 it$PM')
assert.format_it('%T', null, 'it$18:51:45')
assert.format_it('%v', 'it$7-giu-2011')

ok('Locales')


/// helpers

function words(s) { return (s || '').split(' '); }

function ok(s) { console.log('[ \033[32mOK\033[0m ] ' + s) }

Expand Down

0 comments on commit f7e89bd

Please sign in to comment.