mirror of
https://github.com/samsonjs/strftime.git
synced 2026-03-25 09:05:48 +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
|
||||
function strftime(fmt, d, locale) {
|
||||
function _strftime(fmt, d, locale, _useUTC) {
|
||||
// d and locale are optional so check if d is really the locale
|
||||
if (d && !(d instanceof Date)) {
|
||||
locale = d;
|
||||
|
|
@ -43,6 +43,10 @@
|
|||
locale = locale || DefaultLocale;
|
||||
locale.formats = locale.formats || {}
|
||||
|
||||
if (_useUTC) {
|
||||
d = new Date(d.getTime() + ((d.getTimezoneOffset() || 0) * 60000));
|
||||
}
|
||||
|
||||
// Most of the specifiers supported by C's strftime
|
||||
return fmt.replace(/%(.)/g, function(_, c) {
|
||||
switch (c) {
|
||||
|
|
@ -79,16 +83,34 @@
|
|||
var y = String(d.getFullYear());
|
||||
return y.slice(y.length - 2);
|
||||
case 'Z':
|
||||
var tz = d.toString().match(/\((\w+)\)/);
|
||||
return tz && tz[1] || '';
|
||||
if (_useUTC) {
|
||||
return "GMT";
|
||||
}
|
||||
else {
|
||||
var tz = d.toString().match(/\((\w+)\)/);
|
||||
return tz && tz[1] || '';
|
||||
}
|
||||
case 'z':
|
||||
var off = d.getTimezoneOffset();
|
||||
return (off < 0 ? '-' : '+') + pad(off / 60) + pad(off % 60);
|
||||
if (_useUTC) {
|
||||
return "+0000";
|
||||
}
|
||||
else {
|
||||
var off = d.getTimezoneOffset();
|
||||
return (off < 0 ? '-' : '+') + pad(off / 60) + pad(off % 60);
|
||||
}
|
||||
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) {
|
||||
return function(fmt, d) {
|
||||
return strftime(fmt, d, locale);
|
||||
|
|
@ -97,10 +119,12 @@
|
|||
|
||||
if (typeof exports !== 'undefined') {
|
||||
exports.strftime = strftime;
|
||||
exports.strftimeUTC = strftimeUTC;
|
||||
exports.getLocalizedStrftime = getLocalizedStrftime;
|
||||
} else {
|
||||
(function(global) {
|
||||
global.strftime = strftime;
|
||||
global.strftimeUTC = strftimeUTC;
|
||||
global.getLocalizedStrftime = getLocalizedStrftime;
|
||||
}(this));
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue