Skip to content

Commit

Permalink
don't check for Date instances, just required methods
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonjs committed Mar 7, 2013
1 parent 403504a commit 8565e69
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
// locale is an object with the same structure as DefaultLocale
function _strftime(fmt, d, locale, _useUTC) {
// d and locale are optional so check if d is really the locale
if (d && !(d instanceof Date)) {
if (d && !quacksLikeDate(d)) {
locale = d;
d = undefined;
}
Expand Down Expand Up @@ -135,6 +135,19 @@
});
}

RequiredDateMethods = ['getTime', 'getTimezoneOffset', 'getDay', 'getDate', 'getMonth', 'getFullYear', 'getYear', 'getHours', 'getMinutes', 'getSeconds'];
function quacksLikeDate(x) {
var i = 0
, n = RequiredDateMethods.length
;
for (i = 0; i < n; ++i) {
if (typeof x[RequiredDateMethods[i]] != 'function') {
return false;
}
}
return true;
}

// Default padding is '0' and default length is 2, both are optional.
function pad(n, padding, length) {
// pad(n, <length>)
Expand Down

0 comments on commit 8565e69

Please sign in to comment.