0

我对时间逻辑不是很好,但想检查用户是否在 2 分钟的时间间隔内使用整数 (INT) 和布尔 (DN) 完成了特定任务。

伪代码是这样的

演绎功能1

如果 INT <= 4 在 2 分钟内然后 -0.5 否则如果 INT > 4 在 2 分钟内然后 -1.0

加法功能2

如果 INT < 3 && DN >= 2 在 2 分钟内然后 +0.5 否则如果 INT < 4 && DN >= 3 在 2 分钟内然后 +0.25

目前我这样做只是为了找出间隔

//time check if more then 2 minutes
#define TIME_INTERVAL   -120

-(void)shouldCheckForTime
{
    NSString *updateUTCPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"UTCDate"];
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:updateUTCPath];

    if (!fileExists)
    {
        NSDate *currentDate = [NSDate date];
        NSArray *array = [NSArray arrayWithObject:currentDate];
        [array writeToFile:updateUTCPath atomically:YES];

        NSTimeInterval currentUTCTimeStamp = [[NSDate date] timeIntervalSince1970];
        NSLog(@"currentUTCTimeStamp %.0f",currentUTCTimeStamp);
    }

    NSMutableArray *rawArray = [NSMutableArray arrayWithContentsOfFile:updateUTCPath];
    if (rawArray == nil)
    {
        //return NO;
    }

    //get date from file UTCDate
    NSDate *lastUTCDate = [rawArray objectAtIndex:0];
    //NSLog(@"CDTS timeIntervalSince1970 %.0f",[lastUTCDate timeIntervalSince1970]);
   // NSLog(@"CDTS timeIntervalSinceNow %.0f",[lastUTCDate timeIntervalSinceNow]);

    if ([lastUTCDate timeIntervalSinceNow] < TIME_INTERVAL)
    {
         //NSLog(@"more then 2 mins");
    }
    else
    {
         //NSLog(@"less then 2 mins");
    }

    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
    [dateFormatter setDateFormat:@"mm:ss:SS"];

    NSDate* firstDate = [dateFormatter dateFromString:@"01:00:00"];
    NSDate* secondDate = [dateFormatter dateFromString:@"01:02:00"];

    NSTimeInterval timeDifference = [secondDate timeIntervalSinceDate:firstDate];

    //NSLog(@"time diff %f",timeDifference);
}

感谢您阅读并感谢任何评论。

4

1 回答 1

1

根据要求,这是您需要的示例。此特定示例用于确定用户创建对象后的时间。然后它决定是否需要在几分钟或几小时内完成。

NSTimeInterval timeDifference = [[NSDate date] timeIntervalSinceDate:dateToCompare;
NSTimeInterval finalTime = timeDifference / 60;

NSString *timePostedString = nil;
if (finalTime < 1) {
    timePostedString = [NSString stringWithFormat:@"1m"];
} else if (finalTime >= 60) {
    timePostedString = [NSString stringWithFormat:@"%.0fh", finalTime / 60];
} else if (finalTime >= 1440) {
    timePostedString = [NSString stringWithFormat:@"%.1fd", finalTime / 1440];
}
于 2013-01-26T09:50:44.247 回答