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