-
-
Notifications
You must be signed in to change notification settings - Fork 976
-
-
Notifications
You must be signed in to change notification settings - Fork 976
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
Dates with formats starting with a leading '+' sign before the year part are getting parsed improperly #86
Comments
I looked through the code to see what might be happening. I think this is happening because of the If you think that is the right approach to fix this, I would love to submit this fix as a pull request. I have verified that all existing test cases pass successfully after this fix. Let me know if I should add any test cases. I have a small test class which I have used to test the fix:
Here are the outputs:
After the fix:
I can add these tests to the proper test class if you can point me to the right place to add them! |
You should probably add tests as JUnit test cases in https://github.com/JodaOrg/joda-time/blob/master/src/test/java/org/joda/time/format/TestDateTimeFormatterBuilder.java You should add four test methods for each of these cases. It would appear your approach is to detect the presence of a "+" and skip it. Personally, this would not match my expectations and lead to my own surprised results: namely, I specify a specific format (e.g. "yyyyMMdd"), then inputs that are "like my format" (e.g. "+20130101") are accepted. I would expect any deviation from the prescribed format to cause an exception. Though that does make the existing behavior strange. |
There was a change to parseInt at one point to either accept or reject '+', I can't remember which. Its possible that this is to do that. Whether this can be adopted depends on whether it breaks other assumptions users of Joda-Time may have made. The correct use of '+' is only when the year exceeds 4 digits. |
Even I did not find the acceptance of I did verify that parseInt would break if we keep the There is definitely a bug, because |
The ISO standard only uses + when outputting 5 or more characters. That is almost certainly why it parses 5 characters on input. |
Was this never fixed because this isn't considered a bug? I notice this works fine: /**
* https://github.com/JodaOrg/joda-time/issues/86
*/
public void correctly_parses_plus_in_front_of_year() {
// Given: a date that includes + per the spec of allowing it for 5 digit years
//final String pattern = "yyyyMMdd";
//final String dateTime = "+20130109";
final String dateTime = "+2013-01-09";
// When: I try to parse this date
//final DateTime result = DateTimeFormat.forPattern(pattern).parseDateTime(dateTime);
final DateTime result = ISODateTimeFormat.dateTimeParser().parseDateTime(dateTime);
// Then: I get back the correct year
assertEquals(2013, result.getYear());
// And: I get back the correct month
assertEquals(1, result.getMonthOfYear());
// And: I get back the correct day
assertEquals(9, result.getDayOfMonth());
} It does seem strange to me to explicitly specify a pattern like Why someone would manually specify that as a pattern instead of using Alternately, for semantic clarity, I would expect the ISO definition of a year (with the strange +/- Y business https://en.wikipedia.org/wiki/ISO_8601#Years) to only be applied in that case. A pattern for a given input format would be applied strictly, in which case any non-numeric characters in a string expected to conform to What is correct behavior here? |
Change made based on Hari Shankar's code. |
I stumbled upon an issue where dates with leading '+' signs in front of year part were getting parsed improperly. I have described the issue in stackoverflow (http://stackoverflow.com/q/19910018/373151). I'll paste the code again here:
The year is getting parsed as 20130 instead of 2013 in cases 2 and 3.
The text was updated successfully, but these errors were encountered: