Question

I'm not very good with time logic, but will like to check if an user had done a specific task with integer (INT) and Bool (DN) in an interval of 2 minutes.

The pseudo code is this

Deduction Function 1

if INT <= 4 in 2 mins then -0.5 else if INT > 4 in 2 mins then -1.0

Addition Function 2

if INT < 3 && DN >= 2 in 2 mins then +0.5 else if INT < 4 && DN >= 3 in 2 mins then +0.25

current i only did this to find out the interval

//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);
}

Thanks for reading and appreciated any comments.

Était-ce utile?

La solution

As per request, here is an example of what you need. This specific example is used to determine how long it's been since a user has created an object. It then determines whether it needs to be in minutes or hours.

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];
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top