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
Can js-joda parse "UTC strings" out of the box? #549
Comments
Not out of the box, but you can define a parser for it. see code below.
Does this answer your question ? |
@pithu It kind of does... But at the same time, it shows why it would be beneficial for the standard format like "UTC String" / RFC 7321 to be included in the library itself. Your hand-crafted solution has a bug, exactly like the one I've crafted myself... |
There many, many time format patterns, with often mis-leading names and unclear spec. We have no plans to support any special patterns, beside the ones defined in threeten.
Whats the bug ? |
I won't disagree with the fact that there are many date/time string patterns in the wild! It's true. But at the same time, js-joda is a JavaScript library, not a Java one. It would still, in my opinion, be great to include the counterpart of the I'm personally a great fan of js-joda, as it solves the problem in the way that feels right for me. I'm sad that it's not as popular as Moment / Luxon. I believe that leaning more towards JS, less towards Java, could help a bit! Without sacrificing the fundamental ideas that Java's Joda got right, of course. In the end, the decision will be yours, of course.
I was just editing the answer to include the code 😉 const instant = ZonedDateTime.parse('Tue, 05 Oct 2021 17:08:24 GMT', formatter).toInstant();
instant.toString();
// Expected: "2021-10-05T17:08:24Z"
// Received: "2021-10-05T16:08:24Z" It seems that I guess the correct format is |
Ok, looks like this is a bug, parsing GMT in a Locale context, we are open for a PR to fix this |
Not sure if i got your point right, i agree js-joda is a javascript library, we used to have plans to make js-joda more "javascriptish". But since the temporal proposal gets closer, we see the future of js-joda more as a one to one port of the threeten and java.time API. And yes, the js-joda documentation is more then improvable, so it's hard to find, but the recommended way to convert from native js to js-joda is using the And actually i find the threeten pattern parsing quite powerful and extendable to any use-case (as long as you don't stumble over this terrible GMT convert bug, we just found). Yes, it could be much better documented, true! But IMO much better than to provide a bunch of special implemented custom made parser functions that we would have to attach to each Temporal. How would the API look like ? |
I could narrow down the bug, its located here
The corresponding threeten code is here and it seems that there have been some fixes for that function over the time, that we did not port. https://github.com/ThreeTen/threetenbp/blob/973f2b7120d2c173b0181bde39ce416d1e8edfe0/src/main/java/org/threeten/bp/format/DateTimeFormatterBuilder.java#L3385 |
Oh, I get it. I've heard about that proposal some time ago, but didn't know it's close to being released.
Oh, I did't know that... But still, like I said earlier, ECMAScript doesn't guarantee that
I agree! Still, meany great libraries and programming interfaces I know provide both powerful and extendable mechanisms and built-in ready-to-use solutions for popular problems or standard formats / concepts / schemes. I understand that there's always a cutoff line, though; nobody wants their libraries bloated. In my specific case, even ignoring the GMT bug (I've just learned that this is actual bug, I thought it's just unexpected consequence of these mysterious tz databases, or something like that), it took me a while to build the proper pattern string. Actually, finding the right number of repetitions for these pattern letters made me a bit irritated, if I was to be honest. At the same time, I knew I just wanted to parse the opposite of ECMA-standard
We're going into details now, from my perspective. I don't think such method would be needed for all these types, because RFC7231 date/time string seems to conceptually represent instants (the GMT suffix is obligatory).
So we've went down to one
I don't think it needs timezone database, as the "GMT" suffix is obligatory, and I don't think it actually needs the
Yeah, probably. Well, the format is as outdated as the In my personal opinion, 1. being the mirror of @pithu Thank you very much for all your responses, it was great to see that js-joda has such quick and good support from the authors. As for the RFC7231 string format, everything has been said. I'll leave the decision to your best judgement! If you decide that it would be acceptable to have such custom implementation, but the project has no resources to provide it (i.e. "waiting for PR"), please also note it. From my perspective the issue is closed (as it was stated as a question, which was answered). I see that you use it to track the "GMT bug", so I'll leave eventual closing to you. |
@cubuspl42 just for curiosity. What is your special use case for parsing an http-date RFC7231 string ? |
It's not really special, I use (the mentioned before) Node.js Firebase Admin Auth library for automated management of Firebase users. Why they chose this format, god knows. |
Thanks @cubuspl42 for your input, i will leave the Issue open and use it for the bug. |
I stumbled over the RFC_1123_DATE_TIME implemented in threeten. I managed to port it to @js-joda/locale as a built-in formatter. it will be available in the next locale release. So @js-joda/locale is still required, but it will work with any locale (EN and non EN locales) |
Woah, that's great news! 🥳 Thank you, I'll switch to the built-in formatter as soon as it's available in NPM! |
Just out of curiosity, I've tried to understand this standards / RFC dependency hell... So, if I got this correctly: The ultimate specification for MDN interprets the algorithm specified in ECMA-262 as "based on" RFC 7231 (HTTP/1.1 Semantics and Content). From RFC 7231, chapter 7.1.1.1. Date/Time Formats:
(full format specification follows, including grammar) RFC 5322 (Internet Message Format) states, broadly, that...
From RFC 5322, chapter 3.3. Date and Time Specification:
(full format specification follows, including grammar) RFC 5322 recommends switching to offset-based time zones, but still supports From RFC 1123 (Requirements for Internet Hosts -- Application and Support), chapter 5.2.14 RFC-822 Date and Time Specification: RFC-822 Section 5
(so this RFC just amends RFC 822 when it comes to date formats) From RFC 822 (Standard for ARPA Internet Text Messages), chapter 5.1: (full format specification, supporting time zones: UTC ("UT" back then?) / "GMT" and a few (American) time zones: "EST" / "EDT / "CST" / "CDT" / "MST" / "MDT" / "PST" / "PDT" and military zones, whatever they are/were)
So, it seems that the provided implementation supports:
|
@pithu Did you update the TS type definitions for the new "stock" formatter? The types should be augmented when the locale package is imported, I think. |
Also I'm curious. I think is great to have this new parser/formatter built into the library but this could also be solved using const i = Instant.from(nativeJs(new Date('Tue, 05 Oct 2021 17:08:24 GMT'))); This way you ensure compatibility with whatever algorithm |
As mentioned above, its not recommended to use Date for parsing -> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse. And as its part of the threeten API, i guess its fine to have it, even if it seems to be a kind of old fashion format. |
That's exactly the problem, it's unspecified what algorithm So it boils down to whether you code for a standard (if possible!), or just for a set of implementations that you want to support. It's a broad (and a bit philosophical) issue, but I'm personally on the "standard if possible" side. |
Good point, i will add it |
I did not went through this specs, but yes probably its very close to this. It expects fixed-length format up to the timezone, but actually it would accept any timezone, that is defined in the @js-joda/timezone package, so not only things like GMT, UTC or EST, but even things like Europe/Berlin. To fix that, there is some more work to do in the locale print parser. |
It appears that there is an old date/time representation format, sometimes referred to as "UTC string". It was originally specified in RFC 7321 (HTTP/1.1 Semantics and Content). String in this format are returned by
Date.prototype.toUTCString
.ECMAScript standard does not require
Date.parse
to support this format.(Date Time String Format (20.3.1.15) means ISO 8601-like, e.g. 1995-02-05T00:00)
It seems that, in practice, some popular JavaScript implementations decide to support "UTC string" as one of implementation-specific date formats. It kind of makes sense, as it mirrors
toUTCString
. I haven't found any documentation to support this, though. It can be verified empirically. Unfortunately, depending on such implementation-specific behaviour is not portable or guaranteed to be forward-compatible, especially when the implementation-specific behaviour is undocumented.What is more, MDN recommends to use third-party libraries to parse implementation-specific date formats like "UTC string":
Strings in "UTC string" format are returned at least by Node.js Firebase Admin Auth, but probably many more.
Is there a simple way to parse "UTC strings" using js-joda? I've searched the API and the source code, without success.
The text was updated successfully, but these errors were encountered: