Skip to content

Commit

Permalink
Create date wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Nov 13, 2018
1 parent 864ae9e commit 33f24f1
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 92 deletions.
135 changes: 135 additions & 0 deletions src/core/core.date.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
'use strict';

var moment, luxon, DateTime;
try {
moment = require('moment'); // eslint-disable-line global-require
moment = typeof moment === 'function' ? moment : window.moment;
} catch (mte) {
try {
luxon = require('luxon'); // eslint-disable-line global-require
luxon = (luxon && luxon.DateTime) ? luxon : window.luxon;
DateTime = luxon && luxon.DateTime;
} catch (lxe) {
throw new Error('Chart.js - Neither Moment.js no Luxon could be found! You must include one of these date libraries to use the time scale. For more info, see https://www.chartjs.org/docs/latest/getting-started/installation.html');
}
}

var helpers = require('../helpers/index');

var CDate = function(date) {
this.underlying = date;
this.valid = true;
this.initialize.apply(this, arguments);
};

helpers.extend(CDate.prototype, {

initialize: function(value) {
if (value === undefined) {
value = new Date();
}

if (luxon) {
if (typeof value === 'number') {
this.underlying = DateTime.fromMillis(value);
return;
}
if (value instanceof Date) {
this.underlying = DateTime.fromJSDate(value);
return;
}
if (value instanceof luxon) {
this.underlying = value;
return;
}
this.valid = false;
return
}

if (!(value instanceof moment)) {
value = moment(value);
}

this.underlying = value;
},

/**
* @private
*/
_underlying: function() {
return luxon ? this.underlying : this.underlying.clone();
},

isValid: function() {
return this.isValid && (luxon ? this.underlying.isValid : this.underlying.isValid());
},

millisecond: function() {
return luxon ? this.underlying.millisecond : this.underlying.millisecond();
},

second: function() {
return luxon ? this.underlying.second : this.underlying.second();
},

minute: function() {
return luxon ? this.underlying.minute : this.underlying.minute();
},

hour: function() {
return luxon ? this.underlying.hour : this.underlying.hour();
},

isoWeekday: function(value) {
return this._underlying().isoWeekday(value);
},

startOf: function(value) {
return this._underlying().startOf(value);
},

endOf: function(value) {
return this._underlying().endOf(value);
},

valueOf: function() {
return this.underlying.valueOf();
},

toFormat: function(format) {
return luxon ? this.underlying.toFormat(format) : this.underlying.format(format);
},

add: function(value, type) {
if (luxon) {
var arg = {};
arg[type] = value;
this.underlying.plus(arg);
}
return this._underlying().add(value, type);
},

diff: function(other, unit) {
other = new CDate(other).underlying;
var duration = luxon ? this.underlying.diff(other)
: moment.duration(this.underlying.diff(other));
return duration.as(unit);
}

});

module.exports = {
CDate: CDate,
moment: moment,
defaultDisplayFormats: {
millisecond: 'h:mm:ss.SSS a', // 11:20:01.123 AM,
second: 'h:mm:ss a', // 11:20:01 AM
minute: 'h:mm a', // 11:20 AM
hour: luxon ? 'ha' : 'hA', // 5PM
day: luxon ? 'MMM d' : 'MMM D', // Sep 4
week: luxon ? 'WW' : 'll', // Week 46, or maybe "[W]WW - YYYY" ?
month: luxon ? 'MMM yyyy' : 'MMM YYYY', // Sept 2015
quarter: luxon ? '\'Q\'q - yyyy' : '[Q]Q - YYYY', // Q3 - 2015
year: luxon ? 'yyyy' : 'YYYY' // 2015
}
};
Loading

0 comments on commit 33f24f1

Please sign in to comment.