diff --git a/public/app/components/kbn.js b/public/app/components/kbn.js
index e1047243d1f..a8bce04292a 100644
--- a/public/app/components/kbn.js
+++ b/public/app/components/kbn.js
@@ -163,172 +163,6 @@ function($, _, moment) {
return info.sec * info.count;
};
- /* This is a simplified version of elasticsearch's date parser */
- kbn.parseDate = function(text) {
- if(_.isDate(text)) {
- return text;
- }
-
- var time;
- var mathString = "";
- var index;
- var parseString;
-
- if (text.substring(0,3) === "now") {
- time = new Date();
- mathString = text.substring(3);
- }
- else if (text.substring(0,5) === 'today') {
- time = new Date();
- time.setHours(0,0,0,0);
- mathString = text.substring(5);
- }
- else {
- index = text.indexOf("||");
- parseString;
- if (index === -1) {
- parseString = text;
- mathString = ""; // nothing else
- } else {
- parseString = text.substring(0, index);
- mathString = text.substring(index + 2);
- }
- // We're going to just require ISO8601 timestamps, k?
- time = new Date(parseString);
- }
-
- if (!mathString.length) {
- return time;
- }
-
- //return [time,parseString,mathString];
- return kbn.parseDateMath(mathString, time);
- };
-
- kbn.getRelativeTimeInfo = function(str) {
- var info = {value: str};
- if (str === 'today') {
- info.text = 'Today';
- info.from = 'today';
- info.to = 'now';
- } else {
- info.text = 'Last ' + str;
- info.from = 'now-'+str;
- info.to = 'now';
- }
- return info;
- };
-
- kbn._timespanRegex = /^(\d+[h,m,M,w,s,H,d])|today$/;
- kbn.isValidTimeSpan = function(str) {
- return kbn._timespanRegex.test(str);
- };
-
- kbn.parseDateMath = function(mathString, time, roundUp) {
- var dateTime = moment(time);
- for (var i = 0; i < mathString.length;) {
- var c = mathString.charAt(i++),
- type,
- num,
- unit;
- if (c === '/') {
- type = 0;
- } else if (c === '+') {
- type = 1;
- } else if (c === '-') {
- type = 2;
- } else {
- return false;
- }
-
- if (isNaN(mathString.charAt(i))) {
- num = 1;
- } else {
- var numFrom = i;
- while (!isNaN(mathString.charAt(i))) {
- i++;
- }
- num = parseInt(mathString.substring(numFrom, i),10);
- }
- if (type === 0) {
- // rounding is only allowed on whole numbers
- if (num !== 1) {
- return false;
- }
- }
- unit = mathString.charAt(i++);
- switch (unit) {
- case 'y':
- if (type === 0) {
- roundUp ? dateTime.endOf('year') : dateTime.startOf('year');
- } else if (type === 1) {
- dateTime.add(num, 'years');
- } else if (type === 2) {
- dateTime.subtract(num, 'years');
- }
- break;
- case 'M':
- if (type === 0) {
- roundUp ? dateTime.endOf('month') : dateTime.startOf('month');
- } else if (type === 1) {
- dateTime.add(num, 'months');
- } else if (type === 2) {
- dateTime.subtract(num, 'months');
- }
- break;
- case 'w':
- if (type === 0) {
- roundUp ? dateTime.endOf('week') : dateTime.startOf('week');
- } else if (type === 1) {
- dateTime.add(num, 'weeks');
- } else if (type === 2) {
- dateTime.subtract(num, 'weeks');
- }
- break;
- case 'd':
- if (type === 0) {
- roundUp ? dateTime.endOf('day') : dateTime.startOf('day');
- } else if (type === 1) {
- dateTime.add(num, 'days');
- } else if (type === 2) {
- dateTime.subtract(num, 'days');
- }
- break;
- case 'h':
- case 'H':
- if (type === 0) {
- roundUp ? dateTime.endOf('hour') : dateTime.startOf('hour');
- } else if (type === 1) {
- dateTime.add(num, 'hours');
- } else if (type === 2) {
- dateTime.subtract(num,'hours');
- }
- break;
- case 'm':
- if (type === 0) {
- roundUp ? dateTime.endOf('minute') : dateTime.startOf('minute');
- } else if (type === 1) {
- dateTime.add(num, 'minutes');
- } else if (type === 2) {
- dateTime.subtract(num, 'minutes');
- }
- break;
- case 's':
- if (type === 0) {
- roundUp ? dateTime.endOf('second') : dateTime.startOf('second');
- } else if (type === 1) {
- dateTime.add(num, 'seconds');
- } else if (type === 2) {
- dateTime.subtract(num, 'seconds');
- }
- break;
- default:
- return false;
- }
- }
- return dateTime.toDate();
- };
-
kbn.query_color_dot = function (color, diameter) {
return '
{
expect(dateMath.parse('now/' + span, true).format(format)).to.eql(now.endOf(span).format(format));
});
});
+ });
+ describe('isValid', () => {
+ it('should return false when invalid date text', () => {
+ expect(dateMath.isValid('asd')).to.be(false);
+ });
+ it('should return true when valid date text', () => {
+ expect(dateMath.isValid('now-1h')).to.be(true);
+ });
+ });
+
+ describe('relative time to date parsing', function() {
+ it('should handle negative time', function() {
+ var date = dateMath.parseDateMath('-2d', moment([2014, 1, 5]));
+ expect(date.valueOf()).to.equal(moment([2014, 1, 3]).valueOf());
+ });
+
+ it('should handle multiple math expressions', function() {
+ var date = dateMath.parseDateMath('-2d-6h', moment([2014, 1, 5]));
+ expect(date.valueOf()).to.equal(moment([2014, 1, 2, 18]).valueOf());
+ });
+
+ it('should return false when invalid expression', function() {
+ var date = dateMath.parseDateMath('2', moment([2014, 1, 5]));
+ expect(date).to.equal(undefined);
+ });
});
});
diff --git a/public/test/specs/elasticsearch-specs.js b/public/test/specs/elasticsearch-specs.js
index 04427f57401..086413c4861 100644
--- a/public/test/specs/elasticsearch-specs.js
+++ b/public/test/specs/elasticsearch-specs.js
@@ -57,10 +57,8 @@ define([
};
ctx.ds.query({
- range: {
- from: new Date(2015, 4, 30, 10),
- to: new Date(2015, 5, 1, 10)
- },
+ timeFrom: moment(new Date(2015, 4, 30, 10)),
+ timeTo: moment(new Date(2015, 5, 1, 10)),
targets: [{ bucketAggs: [], metrics: [] }]
});
diff --git a/public/test/specs/helpers.js b/public/test/specs/helpers.js
index 00c33ab5631..011c0cc70d6 100644
--- a/public/test/specs/helpers.js
+++ b/public/test/specs/helpers.js
@@ -1,7 +1,8 @@
define([
'kbn',
- 'lodash'
-], function(kbn, _) {
+ 'lodash',
+ 'app/core/utils/datemath',
+], function(kbn, _, dateMath) {
'use strict';
function ControllerTestContext() {
@@ -107,8 +108,8 @@ define([
return this.time;
}
return {
- from : kbn.parseDate(this.time.from),
- to : kbn.parseDate(this.time.to)
+ from : dateMath.parse(this.time.from, false),
+ to : dateMath.parse(this.time.to, true)
};
};
diff --git a/public/test/specs/kbn-format-specs.js b/public/test/specs/kbn-format-specs.js
index e000aae4765..366ab575491 100644
--- a/public/test/specs/kbn-format-specs.js
+++ b/public/test/specs/kbn-format-specs.js
@@ -1,6 +1,7 @@
define([
- 'kbn'
-], function(kbn) {
+ 'kbn',
+ 'app/core/utils/datemath'
+], function(kbn, dateMath) {
'use strict';
function describeValueFormat(desc, value, tickSize, tickDecimals, result) {
@@ -60,60 +61,33 @@ define([
describe('calculateInterval', function() {
it('1h 100 resultion', function() {
- var range = { from: kbn.parseDate('now-1h'), to: kbn.parseDate('now') };
+ var range = { from: dateMath.parse('now-1h'), to: dateMath.parse('now') };
var str = kbn.calculateInterval(range, 100, null);
expect(str).to.be('30s');
});
it('10m 1600 resolution', function() {
- var range = { from: kbn.parseDate('now-10m'), to: kbn.parseDate('now') };
+ var range = { from: dateMath.parse('now-10m'), to: dateMath.parse('now') };
var str = kbn.calculateInterval(range, 1600, null);
expect(str).to.be('100ms');
});
it('fixed user interval', function() {
- var range = { from: kbn.parseDate('now-10m'), to: kbn.parseDate('now') };
+ var range = { from: dateMath.parse('now-10m'), to: dateMath.parse('now') };
var str = kbn.calculateInterval(range, 1600, '10s');
expect(str).to.be('10s');
});
it('short time range and user low limit', function() {
- var range = { from: kbn.parseDate('now-10m'), to: kbn.parseDate('now') };
+ var range = { from: dateMath.parse('now-10m'), to: dateMath.parse('now') };
var str = kbn.calculateInterval(range, 1600, '>10s');
expect(str).to.be('10s');
});
it('large time range and user low limit', function() {
- var range = { from: kbn.parseDate('now-14d'), to: kbn.parseDate('now') };
+ var range = { from: dateMath.parse('now-14d'), to: dateMath.parse('now') };
var str = kbn.calculateInterval(range, 1000, '>10s');
expect(str).to.be('30m');
});
-
});
-
- describe('relative time to date parsing', function() {
- it('should handle negative time', function() {
- var date = kbn.parseDateMath('-2d', new Date(2014,1,5));
- expect(date.getTime()).to.equal(new Date(2014, 1, 3).getTime());
- });
-
- it('should handle today', function() {
- var date = kbn.parseDate('today');
- var today = new Date();
- today.setHours(0,0,0,0);
- expect(date.getTime()).to.equal(today.getTime());
- });
-
- it('should handle multiple math expressions', function() {
- var date = kbn.parseDateMath('-2d-6h', new Date(2014, 1, 5));
- expect(date.toString()).to.equal(new Date(2014, 1, 2, 18).toString());
- });
-
- it('should return false when invalid expression', function() {
- var date = kbn.parseDateMath('2', new Date(2014, 1, 5));
- expect(date).to.equal(false);
- });
-
- });
-
});