NSDateComponentsFormatter can return more than one component even if maximumUnitCount == 1

Originator:igor.palaguta
Number:rdar://26354907 Date Originated:19-May-2016
Status:Duplicate of 17803182 Resolved:
Product:iOS SDK Product Version:iOS 9.3.1 (13E238)
Classification:Bug Reproducible:Always
 
Summary:
NSDateComponentsFormatter works incorrectly if difference between dates approx 1 day

Steps to Reproduce:
Add to playground code:

let formatter = NSDateComponentsFormatter()
formatter.unitsStyle = .Short
formatter.maximumUnitCount = 1
formatter.allowedUnits = [.Year, .Month, .Day, .Hour, .Minute, .Second]

let df = NSDateFormatter()
df.dateFormat = "yyyy-MM-dd HH:mm:ss zzz"

let from = df.dateFromString("2016-05-17 12:10:00 UTC")!
let to1 = df.dateFromString("2016-05-18 13:00:32 UTC")!
let timestamp1 = formatter.stringFromDate(from, toDate: to1)!
print(timestamp1) //"1 day, 1 hr" but it is approx 1 day and 50 min

let to2 = df.dateFromString("2016-05-18 13:20:06 UTC")!
let timestamp2 = formatter.stringFromDate(from, toDate: to2)!
print(timestamp2) //"1 day" but it is approx 1 day and 70 min


Expected Results:
For both cases "1 day" should be printed

Actual Results:
We can see two problems here
1) 2 components are returned "1 day, 1 hr", but formatter.maximumUnitCount = 1
2) For greater difference 1 day and 70 min we have "1 day" result, but for smaller 1 day and 50 min difference we have greater result "1 day, 1 hr"
1 day and 70 min > 1 day and 50 min, but "1 day" < "1 day, 1 hr"


Version:
iOS 9.3.1 (13E238)


Configuration:
iPhone 5s

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!