mirror of
https://github.com/samsonjs/strftime.git
synced 2026-04-27 14:57:37 +00:00
Refactoring
This commit is contained in:
parent
f3bed1d081
commit
033126760d
1 changed files with 342 additions and 287 deletions
629
strftime.js
629
strftime.js
|
|
@ -9,358 +9,413 @@
|
||||||
// http://sjs.mit-license.org
|
// http://sjs.mit-license.org
|
||||||
//
|
//
|
||||||
|
|
||||||
;(function() {
|
;
|
||||||
|
(function () {
|
||||||
|
//// Where to export the API
|
||||||
|
var namespace;
|
||||||
|
|
||||||
//// Where to export the API
|
try {
|
||||||
var namespace;
|
// CommonJS / Node module
|
||||||
|
namespace = module.exports = strftime;
|
||||||
// CommonJS / Node module
|
} catch (error) {
|
||||||
if (typeof module !== 'undefined') {
|
// Browsers and other environments
|
||||||
namespace = module.exports = strftime;
|
// Get the global object. Works in ES3, ES5, and ES5 strict mode.
|
||||||
}
|
namespace = new Function('return this')();
|
||||||
|
|
||||||
// Browsers and other environments
|
|
||||||
else {
|
|
||||||
// Get the global object. Works in ES3, ES5, and ES5 strict mode.
|
|
||||||
namespace = (function(){ return this || (1,eval)('this') }());
|
|
||||||
}
|
|
||||||
|
|
||||||
function words(s) { return (s || '').split(' '); }
|
|
||||||
|
|
||||||
var DefaultLocale =
|
|
||||||
{ days: words('Sunday Monday Tuesday Wednesday Thursday Friday Saturday')
|
|
||||||
, shortDays: words('Sun Mon Tue Wed Thu Fri Sat')
|
|
||||||
, months: words('January February March April May June July August September October November December')
|
|
||||||
, shortMonths: words('Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec')
|
|
||||||
, AM: 'AM'
|
|
||||||
, PM: 'PM'
|
|
||||||
, am: 'am'
|
|
||||||
, pm: 'pm'
|
|
||||||
};
|
|
||||||
|
|
||||||
namespace.strftime = strftime;
|
|
||||||
function strftime(fmt, d, locale) {
|
|
||||||
return _strftime(fmt, d, locale);
|
|
||||||
}
|
|
||||||
|
|
||||||
// locale is optional
|
|
||||||
namespace.strftimeTZ = strftime.strftimeTZ = strftimeTZ;
|
|
||||||
function strftimeTZ(fmt, d, locale, timezone) {
|
|
||||||
if ((typeof locale == 'number' || typeof locale == 'string') && timezone == null) {
|
|
||||||
timezone = locale;
|
|
||||||
locale = undefined;
|
|
||||||
}
|
}
|
||||||
return _strftime(fmt, d, locale, { timezone: timezone });
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace.strftimeUTC = strftime.strftimeUTC = strftimeUTC;
|
var DefaultLocale = {
|
||||||
function strftimeUTC(fmt, d, locale) {
|
days: 'Sunday Monday Tuesday Wednesday Thursday Friday Saturday'.split(' '),
|
||||||
return _strftime(fmt, d, locale, { utc: true });
|
shortDays: 'Sun Mon Tue Wed Thu Fri Sat'.split(' '),
|
||||||
}
|
months: 'January February March April May June July August September October November December'.split(' '),
|
||||||
|
shortMonths: 'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec'.split(' '),
|
||||||
namespace.localizedStrftime = strftime.localizedStrftime = localizedStrftime;
|
AM: 'AM',
|
||||||
function localizedStrftime(locale) {
|
PM: 'PM',
|
||||||
return function(fmt, d, options) {
|
am: 'am',
|
||||||
return strftime(fmt, d, locale, options);
|
pm: 'pm'
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
|
||||||
// d, locale, and options are optional, but you can't leave
|
var RequiredDateMethods = [
|
||||||
// holes in the argument list. If you pass options you have to pass
|
'getTime',
|
||||||
// in all the preceding args as well.
|
'getTimezoneOffset',
|
||||||
//
|
'getDay',
|
||||||
// options:
|
'getDate',
|
||||||
// - locale [object] an object with the same structure as DefaultLocale
|
'getMonth',
|
||||||
// - timezone [number] timezone offset in minutes from GMT
|
'getFullYear',
|
||||||
function _strftime(fmt, d, locale, options) {
|
'getYear',
|
||||||
options = options || {};
|
'getHours',
|
||||||
|
'getMinutes',
|
||||||
|
'getSeconds'
|
||||||
|
];
|
||||||
|
|
||||||
// d and locale are optional so check if d is really the locale
|
function strftime(fmt, d, locale) {
|
||||||
if (d && !quacksLikeDate(d)) {
|
return _strftime(fmt, d, locale);
|
||||||
locale = d;
|
|
||||||
d = undefined;
|
|
||||||
}
|
|
||||||
d = d || new Date();
|
|
||||||
|
|
||||||
locale = locale || DefaultLocale;
|
|
||||||
locale.formats = locale.formats || {};
|
|
||||||
|
|
||||||
// Hang on to this Unix timestamp because we might mess with it directly below.
|
|
||||||
var timestamp = d.getTime();
|
|
||||||
|
|
||||||
var tz = options.timezone;
|
|
||||||
var tzType = typeof tz;
|
|
||||||
|
|
||||||
if (options.utc || tzType == 'number' || tzType == 'string') {
|
|
||||||
d = dateToUTC(d);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tz) {
|
function strfTimeTZ(fmt, d, locale, timezone) {
|
||||||
// ISO 8601 format timezone string, [-+]HHMM
|
var _locale = locale;
|
||||||
//
|
var _timezone = timezone;
|
||||||
// Convert to the number of minutes and it'll be applied to the date below.
|
|
||||||
if (tzType == 'string') {
|
|
||||||
var sign = tz[0] == '-' ? -1 : 1;
|
|
||||||
var hours = parseInt(tz.slice(1, 3), 10);
|
|
||||||
var mins = parseInt(tz.slice(3, 5), 10);
|
|
||||||
tz = sign * (60 * hours) + mins;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tzType) {
|
if ((typeof locale === 'number' || typeof locale === 'string') && timezone == null) {
|
||||||
d = new Date(d.getTime() + (tz * 60000));
|
_timezone = locale;
|
||||||
}
|
_locale = undefined;
|
||||||
}
|
|
||||||
|
|
||||||
// Most of the specifiers supported by C's strftime, and some from Ruby.
|
|
||||||
// Some other syntax extensions from Ruby are supported: %-, %_, and %0
|
|
||||||
// to pad with nothing, space, or zero (respectively).
|
|
||||||
return fmt.replace(/%([-_0]?.)/g, function(_, c) {
|
|
||||||
var mod, padding;
|
|
||||||
|
|
||||||
if (c.length == 2) {
|
|
||||||
mod = c[0];
|
|
||||||
// omit padding
|
|
||||||
if (mod == '-') {
|
|
||||||
padding = '';
|
|
||||||
}
|
}
|
||||||
// pad with space
|
|
||||||
else if (mod == '_') {
|
return _strftime(fmt, d, _locale, {
|
||||||
padding = ' ';
|
timezone: _timezone
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function strftimeUTC(fmt, d, locale) {
|
||||||
|
return _strftime(fmt, d, locale, {
|
||||||
|
utc: true
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function localizedStrftime(locale) {
|
||||||
|
return function (fmt, d, options) {
|
||||||
|
return _strftime(fmt, d, locale, options);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// d, locale, and options are optional, but you can't leave
|
||||||
|
// holes in the argument list. If you pass options you have to pass
|
||||||
|
// in all the preceding args as well.
|
||||||
|
//
|
||||||
|
// options:
|
||||||
|
// - locale [object] an object with the same structure as DefaultLocale
|
||||||
|
// - timezone [number] timezone offset in minutes from GMT
|
||||||
|
function _strftime(format, d, locale, options) {
|
||||||
|
var _options = options || {};
|
||||||
|
var _locale = locale;
|
||||||
|
var _d = d;
|
||||||
|
|
||||||
|
// d and locale are optional so check if d is really the locale
|
||||||
|
if (_d && !quacksLikeDate(_d)) {
|
||||||
|
_locale = _d;
|
||||||
|
_d = undefined;
|
||||||
}
|
}
|
||||||
// pad with zero
|
|
||||||
else if (mod == '0') {
|
_d = _d || new Date();
|
||||||
padding = '0';
|
|
||||||
|
_locale = _locale || DefaultLocale;
|
||||||
|
_locale.formats = _locale.formats || {};
|
||||||
|
|
||||||
|
// Hang on to this Unix timestamp because we might mess with it directly below.
|
||||||
|
var timestamp = _d.getTime();
|
||||||
|
var tz = _options.timezone;
|
||||||
|
var tzType = typeof tz;
|
||||||
|
|
||||||
|
if (_options.utc || tzType === 'number' || tzType === 'string') {
|
||||||
|
_d = dateToUTC(_d);
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
// unrecognized, return the format
|
if (tz) {
|
||||||
return _;
|
// ISO 8601 format timezone string, [-+]HHMM
|
||||||
|
// Convert to the number of minutes and it'll be applied to the date below.
|
||||||
|
if (tzType === 'string') {
|
||||||
|
var sign = tz[0] === '-' ? -1 : 1;
|
||||||
|
var hours = parseInt(tz.slice(1, 3), 10);
|
||||||
|
var mins = parseInt(tz.slice(3, 5), 10);
|
||||||
|
|
||||||
|
tz = sign * (60 * hours) + mins;
|
||||||
|
}
|
||||||
|
|
||||||
|
_d = new Date(_d.getTime() + (tz * 60000));
|
||||||
}
|
}
|
||||||
c = c[1];
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (c) {
|
// Most of the specifiers supported by C's strftime, and some from Ruby.
|
||||||
|
// Some other syntax extensions from Ruby are supported: %-, %_, and %0
|
||||||
|
// to pad with nothing, space, or zero (respectively).
|
||||||
|
return format.replace(/%([-_0]?.)/g, function (_, c) {
|
||||||
|
var padding;
|
||||||
|
var _c = c;
|
||||||
|
|
||||||
// Examples for new Date(0) in GMT
|
if (_c.length === 2) {
|
||||||
|
switch (_c[0]) {
|
||||||
|
// omit padding
|
||||||
|
case '-':
|
||||||
|
padding = '';
|
||||||
|
break;
|
||||||
|
|
||||||
// 'Thursday'
|
// pad with space
|
||||||
case 'A': return locale.days[d.getDay()];
|
case '_':
|
||||||
|
padding = ' ';
|
||||||
|
break;
|
||||||
|
|
||||||
// 'Thu'
|
// pad with zero
|
||||||
case 'a': return locale.shortDays[d.getDay()];
|
case '0':
|
||||||
|
padding = '0';
|
||||||
|
break;
|
||||||
|
|
||||||
// 'January'
|
// unrecognized, return the format
|
||||||
case 'B': return locale.months[d.getMonth()];
|
default:
|
||||||
|
return _;
|
||||||
|
}
|
||||||
|
|
||||||
// 'Jan'
|
_c = _c[1];
|
||||||
case 'b': return locale.shortMonths[d.getMonth()];
|
}
|
||||||
|
|
||||||
// '19'
|
switch (_c) {
|
||||||
case 'C': return pad(Math.floor(d.getFullYear() / 100), padding);
|
// Examples for new Date(0) in GMT
|
||||||
|
|
||||||
// '01/01/70'
|
// 'Thursday'
|
||||||
case 'D': return _strftime(locale.formats.D || '%m/%d/%y', d, locale);
|
case 'A':
|
||||||
|
return _locale.days[_d.getDay()];
|
||||||
|
|
||||||
// '01'
|
// 'Thu'
|
||||||
case 'd': return pad(d.getDate(), padding);
|
case 'a':
|
||||||
|
return _locale.shortDays[_d.getDay()];
|
||||||
|
|
||||||
// '01'
|
// 'January'
|
||||||
case 'e': return d.getDate();
|
case 'B':
|
||||||
|
return _locale.months[_d.getMonth()];
|
||||||
|
|
||||||
// '1970-01-01'
|
// 'Jan'
|
||||||
case 'F': return _strftime(locale.formats.F || '%Y-%m-%d', d, locale);
|
case 'b':
|
||||||
|
return _locale.shortMonths[_d.getMonth()];
|
||||||
|
|
||||||
// '00'
|
// '19'
|
||||||
case 'H': return pad(d.getHours(), padding);
|
case 'C':
|
||||||
|
return pad(Math.floor(_d.getFullYear() / 100), padding);
|
||||||
|
|
||||||
// 'Jan'
|
// '01/01/70'
|
||||||
case 'h': return locale.shortMonths[d.getMonth()];
|
case 'D':
|
||||||
|
return _strftime(_locale.formats.D || '%m/%d/%y', _d, _locale);
|
||||||
|
|
||||||
// '12'
|
// '01'
|
||||||
case 'I': return pad(hours12(d), padding);
|
case 'd':
|
||||||
|
return pad(_d.getDate(), padding);
|
||||||
|
|
||||||
// '000'
|
// '01'
|
||||||
case 'j':
|
case 'e':
|
||||||
var y = new Date(d.getFullYear(), 0, 1);
|
return _d.getDate();
|
||||||
var day = Math.ceil((d.getTime() - y.getTime()) / (1000 * 60 * 60 * 24));
|
|
||||||
return pad(day, 3);
|
|
||||||
|
|
||||||
// ' 0'
|
// '1970-01-01'
|
||||||
case 'k': return pad(d.getHours(), padding == null ? ' ' : padding);
|
case 'F':
|
||||||
|
return _strftime(_locale.formats.F || '%Y-%m-%d', _d, _locale);
|
||||||
|
|
||||||
// '000'
|
// '00'
|
||||||
case 'L': return pad(Math.floor(timestamp % 1000), 3);
|
case 'H':
|
||||||
|
return pad(_d.getHours(), padding);
|
||||||
|
|
||||||
// '12'
|
// 'Jan'
|
||||||
case 'l': return pad(hours12(d), padding == null ? ' ' : padding);
|
case 'h':
|
||||||
|
return _locale.shortMonths[_d.getMonth()];
|
||||||
|
|
||||||
// '00'
|
// '12'
|
||||||
case 'M': return pad(d.getMinutes(), padding);
|
case 'I':
|
||||||
|
return pad(hours12(_d), padding);
|
||||||
|
|
||||||
// '01'
|
// '000'
|
||||||
case 'm': return pad(d.getMonth() + 1, padding);
|
case 'j':
|
||||||
|
var y = new Date(_d.getFullYear(), 0, 1);
|
||||||
|
var day = Math.ceil((_d.getTime() - y.getTime()) / (1000 * 60 * 60 * 24));
|
||||||
|
return pad(day, 3);
|
||||||
|
|
||||||
// '\n'
|
// ' 0'
|
||||||
case 'n': return '\n';
|
case 'k':
|
||||||
|
return pad(_d.getHours(), padding == null ? ' ' : padding);
|
||||||
|
|
||||||
// '1st'
|
// '000'
|
||||||
case 'o': return String(d.getDate()) + ordinal(d.getDate());
|
case 'L':
|
||||||
|
return pad(Math.floor(timestamp % 1000), 3);
|
||||||
|
|
||||||
// 'am'
|
// '12'
|
||||||
case 'P': return d.getHours() < 12 ? locale.am : locale.pm;
|
case 'l':
|
||||||
|
return pad(hours12(_d), padding == null ? ' ' : padding);
|
||||||
|
|
||||||
// 'AM'
|
// '00'
|
||||||
case 'p': return d.getHours() < 12 ? locale.AM : locale.PM;
|
case 'M':
|
||||||
|
return pad(_d.getMinutes(), padding);
|
||||||
|
|
||||||
// '00:00'
|
// '01'
|
||||||
case 'R': return _strftime(locale.formats.R || '%H:%M', d, locale);
|
case 'm':
|
||||||
|
return pad(_d.getMonth() + 1, padding);
|
||||||
|
|
||||||
// '12:00:00 AM'
|
// '\n'
|
||||||
case 'r': return _strftime(locale.formats.r || '%I:%M:%S %p', d, locale);
|
case 'n':
|
||||||
|
return '\n';
|
||||||
|
|
||||||
// '00'
|
// '1st'
|
||||||
case 'S': return pad(d.getSeconds(), padding);
|
case 'o':
|
||||||
|
return String(_d.getDate()) + ordinal(_d.getDate());
|
||||||
|
|
||||||
// '0'
|
// 'am'
|
||||||
case 's': return Math.floor(timestamp / 1000);
|
case 'P':
|
||||||
|
return _d.getHours() < 12 ? _locale.am : _locale.pm;
|
||||||
|
|
||||||
// '00:00:00'
|
// 'AM'
|
||||||
case 'T': return _strftime(locale.formats.T || '%H:%M:%S', d, locale);
|
case 'p':
|
||||||
|
return _d.getHours() < 12 ? _locale.AM : _locale.PM;
|
||||||
|
|
||||||
// '\t'
|
// '00:00'
|
||||||
case 't': return '\t';
|
case 'R':
|
||||||
|
return _strftime(_locale.formats.R || '%H:%M', _d, _locale);
|
||||||
|
|
||||||
// '00'
|
// '12:00:00 AM'
|
||||||
case 'U': return pad(weekNumber(d, 'sunday'), padding);
|
case 'r':
|
||||||
|
return _strftime(_locale.formats.r || '%I:%M:%S %p', _d, _locale);
|
||||||
|
|
||||||
// '4'
|
// '00'
|
||||||
case 'u':
|
case 'S':
|
||||||
var day = d.getDay();
|
return pad(_d.getSeconds(), padding);
|
||||||
return day == 0 ? 7 : day; // 1 - 7, Monday is first day of the week
|
|
||||||
|
|
||||||
// '1-Jan-1970'
|
// '0'
|
||||||
case 'v': return _strftime(locale.formats.v || '%e-%b-%Y', d, locale);
|
case 's':
|
||||||
|
return Math.floor(timestamp / 1000);
|
||||||
|
|
||||||
// '00'
|
// '00:00:00'
|
||||||
case 'W': return pad(weekNumber(d, 'monday'), padding);
|
case 'T':
|
||||||
|
return _strftime(_locale.formats.T || '%H:%M:%S', _d, _locale);
|
||||||
|
|
||||||
// '4'
|
// '\t'
|
||||||
case 'w': return d.getDay(); // 0 - 6, Sunday is first day of the week
|
case 't':
|
||||||
|
return '\t';
|
||||||
|
|
||||||
// '1970'
|
// '00'
|
||||||
case 'Y': return d.getFullYear();
|
case 'U':
|
||||||
|
return pad(weekNumber(_d, 'sunday'), padding);
|
||||||
|
|
||||||
// '70'
|
// '4'
|
||||||
case 'y':
|
case 'u':
|
||||||
var y = String(d.getFullYear());
|
var day = _d.getDay();
|
||||||
return y.slice(y.length - 2);
|
return day === 0 ? 7 : day; // 1 - 7, Monday is first day of the week
|
||||||
|
|
||||||
// 'GMT'
|
// '1-Jan-1970'
|
||||||
case 'Z':
|
case 'v':
|
||||||
if (options.utc) {
|
return _strftime(_locale.formats.v || '%e-%b-%Y', _d, _locale);
|
||||||
return "GMT";
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
var tzString = d.toString().match(/\((\w+)\)/);
|
|
||||||
return tzString && tzString[1] || '';
|
|
||||||
}
|
|
||||||
|
|
||||||
// '+0000'
|
// '00'
|
||||||
case 'z':
|
case 'W':
|
||||||
if (options.utc) {
|
return pad(weekNumber(_d, 'monday'), padding);
|
||||||
return "+0000";
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
var off = typeof tz == 'number' ? tz : -d.getTimezoneOffset();
|
|
||||||
return (off < 0 ? '-' : '+') + pad(Math.abs(off / 60)) + pad(off % 60);
|
|
||||||
}
|
|
||||||
|
|
||||||
default: return c;
|
// '4'
|
||||||
}
|
case 'w':
|
||||||
});
|
return _d.getDay(); // 0 - 6, Sunday is first day of the week
|
||||||
}
|
|
||||||
|
|
||||||
function dateToUTC(d) {
|
// '1970'
|
||||||
var msDelta = (d.getTimezoneOffset() || 0) * 60000;
|
case 'Y':
|
||||||
return new Date(d.getTime() + msDelta);
|
return _d.getFullYear();
|
||||||
}
|
|
||||||
|
|
||||||
var RequiredDateMethods = ['getTime', 'getTimezoneOffset', 'getDay', 'getDate', 'getMonth', 'getFullYear', 'getYear', 'getHours', 'getMinutes', 'getSeconds'];
|
// '70'
|
||||||
function quacksLikeDate(x) {
|
case 'y':
|
||||||
var i = 0
|
return String(_d.getFullYear()).slice(-2);
|
||||||
, n = RequiredDateMethods.length
|
|
||||||
;
|
|
||||||
for (i = 0; i < n; ++i) {
|
|
||||||
if (typeof x[RequiredDateMethods[i]] != 'function') {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Default padding is '0' and default length is 2, both are optional.
|
// 'GMT'
|
||||||
function pad(n, padding, length) {
|
case 'Z':
|
||||||
// pad(n, <length>)
|
if (_options.utc) {
|
||||||
if (typeof padding === 'number') {
|
return 'GMT';
|
||||||
length = padding;
|
} else {
|
||||||
padding = '0';
|
var tzString = _d.toString().match(/\((\w+)\)/);
|
||||||
|
return tzString && tzString[1] || '';
|
||||||
|
}
|
||||||
|
|
||||||
|
// '+0000'
|
||||||
|
case 'z':
|
||||||
|
if (_options.utc) {
|
||||||
|
return '+0000';
|
||||||
|
} else {
|
||||||
|
var off = typeof tz === 'number' ? tz : -_d.getTimezoneOffset();
|
||||||
|
return (off < 0 ? '-' : '+') + pad(Math.abs(off / 60)) + pad(off % 60);
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
return _c;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Defaults handle pad(n) and pad(n, <padding>)
|
function dateToUTC(d) {
|
||||||
if (padding == null) {
|
var msDelta = (d.getTimezoneOffset() || 0) * 60000;
|
||||||
padding = '0';
|
|
||||||
}
|
|
||||||
length = length || 2;
|
|
||||||
|
|
||||||
var s = String(n);
|
return new Date(d.getTime() + msDelta);
|
||||||
// padding may be an empty string, don't loop forever if it is
|
|
||||||
if (padding) {
|
|
||||||
while (s.length < length) s = padding + s;
|
|
||||||
}
|
}
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
function hours12(d) {
|
function quacksLikeDate(x) {
|
||||||
var hour = d.getHours();
|
var index = RequiredDateMethods.length;
|
||||||
if (hour == 0) hour = 12;
|
|
||||||
else if (hour > 12) hour -= 12;
|
|
||||||
return hour;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the ordinal suffix for a number: st, nd, rd, or th
|
while (index--) {
|
||||||
function ordinal(n) {
|
if (typeof x[RequiredDateMethods[index]] !== 'function') {
|
||||||
var i = n % 10
|
return false;
|
||||||
, ii = n % 100
|
}
|
||||||
;
|
}
|
||||||
if ((ii >= 11 && ii <= 13) || i === 0 || i >= 4) {
|
|
||||||
return 'th';
|
return true;
|
||||||
}
|
}
|
||||||
switch (i) {
|
|
||||||
case 1: return 'st';
|
|
||||||
case 2: return 'nd';
|
|
||||||
case 3: return 'rd';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// firstWeekday: 'sunday' or 'monday', default is 'sunday'
|
// Default padding is '0' and default length is 2, both are optional.
|
||||||
//
|
function pad(n, padding) {
|
||||||
// Pilfered & ported from Ruby's strftime implementation.
|
var _padding = padding == null ? '0' : padding;
|
||||||
function weekNumber(d, firstWeekday) {
|
var _n = String(n);
|
||||||
firstWeekday = firstWeekday || 'sunday';
|
|
||||||
|
|
||||||
// This works by shifting the weekday back by one day if we
|
// padding may be an empty string, don't loop forever if it is
|
||||||
// are treating Monday as the first day of the week.
|
switch (_n.length) {
|
||||||
var wday = d.getDay();
|
case 0:
|
||||||
if (firstWeekday == 'monday') {
|
_n = _padding + _padding;
|
||||||
if (wday == 0) // Sunday
|
break;
|
||||||
wday = 6;
|
case 1:
|
||||||
else
|
_n = _padding + _n;
|
||||||
wday--;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _n;
|
||||||
}
|
}
|
||||||
var firstDayOfYear = new Date(d.getFullYear(), 0, 1)
|
|
||||||
, yday = (d - firstDayOfYear) / 86400000
|
function hours12(d) {
|
||||||
, weekNum = (yday + 7 - wday) / 7
|
var hour = d.getHours();
|
||||||
;
|
|
||||||
return Math.floor(weekNum);
|
if (hour === 0) {
|
||||||
}
|
hour = 12;
|
||||||
|
} else if (hour > 12) {
|
||||||
|
hour -= 12;
|
||||||
|
}
|
||||||
|
|
||||||
|
return hour;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the ordinal suffix for a number: st, nd, rd, or th
|
||||||
|
function ordinal(n) {
|
||||||
|
var i = n % 10;
|
||||||
|
var ii = n % 100;
|
||||||
|
|
||||||
|
if ((ii >= 11 && ii <= 13) || i === 0 || i >= 4) {
|
||||||
|
return 'th';
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (i) {
|
||||||
|
case 1:
|
||||||
|
return 'st';
|
||||||
|
case 2:
|
||||||
|
return 'nd';
|
||||||
|
case 3:
|
||||||
|
return 'rd';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// firstWeekday: 'sunday' or 'monday', default is 'sunday'
|
||||||
|
// Pilfered & ported from Ruby's strftime implementation.
|
||||||
|
function weekNumber(d, firstWeekday) {
|
||||||
|
var firstDayOfYear = new Date(d.getFullYear(), 0, 1);
|
||||||
|
// This works by shifting the weekday back by one day if we
|
||||||
|
// are treating Monday as the first day of the week.
|
||||||
|
var wDay = d.getDay();
|
||||||
|
var yDay = (d - firstDayOfYear) / 86400000;
|
||||||
|
|
||||||
|
if (firstWeekday === 'monday') {
|
||||||
|
if (wDay === 0){
|
||||||
|
// Sunday
|
||||||
|
wDay = 6;
|
||||||
|
} else {
|
||||||
|
wDay--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Math.floor((yDay + 7 - wDay) / 7);
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace.strftime = strftime;
|
||||||
|
namespace.strftimeTZ = strftime.strftimeTZ = strfTimeTZ;
|
||||||
|
namespace.strftimeUTC = strftime.strftimeUTC = strftimeUTC;
|
||||||
|
namespace.localizedStrftime = strftime.localizedStrftime = localizedStrftime;
|
||||||
|
|
||||||
}());
|
}());
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue