use a locale object instead of a l10n function and localize formats

This commit is contained in:
Sami Samhuri 2011-03-03 19:16:09 -08:00
parent 3ed3282d23
commit 157a85e8c2

View file

@ -7,97 +7,92 @@
;(function() { ;(function() {
var strftime = (function() { var DefaultLocale = {
var Weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', days: [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ],
'Friday', 'Saturday']; shortDays: [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ],
var WeekdaysShort = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; months: [ 'January', 'February', 'March', 'April', 'May', 'June', 'July',
'August', 'September', 'October', 'November', 'December' ],
var Months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', shortMonths: [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
'August', 'September', 'October', 'November', 'December']; 'Sep', 'Oct', 'Nov', 'Dec' ],
AM: 'AM',
PM: 'PM'
}
var MonthsShort = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', function pad(n, padding) {
'Sep', 'Oct', 'Nov', 'Dec']; padding = padding || '0';
return n < 10 ? (padding + n) : n;
}
var AM = 'AM', function hours12(d) {
PM = 'PM'; var hour = d.getHours();
if (hour == 0) hour = 12;
else if (hour > 12) hour -= 12;
return hour;
}
function pad(n, padding) { // loc is a function that maps the default English names to localized
padding = padding || '0'; // names. In most cases it will just look up strings in a map but it's
return n < 10 ? (padding + n) : n; // a function for added flexibility.
function strftime(fmt, d, locale) {
// d and loc are optional, check if d is really loc
if (d && !(d instanceof Date)) {
locale = d;
d = new Date();
} }
locale = locale || DefaultLocale;
d = d || new Date();
function hours12(d) { // Most of the specifiers supported by C's strftime
var hour = d.getHours(); return fmt.replace(/%(.)/g, function(_, c) {
if (hour == 0) hour = 12; switch (c) {
else if (hour > 12) hour -= 12; case 'A': return locale.days[d.getDay()];
return hour; case 'a': return locale.shortDays[d.getDay()];
} case 'B': return locale.months[d.getMonth()];
case 'b': // fall through
// loc is a function that maps the default English names to localized case 'h': return locale.shortMonths[d.getMonth()];
// names. In most cases it will just look up strings in a map but it's case 'D': return strftime(locale.formats.D || '%m/%d/%y', d, locale);
// a function for added flexibility. case 'd': return pad(d.getDate());
return function strftime(fmt, d, loc) { case 'e': return d.getDate();
// d and loc are optional, check if d is really loc case 'F': return strftime(locale.formats.F || '%Y-%m-%d', d, locale);
if (typeof d === 'function') { case 'H': return pad(d.getHours());
loc = d; case 'I': return pad(hours12(d));
d = new Date(); case 'k': return pad(d.getHours(), ' ');
} else if (!loc) { case 'l': return pad(hours12(d), ' ');
// no localization case 'M': return pad(d.getMinutes());
loc = function(x) { return x; }; 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 year = d.getYear();
return year < 100 ? year : year - 100;
case 'Z':
var tz = d.toString().match(/\((\w+)\)/);
return tz && tz[1] || '';
case 'z':
var off = d.getTimezoneOffset();
return (off < 0 ? '-' : '+') + pad(off / 60) + pad(off % 60);
default: return c;
} }
d = d || new Date(); });
}
// Most of the specifiers supported by C's strftime function getLocalizedStrftime(locale) {
return fmt.replace(/%(.)/g, function(_, c) {
switch (c) {
case 'A': return loc(Weekdays[d.getDay()]);
case 'a': return loc(WeekdaysShort[d.getDay()]);
case 'B': return loc(Months[d.getMonth()]);
case 'b': // fall through
case 'h': return loc(MonthsShort[d.getMonth()]);
case 'D': return strftime('%m/%d/%y', d, loc);
case 'd': return pad(d.getDate());
case 'e': return d.getDate();
case 'F': return strftime('%Y-%m-%d', d, loc);
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 ? loc(AM) : loc(PM);
case 'R': return strftime('%H:%M', d, loc);
case 'r': return strftime('%I:%M:%S %p', d, loc);
case 'S': return pad(d.getSeconds());
case 's': return d.getTime();
case 'T': return strftime('%H:%M:%S', d, loc);
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('%e-%b-%Y', d, loc);
case 'w': return d.getDay(); // 0 - 6, Sunday is first day of the week
case 'Y': return d.getFullYear();
case 'y':
var year = d.getYear();
return year < 100 ? year : year - 100;
case 'Z':
var tz = d.toString().match(/\((\w+)\)/);
return tz && tz[1] || '';
case 'z':
var off = d.getTimezoneOffset();
return (off < 0 ? '-' : '+') + pad(off / 60) + pad(off % 60);
default: return c;
}
});
};
}());
function getLocalizedStrftime(loc) {
return function(fmt, d) { return function(fmt, d) {
return strftime(fmt, d, loc); return strftime(fmt, d, locale);
}; };
} }