crystal-lang / crystal

The Crystal Programming Language
https://crystal-lang.org
Apache License 2.0
19.48k stars 1.62k forks source link

ISO 8601 Time precision #7425

Closed vladfaust closed 5 years ago

vladfaust commented 5 years ago

https://en.wikipedia.org/wiki/ISO_8601#Times says that the precision cannot be more than milliseconds, and we got nano instead: https://carc.in/#/r/68k2

"2019-02-13T12:39:21.984607000Z" # Should be 2019-02-13T12:39:21.984Z

Also https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString

Edit:

There is no limit on the number of decimal places for the decimal fraction. However, the number of decimal places needs to be agreed to by the communicating parties

However, I'm having troubles with https://realworld.io specs, they require milliseconds precision. Maybe make it possible to specify the precision as an argument?

straight-shoota commented 5 years ago

The ISO specification states The number of digits in the decimal fraction shall be determined by the interchange parties, dependent upon the application. And even the Wikipedia article doesn't mention any limit to milliseconds. On the contrary, it states there is no limit.

If you need a string representation with less precision, you can simply adjust the time value prior to formatting. I don't think there is a need to make this configurable in the formatter.

asterite commented 5 years ago

I would instead open an issue in https://realworld.io and ask them to support any number of digits in the decimal fraction.

vladfaust commented 5 years ago

Indeed, but different third parties may have different number of decimals. Shouldn't it be better to allow to specify this number upon formatting?

asterite commented 5 years ago

@vladfaust You can use %3N, %6N, etc. You can build the format yourself.

vladfaust commented 5 years ago

Absolutely. I use "%FT%T.%LZ" in the application. But I'm speaking of possible enhancements of Time::Format::ISO_8601_DATE_TIME.format and Time::Format::ISO_8601_TIME.format methods.

asterite commented 5 years ago

Sorry, but we can't keep doing small changes to accommodate every use case on earth, specially if there's already a way to do what you want.