Alarm Clock App

Imalter

  • Posts: 96
Basing on that code and some more info i transformed it to a way of calculating the time between two events in seconds. I use that for my game to simulate progress when the game is turned off. Maybe i'll put it on StencylForge someday, but for now let it stay here in case some1 gets it.

I call this function once a second and that is precise enough for me.

TempTime - local string variable with the current time.
TempTime2 - local string variable storing previous time stamp.
TempNum - local num variable that gets the time difference after the function execution.

I set TempTime2 = EMPTY on start, and in that case time difference will be 0, not to cause any time leaps.

Code: [Select]
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

//Setting TimeTemp to current date.
[dateFormatter setDateFormat:@"yyyy-MM-dd-hh-mm-ss"];
tempHolder = _TempTime;_TempTime = [dateFormatter stringFromDate:[NSDate date]];
[_TempTime retain];
[tempHolder release];

//Checking if TempTime2 is "EMPTY" and setting it to current time if true.
if([self sameAs:_TempTime2 two:@"EMPTY"])
{
tempHolder = _TempTime2;_TempTime2 = _TempTime;
[_TempTime2 retain];
[tempHolder release];
}

//Transforming strings to NSDate format.
[dateFormatter setDateFormat:@"yyyy-MM-dd-hh-mm-ss"];
NSDate *startDate = [dateFormatter dateFromString:_TempTime2];
NSDate *endDate = [dateFormatter dateFromString:_TempTime];

//Executing calendar function to get proper time difference.
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorianCalendar components:NSSecondCalendarUnit
                                                    fromDate:startDate
                                                      toDate:endDate
                                                     options:0];

//Retrieving time difference in seconds.
NSInteger seconds = [components second];

//Uncomment if you want to see Debug info.
//NSLog(@"NSInteger value :%d", seconds);

//Setting TimeNum to dime difference
float _TempNum = (float)(seconds);

[dateFormatter release];

marcwilhelm

  • *
  • Posts: 88
I think that tracking time is an essentially important feature to iOS devs for all that want to use IAPs. Check out this forum post and reply to it there if you agree.

bernard

  • Posts: 120
well, if you set up some text attributes : year, month, day, hour, minute, second
the following code will set them with current date/time :

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:@"yyyy"];
_year = [dateFormatter stringFromDate:[NSDate date]];

.....
.....
.....

[dateFormatter release];

If you want to have number attributes something like that should work :
_second = [[dateFormatter stringFromDate:[NSDate date]] floatValue]
or you could use the "as number" block

Into which block would you put this ObjectiveC code?
And how would you call it from Stencyl and how often? Every tick?
And will it work in the background?

Could you post a screen dump of how it could be used or a sample please?

Thanks