Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Microseconds (%f) #24

Closed
ebergmundur opened this issue Oct 6, 2016 · 14 comments
Closed

Microseconds (%f) #24

ebergmundur opened this issue Oct 6, 2016 · 14 comments

Comments

@ebergmundur
Copy link

Hi

I'm having a problem parsing UTC timestamps with 6 digits.
2016-10-06T20:32:56.523411Z

It works just fine with three digits.
I'm working with realtime data so it could be messy and heavy workaround to shorten this before parsing. And changes at the source could be hard to get trough.

@mbostock
Copy link
Member

mbostock commented Oct 6, 2016

Those aren’t milliseconds; they are nanoseconds er, microseconds.

@ebergmundur
Copy link
Author

The %L does not like them any better
It seems that the parser only works for the ECMA 262 but not ISO 8601 format.

@mbostock
Copy link
Member

mbostock commented Oct 6, 2016

There is no directive for parsing nanoseconds. There is only %L, which is for milliseconds.

@ebergmundur
Copy link
Author

That is actually the core of my problem ;-)
I'll see if I can get changes made to the json feed.

@mbostock mbostock changed the title to many digits in a millisecond Parse nanoseconds? Oct 6, 2016
@ebergmundur
Copy link
Author

ebergmundur commented Oct 6, 2016

I found that a little workaround works, since I'm actually not using the fine details of nanoseconds, and it seems not to screw up the Date() creation.

Using double %L in the template actually works.

'%Y-%m-%dT%H:%M:%S.%L%LZ'

But some might need the precision

@mbostock
Copy link
Member

mbostock commented Oct 6, 2016

I would strongly not recommend that approach because it means that the last three digits of the nanoseconds field are interpreted as the milliseconds, rather than the first three digits.

var parseTime = d3.utcParse("%Y-%m-%dT%H:%M:%S.%L%LZ");

parseTime("2016-10-06T20:32:56.523411Z").getUTCMilliseconds(); // 411, not 523

If you know the format is strictly %Y-%m-%dT%H:%M:%S.%LXXXZ, why not just strip the last three nanosecond digits XXX before parsing?

var parseMilliseconds = d3.utcParse("%Y-%m-%dT%H:%M:%S.%LZ"),
    stripNanoseconds = function(s) { return s.slice(0, -4) + "Z"; },
    parseTime = function(s) { return parseMilliseconds(stripNanoseconds(s)); };

parseTime("2016-10-06T20:32:56.523411Z"); // Thu, 06 Oct 2016 20:32:56 GMT

Example.

@ebergmundur
Copy link
Author

I realize that, but for now it works for me since I'm only using minutes as the finest grinder and just working on preliminary stage.
But as you point out it does not deliver the correct data.

As a matter of fact I'm testing the c3 library that of course relies on d3 and d3-time-format, but it does not give me any handle on the data itself and I was looking for a solution that could create simple graph with few lines of code.

Of course I could fetch the json and create suitible dataset but that would take me astray from this mission of simplifying the making of graphs :-)

@mbostock
Copy link
Member

mbostock commented Oct 6, 2016

I see—so you can specify the format but not an arbitrary function for parsing.

A nanosecond microsecond directive would be reasonable so I’ll leave this open as a feature request.

@mbostock mbostock changed the title Parse nanoseconds? Parse microseconds? Oct 6, 2016
@ebergmundur
Copy link
Author

ebergmundur commented Oct 6, 2016

I actually did that previously, parse the data set, but it took some effort on a full set of data, 3 X60X24, or 4.320 entries and made a simple graph too slow.

mbostock added a commit that referenced this issue Oct 6, 2016
@mbostock
Copy link
Member

mbostock commented Oct 6, 2016

The cost of a single additional string.slice to strip the extra digits does not significantly increase the cost of date parsing (which already calls string.slice many times).

@ventsyv
Copy link

ventsyv commented Jun 19, 2017

It appears that this was committed but not merged? Is there a reason for this? I can really use this feature...

@mbostock mbostock changed the title Parse microseconds? Microseconds (%f) Jul 9, 2017
@liugangnhm
Copy link

var d3 = require("d3-time-format")

var parser = d3.timeParse("%Y-%m-%d %H:%M:%S.%f")

var date1 = parser("2017-09-29 19:20:38.888377")
var date2 = parser("2017-09-29 19:20:38.888376")

console.log(date1 > date2, date1 < date2)

the result is

false false

is there any way to compare the two date obj?

@mbostock
Copy link
Member

mbostock commented Nov 6, 2017

@liugangnhm JavaScript Date objects only have millisecond resolution.

@liugangnhm
Copy link

@mbostock all right, I was looking for a javascript module that can parse time string with millisecond and this is the only one i hava found. but it seems that it could only parse .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants