This commit is contained in:
B~Vladi 2014-05-15 11:26:42 +04:00
parent 785611bffa
commit bef84d6988

View file

@ -11,20 +11,20 @@
; ;
(function () { (function () {
// Where to export the API // Where to export the API
var namespace; var namespace;
var toString = Object.prototype.toString; var toString = Object.prototype.toString;
try { try {
// CommonJS / Node module // CommonJS / Node module
namespace = module.exports = strftime; namespace = module.exports = strftime;
} catch (error) { } catch (error) {
// Browsers and other environments // Browsers and other environments
// Get the global object. Works in ES3, ES5, and ES5 strict mode. // Get the global object. Works in ES3, ES5, and ES5 strict mode.
namespace = new Function('return this')(); namespace = new Function('return this')();
} }
var DefaultLocale = { var DefaultLocale = {
days: 'Sunday Monday Tuesday Wednesday Thursday Friday Saturday'.split(' '), days: 'Sunday Monday Tuesday Wednesday Thursday Friday Saturday'.split(' '),
shortDays: 'Sun Mon Tue Wed Thu Fri Sat'.split(' '), shortDays: 'Sun Mon Tue Wed Thu Fri Sat'.split(' '),
months: 'January February March April May June July August September October November December'.split(' '), months: 'January February March April May June July August September October November December'.split(' '),
@ -33,16 +33,16 @@
PM: 'PM', PM: 'PM',
am: 'am', am: 'am',
pm: 'pm' pm: 'pm'
}; };
// d, locale, and options are optional, but you can't leave // d, locale, and options are optional, but you can't leave
// holes in the argument list. If you pass options you have to pass // holes in the argument list. If you pass options you have to pass
// in all the preceding args as well. // in all the preceding args as well.
// //
// options: // options:
// - locale [object] an object with the same structure as DefaultLocale // - locale [object] an object with the same structure as DefaultLocale
// - timezone [number] timezone offset in minutes from GMT // - timezone [number] timezone offset in minutes from GMT
function _strftime(format, date, locale, options) { function _strftime(format, date, locale, options) {
var o = options || {}; var o = options || {};
var l = locale; var l = locale;
var d = date; var d = date;
@ -65,10 +65,10 @@
i = m.index + m[0].length; i = m.index + m[0].length;
} }
return r; return r + format.substring(i);
} }
var mask = { var mask = {
'A': function (padding, date, locale) { 'A': function (padding, date, locale) {
return locale.days[date.getDay()]; return locale.days[date.getDay()];
}, },
@ -194,12 +194,12 @@
return (off < 0 ? '-' : '+') + pad(Math.abs(off / 60)) + pad(off % 60); return (off < 0 ? '-' : '+') + pad(Math.abs(off / 60)) + pad(off % 60);
} }
} }
}; };
// Most of the specifiers supported by C's strftime, and some from Ruby. // Most of the specifiers supported by C's strftime, and some from Ruby.
// Some other syntax extensions from Ruby are supported: %-, %_, and %0 // Some other syntax extensions from Ruby are supported: %-, %_, and %0
// to pad with nothing, space, or zero (respectively). // to pad with nothing, space, or zero (respectively).
function match(match, date, locale, timestamp, options) { function match(match, date, locale, timestamp, options) {
var p = match[1]; var p = match[1];
var c = match[2]; var c = match[2];
@ -221,14 +221,14 @@
} }
return mask[c] ? mask[c](p, date, locale, timestamp, options) : c; return mask[c] ? mask[c](p, date, locale, timestamp, options) : c;
} }
function dateToUTC(d) { function dateToUTC(d) {
return new Date(d.getTime() + (d.getTimezoneOffset() || 0) * 60000); return new Date(d.getTime() + (d.getTimezoneOffset() || 0) * 60000);
} }
// Default padding is '0' and default length is 2, both are optional. // Default padding is '0' and default length is 2, both are optional.
function pad(n, padding, length) { function pad(n, padding, length) {
var _n = String(n); var _n = String(n);
var _p = padding == null ? '0' : padding; var _p = padding == null ? '0' : padding;
var _l = length || 2; var _l = length || 2;
@ -241,9 +241,9 @@
} }
return _n; return _n;
} }
function hours12(d) { function hours12(d) {
var hour = d.getHours(); var hour = d.getHours();
if (hour === 0) { if (hour === 0) {
@ -253,10 +253,10 @@
} }
return hour; return hour;
} }
// Get the ordinal suffix for a number: st, nd, rd, or th // Get the ordinal suffix for a number: st, nd, rd, or th
function ordinal(n) { function ordinal(n) {
var i = n % 10; var i = n % 10;
var ii = n % 100; var ii = n % 100;
@ -272,11 +272,11 @@
case 3: case 3:
return 'rd'; return 'rd';
} }
} }
// firstWeekday: 'sunday' or 'monday', default is 'sunday' // firstWeekday: 'sunday' or 'monday', default is 'sunday'
// Pilfered & ported from Ruby's strftime implementation. // Pilfered & ported from Ruby's strftime implementation.
function weekNumber(d, firstWeekday) { function weekNumber(d, firstWeekday) {
var firstDayOfYear = new Date(d.getFullYear(), 0, 1); var firstDayOfYear = new Date(d.getFullYear(), 0, 1);
// This works by shifting the weekday back by one day if we // This works by shifting the weekday back by one day if we
// are treating Monday as the first day of the week. // are treating Monday as the first day of the week.
@ -293,19 +293,19 @@
} }
return Math.floor((yDay + 7 - wDay) / 7); return Math.floor((yDay + 7 - wDay) / 7);
} }
function strftime(fmt, d, locale) { function strftime(fmt, d, locale) {
return _strftime(fmt, d, locale); return _strftime(fmt, d, locale);
} }
function isDate(date) { function isDate(date) {
return toString.call(date) === '[object Date]'; return toString.call(date) === '[object Date]';
} }
// ISO 8601 format timezone string, [-+]HHMM // ISO 8601 format timezone string, [-+]HHMM
// Convert to the number of minutes and it'll be applied to the date below. // Convert to the number of minutes and it'll be applied to the date below.
function fixTZ(date, opt) { function fixTZ(date, opt) {
var d = date; var d = date;
var tz = opt.timezone; var tz = opt.timezone;
var tzType = typeof tz; var tzType = typeof tz;
@ -328,11 +328,11 @@
} }
return d; return d;
} }
namespace.strftime = strftime; namespace.strftime = strftime;
namespace.strftimeTZ = strftime.strftimeTZ = function strfTimeTZ(fmt, d, locale, timeZone) { namespace.strftimeTZ = strftime.strftimeTZ = function strfTimeTZ(fmt, d, locale, timeZone) {
var _locale = locale; var _locale = locale;
var _timeZone = timeZone; var _timeZone = timeZone;
@ -344,18 +344,18 @@
return _strftime(fmt, d, _locale, { return _strftime(fmt, d, _locale, {
timezone: _timeZone timezone: _timeZone
}); });
}; };
namespace.strftimeUTC = strftime.strftimeUTC = function strftimeUTC(fmt, d, locale) { namespace.strftimeUTC = strftime.strftimeUTC = function strftimeUTC(fmt, d, locale) {
return _strftime(fmt, d, locale, { return _strftime(fmt, d, locale, {
utc: true utc: true
}); });
}; };
namespace.localizedStrftime = strftime.localizedStrftime = function localizedStrftime(locale) { namespace.localizedStrftime = strftime.localizedStrftime = function localizedStrftime(locale) {
return function (fmt, d, options) { return function (fmt, d, options) {
return _strftime(fmt, d, locale, options); return _strftime(fmt, d, locale, options);
}; };
}; };
}()); }());