-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
DateTime.parse should throw an error on invalid date... #11189
Comments
Added Area-Library, Triaged labels. |
For the DateTime constructor we allow values outside the valid range. I'm not yet convinced that we shouldn't do the same for the DateTime parser, but I can get convinced otherwise. |
This comment was originally written by adrian.avil...@gmail.com "For the DateTime constructor we allow values outside the valid range." Why allow invalid ranges, whay not validate them? |
Turns out that it is actually very convenient. Initially we didn't allow them, but backtracked. |
This comment was originally written by adrian.avila.mt...@gmail.com Convenient? in what way, I was bit by this bug and I didn't see any convenience? |
Issue #12514 has been merged into this issue. |
Issue #2771 was the one where we decided to allow over and underflows. |
The convenience is that you can do: I don't think we should allow it in DateTime.parse. Added Accepted label. |
Removed Priority-Unassigned label. |
Added Library-Core label. |
+1 Having the DateTime parse method throw an exception for components which are outside the valid range is useful for parsing external input. For example, when reading a value from a file, JSON or user input. In those cases, invalid components are probably a sign of an error in the input. If DateTime.parse didn't perform such checking, then everyone writing robust code will have to reinvent-the-wheel and write their own parsing code to check the components. It would be much better if the standard parse method implemented the checking (especially for programs which don't bother implementing their own checking and blindly assume input-is-never-wrong). The standard parse method should implement the checking once and implement it correctly. For example, if programmers are expected to write their own date checking code, then most implementations would probably check if the day-of-month is [1-31], but not many would check if 29 is valid for February in that particular year. Allowing overflows might be "convenient" for internal date manipulations in code you wrote (though I would argue the "correct" way to do that is to use Durations), but it is not safe for parsing input you didn't create. If you want to keep the overflow behaviour, have an optional named parameter to allow/disallow overflow. The default should be to prohibit them, since that is a safer mode; but unfortunately that would break existing code that expects overflows to work. So maybe the default is to allow overflow. I would also like to see some optional named parameters to control how much of the DateTime must be in the string for it to be valid. For example, when reading in a timestamp from a log file, you want the parser to fail if only the date (year, month, day) and hour is provided (since a good timestamp should at least specify it down to the minute or second). Also whether the timezone is mandatory or optional, since if omitted it would be ambiguous when reading in a file (especially, one created in a different timezone); and if optional being able to provide a timezone to interpret it in (since the file being read might have been created in a timezone different from the current local timezone). Without such checking in DateTime.parse, developers are either forced to re-implement their own parsing and checking code; or end up creating fragile programs that could accept unexpected/wrong input. |
I agree that The other |
There are some interesting edge cases with that kind of validation. e.g. validating February 18, 2018 by passing in integers when in Brazil. |
alan-knight writes:
I assume you are talking about the general issue of dealing with daylight saving changes. Daylight saving is a separate and complex issue in itself, so let's not try and solve it as a part of this issue - which is only about how The lrhn writes:
Good reasoning. Creating a DateTime by parsing a string should behave differently from the constructors from integer components: they are different operations used in different situations. |
Related external issue: https://stackoverflow.com/questions/57080005/how-to-check-if-a-given-date-exists-in-dart |
Hello, why this bug still not fixed? |
Could we entertain adding a |
I worry about making this strict for local time. The validation would be simple. Parse numbers, create a date, read numbers back out. If the numbers you read are not the same a the ones you put in, the original wasn't valid. So, perhaps we should instead check that the values are in proper ranges:
|
You might want to look at the Intl logic which jumps through a bunch of hoops for these kind of edge cases. I particularly note that |
There's a fun document, might be internal to Google, about things that programmers believe about internationalization that aren't true. |
Use parseStrict. Can anyone close this? |
The [DateTime.parse](https://api.dart.dev/stable/2.10.4/dart-core/DateTime/parse.html) method accepts out-of-range components. For example, parsing the string "2020-01-42" produces "2020-02-11". This was raised as a bug in #11189, but after more than 7 years that issue was closed with the solution being "use parseStrict from the intl package" instead. This pull request simply **updates the documentation of DateTime's _parse_ method** so that users know: 1. This is how the method behaves. Otherwise they might be surprised when it unexpectedly happens in their code. 2. This behaviour is a feature and not a bug. Otherwise they might raise another issue for it. 3. The _parseStrict_ method in the intl package exists. Otherwise they might reinvent the wheel. Closes #44521 #44521 GitOrigin-RevId: a88ada4 Change-Id: Ic45e4db118bd3fad3a612659a35ca7a5d80937b4 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/176680 Reviewed-by: Lasse R.H. Nielsen <lrn@google.com> Commit-Queue: Lasse R.H. Nielsen <lrn@google.com>
This issue was originally filed by adrian.avil...@gmail.com
What steps will reproduce the problem?
3.
What is the expected output? What do you see instead?
Since the say is 47 it should throw an exception.
What version of the product are you using? On what operating system?
Dart Editor version 0.5.13_r23552
Dart SDK version 0.5.13.1_r23552
Windows 8 64bits.
The text was updated successfully, but these errors were encountered: