mirror of
https://github.com/samsonjs/strftime.git
synced 2026-04-21 13:25:50 +00:00
refactor, support exporting to global scope in ES5 strict mode
This commit is contained in:
parent
200dc2ef5e
commit
cd4d2a3ec5
1 changed files with 119 additions and 115 deletions
234
lib/index.js
234
lib/index.js
|
|
@ -7,126 +7,130 @@
|
||||||
|
|
||||||
;(function() {
|
;(function() {
|
||||||
|
|
||||||
var DefaultLocale = {
|
//// Export the API
|
||||||
days: [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ],
|
var namespace;
|
||||||
shortDays: [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ],
|
|
||||||
|
|
||||||
months: [ 'January', 'February', 'March', 'April', 'May', 'June', 'July',
|
// CommonJS / Node module
|
||||||
'August', 'September', 'October', 'November', 'December' ],
|
if (typeof exports !== 'undefined') {
|
||||||
|
namespace = exports;
|
||||||
|
}
|
||||||
|
|
||||||
shortMonths: [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
|
// Browsers and other environments
|
||||||
'Sep', 'Oct', 'Nov', 'Dec' ],
|
else {
|
||||||
AM: 'AM',
|
// Get the global object. Works in ES3, ES5, and ES5 strict mode.
|
||||||
PM: 'PM'
|
namespace = (function(){ return this || (1,eval)('this') }());
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace.strftime = strftime;
|
||||||
|
namespace.strftimeUTC = strftimeUTC;
|
||||||
|
namespace.getLocalizedStrftime = getLocalizedStrftime;
|
||||||
|
////
|
||||||
|
|
||||||
|
function words(s) { return (s || '').split(' '); }
|
||||||
|
|
||||||
|
var DefaultLocale =
|
||||||
|
{ days: words('Sunday Monday Tuesday Wednesday Thursday Friday Saturday')
|
||||||
|
, shortDays: words('Sun Mon Tue Wed Thu Fri Sat')
|
||||||
|
, months: words('January February March April May June July August September October November December')
|
||||||
|
, shortMonths: words('Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec')
|
||||||
|
, AM: 'AM'
|
||||||
|
, PM: 'PM'
|
||||||
|
}
|
||||||
|
|
||||||
|
function pad(n, padding) {
|
||||||
|
padding = padding || '0';
|
||||||
|
return n < 10 ? (padding + n) : n;
|
||||||
|
}
|
||||||
|
|
||||||
|
function hours12(d) {
|
||||||
|
var hour = d.getHours();
|
||||||
|
if (hour == 0) hour = 12;
|
||||||
|
else if (hour > 12) hour -= 12;
|
||||||
|
return hour;
|
||||||
|
}
|
||||||
|
|
||||||
|
function strftime(fmt, d, locale) {
|
||||||
|
return _strftime(fmt, d, locale, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
function strftimeUTC(fmt, d, locale) {
|
||||||
|
return _strftime(fmt, d, locale, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getLocalizedStrftime(locale) {
|
||||||
|
return function(fmt, d) {
|
||||||
|
return strftime(fmt, d, locale);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)) {
|
||||||
|
locale = d;
|
||||||
|
d = undefined;
|
||||||
|
}
|
||||||
|
d = d || new Date();
|
||||||
|
locale = locale || DefaultLocale;
|
||||||
|
locale.formats = locale.formats || {}
|
||||||
|
if (_useUTC) {
|
||||||
|
d = new Date(d.getTime() + ((d.getTimezoneOffset() || 0) * 60000));
|
||||||
}
|
}
|
||||||
|
|
||||||
function pad(n, padding) {
|
// Most of the specifiers supported by C's strftime
|
||||||
padding = padding || '0';
|
return fmt.replace(/%(.)/g, function(_, c) {
|
||||||
return n < 10 ? (padding + n) : n;
|
switch (c) {
|
||||||
}
|
case 'A': return locale.days[d.getDay()];
|
||||||
|
case 'a': return locale.shortDays[d.getDay()];
|
||||||
function hours12(d) {
|
case 'B': return locale.months[d.getMonth()];
|
||||||
var hour = d.getHours();
|
case 'b': // fall through
|
||||||
if (hour == 0) hour = 12;
|
case 'h': return locale.shortMonths[d.getMonth()];
|
||||||
else if (hour > 12) hour -= 12;
|
case 'D': return strftime(locale.formats.D || '%m/%d/%y', d, locale);
|
||||||
return hour;
|
case 'd': return pad(d.getDate());
|
||||||
}
|
case 'e': return d.getDate();
|
||||||
|
case 'F': return strftime(locale.formats.F || '%Y-%m-%d', d, locale);
|
||||||
// locale is an object with the same structure as DefaultLocale
|
case 'H': return pad(d.getHours());
|
||||||
function _strftime(fmt, d, locale, _useUTC) {
|
case 'I': return pad(hours12(d));
|
||||||
// d and locale are optional so check if d is really the locale
|
case 'k': return pad(d.getHours(), ' ');
|
||||||
if (d && !(d instanceof Date)) {
|
case 'l': return pad(hours12(d), ' ');
|
||||||
locale = d;
|
case 'M': return pad(d.getMinutes());
|
||||||
d = undefined;
|
case 'm': return pad(d.getMonth() + 1);
|
||||||
}
|
case 'n': return '\n';
|
||||||
d = d || new Date();
|
case 'p': return d.getHours() < 12 ? locale.AM : locale.PM;
|
||||||
locale = locale || DefaultLocale;
|
case 'R': return strftime(locale.formats.R || '%H:%M', d, locale);
|
||||||
locale.formats = locale.formats || {}
|
case 'r': return strftime(locale.formats.r || '%I:%M:%S %p', d, locale);
|
||||||
|
case 'S': return pad(d.getSeconds());
|
||||||
|
case 's': return d.getTime();
|
||||||
|
case 'T': return strftime(locale.formats.T || '%H:%M:%S', d, locale);
|
||||||
|
case 't': return '\t';
|
||||||
|
case 'u':
|
||||||
|
var day = d.getDay();
|
||||||
|
return day == 0 ? 7 : day; // 1 - 7, Monday is first day of the week
|
||||||
|
case 'v': return strftime(locale.formats.v || '%e-%b-%Y', d, locale);
|
||||||
|
case 'w': return d.getDay(); // 0 - 6, Sunday is first day of the week
|
||||||
|
case 'Y': return d.getFullYear();
|
||||||
|
case 'y':
|
||||||
|
var y = String(d.getFullYear());
|
||||||
|
return y.slice(y.length - 2);
|
||||||
|
case 'Z':
|
||||||
if (_useUTC) {
|
if (_useUTC) {
|
||||||
d = new Date(d.getTime() + ((d.getTimezoneOffset() || 0) * 60000));
|
return "GMT";
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
// Most of the specifiers supported by C's strftime
|
var tz = d.toString().match(/\((\w+)\)/);
|
||||||
return fmt.replace(/%(.)/g, function(_, c) {
|
return tz && tz[1] || '';
|
||||||
switch (c) {
|
}
|
||||||
case 'A': return locale.days[d.getDay()];
|
case 'z':
|
||||||
case 'a': return locale.shortDays[d.getDay()];
|
if (_useUTC) {
|
||||||
case 'B': return locale.months[d.getMonth()];
|
return "+0000";
|
||||||
case 'b': // fall through
|
}
|
||||||
case 'h': return locale.shortMonths[d.getMonth()];
|
else {
|
||||||
case 'D': return strftime(locale.formats.D || '%m/%d/%y', d, locale);
|
var off = d.getTimezoneOffset();
|
||||||
case 'd': return pad(d.getDate());
|
return (off < 0 ? '-' : '+') + pad(off / 60) + pad(off % 60);
|
||||||
case 'e': return d.getDate();
|
}
|
||||||
case 'F': return strftime(locale.formats.F || '%Y-%m-%d', d, locale);
|
default: return c;
|
||||||
case 'H': return pad(d.getHours());
|
}
|
||||||
case 'I': return pad(hours12(d));
|
});
|
||||||
case 'k': return pad(d.getHours(), ' ');
|
}
|
||||||
case 'l': return pad(hours12(d), ' ');
|
|
||||||
case 'M': return pad(d.getMinutes());
|
|
||||||
case 'm': return pad(d.getMonth() + 1);
|
|
||||||
case 'n': return '\n';
|
|
||||||
case 'p': return d.getHours() < 12 ? locale.AM : locale.PM;
|
|
||||||
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 'S': return pad(d.getSeconds());
|
|
||||||
case 's': return d.getTime();
|
|
||||||
case 'T': return strftime(locale.formats.T || '%H:%M:%S', d, locale);
|
|
||||||
case 't': return '\t';
|
|
||||||
case 'u':
|
|
||||||
var day = d.getDay();
|
|
||||||
return day == 0 ? 7 : day; // 1 - 7, Monday is first day of the week
|
|
||||||
case 'v': return strftime(locale.formats.v || '%e-%b-%Y', d, locale);
|
|
||||||
case 'w': return d.getDay(); // 0 - 6, Sunday is first day of the week
|
|
||||||
case 'Y': return d.getFullYear();
|
|
||||||
case 'y':
|
|
||||||
var y = String(d.getFullYear());
|
|
||||||
return y.slice(y.length - 2);
|
|
||||||
case 'Z':
|
|
||||||
if (_useUTC) {
|
|
||||||
return "GMT";
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
var tz = d.toString().match(/\((\w+)\)/);
|
|
||||||
return tz && tz[1] || '';
|
|
||||||
}
|
|
||||||
case 'z':
|
|
||||||
if (_useUTC) {
|
|
||||||
return "+0000";
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
var off = d.getTimezoneOffset();
|
|
||||||
return (off < 0 ? '-' : '+') + pad(off / 60) + pad(off % 60);
|
|
||||||
}
|
|
||||||
default: return c;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function strftime(fmt, d, locale) {
|
|
||||||
return _strftime(fmt, d, locale, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
function strftimeUTC(fmt, d, locale) {
|
|
||||||
return _strftime(fmt, d, locale, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
function getLocalizedStrftime(locale) {
|
|
||||||
return function(fmt, d) {
|
|
||||||
return strftime(fmt, d, locale);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof exports !== 'undefined') {
|
|
||||||
exports.strftime = strftime;
|
|
||||||
exports.strftimeUTC = strftimeUTC;
|
|
||||||
exports.getLocalizedStrftime = getLocalizedStrftime;
|
|
||||||
} else {
|
|
||||||
(function(global) {
|
|
||||||
global.strftime = strftime;
|
|
||||||
global.strftimeUTC = strftimeUTC;
|
|
||||||
global.getLocalizedStrftime = getLocalizedStrftime;
|
|
||||||
}(this));
|
|
||||||
}
|
|
||||||
|
|
||||||
}());
|
}());
|
||||||
Loading…
Reference in a new issue