Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

bengut

macrumors newbie
Original poster
Jun 15, 2011
19
0
I need the "current date and time" in seconds as a type of NSTimeInterval.

I use NSDate *currentTime = [NSDate date] to retrieve the current time which results in an object.
How do I get NSTimeInterval (seconds) out of this object ?

It seems to be very simple, but I did not find an example for this issue ...

Could anyone help ? Would be great …

Cheers
 

bengut

macrumors newbie
Original poster
Jun 15, 2011
19
0
timeIntervalSinceReferenceDate

thanks jiminaus, I forgot to write that I need a timeInterVal that specifies the calender date and time in seconds since midnight, January 1, 1904

timeIntervalSinceReferenceDate specifies a timeInterval since 1. January , 2001 GMT
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
thanks again …

but I need to compare against a given type of double. Not against another object…
That's my problem …

cheers

Huh?! So you have a double which is the number of seconds since your epoch (1 Jan 1904)? And then what?

PLEASE, can you just explain exactly what you need to do?!
 

bengut

macrumors newbie
Original poster
Jun 15, 2011
19
0
Huh?! So you have a double which is the number of seconds since your epoch (1 Jan 1904)? And then what?

PLEASE, can you just explain exactly what you need to do?!

Okay. For my app I retrieve a Quicktime movie trackCreation time which is returned as an unsigned long type. This given number I want to set in context with the current time (wich is unfortunatly an object).

I think there is a way going through the NSFormatter Class via Strings. But then I have to convert it again as a double or an unsigned long. Isn' t there simplier way ?
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
Okay. For my app I retrieve a Quicktime movie trackCreation time which is returned as an unsigned long type. This given number I want to set in context with the current time (wich is unfortunatly an object).

I think there is a way going through the NSFormatter Class via Strings. But then I have to convert it again as a double or an unsigned long. Isn' t there simplier way ?

Construct an NSDate representing the QuickTime epoch date.
Code:
NSDateComponents* dateComponents = [[NSDateComponents alloc] init];
[dateComponents setYear:1904];
[dateComponents setMonth:1]; // Jan
[dateComponents setDay:1];
        
NSCalendar* calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; // UTC/GMT
quickTimeEpoch = [calendar dateFromComponents:dateComponents];
        
[calendar release];
[dateComponents release];

This is an expensive operation. Do it once and store it somewhere.

Get NSTimeInterval being current time as seconds since QuickTime epoch.
Code:
NSTimeInterval nowSinceQuickTimeEpoch = 
  [[NSDate date] timeIntervalSinceDate:quickTimeEpoch];

Type-cast the long into an NSTimeInterval. Then I think you can just compare.
 
Last edited:

bengut

macrumors newbie
Original poster
Jun 15, 2011
19
0
thanks a lot for your direct response, jiminaus

thanks from Berlin Germany

I refer to you later
 

bengut

macrumors newbie
Original poster
Jun 15, 2011
19
0
thanks jiminaus you showed me the better way …

concerning my problem I made it to complicate while the solution is very simple.
Instead of using the trackCreation time of Quicktime which refers to the 1. Jan 1904 and results in an unsigned long I use now the file creation time which results in an NSDate object, which in turn I can compare to the current time. The result is an NSInterval in seconds which I can use then for further arithmetic in my programm. So the code is following:

NSString * gFilePath = @"/user/movies/myMovie.mp4";
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:gFilePath error:NULL];
NSDate *fileCreationDate = [fileAttributes objectForKey:NSFileCreationDate];
double timePassed = [fileCreationDate timeIntervalSinceNow];

NSLog(@"fileCreationDate (%@) ",fileCreationDate);
NSLog(@"timePassed (%f) ",timePassed);

--------------------------
fileCreationDate (2011-06-27 17:00:06 +0200)
timePassed (-57110.873383)
--------------------------

"timePassed" is in seconds and represents the passed time between file creation and current time on which the file (movie) was opened for browsing etc.

"timePassed" is negative because the file creation time is earlier than the current time

regards from Berlin, Germany
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.