From 200dc2ef5ec579137f58ff71fdcdcf9fde254458 Mon Sep 17 00:00:00 2001 From: Andrew Schaaf Date: Fri, 3 Jun 2011 07:51:43 -0400 Subject: [PATCH] add strftimeUTC --- lib/index.js | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/lib/index.js b/lib/index.js index 3dccd72..4c481e5 100644 --- a/lib/index.js +++ b/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)); }