-
Notifications
You must be signed in to change notification settings - Fork 730
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
fromISO too loose / fromFormat restrictive #1628
Comments
The problem with
The reason Luxon does not allow loose parsing is because it is error prone and buggy. What is 11/04/2015? Is that April 11th or November 4th? It's impossible to know without knowing the format. |
Thank you pretty much for this well-founded answer! And from this library's perspective I totally get the decision! But can you maybe give me a recommendation how to solve the following use-case: I want a (localized) user-input to be loosely parsed. So if a German user enters Does Luxon offer a method to get the Maybe Luxon could support this, too? But with no |
Yes:
That should be very easy to add. Ultimately all parsing methods end up in You should already be able to achieve this now with this (admittedly quite awful) hack: const referenceDate = DateTime.whatever(...);
const previousNow = Settings.now;
let parsed;
try {
Settings.now = () => referenceDate;
parsed = DateTime.fromFormat(...); // or other parsing methods
} finally {
Settings.now = previousNow;
} I am not sure how I feel about the "loose" parsing, because it sounds quite error prone and could lead to parsing stuff the wrong way. |
If everything goes down to I know this library is pretty much about size so you want to keep it as clean as possible. But guess that'd be a good improvement. Next I'd be thinking about supporting And last but not least ... I'd come back to my initial issue: don't you think the lib might offer a Sure this could be achieved by the solution consuming your lib. But wouldn't this make totally sense to be part of Luxon (like it was of moment)? See this practical example: Here you see the Now check the Luxon's |
Could really use a loose paring now! What du you guys think? Push this request a bit? |
What does this number represent? Seconds since the epoch? Milliseconds? Nanoseconds? It is not clear and therefor I do not think this is a good idea.
I do not know. This library is not part of Luxon. You'll have to report this issue there. I still do not understand 100% how you want this "loose" parsing to work. You can't just remove tokens from the format until you get some result out and expect it to somehow be sensible. Just for example: |
Just seen that you've also checked my PR for the refDate - thank you pretty much! 👍 Concerning the loose parse-method: I'd expect it to take the resolved token (i.e. 10/10 : M/d/yyyy --> false Sure it could step on invalid tokens or tokens expecting a alphanumeric input. But it's pretty unlikely they could fit the input. And hey, it is loose! So don't expect it to be perfect. It just tries to guess human input. Guess that's fair. Imho this is pretty easy implemented and a true added value! But if you don't like it, let's get my PR on the road so I could at least implement a custom parser using this logic. |
Personally, I don't think this is something that should be in Luxon, because this is based on your specific needs. Someone else might have other loose parsing needs. In my opinion something like this should live in application code. |
See your point but still the logic is pretty small and easily described. And I guess any other loose parsing would be use-case specific, so no need for other build-in ones. But yes, sure it could totally even be part of application code! No hard feeling here. I'm just into |
@diesieben07 now that I'm implementing my own parser, I'm noticing a behavior I didn't expect this way (and that makes my loose-parse-suggestion worthless, anyway): I'm trying to parse a pretty common input Is there a helper-method or similar in Luxon for this? Guess even the Or is it actually the application's job to parse the input first, format it as expected and than use Luxon's |
DateTime.fromFormat('20', 'yy').toISODate(); // 2020-01-01
DateTime.fromFormat('62', 'yy').toISODate(); // 1962-01-01 The "cutoff year" is 1960 currently, but can be configured using |
Thank you pretty much, that'll do the job :) Guess there actually is no use-case to build-in support one digit year to behave the same. So this case would be incrementing with leading zero in app-logic I guess? |
I have never seen or heard of anyone entering the year "2005" as "5". If you really need to support this then yes, you'll have to prepare the input manually. |
Don't get me wrong, but I don't like the design of the |
Sure, a PR that adds a |
I'm just wondering why
fromISO
accepts pretty much every input whilefromFormat
need to match 100%:(https://stackblitz.com/edit/vitejs-vite-3v4qvn)
I'm asking because (using Angular) I switched to Luxon and their implementation of the Luxon DateAdapter feels pretty broken: https://github.com/angular/components/blob/main/src/material-luxon-adapter/adapter/luxon-date-adapter.ts
But in the end it goes down to that question: why is
fromISO
so loose /fromFormat
so restrictive? Is there an equivalent to old moments'strict: false
? To would I need to loop over several input formats (from strict to loose) until the first one matches?date-fns
solved it quite handy: a default date can be passed to fill the missing positions.The text was updated successfully, but these errors were encountered: