mirror of
https://github.com/samsonjs/strftime.git
synced 2026-04-27 14:57:37 +00:00
Optimization of inlining
This commit is contained in:
parent
895a3361b0
commit
05700a7ded
1 changed files with 44 additions and 58 deletions
102
strftime.js
102
strftime.js
|
|
@ -43,34 +43,29 @@
|
||||||
// - 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(format, date, locale, options) {
|
||||||
var matches;
|
var m;
|
||||||
var _options = options || {};
|
var o = options || {};
|
||||||
var _locale = locale;
|
var l = locale;
|
||||||
var _date = date;
|
var d = date;
|
||||||
var timestamp;
|
var i = 0;
|
||||||
var index = 0;
|
var r = '';
|
||||||
var result = '';
|
var re = /%([-_0]?)(.)/g;
|
||||||
var regExp = /%([-_0]?)(.)/g;
|
|
||||||
|
|
||||||
// date and locale are optional so check if date is really the locale
|
if (d && !isDate(d)) {
|
||||||
if (_date && !isDate(_date)) {
|
l = d;
|
||||||
_locale = _date;
|
d = null;
|
||||||
_date = undefined;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_date = _date || new Date();
|
l = l || DefaultLocale;
|
||||||
timestamp = _date.getTime();
|
l.formats = l.formats || {};
|
||||||
_locale = _locale || DefaultLocale;
|
d = fixTimeZone(d || new Date(), o);
|
||||||
_locale.formats = _locale.formats || {};
|
|
||||||
|
|
||||||
_date = fixTimeZone(_date, _options);
|
while (m = re.exec(format)) {
|
||||||
|
r += format.substring(i, m.index) + match(m, d, l, d.getTime(), o);
|
||||||
while (matches = regExp.exec(format)) {
|
i = m.index + m[0].length;
|
||||||
result += format.substring(index, matches.index) + processing(matches, _date, _locale, timestamp, _options);
|
|
||||||
index = matches.index + matches[0].length;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
var mask = {
|
var mask = {
|
||||||
|
|
@ -201,38 +196,29 @@
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function processing(match, date, locale, timestamp, options) {
|
// Most of the specifiers supported by C's strftime, and some from Ruby.
|
||||||
var padding = match[1];
|
// Some other syntax extensions from Ruby are supported: %-, %_, and %0
|
||||||
var char = match[2];
|
// to pad with nothing, space, or zero (respectively).
|
||||||
|
function match(match, date, locale, timestamp, options) {
|
||||||
|
var p = match[1];
|
||||||
|
var c = match[2];
|
||||||
|
|
||||||
// Most of the specifiers supported by C's strftime, and some from Ruby.
|
if (p) {
|
||||||
// Some other syntax extensions from Ruby are supported: %-, %_, and %0
|
switch (p) {
|
||||||
// to pad with nothing, space, or zero (respectively).
|
|
||||||
if (padding) {
|
|
||||||
switch (padding) {
|
|
||||||
// omit padding
|
|
||||||
case '-':
|
case '-':
|
||||||
padding = '';
|
p = '';
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// pad with space
|
|
||||||
case '_':
|
case '_':
|
||||||
padding = ' ';
|
p = ' ';
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// pad with zero
|
|
||||||
case '0':
|
case '0':
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// unrecognized, return the format
|
|
||||||
default:
|
default:
|
||||||
return match[0];
|
return match[0];
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
padding = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return mask[char] ? mask[char](padding, date, locale, timestamp, options) : char;
|
return mask[c] ? mask[c](p, date, locale, timestamp, options) : c;
|
||||||
}
|
}
|
||||||
|
|
||||||
function dateToUTC(d) {
|
function dateToUTC(d) {
|
||||||
|
|
@ -241,7 +227,7 @@
|
||||||
|
|
||||||
// Default padding is '0' and default length is 2, both are optional.
|
// Default padding is '0' and default length is 2, both are optional.
|
||||||
function pad(n, padding, length) {
|
function pad(n, padding, length) {
|
||||||
var _padding = padding == null ? '0' : padding;
|
var _padding = padding ? '0' : padding;
|
||||||
var _n = String(n);
|
var _n = String(n);
|
||||||
var _length = length || 2;
|
var _length = length || 2;
|
||||||
|
|
||||||
|
|
@ -317,29 +303,29 @@
|
||||||
|
|
||||||
// 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 fixTimeZone(date, options) {
|
function fixTimeZone(date, opt) {
|
||||||
var _date = date;
|
var d = date;
|
||||||
var timeZone = options.timezone;
|
var tz = opt.timezone;
|
||||||
var tzType = typeof timeZone;
|
var tzType = typeof tz;
|
||||||
|
|
||||||
if (options.utc || tzType === 'number' || tzType === 'string') {
|
if (opt.utc || tzType == 'number' || tzType == 'string') {
|
||||||
_date = dateToUTC(_date);
|
d = dateToUTC(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (timeZone) {
|
if (tz) {
|
||||||
if (tzType === 'string') {
|
if (tzType == 'string') {
|
||||||
var sign = timeZone[0] === '-' ? -1 : 1;
|
var s = tz[0] === '-' ? -1 : 1;
|
||||||
var hours = parseInt(timeZone.slice(1, 3), 10);
|
var h = parseInt(tz.slice(1, 3), 10);
|
||||||
var mins = parseInt(timeZone.slice(3, 5), 10);
|
var m = parseInt(tz.slice(3, 5), 10);
|
||||||
|
|
||||||
timeZone = sign * (60 * hours) + mins;
|
tz = s * (60 * h) + m;
|
||||||
}
|
}
|
||||||
|
|
||||||
_date = new Date(_date.getTime() + (timeZone * 60000));
|
d = new Date(d.getTime() + (tz * 60000));
|
||||||
options.timezone = timeZone;
|
opt.timezone = tz;
|
||||||
}
|
}
|
||||||
|
|
||||||
return _date;
|
return d;
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace.strftime = strftime;
|
namespace.strftime = strftime;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue