我对时间逻辑不太满意,但想检查用户是否在2分钟的间隔内完成了整数(INT)和Bool(DN)的特定任务。
伪代码是
扣除功能1
如果INT <= 4,则在2分钟内,然后是-0.5 否则,如果INT&gt; 4分2分钟然后-1.0
加法功能2
如果INT&lt; 3&amp;&amp; DN> = 2分钟,然后+0.5 否则如果INT&lt; 4&amp;&amp; DN> = 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);
}
感谢阅读并感谢任何评论。
答案 0 :(得分: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];
}