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,78 +128,162 @@ Ox.formatDate <f> Formats a date according to a format string
'%' '%'
@*/ @*/
Ox.formatDate = function(date, str, utc) { (function() {
var format = [
['%', function() {
return '%{%}';
}],
['c', function() {
return '%D %r';
}],
['D', function() {
return '%m/%d/%y';
}],
['F', function() {
return '%Y-%m-%d';
}],
['h', function() {
return '%b';
}],
['R', function() {
return '%H:%M';
}],
['r', function() {
return '%I:%M:%S %p';
}],
['T', function() {
return '%H:%M:%S';
}],
['v', function() {
return '%e-%b-%Y';
}],
['\\+', function() {
return '%a %b %e %H:%M:%S %Z %Y';
}],
['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);
}],
['U', function(date, utc) {
return Ox.pad(Ox.getWeek(date, utc), 2);
}],
['u', function(date, utc) {
return Ox.getISODay(date, 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];
}],
['x', function(date, utc) {
var y = Ox.getFullYear(date, utc);
return Math.abs(y) + (y < 1000 ? ' ' + Ox.BCAD[y < 0 ? 0 : 1] : '');
}],
['Y', function(date, utc) {
return Ox.getFullYear(date, utc);
}],
['y', function(date, utc) {
return Ox.getFullYear(date, utc).toString().slice(-2);
}],
['Z', function(date) {
return date.toString().split('(')[1].replace(')', '');
}],
['z', function(date) {
return Ox.getTimezoneOffsetString(date);
}],
['n', function() {
return '\n';
}],
['t', function() {
return '\t';
}],
['\\{%\\}', function() {
return '%';
}]
].map(function(value) {
return [new RegExp('%' + value[0], 'g'), value[1]];
});
Ox.formatDate = function(date, string, utc) {
if (date === '') { if (date === '') {
return ''; return '';
} }
date = Ox.makeDate(date); date = Ox.makeDate(date);
var format = [ format.forEach(function(value) {
['%', function() {return '%{%}';}], string = string.replace(value[0], value[1](date, utc));
['c', function() {return '%D %r';}],
['D', function() {return '%m/%d/%y';}],
['F', function() {return '%Y-%m-%d';}],
['h', function() {return '%b';}],
['R', function() {return '%H:%M';}],
['r', function() {return '%I:%M:%S %p';}],
['T', function() {return '%H:%M:%S';}],
['v', function() {return '%e-%b-%Y';}],
['\\+', function() {return '%a %b %e %H:%M:%S %Z %Y';}],
['A', function(d) {return Ox.WEEKDAYS[(Ox.getDay(d, utc) + 6) % 7];}],
['a', function(d) {return Ox.SHORT_WEEKDAYS[(Ox.getDay(d, utc) + 6) % 7];}],
['B', function(d) {return Ox.MONTHS[Ox.getMonth(d, utc)];}],
['b', function(d) {return Ox.SHORT_MONTHS[Ox.getMonth(d, utc)];}],
['C', function(d) {return Math.floor(Ox.getFullYear(d, utc) / 100).toString();}],
['d', function(d) {return Ox.pad(Ox.getDate(d, utc), 2);}],
['e', function(d) {return Ox.pad(Ox.getDate(d, utc), 2, ' ');}],
['G', function(d) {return Ox.getISOYear(d, utc);}],
['g', function(d) {return Ox.getISOYear(d, utc).toString().slice(-2);}],
['H', function(d) {return Ox.pad(Ox.getHours(d, utc), 2);}],
['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);}],
['k', function(d) {return Ox.pad(Ox.getHours(d, utc), 2, ' ');}],
['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);}],
['m', function(d) {return Ox.pad((Ox.getMonth(d, utc) + 1), 2);}],
['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;}],
['S', function(d) {return Ox.pad(Ox.getSeconds(d, utc), 2);}],
['s', function(d) {
return Math.floor(d.getTime() / 1000)
- (utc ? Ox.getTimezoneOffset() / 1000 : 0);
}],
['U', function(d) {return Ox.pad(Ox.getWeek(d, utc), 2);}],
['u', function(d) {return Ox.getISODay(d, utc);}],
['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);}],
['X', function(d) {
var y = Ox.getFullYear(d, utc);
return Math.abs(y) + ' ' + Ox.BCAD[y < 0 ? 0 : 1];
}],
['x', function(d) {
var y = Ox.getFullYear(d, utc);
return Math.abs(y) + (y < 1000 ? ' ' + Ox.BCAD[y < 0 ? 0 : 1] : '');
}],
['Y', function(d) {return Ox.getFullYear(d, utc);}],
['y', function(d) {return Ox.getFullYear(d, utc).toString().slice(-2);}],
['Z', function(d) {return d.toString().split('(')[1].replace(')', '');}],
['z', function(d) {return Ox.getTimezoneOffsetString(d);}],
['n', function() {return '\n';}],
['t', function() {return '\t';}],
['\\{%\\}', function() {return '%';}]
];
format.forEach(function(v) {
var regexp = new RegExp('%' + v[0], 'g');
if (regexp.test(str)) {
str = str.replace(regexp, v[1](date));
}
}); });
return str; return string;
}; };
})();
/*@ /*@
Ox.formatDateRange <f> Formats a date range as a string Ox.formatDateRange <f> Formats a date range as a string
A date range is a pair of arbitrary-presicion date strings A date range is a pair of arbitrary-presicion date strings