在Obj-C中显示负时间

时间:2015-01-08 07:30:12

标签: ios objective-c nsdate nsdateformatter nstimeinterval

这可能听起来很简单,但我不确定如何做到这一点。如何以下列格式显示时间?:

-00:00:00

我尝试使用两次间隔差异的float和int值,但是在00:00:00格式中都没有给出一致的显示。我也尝试将时差转换为日期,然后显示为String。

这是我用来转换间隔的代码:

NSDate * now = [NSDate date];
NSTimeInterval totalTime1 = [now timeIntervalSinceDate: timeEntry1];
NSTimeInterval totalTime2 = [now timeIntervalSinceDate: timeEntry2];

//must always be this way
int adjustedTime = totalTime1 - totalTime2;

int hours = adjustedTime / 3600;
int minutes = (adjustedTime / 60) % 60;
int seconds = adjustedTime % 60;

NSString * newTime = [NSString stringWithFormat:@"%02u:%02u:%02u", hours, minutes, seconds];

以上情况适用于显示正时差。但是当它在NSLog和Label中都为负时,会出现各种00:423456:978098等等。

当我转换并保存为NSDate类型时,我在NSLog中得到(null)而在我的Label中没有。

当我使用float时,它可以工作但不会始终以00:00:00格式显示。

注意

我正在使用的代码完美无缺地用于积极的时间差异。我还需要显示负时差。

我还需要能够将负面时间保存到CoreData。如果这是不可能的,那么我将解决,但显示正确格式的负时间是主要问题。

修改

我的新修订代码:

NSDate * now = [NSDate date];
NSTimeInterval totalTime1 = [now timeIntervalSinceDate: timeEntry1];
NSTimeInterval totalTime2 = [now timeIntervalSinceDate: timeEntry2];

//must always be this way
int adjustedTime = (int) (totalTime1 - totalTime2);
NSLog (@"What is the adjustedTime? %d", adjustedTime);

int hours = adjustedTime / 3600;
int minutes = (adjustedTime / 60) % 60;
int seconds = adjustedTime % 60;

NSString * newTime = [NSString stringWithFormat:@"%@%02d:%02d:%02d", adjustedTime < 0 ?@"-":@"", hours, minutes, seconds];
NSLog(@"What is the newTime? %@", newTime);

它越接近,因为它现在显示负数,但显示在负数时仍然不正确。

编辑2

下面回答的人建议我尝试检查是否为负,如果它是布尔值。显示没有改变。下面是更多日志语句来演示。 注意我停止使用更新的秒数,以便确定它是否会影响显示并将存储的秒数分开测试,这就是为什么秒没有-符号或更改的原因。

2015-01-09 09:30:14.526 App2.0[8398:498707] What is time 2 : 720
2015-01-09 09:30:14.526 App2.0[8398:498707] What is time 1 : 771
2015-01-09 09:30:14.526 App2.0[8398:498707] What is the adjusted time? 51
2015-01-09 09:30:14.527 App2.0[8398:498707] New Time: 00:00:51

2015-01-09 09:30:18.249 App2.0[8398:498707] What is time 2 : 900
2015-01-09 09:30:18.249 App2.0[8398:498707] What is time 1 : 771
2015-01-09 09:30:18.249 App2.0[8398:498707] What is the adjusted time? -129
2015-01-09 09:30:18.249 App2.0[8398:498707] New Time: -00:-2:51

2015-01-09 09:30:20.281 App2.0[8398:498707] What is time 2 : 840
2015-01-09 09:30:20.281 App2.0[8398:498707] What is time 1 : 771
2015-01-09 09:30:20.281 App2.0[8398:498707] What is the adjusted time? -69
2015-01-09 09:30:20.281 App2.0[8398:498707] New Time: -00:-1:51

2015-01-09 09:30:21.725 App2.0[8398:498707] What is time 2 : 780
2015-01-09 09:30:21.726 App2.0[8398:498707] What is time 1 : 771
2015-01-09 09:30:21.726 App2.0[8398:498707] What is the adjusted time? -9
2015-01-09 09:30:21.726 App2.0[8398:498707] New Time: -00:00:51

2015-01-09 09:30:30.161 App2.0[8398:498707] What is time 2 : 1080
2015-01-09 09:30:30.161 App2.0[8398:498707] What is time 1 : 771
2015-01-09 09:30:30.162 App2.0[8398:498707] What is the adjusted time? -309
2015-01-09 09:30:30.162 App2.0[8398:498707] New Time: -00:-5:51

2015-01-09 09:30:33.389 App2.0[8398:498707] What is time 2 : 4680
2015-01-09 09:30:33.389 App2.0[8398:498707] What is time 1 : 771
2015-01-09 09:30:33.390 App2.0[8398:498707] What is the adjusted time? -3909
2015-01-09 09:30:33.390 App2.0[8398:498707] New Time: --1:-5:51

2015-01-09 09:30:36.186 App2.0[8398:498707] What is time 2 : 8280
2015-01-09 09:30:36.187 App2.0[8398:498707] What is time 1 : 771
2015-01-09 09:30:36.187 App2.0[8398:498707] What is the adjusted time? -7509
2015-01-09 09:30:36.187 App2.0[8398:498707] New Time: --2:-5:51

2015-01-09 09:30:43.918 App2.0[8398:498707] What is time 2 : 660
2015-01-09 09:30:43.918 App2.0[8398:498707] What is time 1 : 771
2015-01-09 09:30:43.919 App2.0[8398:498707] What is the adjusted time? 111
2015-01-09 09:30:43.919 App2.0[8398:498707] New Time: 00:01:51

4 个答案:

答案 0 :(得分:2)

由于您需要单独显示时间组件,因此您可能需要一些条件逻辑来调整显示,具体取决于totalTime2totalTime1之前还是之后NSString *newTime = nil; if (adjustedTime < 0) { newTime = [NSString stringWithFormat:@"-%02d:%02d:%02d", hours, minutes, seconds]; } else { newTime = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds]; }

NSString *newTime = adjustedTime < 0 ? @"-" : @"";
newTime = [newTime stringByAppendingFormat:@"%02d:%02d:%02d", hours, minutes, seconds];

或者如果你喜欢更紧凑的东西:

int hours = abs(adjustedTime / 3600);
int minutes = abs((adjustedTime / 60) % 60);
int seconds = abs(adjustedTime % 60);

另外,正如Wain在评论中指出的那样,在使用每个组件显示之前,您需要获取每个组件的绝对值:

{{1}}

答案 1 :(得分:0)

应该这样做:

  NSDate *now = [NSDate date];
  NSTimeInterval totalTime1 = [now timeIntervalSinceDate: timeEntry1];
  NSTimeInterval totalTime2 = [now timeIntervalSinceDate: timeEntry2];

  NSLog(@"totalTime1: %f", totalTime1); // =>  -60.00;
  NSLog(@"totalTime2: %f", totalTime2); // => 6023.00;

  //must always be this way
  int timeOffset = (int) (totalTime1 - totalTime2);
  NSLog(@"timeOffset: %d", timeOffset); // => -6083;

  BOOL showNegativePrefix = timeOffset < 0;

  int hours   = abs(timeOffset / 3600);       // => 01
  int minutes = abs((timeOffset / 60 ) % 60); // => 41
  int seconds = abs(timeOffset % 60);         // => 23

  NSString * newTime = [NSString stringWithFormat:@"%@%02d:%02d:%02d", showNegativePrefix ? @"-" : @"", hours, minutes, seconds];
  NSLog(@"%@", newTime); // => -01:41:23

答案 2 :(得分:0)

你几乎就在那里,你需要额外的是一个标志,如果值为负,然后格式化差异,如果需要,前面有一个符号。首先设置一个标志,使差异始终为正:

int adjustedTime = ...;

BOOL isNeg;
if (adjustedTime < 0)
{
   isNeg = YES;
   adjustedTime = -adjustedTime; // make value +ve
}
else
   isNeg = NO;

然后像以前一样做数学并将格式行更改为:

NSString *newTime = [NSString stringWithFormat:@"%@%02d:%02d:%02d", (isNeg ? @"-" : @""), hours, minutes, seconds];

注意:您需要使用%d,因为您的值为int而不是unsigned int

HTH

<强>附录

以下是代码,包括我跳过的一些代码(&#34;像以前一样做数学&#34;),添加了评论:

int adjustedTime = totalTime1 - totalTime2;

// At this point adjustedTime may be negative, use the standard approach
// test if it is -ve, and if so set a flag and make the value +ve

BOOL isNeg;
if (adjustedTime < 0)
{
   // we are here if -ve, set flag
   isNeg = YES;
   // and make the value +ve
   adjustedTime = -adjustedTime;
}
else
   isNeg = NO;

// at this point adjustedValue is ALWAYS +ve, isNeg is set if it was originally -ve

int hours = adjustedTime / 3600;
int minutes = (adjustedTime / 60) % 60;
int seconds = adjustedTime % 60;

// at this point hours, minutes & seconds MUST BE +VE as adjustedTime is +ve

// format the values, an optional sign based on isNeg followed by three POSITIVE numbers
NSString *newTime = [NSString stringWithFormat:@"%@%02d:%02d:%02d", (isNeg ? @"-" : @""), hours, minutes, seconds];

这只能在字符串的开头打印AT MOST ONE减号。

这种方法(尽管测试了最小的负整数是不能否定的)是处理这个问题的标准方法。

答案 3 :(得分:0)

塞巴斯蒂安凯勒和CRD提出的解决方案在从负值滚动到正值时遇到问题,例如显示以负值开头的倒计时时:

-1.896893 -00:00:01
-1.498020 -00:00:01
-1.099442 -00:00:01
-0.996686 00:00:00
-0.896971 00:00:00
-0.195021 00:00:00
-0.095020 00:00:00
0.004988 00:00:00
0.104940 00:00:00
0.504980 00:00:00
0.904981 00:00:00
1.000516 00:00:01
1.104926 00:00:01

可以看出,两个解决方案实际显示00:00:00 2秒钟。 下面的代码生成正确的输出:

-1.899441 -00:00:02
-1.499838 -00:00:02
-1.095019 -00:00:02
-0.994941 -00:00:01
-0.899903 -00:00:01
-0.195743 -00:00:01
-0.097564 -00:00:01
0.001758  00:00:00
0.100495  00:00:00
0.503691  00:00:00
0.904986  00:00:00
1.004998  00:00:01
1.103652  00:00:01
2.004936  00:00:02

它实际上是我在NSNumber上写的类别,但该原则也适用于任何浮点数:

- (NSString *)ut_hmsString {
    NSString *sign  = (self.floatValue < 0.0f) ? @"-" : @" ";
    float corr      = (self.floatValue < 0.0f) ? 1.0000001f : 0;
    int seconds     = fmodf(fabsf(floorf(self.floatValue)), 60.0f);
    int fakesec     = fabsf(self.floatValue - corr);
    int minutes     = fakesec / 60 % 60;
    int hours       = fakesec / 3600;
    return [NSString stringWithFormat:@"%@%02i:%02i:%02i", sign, hours, minutes, seconds];
}