From 2d0fdf6eb18eafde50830b8109f42e20b82247b1 Mon Sep 17 00:00:00 2001 From: Pierrick Koch Date: Thu, 15 Sep 2016 15:23:48 +0200 Subject: [PATCH] Dygraph.dateString_: shows milliseconds if any. (#774) * [utils] dateString_ display Milliseconds if any * [utils] fix dateAxisLabelFormatter milliseconds * [util] dateString_: compute ms in frac * [utils] clean hmsString_ format * [utils] add milliseconds padding in hmsString_ * [develop] add note on npm install * [tests] add labelsDateMilliseconds test * [utils] add millis in dateAxisLabelFormatter * [tests] fix requested changes in labelsDateMillis * [auto_tests] add date_formats testMillisecondsDate --- DEVELOP.md | 4 +++ auto_tests/tests/date_formats.js | 8 ++++++ src/dygraph-utils.js | 15 +++++++---- tests/labelsDateMilliseconds.html | 56 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 tests/labelsDateMilliseconds.html diff --git a/DEVELOP.md b/DEVELOP.md index 9a13888..6eb43fb 100644 --- a/DEVELOP.md +++ b/DEVELOP.md @@ -6,6 +6,10 @@ This is a step-by-step guide explaining how to do it. ### How-to +To install dependencies, run + + npm install + To build dygraphs, run npm run build diff --git a/auto_tests/tests/date_formats.js b/auto_tests/tests/date_formats.js index f157b8d..5148fe7 100644 --- a/auto_tests/tests/date_formats.js +++ b/auto_tests/tests/date_formats.js @@ -36,4 +36,12 @@ it('testHyphenatedDate', function() { assert.equal(Date.UTC(2000, 1, 2), utils.dateParser(str)); }); +it('testMillisecondsDate', function() { + // Format: YYYY-MM-DD HH:MM:SS.MS + + // Midnight February 2, 2000 14:25:42.123 UTC + var ts = Date.UTC(2000, 1, 2, 14, 25, 42, 123); + assert.equal("2000/02/02 14:25:42.123", utils.dateString_(ts, true)); +}); + }); diff --git a/src/dygraph-utils.js b/src/dygraph-utils.js index 3a3e637..79215d4 100644 --- a/src/dygraph-utils.js +++ b/src/dygraph-utils.js @@ -362,10 +362,14 @@ export var DateAccessorsUTC = { * @return {string} A time of the form "HH:MM" or "HH:MM:SS" * @private */ -export function hmsString_(hh, mm, ss) { +export function hmsString_(hh, mm, ss, ms) { var ret = zeropad(hh) + ":" + zeropad(mm); if (ss) { ret += ":" + zeropad(ss); + if (ms) { + var str = "" + ms; + ret += "." + ('000'+str).substring(str.length); + } } return ret; }; @@ -387,16 +391,17 @@ export function dateString_(time, utc) { var hh = accessors.getHours(date); var mm = accessors.getMinutes(date); var ss = accessors.getSeconds(date); + var ms = accessors.getMilliseconds(date); // Get a year string: var year = "" + y; // Get a 0 padded month string var month = zeropad(m + 1); //months are 0-offset, sigh // Get a 0 padded day string var day = zeropad(d); - var frac = hh * 3600 + mm * 60 + ss; + var frac = hh * 3600 + mm * 60 + ss + 1e-3 * ms; var ret = year + "/" + month + "/" + day; if (frac) { - ret += " " + hmsString_(hh, mm, ss); + ret += " " + hmsString_(hh, mm, ss, ms); } return ret; }; @@ -1213,7 +1218,7 @@ export function dateAxisLabelFormatter(date, granularity, opts) { hours = accessors.getHours(date), mins = accessors.getMinutes(date), secs = accessors.getSeconds(date), - millis = accessors.getSeconds(date); + millis = accessors.getMilliseconds(date); if (granularity >= DygraphTickers.Granularity.DECADAL) { return '' + year; @@ -1225,7 +1230,7 @@ export function dateAxisLabelFormatter(date, granularity, opts) { // e.g. '21 Jan' (%d%b) return zeropad(day) + ' ' + SHORT_MONTH_NAMES_[month]; } else { - return hmsString_(hours, mins, secs); + return hmsString_(hours, mins, secs, millis); } } }; diff --git a/tests/labelsDateMilliseconds.html b/tests/labelsDateMilliseconds.html new file mode 100644 index 0000000..332e8c4 --- /dev/null +++ b/tests/labelsDateMilliseconds.html @@ -0,0 +1,56 @@ + + + + Milliseconds date labels + + + + + +

Milliseconds display in date and time labels

+ +

This shows how milliseconds are displayed when present.

+ +
+
+ +

You can check it by hovering over corresponding points and comparing + the value labels.

+ + + + + -- 2.7.4