mirror of
https://github.com/samsonjs/strftime.git
synced 2026-04-22 13:35:50 +00:00
add strftimeUTC
This commit is contained in:
parent
de4256a985
commit
200dc2ef5e
1 changed files with 29 additions and 5 deletions
34
lib/index.js
34
lib/index.js
|
|
@ -33,7 +33,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
// locale is an object with the same structure as DefaultLocale
|
// locale is an object with the same structure as DefaultLocale
|
||||||
function strftime(fmt, d, locale) {
|
function _strftime(fmt, d, locale, _useUTC) {
|
||||||
// d and locale are optional so check if d is really the locale
|
// d and locale are optional so check if d is really the locale
|
||||||
if (d && !(d instanceof Date)) {
|
if (d && !(d instanceof Date)) {
|
||||||
locale = d;
|
locale = d;
|
||||||
|
|
@ -43,6 +43,10 @@
|
||||||
locale = locale || DefaultLocale;
|
locale = locale || DefaultLocale;
|
||||||
locale.formats = locale.formats || {}
|
locale.formats = locale.formats || {}
|
||||||
|
|
||||||
|
if (_useUTC) {
|
||||||
|
d = new Date(d.getTime() + ((d.getTimezoneOffset() || 0) * 60000));
|
||||||
|
}
|
||||||
|
|
||||||
// Most of the specifiers supported by C's strftime
|
// Most of the specifiers supported by C's strftime
|
||||||
return fmt.replace(/%(.)/g, function(_, c) {
|
return fmt.replace(/%(.)/g, function(_, c) {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
|
|
@ -79,16 +83,34 @@
|
||||||
var y = String(d.getFullYear());
|
var y = String(d.getFullYear());
|
||||||
return y.slice(y.length - 2);
|
return y.slice(y.length - 2);
|
||||||
case 'Z':
|
case 'Z':
|
||||||
var tz = d.toString().match(/\((\w+)\)/);
|
if (_useUTC) {
|
||||||
return tz && tz[1] || '';
|
return "GMT";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var tz = d.toString().match(/\((\w+)\)/);
|
||||||
|
return tz && tz[1] || '';
|
||||||
|
}
|
||||||
case 'z':
|
case 'z':
|
||||||
var off = d.getTimezoneOffset();
|
if (_useUTC) {
|
||||||
return (off < 0 ? '-' : '+') + pad(off / 60) + pad(off % 60);
|
return "+0000";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var off = d.getTimezoneOffset();
|
||||||
|
return (off < 0 ? '-' : '+') + pad(off / 60) + pad(off % 60);
|
||||||
|
}
|
||||||
default: return c;
|
default: return c;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function strftime(fmt, d, locale) {
|
||||||
|
return _strftime(fmt, d, locale, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
function strftimeUTC(fmt, d, locale) {
|
||||||
|
return _strftime(fmt, d, locale, true);
|
||||||
|
}
|
||||||
|
|
||||||
function getLocalizedStrftime(locale) {
|
function getLocalizedStrftime(locale) {
|
||||||
return function(fmt, d) {
|
return function(fmt, d) {
|
||||||
return strftime(fmt, d, locale);
|
return strftime(fmt, d, locale);
|
||||||
|
|
@ -97,10 +119,12 @@
|
||||||
|
|
||||||
if (typeof exports !== 'undefined') {
|
if (typeof exports !== 'undefined') {
|
||||||
exports.strftime = strftime;
|
exports.strftime = strftime;
|
||||||
|
exports.strftimeUTC = strftimeUTC;
|
||||||
exports.getLocalizedStrftime = getLocalizedStrftime;
|
exports.getLocalizedStrftime = getLocalizedStrftime;
|
||||||
} else {
|
} else {
|
||||||
(function(global) {
|
(function(global) {
|
||||||
global.strftime = strftime;
|
global.strftime = strftime;
|
||||||
|
global.strftimeUTC = strftimeUTC;
|
||||||
global.getLocalizedStrftime = getLocalizedStrftime;
|
global.getLocalizedStrftime = getLocalizedStrftime;
|
||||||
}(this));
|
}(this));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue