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

Nanosecond precision #2022

Closed
codingtony opened this issue Oct 30, 2014 · 16 comments
Closed

Nanosecond precision #2022

codingtony opened this issue Oct 30, 2014 · 16 comments

Comments

@codingtony
Copy link

momentjs should support nanosecond precision (9 digits after the second), at least for parsing and formatting.

The following code should output 2014-07-30 02:59:32.109590000

moment('2014-07-30 02:59:32.10959','YYYY-MM-DD HH:mm:ss.SSSSSSSSS').format('YYYY-MM-DD HH:mm:ss.SSSSSSSSS') 
@ichernev
Copy link
Contributor

There is no Date object that supports nanoseconds. We might have parsing, storage, setting, we might even try to extract from process.hrtime (node) or performance.now (some browsers). We might add this in, not sure how useful it would be.

@icambron
Copy link
Member

👎 Tracking dates with more precision than JS doesn't seem like a good fit for Moment.

@vhmth
Copy link

vhmth commented Jun 22, 2015

Hmm I would not mind bumping this. Since 64-bit numbers are supported and widely used in many backend languages, there may be valid cases where a company who is native-client-focused supplies a timestamp in accordance with RFC3339Nano. Take a look at the godocs for the time package for instance:

const (
        ANSIC       = "Mon Jan _2 15:04:05 2006"
        UnixDate    = "Mon Jan _2 15:04:05 MST 2006"
        RubyDate    = "Mon Jan 02 15:04:05 -0700 2006"
        RFC822      = "02 Jan 06 15:04 MST"
        RFC822Z     = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
        RFC850      = "Monday, 02-Jan-06 15:04:05 MST"
        RFC1123     = "Mon, 02 Jan 2006 15:04:05 MST"
        RFC1123Z    = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
        RFC3339     = "2006-01-02T15:04:05Z07:00"
        RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
        Kitchen     = "3:04PM"
        // Handy time stamps.
        Stamp      = "Jan _2 15:04:05"
        StampMilli = "Jan _2 15:04:05.000"
        StampMicro = "Jan _2 15:04:05.000000"
        StampNano  = "Jan _2 15:04:05.000000000"
)

I definitely think that parsing at the least should be supported. I'm not steadfast on doing the additional padding as a convenience when creating strings, but it would be nice to have.

@mattjohnsonpint
Copy link
Contributor

Parsing is already supported, with any number of decimal places. It's only formatting that is restricted to 3 decimal places. The only bit of weirdness is that it repeats the value in the formatting.

moment('2014-07-30 02:59:32.123456789','YYYY-MM-DD HH:mm:ss.SSSSSSSSS').format('YYYY-MM-DD HH:mm:ss.SSSSSSSSS')
// returns: "2014-07-30 02:59:32.1231231"
// expected: "2014-07-30 02:59:32.1230000"

@mattjohnsonpint
Copy link
Contributor

Logged that issue separately. Closing this one.

@vhmth
Copy link

vhmth commented Jun 23, 2015

👍

@Petah
Copy link

Petah commented Jul 16, 2015

What about duration support?

moment.duration(31536000000000000, 'nanoseconds') // 1 year

@mattjohnsonpint
Copy link
Contributor

I can't imagine what purpose that would serve. I highly doubt anyone working with time on the nanosecond scale cares about calendar concepts like years.

If one wants to deal in nanoseconds, they can just work with numbers.

Besides, a moment-duration's year and month are approximations anyway.

@Petah
Copy link

Petah commented Jul 17, 2015

I care :(

And numbers don't work as 1 year in nanoseconds is > Number.MAX_SAFE_INTEGER

@mattjohnsonpint
Copy link
Contributor

Can you provide more details? What is the use case?

@Petah
Copy link

Petah commented Jul 17, 2015

Basically I am collecting some data that is nanosecond precision, it is kept for more than 1 year. So in my app I want to parse and format duration that can be < 1ms or > 1 year

@icambron
Copy link
Member

I totally get there's a use case for this, but it's just not common enough to put into Moment, especially given how hard it would be. We'd have the same problem internally with not being able to fit the numbers in JS types, too, for example. Moment has a lot of features now, but we still need to be judicious about what we add, even if that excludes some use case.

@Petah
Copy link

Petah commented Jul 17, 2015

Fair enough

@jcgertig
Copy link

There is a date class that supports down to nano seconds https://www.npmjs.com/package/nano-date

@michael-lewis-rft
Copy link

"I totally get there's a use case for this, but it's just not common enough to put into Moment."
That may have been so once, but it is becoming more common.
For example, the Financial industry is moving, as a result of regulatory requirements, to microsecond (and in practice nanosecond) resolution timestamps for describing market events.
Java8 now uses hi-res (nanosecond) timestamps. There's a growing need for something similar in Javascript.

@mattjohnsonpint
Copy link
Contributor

The final word on this is no. Moment will not get support for nanosecond precision.

However, @maggiepint and I are working on a new native date/time API that will indeed support nanoseconds.

https://github.com/tc39/proposal-temporal

See particularly tc39/proposal-temporal#34

@moment moment locked and limited conversation to collaborators Sep 5, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

8 participants