Skip to content
Permalink
Browse files

Added debugging

  • Loading branch information...
davidmerfield committed Mar 13, 2019
1 parent 23ec763 commit e91b6f42373074ed9ed28daefe39e4eda963389e
Showing with 40 additions and 21 deletions.
  1. +40 −21 app/build/prepare/dateStamp.js
@@ -1,33 +1,45 @@
var helper = require('helper');
var helper = require("helper");
var ensure = helper.ensure;
var debug = require("debug")("blot:build:dateStamp");

var dateFromFileName = helper.dateFromFileName;
var parseDate = helper.parseDate;
var type = helper.type;

var moment = require('moment');
require('moment-timezone');
var moment = require("moment");
require("moment-timezone");

module.exports = function (blog, path, metadata) {
module.exports = function(blog, path, metadata) {
ensure(blog, "object")
.and(path, "string")
.and(metadata, "object");

ensure(blog, 'object')
.and(path, 'string')
.and(metadata, 'object');
debug(
"Blog:",
blog.id,
"dateFormat:",
dateFormat,
"timeZone",
timeZone,
path
);

// Now we deal with a custom date!
var dateFormat = blog.dateFormat;
var timeZone = blog.timeZone;

var date = metadata.date || '';
var date = metadata.date || "";
var dateStamp = metadata.dateStamp || undefined;

ensure(dateFormat, 'string')
.and(timeZone, 'string');
debug("Blog:", blog.id, "dateStamp #1", dateStamp);

ensure(dateFormat, "string").and(timeZone, "string");

// The user specified a date stamp
// directly. Try to turn it into an integer
if (dateStamp !== undefined)
dateStamp = validate(parseInt(dateStamp));
if (dateStamp !== undefined) dateStamp = validate(parseInt(dateStamp));

debug("Blog:", blog.id, "dateStamp #2", dateStamp);

// Return early since we have a date stamp
if (dateStamp) return dateStamp;
@@ -38,13 +50,17 @@ module.exports = function (blog, path, metadata) {
if (date && dateStamp === undefined)
dateStamp = validate(parseDate(date, dateFormat));

debug("Blog:", blog.id, "dateStamp #3", dateStamp);

// The user didn't specify a valid
// date in the entry's metadata. Try
// and extract one from the file's path
if (dateStamp === undefined) {
dateStamp = validate(dateFromFileName(path).created);
}

debug("Blog:", blog.id, "dateStamp #4", dateStamp);

// If there is a date string specified as
// part of this post's metadata, try to parse
// a timestamp from this. We need to relativize
@@ -54,23 +70,26 @@ module.exports = function (blog, path, metadata) {
if (dateStamp !== undefined)
dateStamp = validate(adjust(timeZone, dateStamp));

debug("Blog:", blog.id, "dateStamp #5", dateStamp);

return dateStamp;
};

function validate (stamp) {

if (type(stamp, 'number') && !isNaN(stamp) && moment.utc(stamp).isValid()) return stamp;
function validate(stamp) {
if (type(stamp, "number") && !isNaN(stamp) && moment.utc(stamp).isValid())
return stamp;

return undefined;
}

function adjust (timeZone, stamp) {

ensure(timeZone, 'string')
.and(stamp, 'number');
function adjust(timeZone, stamp) {
ensure(timeZone, "string").and(stamp, "number");

var zone = moment.tz.zone(timeZone);
var offset = zone.offset(stamp);

return moment.utc(stamp).add(offset, 'minutes').valueOf();
}
return moment
.utc(stamp)
.add(offset, "minutes")
.valueOf();
}

0 comments on commit e91b6f4

Please sign in to comment.
You can’t perform that action at this time.