1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Cocoa NSDate object converting to seconds (double)

Discussion in 'Mac Programming' started by bengut, Jun 27, 2011.

  1. macrumors newbie

    #1
    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
     
  2. macrumors 65816

    jiminaus

    #2
    timeIntervalSinceReferenceDate
     
  3. macrumors newbie

    #3
    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
     
  4. macrumors 65816

    jiminaus

    #4
    timeIntervalSinceDate:
     
  5. macrumors newbie

    #5
    thanks again …

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

    cheers
     
  6. macrumors 65816

    jiminaus

    #6
    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?!
     
  7. macrumors newbie

    #7
    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 ?
     
  8. jiminaus, Jun 27, 2011
    Last edited: Jun 27, 2011

    macrumors 65816

    jiminaus

    #8
    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.
     
  9. macrumors newbie

    #9
    thanks a lot for your direct response, jiminaus

    thanks from Berlin Germany

    I refer to you later
     
  10. macrumors newbie

    #10
    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
     

Share This Page