1

I got a NSDateFormatter initiliased the following way:

NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"HH:mm:ss"];

In my Unit tests I do now check, whether the formatter behaves the correct way for certain invalid input data that does not match "HH:mm:ss". E.g:

NSDate *date = [dateFormatter dateFromString:@"10-31-01"]; // obviously not a time format in HH:mm:ss

I would expect the resulting NSDate to be nil, as the string should not match the dateFormatter's pattern.

In fact, the result is:

Printing description of date:
2000-01-01 09:31:01 +0000

1 hour offset due to the timezone, I guess. But still I don't want the formatter to return a result for an invalid input and would expect "nil":

XCTAssertNil(date);

Same (unexpected) result for the following formats:

@"23-59-59" // (see format above)
@"23-59:59"
@"23:59-59"

The formatter seems to interpret the dash as a colon and is thus able to interpret the string.

Same problem in Swift (see image from short playground implementation).

enter code here

Anybody got an idea, whether I am missing an explicit setting or whether this is expected and especially appreciated behavior?


UPDATED with Bug Report from Apple

Please know that our engineering team has determined that this issue behaves as intended based on the information provided.

It appears that ICU’s udat_parseCalendar() is very lenient and still is able to parse even if the string doesn’t exactly match the format. We understand preferring that the formatter return nil in these cases but (1) there’s no easy way for us to know that the input string doesn’t match the format since ICU allows it and doesn’t throw an error and (2) suddenly returning nil in these cases would almost certainly be a bincompat issue.

--> Will not be fixed, as it does NOT throw an error (it is rather error tolerant) and a change could break working code now.

Lepidopteron
  • 6,056
  • 5
  • 41
  • 53
  • Why 10-31-01 is invalid ? – Oleg Gordiichuk Apr 12 '17 at 09:23
  • Its not in the expected format with colons instead of dashes: "10:31:01" vs. "10-31-01" - whereas I would expect the last date to be Date-Month-Mear. I would expect "10:31:01" to be Hours:Mins:Seconds. – Lepidopteron Apr 12 '17 at 09:25
  • Here problem with time format. Please use this HH from 0 to 23 and hh from 1 to 12 (AM/PM) – Sivajee Battina Apr 12 '17 at 09:25
  • @Sivajee, I am afraid I have to disagree, see the other example with "23:59:59" <-- I do want to have "HH" instead of "hh" – Lepidopteron Apr 12 '17 at 09:27
  • @Martin R: Thank you for the work around approach. As the post is now 2 years old. And I did not explicitly ask for a workaround: Is there (despite from the lenient attribute) a reason for this behavior or is it still open as bug. Then I would file a bug report. – Lepidopteron Apr 12 '17 at 09:47
  • @Lepidopteron: The *answer* is that the date formatter is extremely lenient when parsing, and apparently that did not change in the last 2 years. If you consider that a bug then you should file a report. – Martin R Apr 12 '17 at 10:01
  • Already did so, thank you. – Lepidopteron Apr 12 '17 at 10:52
  • Question is updated with response from the Bugreport, filed at Apple. Will also answer the original question with the above statement from Apple. – Lepidopteron Apr 18 '17 at 13:16

0 Answers0