NSDateComponentsFormatter的allowsFractionalUnits属性用法是否有误?

35

基本上我想要的是只获取表示时间间隔的小时值,而不将其舍入为整数小时(使用NSDateComponentsFormatter进行正确格式化和本地化)。 我不知道我是否误解了NSDateComponentsFormatter.allowsFractionalUnits的用法,但我无法让格式化程序给我一个小数值。 有人能帮我找出我的错误或告诉我我在哪方面误解了吗?

关于allowsFractionalUnits属性的Apple文档:

  

当使用可用单位无法精确表示值时,可以使用分数单位。例如,如果不允许使用分钟,则可以将值“1h 30m”格式化为“1.5h”。

Swift示例代码:

let formatter = NSDateComponentsFormatter()
formatter.unitsStyle = .Abbreviated
formatter.allowedUnits = .Hour
formatter.allowsFractionalUnits = true

let onePointFiveHoursInSeconds = NSTimeInterval(1.5 * 60.0 * 60.0)
print(formatter.stringFromTimeInterval(onePointFiveHoursInSeconds)!)
//"1h" instead of expected "1.5h"

在 Objective-C 代码中的同一示例:

NSDateComponentsFormatter *formatter = [[NSDateComponentsFormatter alloc] init];
formatter.unitsStyle = NSDateComponentsFormatterUnitsStyleAbbreviated;
formatter.allowedUnits = NSCalendarUnitHour;
formatter.allowsFractionalUnits = YES;

NSTimeInterval onePointFiveHoursInSeconds = 1.5 * 60.0 * 60.0;
NSLog(@"%@", [formatter stringFromTimeInterval:onePointFiveHoursInSeconds]);
//"1h" instead of expected "1.5h"

更新:我已向苹果报告了这个问题的一个错误(rdar:// 22660145)。


6
有趣的问题。似乎 allowsFractionalUnits 完全不起作用。 - Martin R
我认为这基本上是个坏主意。例如,1.33小时,那是多少?如果格式化给最终用户,这可能会误导他们。我宁愿显示1:20。 - Krešimir Prcela
@Prcela:这是我的简化示例,我使用它的动机在这里有点不相关。由于已经记录了NSDateComponentsFormatter应该支持此功能,因此应该可以获得该输出。 - osanoj
在Xcode 10.2.1中仍然是这种情况(即Swift 5,或者更确切地说是它的Foundation等效物)。 - gmw
看起来在XCode 13.2.1中仍然存在问题。(我不能说我感到惊讶,我甚至无法 muster 能量去失望) - Gavin
我实际上在2019年就收到了来自我的雷达的回应:“不幸的是,此处的修复并不容易,并且其他任务优先级更高。这是我们仍然希望解决的问题,我们将为其打上更高优先级的标记。” - osanoj
3个回答

11

根据Open Radar #32024200的说法:

经过深入挖掘(反汇编Foundation),看起来在-[NSDateComponentsFormatter _stringFromDateComponents:]中的每个对-[_unitFormatter stringFromNumber:]的调用都传递了+[NSNumber numberWithInteger:],它会丢失浮点数据。

你没有做错什么。该标志只是有问题。


2

查看文档,发现它使用了很多有趣的语言(我强调):

当一个值无法使用可用单位精确表示时,可以使用小数单位(可能)。例如,如果不允许使用分钟,则“1小时30分钟”可以格式化为“1.5小时”。

对我来说,文档中的值似乎只有实际可用的值才有意义,但是可能存在某种时间值、格式选项以及日历/语言环境设置的组合,使其起作用。绝对值得就功能性和文档撰写提出反馈。


到目前为止,我的错误报告还没有任何进展,所以如果有人感觉自己可以提交一个雷达,请这样做,因为这可能有助于引起苹果对此问题的关注。 - osanoj
人们会期望,如果1小时30分钟变成了1.5小时,那么8小时30分钟就应该变成8.5小时,但事实并非如此。这个例子也很有趣,因为不能显示1小时30分钟的唯一原因是不允许显示分钟数。就我个人而言,我觉得Swift整个时间/日期库都不怎么样,它的API在我看来肯定是非常不一致的。 - Gavin

-5

据我所理解,您想以12小时制显示时间,对吗? 以下是代码: Swift->

let dateAsString = "20:30"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "HH:mm"
let date = dateFormatter.dateFromString(dateAsString)

dateFormatter.dateFormat = "h:mm a"
let date12 = dateFormatter.stringFromDate(date!)
txtText.text = date12 

2
这与问题有什么关系? - rmaddy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接