PDA

View Full Version : Cocoa NSDate object converting to seconds (double)




bengut
Jun 27, 2011, 04:01 AM
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



jiminaus
Jun 27, 2011, 05:10 AM
timeIntervalSinceReferenceDate

bengut
Jun 27, 2011, 05:35 AM
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
Jun 27, 2011, 05:45 AM
timeIntervalSinceDate:

bengut
Jun 27, 2011, 06:04 AM
timeIntervalSinceDate:

thanks again …

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

cheers

jiminaus
Jun 27, 2011, 06:13 AM
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
Jun 27, 2011, 06:35 AM
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
Jun 27, 2011, 07:12 AM
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.

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.

NSTimeInterval nowSinceQuickTimeEpoch =
[[NSDate date] timeIntervalSinceDate:quickTimeEpoch];


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

bengut
Jun 27, 2011, 07:53 AM
thanks from Berlin Germany

I refer to you later

bengut
Jun 28, 2011, 02:16 AM
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