Merge in SecondsFormatter improvements from Play.

This commit is contained in:
Jan Weiß 2021-05-05 13:14:53 +02:00
parent cb2dd0b75e
commit 50850c6927

View file

@ -24,27 +24,25 @@
- (NSString *) stringForObjectValue:(id)object - (NSString *) stringForObjectValue:(id)object
{ {
NSString *result = nil;
unsigned value;
unsigned days = 0;
unsigned hours = 0;
unsigned minutes = 0;
unsigned seconds = 0;
if (nil == object || NO == [object isKindOfClass:[NSNumber class]]) { if (nil == object || NO == [object isKindOfClass:[NSNumber class]]) {
// Docs state: Returns nil if object is not of the correct class. // Docs state: Returns nil if object is not of the correct class.
return nil; return nil;
} }
float floatValue = [object floatValue]; double floatValue = [object doubleValue];
if (isnan(floatValue)) { return @"NaN"; } if (isnan(floatValue)) { return @"NaN"; }
if (isinf(floatValue)) { return @"Inf"; } if (isinf(floatValue)) { return @"Inf"; }
value = (unsigned)(floatValue); unsigned totalSeconds = (unsigned)floatValue;
seconds = value % 60; unsigned seconds = totalSeconds % 60;
minutes = value / 60; unsigned minutes = totalSeconds / 60;
unsigned hours = 0;
unsigned days = 0;
seconds = totalSeconds % 60;
minutes = totalSeconds / 60;
while(60 <= minutes) { while(60 <= minutes) {
minutes -= 60; minutes -= 60;
@ -56,6 +54,8 @@
++days; ++days;
} }
NSString *result = nil;
if(0 < days) { if(0 < days) {
result = [NSString stringWithFormat:@"%u:%.2u:%.2u:%.2u", days, hours, minutes, seconds]; result = [NSString stringWithFormat:@"%u:%.2u:%.2u:%.2u", days, hours, minutes, seconds];
} }
@ -72,7 +72,7 @@
return result; return result;
} }
- (BOOL) getObjectValue:(id *)object forString:(NSString *)string errorDescription:(NSString **)error - (BOOL)getObjectValue:(out id _Nullable __autoreleasing *)object forString:(NSString *)string errorDescription:(out NSString * _Nullable __autoreleasing *)error
{ {
NSScanner *scanner = nil; NSScanner *scanner = nil;
BOOL result = NO; BOOL result = NO;
@ -106,9 +106,11 @@
- (NSAttributedString *) attributedStringForObjectValue:(id)object withDefaultAttributes:(NSDictionary *)attributes - (NSAttributedString *) attributedStringForObjectValue:(id)object withDefaultAttributes:(NSDictionary *)attributes
{ {
NSAttributedString *result = nil; NSString *stringValue = [self stringForObjectValue:object];
if(nil == stringValue)
return nil;
result = [[NSAttributedString alloc] initWithString:[self stringForObjectValue:object] attributes:attributes]; NSAttributedString *result = [[NSAttributedString alloc] initWithString:stringValue attributes:attributes];
return result; return result;
} }