0

I'm working through a challenge in Objective-C Programming, The Big Nerd Ranch Guide, and I'm a little flummoxed by one of the challenges.

Use two instances of NSDate to figure out how many seconds you have been alive. Hint: here is how you create a new date object from the year, month, etc.:

So I need to the difference between now and my date of birth in seconds. Sounds good. Then the hint shows up:

@autoreleasepool {
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setYear:1981];
    [comps setMonth:7];
    [comps setDay:12];
    [comps setHour:1];
    [comps setMinute:55];
    [comps setSecond:33];

    NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

    NSDate *dateOfBirth = [g dateFromComponents:comps];

The first issue is I don't understand what *g is meant to be. I know it's a pointer to an NSCalendar object. But why do we need it? And what is g meant to stand for?

The sample code then uses the g variable to grab a date. In another language, this would be as easy as DateDiff(dateOne, dateTwo, interval). I'm not clear on why the Calendar object is necessary in the first place, and why we have to create date components to feed the object.

This is all new to me, and I've worked with dynamic languages in the past. So a "dummies" like explanation would be great!

Mohamad
  • 34,731
  • 32
  • 140
  • 219
  • possible duplicate of [Subtracting two NSDate objects](http://stackoverflow.com/questions/6306661/subtracting-two-nsdate-objects) – James Black Aug 02 '12 at 17:04
  • @JamesBlack: That looks like a duplicate of the difference bit, but not the initial "what's the calendar for" bit. – Jon Skeet Aug 02 '12 at 17:08

2 Answers2

3

The sample code then uses the g variable to grab a date. In another language, this would be as easy as DateDiff(dateOne, dateTwo, interval). I'm not clear on why the Calendar object is necessary in the first place, and why we have to create date components to feed the object.

I'm not an Objective-C programmer, but I know a bit about date/time APIs.

In order to specify your birth date/time, you're giving a year of 1981, a month of 7 etc. What does a year of 1981 mean? To you, it may mean about 31 years ago... but to someone using a different calendar system, it could mean something entirely different. Converting from "year/month/day etc" to "point in time" is a bit like converting from a string to an integer: does "10" mean ten, or sixteen? It all depends on your frame of reference (the base, in this case - the calendar system in the date case).

The calendar - initialized as a Gregorian calendar - takes those date components and is able to give you back an NSDate which is a sort of "absolute" value in time.

As for computing the difference between two NSDate values - I suspect there's a member which gives you something like "seconds since the Unix epoch" (or possible milliseconds). So take that from both dates (your birth and "now"), subtract one from the other, and you'll get the elapsed number of seconds (or milliseconds) between the two.

EDIT: In fact, the timeIntervalSinceNow function is probably the one you want, once you've got your birth date.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks! I understand. It just seemed strange to go through all that trouble when I'm in the past I used a simple function that took three arguments: now, then, and interval type! – Mohamad Aug 02 '12 at 17:10
3

You can do it this way

NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:1981];
[comps setMonth:7];
[comps setDay:12];
[comps setHour:1];
[comps setMinute:55];
[comps setSecond:33];

NSDate *dateOfBirth = [[NSCalendar currentCalendar] dateFromComponents:comps];


NSTimeInterval timeGap=[[NSDate new] timeIntervalSinceDate:dateOfBirth ];
Neo
  • 2,807
  • 1
  • 16
  • 18