From 1e6aa2975f0f6507a3d64f3dae29fd148b9a6aaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Wei=C3=9F?= Date: Thu, 6 May 2021 14:06:27 +0200 Subject: [PATCH] Add negative zero support. --- Formatters/SecondsFormatter.m | 8 ++++++-- SecondsFormatterTests/SecondsFormatterTests.m | 3 ++- Window/TimeField.m | 11 +++++++---- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/Formatters/SecondsFormatter.m b/Formatters/SecondsFormatter.m index 7c9036c86..4b7bcf1c6 100644 --- a/Formatters/SecondsFormatter.m +++ b/Formatters/SecondsFormatter.m @@ -34,7 +34,7 @@ if (isnan(floatValue)) { return @"NaN"; } if (isinf(floatValue)) { return @"Inf"; } - BOOL isNegative = floatValue < 0; + BOOL isNegative = signbit(floatValue); int totalSeconds = (int)(isNegative ? -floatValue : floatValue); @@ -191,7 +191,11 @@ const BOOL result = (malformed == NO); if (result && NULL != object) { - *object = [NSNumber numberWithInt:seconds]; + NSTimeInterval timeInterval = (NSTimeInterval)seconds; + // NOTE: The floating point standard has support for negative zero. + // We use that to represent the parsing result without information loss. + if (isNegative && (timeInterval == 0.0)) { timeInterval = -0.0; } + *object = @(timeInterval); } else if(NULL != error) { *error = @"Couldn't convert value to seconds"; diff --git a/SecondsFormatterTests/SecondsFormatterTests.m b/SecondsFormatterTests/SecondsFormatterTests.m index 56e1b5069..108c54180 100644 --- a/SecondsFormatterTests/SecondsFormatterTests.m +++ b/SecondsFormatterTests/SecondsFormatterTests.m @@ -32,7 +32,7 @@ NSDictionary *testsDict = @{ // key: test name, value: test string - //@"Example": @"0:00", + @"Zero": @"0:00", @"One Second": @"0:01", @"One Minute": @"1:00", @"One Hour": @"1:00:00", @@ -62,6 +62,7 @@ NSDictionary *testsDict = @{ // key: test name, value: test string + @"Negative Zero": @"-0:00", @"Negative One Second": @"-0:01", @"Negative One Minute": @"-1:00", @"Negative One Hour": @"-1:00:00", diff --git a/Window/TimeField.m b/Window/TimeField.m index 832414376..277ad90cf 100644 --- a/Window/TimeField.m +++ b/Window/TimeField.m @@ -10,9 +10,9 @@ static NSString *kTimerModeKey = @"timerShowTimeRemaining"; -NSString * timeStringForTimeInterval(NSTimeInterval timeInterval, BOOL enforceMinusSign) { +NSString * timeStringForTimeInterval(NSTimeInterval timeInterval) { const int64_t signed_total_seconds = (int64_t)timeInterval; - const bool need_minus_sign = enforceMinusSign || signed_total_seconds < 0; + const bool need_minus_sign = signbit(timeInterval); const int64_t total_seconds = (need_minus_sign ? -1 : 1) * signed_total_seconds; const int64_t seconds = total_seconds % 60; const int64_t total_minutes = (total_seconds - seconds) / 60; @@ -74,12 +74,15 @@ NSString * timeStringForTimeInterval(NSTimeInterval timeInterval, BOOL enforceMi if (showTimeRemaining == NO) { NSTimeInterval sec = self.currentTime; - text = timeStringForTimeInterval(sec, NO); + text = timeStringForTimeInterval(sec); } else { NSTimeInterval sec = self.currentTime - self.duration; - text = timeStringForTimeInterval(sec, YES); + // NOTE: The floating point standard has support for negative zero. + // We use that to enforce the sign prefix. + if (sec == 0.0) { sec = -0.0; } + text = timeStringForTimeInterval(sec); } NSAttributedString *string = [[NSAttributedString alloc] initWithString:text attributes:fontAttributes];