Skip to content

Commit

Permalink
Merge 7278da6 into 5c46552
Browse files Browse the repository at this point in the history
  • Loading branch information
borisschapira committed Jun 24, 2015
2 parents 5c46552 + 7278da6 commit 7b39fbd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
25 changes: 18 additions & 7 deletions lib/plugins/helper/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ var moment = require('moment-timezone');
var isMoment = moment.isMoment;
var isDate = require('util').isDate;

function output(date, format, lang, timezone){
function getMoment(date, lang, timezone){
if (date == null) date = moment();
if (!isMoment(date)) date = moment(isDate(date) ? date : new Date(date));

if (lang) date = date.locale(lang);
if (timezone) date = date.tz(timezone);

return date.format(format);
return date;
}

function toISOString(date){
Expand All @@ -28,20 +28,30 @@ function toISOString(date){

function dateHelper(date, format){
/* jshint validthis: true */
var config = this.config;
return output(date, format || config.date_format, getLanguage(this), config.timezone);
var config = this.config,
moment = getMoment(date, getLanguage(this), config.timezone);
return moment.format(format || config.date_format);
}

function fromNowHelper(date){
/* jshint validthis: true */
var config = this.config,
moment = getMoment(date, getLanguage(this), config.timezone);
return moment.fromNow();
}

function timeHelper(date, format){
/* jshint validthis: true */
var config = this.config;
return output(date, format || config.time_format, getLanguage(this), config.timezone);
var config = this.config,
moment = getMoment(date, getLanguage(this), config.timezone);
return moment.format(format || config.time_format);
}

function fullDateHelper(date, format){
/* jshint validthis: true */
if (format){
return output(date, format, getLanguage(this), this.config.timezone);
var moment = getMoment(date, getLanguage(this), this.config.timezone);
return moment.format(format);
} else {
return this.date(date) + ' ' + this.time(date);
}
Expand All @@ -58,6 +68,7 @@ function getLanguage(ctx){
}

exports.date = dateHelper;
exports.date_fromNow = fromNowHelper;
exports.date_xml = toISOString;
exports.time = timeHelper;
exports.full_date = fullDateHelper;
Expand Down
1 change: 1 addition & 0 deletions lib/plugins/helper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = function(ctx){
var date = require('./date');

helper.register('date', date.date);
helper.register('date_fromNow', date.date_fromNow);
helper.register('date_xml', date.date_xml);
helper.register('time', date.time);
helper.register('full_date', date.full_date);
Expand Down
21 changes: 21 additions & 0 deletions test/scripts/helpers/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,27 @@ describe('date', function(){
dateXML(Date.now()).should.eql(moment().toISOString());
});

it('date_fromNow', function(){
var ctx = {
config: hexo.config,
page: {}
};

var dateFromNow = dateHelper.date_fromNow.bind(ctx);

// now
dateFromNow().should.eql(moment().fromNow());

// moment
dateFromNow(moment()).should.eql(moment().fromNow());

// date
dateFromNow(new Date()).should.eql(moment().fromNow());

// number
dateFromNow(Date.now()).should.eql(moment().fromNow());
});

it('time', function(){
var ctx = {
config: hexo.config,
Expand Down

0 comments on commit 7b39fbd

Please sign in to comment.