Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-13-2011, 11:29 AM   #1 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 194
mavrik5150 is on a distinguished road
Default How to calculate time time between two NSDate

So part of a new feature that my boss wants added to our app is the ability to track how many incorrect login attempts have been made and if there were 5 attempts within 15 minutes to disable the login feature for 15 minutes. I can do everything except for the comparing of my two NSDate's that are Formatted as "HH:mm:ss". I thought that I could use compare but most of the examples that I've found just show how to see if the two dates are equal to each other or if one is greater than the other, where I just want to find the difference in minutes between the two. Here's what I have for the two dates I want to compare:

Code:
NSLog(@"5 Error Attempts Made");
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"HH:mm:ss"];
        NSDate *firstTry = [dateFormat dateFromString:[[errorArray valueForKey:@"ErrorTime"] objectAtIndex:0]];
        NSDate *lastTry = [dateFormat dateFromString:[[errorArray valueForKey:@"ErrorTime"] objectAtIndex:4]];
        NSLog(@"First Attempt Time: %@, Last Attempt Time: %@", firstTry, lastTry);
I'm using an Array since I have to store the Error Attempts in NSUserDefaults since we need to check within a 15 minute interval so I had to convert the Dates to Strings to store them and then convert them back for the error checking part.
mavrik5150 is offline   Reply With Quote
Old 09-13-2011, 11:43 AM   #2 (permalink)
Registered Member
 
DenVog's Avatar
 
Join Date: Jan 2009
Location: Silicon Valley, USA
Posts: 624
DenVog is on a distinguished road
Default

Quote:
Originally Posted by mavrik5150 View Post
I can do everything except for the comparing of my two NSDate's that are Formatted as "HH:mm:ss". I thought that I could use compare but most of the examples that I've found just show how to see if the two dates are equal to each other or if one is greater than the other, where I just want to find the difference in minutes between the two.
Don't you want
Code:
timeIntervalSinceDate:
It is in NSDate Class Reference
DenVog is offline   Reply With Quote
Old 09-13-2011, 11:46 AM   #3 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 864
nobre84 is on a distinguished road
Default

You can use :
Code:
 if ([lastEntry timeIntervalSinceDate:firstEntry] / 60 > 15) {
nobre84 is offline   Reply With Quote
Old 09-13-2011, 11:50 AM   #4 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 194
mavrik5150 is on a distinguished road
Default

Thanks DenVog and Nobre, that's what I was looking for but couldn't think of the name and tried searching but kept coming up with NSDate Compare results that weren't working.
mavrik5150 is offline   Reply With Quote
Old 09-13-2011, 12:23 PM   #5 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by mavrik5150 View Post
So part of a new feature that my boss wants added to our app is the ability to track how many incorrect login attempts have been made and if there were 5 attempts within 15 minutes to disable the login feature for 15 minutes. I can do everything except for the comparing of my two NSDate's that are Formatted as "HH:mm:ss". I thought that I could use compare but most of the examples that I've found just show how to see if the two dates are equal to each other or if one is greater than the other, where I just want to find the difference in minutes between the two. Here's what I have for the two dates I want to compare:

Code:
NSLog(@"5 Error Attempts Made");
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"HH:mm:ss"];
        NSDate *firstTry = [dateFormat dateFromString:[[errorArray valueForKey:@"ErrorTime"] objectAtIndex:0]];
        NSDate *lastTry = [dateFormat dateFromString:[[errorArray valueForKey:@"ErrorTime"] objectAtIndex:4]];
        NSLog(@"First Attempt Time: %@, Last Attempt Time: %@", firstTry, lastTry);
I'm using an Array since I have to store the Error Attempts in NSUserDefaults since we need to check within a 15 minute interval so I had to convert the Dates to Strings to store them and then convert them back for the error checking part.

NSDate is a "property object". You can save NSDate objects to plists or NSUserDefaults directly. No need to convert them to strings.

The full list of property list objects: (NSString, NSData, NSDate, NSNumber, NSArray, or NSDictionary objects)

Note that the documentation for NSArray writeToFile:atomically: does not indicate that NSDate is a plist object, but the docs for the NSDictionary version of writeToFile:atomically: do list NSDate as a property list object.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 09-13-2011, 01:12 PM   #6 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 194
mavrik5150 is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
NSDate is a "property object". You can save NSDate objects to plists or NSUserDefaults directly. No need to convert them to strings.

The full list of property list objects: (NSString, NSData, NSDate, NSNumber, NSArray, or NSDictionary objects)

Note that the documentation for NSArray writeToFile:atomically: does not indicate that NSDate is a plist object, but the docs for the NSDictionary version of writeToFile:atomically: do list NSDate as a property list object.
This may be my limited knowledge but I thought the only way to apply a NSDateFormatter to a Date is to convert it to a string? I really only need to current time, so that's why I was using a Formatter to get just the HH:mm:ss of the login instead of the entire timestamp from just using NSDate. Is there another way of doing so? What I'm doing know is adding the Error Count and Time to a Dictionary and then adding that to the Array so I can have Key values for each (like setObject:CurrentTime forKey:@"ErrorTimeStamp").
mavrik5150 is offline   Reply With Quote
Old 09-13-2011, 01:17 PM   #7 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 864
nobre84 is on a distinguished road
Default

You have no need to discard the date part (what happens for instance if the tries span a different day , but within the 15min interval on the hour ?)

Anyway, when you convert it back to NSDate using the formatter, the current date (or a base date) will be added anyway, you can't get rid of the date part, its stored that way. Just store the NSDate object itself
nobre84 is offline   Reply With Quote
Old 09-13-2011, 01:25 PM   #8 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 194
mavrik5150 is on a distinguished road
Default

Thanks nobre, I didn't realize that and just had a Log statement output the two dates and sure enough there was all the extra I didn't need. I'll adjust it to just use the Date without formatting. Thanks for the help.
mavrik5150 is offline   Reply With Quote
Old 11-18-2011, 07:38 PM   #9 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 135
thephotographer is on a distinguished road
Default

will this work for you? you can put the start time when the user first attempts to login, and for each login there after you can compare the end time to the start time, if its greater than 900 seconds (15 minutes) then you disable login for 15 minutes. which can be using the same method, at that point create a 2nd start time, and not allow another login attempt until the endtime2 is greater than 900.

i use this a lot, its nice and simple

Code:
CFTimeInterval startTime = CFAbsoluteTimeGetCurrent();
Code:
CFTimeInterval endTime = CFAbsoluteTimeGetCurrent();    
    float deltaTimeInSeconds = endTime - startTime;
    NSLog(@"time was %f seconds",deltaTimeInSeconds);
thephotographer is offline   Reply With Quote
Old 11-19-2011, 12:05 AM   #10 (permalink)
Registered Member
 
Join Date: Nov 2011
Location: New Delhi
Posts: 2
Hadaley is on a distinguished road
Default

hi

I think 15 minute Interval is a double that represents the seconds between the two times
Its nice
Event Management Companies in Delhi
Hadaley is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 408
15 members and 393 guests
7twenty7, Eclectic, eski, EvilElf, fiftysixty, HemiMG, iOS.Lover, JackReidy, jarv, sacha1996, teebee74, tim0504, UMAD, VinceYuan, yuncarl28
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,672
Threads: 94,121
Posts: 402,905
Top Poster: BrianSlick (7,990)
Welcome to our newest member, yuncarl28
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:09 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0