Use ISO string format for timeseries graph

c3 does not support moment but only string with a specific format.
We implement .toString on the moment prototype because form widgets does not
use Sao.Date nor Sao.DateTime. This ensures that calling .toString will always
return the ISO format.
And finally the x field type is retrieved from the model instead of the
attribute which is no more filled since changeset 2fc36d9e359b

issue9539
review296221002
This commit is contained in:
Cédric Krier committed 2020-09-27 12:29:38 +02:00
1 parent b9cb36591e
commit 056e43717e
3 files changed
+49 -13

No files matched your search

+21 -10
View File
@@ -168,6 +168,27 @@ var Sao = {};
Sao.Decimal = Number;
var _moment_to_string = moment.prototype.toString;
moment.prototype.toString = function() {
if (this.isDate) {
return this.format('YYYY-MM-DD');
} else if (this.isDateTime) {
if (this.milliseconds()) {
return this.format('YYYY-MM-DD HH:mm:ss.SSSSSS');
} else {
return this.format('YYYY-MM-DD HH:mm:ss');
}
} else if (this.isTime) {
if (this.milliseconds()) {
return this.format('HH:mm:ss.SSSSSS');
} else {
return this.format('HH:mm:ss');
}
} else {
return _moment_to_string.call(this);
}
};
Sao.Date = function(year, month, day, utc) {
var date;
if (month === undefined) {
@@ -185,9 +206,6 @@ var Sao = {};
date.date(day);
date.set({hour: 0, minute: 0, second: 0, millisecond: 0});
date.isDate = true;
date.toString = function() {
return this.format('YYYY-MM-DD');
};
return date;
};
@@ -234,13 +252,6 @@ var Sao = {};
datetime.milliseconds(millisecond);
}
datetime.isDateTime = true;
datetime.toString = function() {
if (this.milliseconds()) {
return this.format('YYYY-MM-DD HH:mm:ss.SSSSSS');
} else {
return this.format('YYYY-MM-DD HH:mm:ss');
}
};
datetime.local();
return datetime;
};
+7 -3
View File
@@ -98,7 +98,7 @@
var x = record.field_get_client(this.xfield.name);
// c3 does not support moment
if (x && (x.isDate || x.isDateTime)) {
x = x.toDate();
x = x.toString();
}
data.columns[0][index + 1] = x;
this._add_id(x, record.id);
@@ -177,18 +177,21 @@
c3_config.data.x = 'labels';
c3_config.data.onclick = this.action.bind(this);
var type = this.xfield.type;
var type = this.view.screen.model.fields[this.xfield.name]
.description.type;
if ((type == 'date') || (type == 'datetime')) {
var format_func, date_format, time_format;
date_format = Sao.common.date_format(
this.view.screen.context.date_format);
time_format = '%X';
if (type == 'datetime') {
c3_config.data.xFormat = '%Y-%m-%d %H:%M:%S';
format_func = function(dt) {
return Sao.common.format_datetime(date_format,
time_format, moment(dt));
};
} else {
c3_config.data.xFormat = '%Y-%m-%d';
format_func = function(dt) {
return Sao.common.format_date(date_format, moment(dt));
};
@@ -286,7 +289,8 @@
delete config.axis;
delete config.data.x;
var format_func;
var type = this.xfield.type;
var type = this.view.screen.model.fields[this.xfield.name]
.description.type;
if ((type == 'date') || (type == 'datetime')) {
var date_format = Sao.common.date_format(
this.view.screen.context.date_format);
+21
View File
@@ -2918,6 +2918,27 @@
["Active: False", "Active: True"]));
});
QUnit.test('Date.toString', function() {
QUnit.strictEqual(Sao.Date(2020, 8, 11).toString(), '2020-09-11');
});
QUnit.test('DateTime.toString', function() {
QUnit.strictEqual(
Sao.DateTime(2020, 8, 11, 10, 30, 42).toString(),
'2020-09-11 10:30:42');
QUnit.strictEqual(
Sao.DateTime(2020, 8, 11, 10, 30, 42, 123).toString(),
'2020-09-11 10:30:42.123000');
});
QUnit.test('Time.toString', function() {
QUnit.strictEqual(Sao.Time(10, 30, 42).toString(), '10:30:42');
QUnit.strictEqual(
Sao.Time(10, 30, 42, 123).toString(), '10:30:42.123000');
});
QUnit.test('HTML Sanitization', function() {
var examples = [
["Test", "Test"],