Add negative zero support.

This commit is contained in:
Jan Weiß 2021-05-06 14:06:27 +02:00
parent 8dd0315aba
commit 1e6aa2975f
3 changed files with 15 additions and 7 deletions

View file

@ -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";

View file

@ -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",

View file

@ -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];