简体   繁体   English

我在NSDateComponentsFormatter上使用allowFractionalUnits做错了什么?

[英]What am I doing wrong with allowsFractionalUnits on NSDateComponentsFormatter?

Basically what I want is to get the value of a time interval represented in hours only, without rounding it to full hours (using NSDateComponentsFormatter to get it properly formatted and localized). 基本上我想要的是获得仅以小时为单位表示的时间间隔的值,而不是将其四舍五入到整小时(使用NSDateComponentsFormatter来使其正确格式化和本地化)。 I don't know if I misunderstand the use of NSDateComponentsFormatter.allowsFractionalUnits , but I can't get the formatter to give me a decimal value. 我不知道我是否误解了NSDateComponentsFormatter.allowsFractionalUnits的使用,但我无法让格式化程序给我一个十进制值。 Can anyone help me spot my error or tell me in what way I misunderstand this? 任何人都可以帮助我发现我的错误或告诉我我以何种方式误解了这个?

From Apple docs about allowsFractionalUnits property: 从Apple docs about allowFractionalUnits属性:

Fractional units may be used when a value cannot be exactly represented using the available units. 当使用可用单位无法准确表示值时,可以使用小数单位。 For example, if minutes are not allowed, the value “1h 30m” could be formatted as “1.5h”. 例如,如果不允许分钟,则值“1h 30m”可以格式化为“1.5h”。

Swift example code: 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"

Same example in Objective-C code: 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"

Update: I have reported a bug to Apple about this problem ( rdar://22660145 ). 更新:我已向Apple报告了有关此问题的错误( rdar:// 22660145 )。

According to Open Radar #32024200 : 根据Open Radar#32024200

After doing some digging (disassembling Foundation), it looks like every call to -[_unitFormatter stringFromNumber:] in -[NSDateComponentsFormatter _stringFromDateComponents:] is passed an +[NSNumber numberWithInteger:] which drops floating point data. 在进行了一些挖掘(反汇编基础)后,看起来每次调用 - [NSDateComponentsFormatter _stringFromDateComponents:]中的 - [unitFormatter stringFromNumber:]都会传递一个+ [NSNumber numberWithInteger:],它会丢弃浮点数据。

You're not doing anything wrong. 你没有做错任何事。 The flag is simply broken. 国旗被打破了。

Looking at the documentation , it's using a lot of interesting language (emphasis mine): 看一下文档 ,它使用了很多有趣的语言(强调我的):

Fractional units may be used when a value cannot be exactly represented using the available units. 当的值不能准确地使用可用的单位表示, 可以使用分数单位。 For example, if minutes are not allowed, the value “1h 30m” could be formatted as “1.5h”. 例如,如果不允许分钟,则值“1h 30m” 可以格式化为“1.5h”。

While to me it seems that it would only make sense for the values in the documentation to be values that actually work, it's certainly possible that there is some combination of time value, formatter options, and calendar/locale settings that would make this work. 虽然对我来说,似乎只有文档中的值才有意义才能成为实际工作的值,当然可能存在时间值,格式化程序选项和日历/区域设置的某种组合,这将使这项工作成为可能。 Definitely worth filing a Radar on both the functionality and the documentation. 绝对值得在功能和文档上提交Radar。

enter code hereAs far as i understood , You want to display time in 12-Hour formate Right ? 在这里输入代码据我所知,你想在12小时格式中显示时间对吗? Below is the code : Swift-> 下面是代码: 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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM