mirror of
https://github.com/samsonjs/strftime.git
synced 2026-04-27 14:57:37 +00:00
Optimization for V8
This commit is contained in:
parent
bef84d6988
commit
281ae01936
1 changed files with 34 additions and 29 deletions
63
strftime.js
63
strftime.js
|
|
@ -24,7 +24,7 @@ try {
|
||||||
namespace = new Function('return this')();
|
namespace = new Function('return this')();
|
||||||
}
|
}
|
||||||
|
|
||||||
var DefaultLocale = {
|
var defaultLocale = {
|
||||||
days: 'Sunday Monday Tuesday Wednesday Thursday Friday Saturday'.split(' '),
|
days: 'Sunday Monday Tuesday Wednesday Thursday Friday Saturday'.split(' '),
|
||||||
shortDays: 'Sun Mon Tue Wed Thu Fri Sat'.split(' '),
|
shortDays: 'Sun Mon Tue Wed Thu Fri Sat'.split(' '),
|
||||||
months: 'January February March April May June July August September October November December'.split(' '),
|
months: 'January February March April May June July August September October November December'.split(' '),
|
||||||
|
|
@ -35,37 +35,40 @@ var DefaultLocale = {
|
||||||
pm: 'pm'
|
pm: 'pm'
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var defaultFormats = {
|
||||||
|
D: '%m/%d/%y',
|
||||||
|
F: '%Y-%m-%d',
|
||||||
|
R: '%H:%M',
|
||||||
|
r: '%I:%M:%S %p',
|
||||||
|
T: '%H:%M:%S',
|
||||||
|
v: '%e-%b-%Y'
|
||||||
|
};
|
||||||
|
|
||||||
// d, locale, and options are optional, but you can't leave
|
// d, locale, and options are optional, but you can't leave
|
||||||
// holes in the argument list. If you pass options you have to pass
|
// holes in the argument list. If you pass options you have to pass
|
||||||
// in all the preceding args as well.
|
// in all the preceding args as well.
|
||||||
//
|
//
|
||||||
// options:
|
// options:
|
||||||
// - locale [object] an object with the same structure as DefaultLocale
|
// - locale [object] an object with the same structure as defaultLocale
|
||||||
// - timezone [number] timezone offset in minutes from GMT
|
// - timezone [number] timezone offset in minutes from GMT
|
||||||
function _strftime(format, date, locale, options) {
|
function _strftime(f, date, locale, options) {
|
||||||
var o = options || {};
|
var o = options || {};
|
||||||
var l = locale;
|
var l = locale;
|
||||||
var d = date;
|
var d = date;
|
||||||
var m, ts, i = 0, r = '';
|
var ts;
|
||||||
var re = /%([-_0]?)(.)/g;
|
|
||||||
|
|
||||||
if (d && !isDate(d)) {
|
if (d && toString.call(d) !== '[object Date]') {
|
||||||
l = d;
|
l = d;
|
||||||
d = null;
|
d = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
l = l || DefaultLocale;
|
l = l || defaultLocale;
|
||||||
l.formats = l.formats || {};
|
l.formats = l.formats || {};
|
||||||
d = d || new Date();
|
d = d || new Date();
|
||||||
ts = d.getTime();
|
ts = d.getTime();
|
||||||
d = fixTZ(d, o);
|
d = fixTZ(d, o);
|
||||||
|
|
||||||
while (m = re.exec(format)) {
|
return match(f, d, l, ts, o);
|
||||||
r += format.substring(i, m.index) + match(m, d, l, ts, o);
|
|
||||||
i = m.index + m[0].length;
|
|
||||||
}
|
|
||||||
|
|
||||||
return r + format.substring(i);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var mask = {
|
var mask = {
|
||||||
|
|
@ -199,28 +202,34 @@ var mask = {
|
||||||
// Most of the specifiers supported by C's strftime, and some from Ruby.
|
// Most of the specifiers supported by C's strftime, and some from Ruby.
|
||||||
// Some other syntax extensions from Ruby are supported: %-, %_, and %0
|
// Some other syntax extensions from Ruby are supported: %-, %_, and %0
|
||||||
// to pad with nothing, space, or zero (respectively).
|
// to pad with nothing, space, or zero (respectively).
|
||||||
function match(match, date, locale, timestamp, options) {
|
function match(f, d, l, ts, o) {
|
||||||
var p = match[1];
|
var m, i = 0, r = '', c;
|
||||||
var c = match[2];
|
var re = /%([-_0]?)(.)/g;
|
||||||
|
|
||||||
|
while (m = re.exec(f)) {
|
||||||
|
c = m[2];
|
||||||
|
r += f.substring(i, m.index) + (mask[c] ? mask[c](fixPadding(m[1], m[0]), d, l, ts, o) : c);
|
||||||
|
i = re.lastIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
return r + f.substring(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
function fixPadding(p, c) {
|
||||||
if (p) {
|
if (p) {
|
||||||
switch (p) {
|
switch (p) {
|
||||||
case '-':
|
case '-':
|
||||||
p = '';
|
return '';
|
||||||
break;
|
|
||||||
case '_':
|
case '_':
|
||||||
p = ' ';
|
return ' ';
|
||||||
break;
|
|
||||||
case '0':
|
case '0':
|
||||||
break;
|
return p;
|
||||||
default:
|
default:
|
||||||
return match[0];
|
return c;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
p = null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return mask[c] ? mask[c](p, date, locale, timestamp, options) : c;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function dateToUTC(d) {
|
function dateToUTC(d) {
|
||||||
|
|
@ -299,10 +308,6 @@ function strftime(fmt, d, locale) {
|
||||||
return _strftime(fmt, d, locale);
|
return _strftime(fmt, d, locale);
|
||||||
}
|
}
|
||||||
|
|
||||||
function isDate(date) {
|
|
||||||
return toString.call(date) === '[object Date]';
|
|
||||||
}
|
|
||||||
|
|
||||||
// ISO 8601 format timezone string, [-+]HHMM
|
// ISO 8601 format timezone string, [-+]HHMM
|
||||||
// Convert to the number of minutes and it'll be applied to the date below.
|
// Convert to the number of minutes and it'll be applied to the date below.
|
||||||
function fixTZ(date, opt) {
|
function fixTZ(date, opt) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue