diff --git a/lib/index.js b/lib/index.js index 2ad40a43..b0380b0f 100755 --- a/lib/index.js +++ b/lib/index.js @@ -3,7 +3,7 @@ var mime = require('mime-types'); var xml = require('xml'); var fs = require('fs'); - +var moment = require('moment-timezone'); function ifTruePush(bool, array, data) { if (bool) { @@ -28,7 +28,21 @@ function getSize(filename) { return fs.statSync(filename).size; } -function generateXML (data){ +function formatDate(dateStr, format, timeZone) { + var date = new Date(dateStr); + if (format && timeZone) { + return moment(date).tz(timeZone).format(format); + } + else if (format) { + return moment(date).utc().format(format); + } + else { + return new Date(date).toUTCString(); + } +} + +function generateXML (data, options){ + options = options || {}; var channel = []; channel.push({ title: { _cdata: data.title } }); @@ -39,11 +53,11 @@ function generateXML (data){ channel.push({ image: [ {url: data.image_url}, {title: data.title}, {link: data.site_url} ] }); } channel.push({ generator: data.generator }); - channel.push({ lastBuildDate: new Date().toUTCString() }); + channel.push({ lastBuildDate: formatDate(new Date().toUTCString(), options.dateFormat, options.timeZone) }); ifTruePush(data.feed_url, channel, { 'atom:link': { _attr: { href: data.feed_url, rel: 'self', type: 'application/rss+xml' } } }); ifTruePush(data.author, channel, { 'author': { _cdata: data.author } }); - ifTruePush(data.pubDate, channel, { 'pubDate': new Date(data.pubDate).toGMTString() }); + ifTruePush(data.pubDate, channel, { 'pubDate': formatDate(data.pubDate, options.dateFormat, options.timeZone) }); ifTruePush(data.copyright, channel, { 'copyright': { _cdata: data.copyright } }); ifTruePush(data.language, channel, { 'language': { _cdata: data.language } }); ifTruePush(data.managingEditor, channel, { 'managingEditor': { _cdata: data.managingEditor } }); @@ -73,7 +87,7 @@ function generateXML (data){ }); ifTruePush(item.author || data.author, item_values, { 'dc:creator': { _cdata: item.author || data.author } }); - ifTruePush(item.date, item_values, { pubDate: new Date(item.date).toGMTString() }); + ifTruePush(item.date, item_values, { pubDate: formatDate(item.date, options.dateFormat, options.timeZone) }); //Set GeoRSS to true if lat and long are set data.geoRSS = data.geoRSS || (item.lat && item.long); @@ -180,9 +194,10 @@ function RSS (options, items) { return this; }; - this.xml = function(indent) { + this.xml = function(options) { + options = options || {}; return '' + - xml(generateXML(this), indent); + xml(generateXML(this, options), {indent: options.indent}); }; } diff --git a/package.json b/package.json index 5947e8fd..907cedf4 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "rss", + "name": "jpackard-rss", "version": "1.2.1", "description": "RSS feed generator. Add RSS feeds to any project. Supports enclosures and GeoRSS.", "keywords": [ @@ -57,6 +57,7 @@ }, "dependencies": { "mime-types": "^2.1.7", + "moment-timezone": "^0.5.5", "xml": "^1.0.0" }, "devDependencies": { diff --git a/test/expectedOutput/timeZoneAndFormat.xml b/test/expectedOutput/timeZoneAndFormat.xml new file mode 100644 index 00000000..9e61a607 --- /dev/null +++ b/test/expectedOutput/timeZoneAndFormat.xml @@ -0,0 +1,10 @@ + + + <![CDATA[title]]> + + http://example.com + RSS for Node + Thu, 11 Dec 2014 04:04:57 +0900 + + + \ No newline at end of file diff --git a/test/index.js b/test/index.js index 342ac422..27bc0982 100644 --- a/test/index.js +++ b/test/index.js @@ -218,6 +218,18 @@ test('PubSubHubbub hub', function(t) { t.equal(feed.xml({indent: true}), expectedOutput.pubSubHubbub); }); +test('Timezone and format override', function(t) { + t.plan(1); + + var feed = new RSS({ + title: 'title', + description: 'description', + feed_url: 'http://example.com/rss.xml', + site_url: 'http://example.com' + }); + + t.equal(feed.xml({indent: true, timeZone: 'Asia/Tokyo', dateFormat: 'ddd, DD MMM YYYY HH:mm:ss ZZ'}), expectedOutput.timeZoneAndFormat); +}); test('custom elements', function(t) { t.plan(1);