From 9c8293da62a3a6cf552653106a30d64a545bbaf5 Mon Sep 17 00:00:00 2001 From: Nathan Vander Wilt Date: Tue, 20 Jun 2017 16:58:39 -0700 Subject: [PATCH] fix by_date view by symlinking in library to views --- views/by_date/map.js | 5 +---- views/lib/date.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 views/lib/date.js diff --git a/views/by_date/map.js b/views/by_date/map.js index c5140f4..67e2e09 100644 --- a/views/by_date/map.js +++ b/views/by_date/map.js @@ -2,9 +2,6 @@ function(doc) { if (doc.type != "http://stemstorage.net/glob/post") return; if (!doc.published) return; - var exports = {}; - // !code lib/date.js - var date = exports; - + var date = require("views/lib/date"); emit(date.toUTCComponents(date.newDate(doc.published))); } diff --git a/views/lib/date.js b/views/lib/date.js new file mode 100644 index 0000000..7ebf295 --- /dev/null +++ b/views/lib/date.js @@ -0,0 +1,33 @@ +/* Simple workaround for older JavaScript engines that + * do not understand the One True Date Format. + * This doesn't totally mimic new Date(), just string parsing. + */ +exports.newDate = function (rfc3339) { + var temp = Date.parse(rfc3339); + if (isNaN(temp)) { + // this technique is borrowed from jquery.couch.app.util's $.prettyDate + temp = rfc3339.replace(/-/g,"/").replace("T", " ").replace("Z", " +0000").replace(/(\d*\:\d*:\d*)\.\d*/g,"$1"); + } + return new Date(temp); +}; + +exports.toUTCComponents = function (date) { + return [ + date.getUTCFullYear(), // 0 + date.getUTCMonth() + 1, // 1 + date.getUTCDate(), // 2 + date.getUTCHours(), // 3 + date.getUTCMinutes(), // 4 + date.getUTCSeconds() + (date.getUTCMilliseconds() / 1000) + ]; +}; + +exports.toRFC3339 = function (date) { + // after https://github.com/couchapp/couchapp/blob/master/vendor/lib/atom.js + + function f(n) { // Format integers to have at least two digits. + return n < 10 ? '0' + n : '' + n; + } + var d = exports.toUTCComponents(date); + return d[0] + '-' + f(d[1]) + '-' + f(d[2]) + 'T' + f(d[3]) + ':' + f(d[4]) + ':' + f(d[5]) + 'Z' +};