[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSCalendarDate bug?

From: Rob Burns
Subject: NSCalendarDate bug?
Date: Wed, 10 Sep 2003 11:51:46 +0700

The small program to calculate age, that follows, usually returns the right answer. But, given a birthdate of 1981/9/23 it will return:

2003-09-08 21:42:38.591 age[3987] Age = 22 years, -1 months, 15 days

(because it uses 'todays date' to do the calculation, its probably worth noting that I ran it on 2003/9/8)

Is there something wrong in the program, or is that a bug?


#include <Foundation/Foundation.h>

int main (int argc, char **argv)
  NSArray *args;
  NSAutoreleasePool *pool;

  NSString  *dateString;
  NSCalendarDate *birthday, *todaysDate;

  int years=0, months=0, days=0;

  pool = [NSAutoreleasePool new];
  args = [[NSProcessInfo processInfo] arguments];

  if([args count] < 1)
    NSLog(@"I think you forgot something");
    [pool release];

  dateString = [NSString stringWithString: [args objectAtIndex: 1]];

birthday = [NSCalendarDate dateWithString: dateString calendarFormat: @"%Y/%m/%d"];
  todaysDate = [NSCalendarDate calendarDate];
  [todaysDate years: &years months: &months days: &days hours: NULL
    minutes: NULL seconds: NULL sinceDate: birthday];

  NSLog(@"Age = %i years, %i months, %i days", years, months, days);

  [pool release];

reply via email to

[Prev in Thread] Current Thread [Next in Thread]