rename vars; more efficient version of Ox.formatDate

This commit is contained in:
rolux 2012-05-25 12:08:20 +02:00
parent 5b8f1dd3b5
commit a37949094d

View file

@ -8,12 +8,12 @@ Ox.formatArea <f> Formats a number of meters as square meters or kilometers
'1 km\u00B2' '1 km\u00B2'
@*/ @*/
Ox.formatArea = function(num, dec) { Ox.formatArea = function(number, decimals) {
dec = Ox.isUndefined(dec) ? 8 : dec; var k = number >= 1000000 ? 'k' : '';
var km = num >= 1000000; decimals = Ox.isUndefined(decimals) ? 8 : decimals;
return Ox.formatNumber( return Ox.formatNumber(
(km ? num / 1000000 : num).toPrecision(dec) (k ? number / 1000000 : number).toPrecision(decimals)
) + ' ' + (km ? 'k' : '') + 'm\u00B2'; ) + ' ' + k + 'm\u00B2';
}; };
/*@ /*@
@ -21,9 +21,8 @@ Ox.formatCurrency <f> Formats a number with a currency symbol
> Ox.formatCurrency(1000, '$', 2) > Ox.formatCurrency(1000, '$', 2)
'$1,000.00' '$1,000.00'
@*/ @*/
Ox.formatCurrency = function(number, string, decimals) {
Ox.formatCurrency = function(num, str, dec) { return string + Ox.formatNumber(number, decimals);
return str + Ox.formatNumber(num, dec);
}; };
/*@ /*@
@ -32,10 +31,10 @@ Ox.formatDate <f> Formats a date according to a format string
<a href="http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/strftime.3.html">strftime</a> <a href="http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/strftime.3.html">strftime</a>
and <a href="http://en.wikipedia.org/wiki/ISO_8601">ISO 8601</a>. and <a href="http://en.wikipedia.org/wiki/ISO_8601">ISO 8601</a>.
'%Q' (quarter) and '%X'/'%x' (year with 'BC'/'AD') are non-standard '%Q' (quarter) and '%X'/'%x' (year with 'BC'/'AD') are non-standard
(str) -> <s> formatted date (string) -> <s> formatted date
(date, str) -> <s> formatted date (date, string) -> <s> formatted date
(date, str, utc) -> <s> formatted date (date, string, utc) -> <s> formatted date
str <s> format string string <s> format string
date <d|n|s> date date <d|n|s> date
utc <b> date is utc utc <b> date is utc
<script> <script>
@ -129,77 +128,161 @@ Ox.formatDate <f> Formats a date according to a format string
'%' '%'
@*/ @*/
Ox.formatDate = function(date, str, utc) { (function() {
if (date === '') {
return '';
}
date = Ox.makeDate(date);
var format = [ var format = [
['%', function() {return '%{%}';}], ['%', function() {
['c', function() {return '%D %r';}], return '%{%}';
['D', function() {return '%m/%d/%y';}], }],
['F', function() {return '%Y-%m-%d';}], ['c', function() {
['h', function() {return '%b';}], return '%D %r';
['R', function() {return '%H:%M';}], }],
['r', function() {return '%I:%M:%S %p';}], ['D', function() {
['T', function() {return '%H:%M:%S';}], return '%m/%d/%y';
['v', function() {return '%e-%b-%Y';}], }],
['\\+', function() {return '%a %b %e %H:%M:%S %Z %Y';}], ['F', function() {
['A', function(d) {return Ox.WEEKDAYS[(Ox.getDay(d, utc) + 6) % 7];}], return '%Y-%m-%d';
['a', function(d) {return Ox.SHORT_WEEKDAYS[(Ox.getDay(d, utc) + 6) % 7];}], }],
['B', function(d) {return Ox.MONTHS[Ox.getMonth(d, utc)];}], ['h', function() {
['b', function(d) {return Ox.SHORT_MONTHS[Ox.getMonth(d, utc)];}], return '%b';
['C', function(d) {return Math.floor(Ox.getFullYear(d, utc) / 100).toString();}], }],
['d', function(d) {return Ox.pad(Ox.getDate(d, utc), 2);}], ['R', function() {
['e', function(d) {return Ox.pad(Ox.getDate(d, utc), 2, ' ');}], return '%H:%M';
['G', function(d) {return Ox.getISOYear(d, utc);}], }],
['g', function(d) {return Ox.getISOYear(d, utc).toString().slice(-2);}], ['r', function() {
['H', function(d) {return Ox.pad(Ox.getHours(d, utc), 2);}], return '%I:%M:%S %p';
['I', function(d) {return Ox.pad((Ox.getHours(d, utc) + 11) % 12 + 1, 2);}], }],
['j', function(d) {return Ox.pad(Ox.getDayOfTheYear(d, utc), 3);}], ['T', function() {
['k', function(d) {return Ox.pad(Ox.getHours(d, utc), 2, ' ');}], return '%H:%M:%S';
['l', function(d) {return Ox.pad(((Ox.getHours(d, utc) + 11) % 12 + 1), 2, ' ');}], }],
['M', function(d) {return Ox.pad(Ox.getMinutes(d, utc), 2);}], ['v', function() {
['m', function(d) {return Ox.pad((Ox.getMonth(d, utc) + 1), 2);}], return '%e-%b-%Y';
['p', function(d) {return Ox.AMPM[Math.floor(Ox.getHours(d, utc) / 12)];}], }],
['Q', function(d) {return Math.floor(Ox.getMonth(d, utc) / 4) + 1;}], ['\\+', function() {
['S', function(d) {return Ox.pad(Ox.getSeconds(d, utc), 2);}], return '%a %b %e %H:%M:%S %Z %Y';
['s', function(d) { }],
return Math.floor(d.getTime() / 1000) ['A', function(date, utc) {
return Ox.WEEKDAYS[(Ox.getDay(date, utc) + 6) % 7];
}],
['a', function(date, utc) {
return Ox.SHORT_WEEKDAYS[(Ox.getDay(date, utc) + 6) % 7];
}],
['B', function(date, utc) {
return Ox.MONTHS[Ox.getMonth(date, utc)];
}],
['b', function(date, utc) {
return Ox.SHORT_MONTHS[Ox.getMonth(date, utc)];
}],
['C', function(date, utc) {
return Math.floor(Ox.getFullYear(date, utc) / 100).toString();
}],
['d', function(date, utc) {
return Ox.pad(Ox.getDate(date, utc), 2);
}],
['e', function(date, utc) {
return Ox.pad(Ox.getDate(date, utc), 2, ' ');
}],
['G', function(date, utc) {
return Ox.getISOYear(date, utc);
}],
['g', function(date, utc) {
return Ox.getISOYear(date, utc).toString().slice(-2);
}],
['H', function(date, utc) {
return Ox.pad(Ox.getHours(date, utc), 2);
}],
['I', function(date, utc) {
return Ox.pad((Ox.getHours(date, utc) + 11) % 12 + 1, 2);
}],
['j', function(date, utc) {
return Ox.pad(Ox.getDayOfTheYear(date, utc), 3);
}],
['k', function(date, utc) {
return Ox.pad(Ox.getHours(date, utc), 2, ' ');
}],
['l', function(date, utc) {
return Ox.pad(((Ox.getHours(date, utc) + 11) % 12 + 1), 2, ' ');
}],
['M', function(date, utc) {
return Ox.pad(Ox.getMinutes(date, utc), 2);
}],
['m', function(date, utc) {
return Ox.pad((Ox.getMonth(date, utc) + 1), 2);
}],
['p', function(date, utc) {
return Ox.AMPM[Math.floor(Ox.getHours(date, utc) / 12)];
}],
['Q', function(date, utc) {
return Math.floor(Ox.getMonth(date, utc) / 4) + 1;
}],
['S', function(date, utc) {
return Ox.pad(Ox.getSeconds(date, utc), 2);
}],
['s', function(date, utc) {
return Math.floor(date.getTime() / 1000)
- (utc ? Ox.getTimezoneOffset() / 1000 : 0); - (utc ? Ox.getTimezoneOffset() / 1000 : 0);
}], }],
['U', function(d) {return Ox.pad(Ox.getWeek(d, utc), 2);}], ['U', function(date, utc) {
['u', function(d) {return Ox.getISODay(d, utc);}], return Ox.pad(Ox.getWeek(date, utc), 2);
['V', function(d) {return Ox.pad(Ox.getISOWeek(d, utc), 2);}],
['W', function(d) {
return Ox.pad(Math.floor((Ox.getDayOfTheYear(d, utc)
+ (Ox.getFirstDayOfTheYear(d, utc) || 7) - 2) / 7), 2);
}], }],
['w', function(d) {return Ox.getDay(d, utc);}], ['u', function(date, utc) {
['X', function(d) { return Ox.getISODay(date, utc);
var y = Ox.getFullYear(d, utc); }],
['V', function(date, utc) {
return Ox.pad(Ox.getISOWeek(date, utc), 2);
}],
['W', function(date, utc) {
return Ox.pad(Math.floor((Ox.getDayOfTheYear(date, utc)
+ (Ox.getFirstDayOfTheYear(date, utc) || 7) - 2) / 7), 2);
}],
['w', function(date, utc) {
return Ox.getDay(date, utc);
}],
['X', function(date, utc) {
var y = Ox.getFullYear(date, utc);
return Math.abs(y) + ' ' + Ox.BCAD[y < 0 ? 0 : 1]; return Math.abs(y) + ' ' + Ox.BCAD[y < 0 ? 0 : 1];
}], }],
['x', function(d) { ['x', function(date, utc) {
var y = Ox.getFullYear(d, utc); var y = Ox.getFullYear(date, utc);
return Math.abs(y) + (y < 1000 ? ' ' + Ox.BCAD[y < 0 ? 0 : 1] : ''); return Math.abs(y) + (y < 1000 ? ' ' + Ox.BCAD[y < 0 ? 0 : 1] : '');
}], }],
['Y', function(d) {return Ox.getFullYear(d, utc);}], ['Y', function(date, utc) {
['y', function(d) {return Ox.getFullYear(d, utc).toString().slice(-2);}], return Ox.getFullYear(date, utc);
['Z', function(d) {return d.toString().split('(')[1].replace(')', '');}], }],
['z', function(d) {return Ox.getTimezoneOffsetString(d);}], ['y', function(date, utc) {
['n', function() {return '\n';}], return Ox.getFullYear(date, utc).toString().slice(-2);
['t', function() {return '\t';}], }],
['\\{%\\}', function() {return '%';}] ['Z', function(date) {
]; return date.toString().split('(')[1].replace(')', '');
format.forEach(function(v) { }],
var regexp = new RegExp('%' + v[0], 'g'); ['z', function(date) {
if (regexp.test(str)) { return Ox.getTimezoneOffsetString(date);
str = str.replace(regexp, v[1](date)); }],
} ['n', function() {
return '\n';
}],
['t', function() {
return '\t';
}],
['\\{%\\}', function() {
return '%';
}]
].map(function(value) {
return [new RegExp('%' + value[0], 'g'), value[1]];
}); });
return str;
}; Ox.formatDate = function(date, string, utc) {
if (date === '') {
return '';
}
date = Ox.makeDate(date);
format.forEach(function(value) {
string = string.replace(value[0], value[1](date, utc));
});
return string;
};
})();
/*@ /*@
Ox.formatDateRange <f> Formats a date range as a string Ox.formatDateRange <f> Formats a date range as a string