• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSCalendar bug.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSCalendar bug.


  • Subject: Re: NSCalendar bug.
  • From: Tommy Nordgren <email@hidden>
  • Date: Tue, 30 Jan 2007 20:40:43 +0100


On 30 jan 2007, at 15.46, Half Activist wrote:

Hello Cocoa-dev members,

I'm posting to the list because I'm wondering if there is a bug in the implementation of NSCalendar, actually the gregorian calendar.

Suppose you've got the following code to compute the number of days between two dates, d1 and d2.

static NSCalendar *gregorian = nil;
if( gregorian == nil )
gregorian = [ NSCalendar currentCalendar ];
unsigned int unitFlags = NSDayCalendarUnit;
NSDateComponents *comps = [ gregorian components: unitFlags fromDate: d1 toDate: d2 options: 0 ];
int days = [ comps day ];

if:
d1 = 2045-10-01 00:00:00 +0100
d2 = 2045-10-30 00:00:00 +0100
then the result of this code is 'days == 28' where it should be 29.


	it also fails for:
	d1 = 2039-04-01 00:00:00 +0100
	d2 = 2039-10-31 00:00:00 +0100
	the result being 212 days instead of 213.

I discovered it while testing 2 other implementations of date interval algorithm,
as I considered I could rely on NSCalendar to detect errors.
The two other algorithms always give the same result, and so do various date interval calculator
on the Internet.


_______________________________________________
Trying out:
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
static NSCalendar *gregorian = nil;
NSDate * d1 = [NSDate dateWithString:@"2045-10-01 00:00:00 +0100"];
NSDate * d2 = [NSDate dateWithString:@"2045-10-30 00:00:00 +0100"];
NSDate * d3 = [NSDate dateWithString:@"2045-10-30 00:00:01 +0100"];
NSDate * d4 = [NSDate dateWithString:@"2045-10-30 02:00:01 +0100"];
if( gregorian == nil )
gregorian = [ NSCalendar currentCalendar ];
unsigned int unitFlags = NSDayCalendarUnit;
NSDateComponents *comps = [ gregorian components: unitFlags fromDate: d1 toDate: d2 options: 0 ];
int days = [ comps day ];
NSLog (@"Days: %d\n",days);
comps = [ gregorian components: unitFlags fromDate: d1 toDate: d3 options: 0 ];
days = [ comps day ];
NSLog (@"Days: %d\n",days);
comps = [ gregorian components: unitFlags fromDate: d1 toDate: d4 options: 0 ];
days = [ comps day ];
NSLog (@"Days: %d\n",days);

[pool release];
return 0;
}
I get the result :
2007-01-30 20:36:50.179 Test[21941] Days: 28
2007-01-30 20:36:50.180 Test[21941] Days: 28
2007-01-30 20:36:50.181 Test[21941] Days: 29


Your date interval includes the point in time where the switch between
Daylight saving time and winter time occurs
------
What is a woman that you forsake her, and the hearth fire and the home acre,
to go with the old grey Widow Maker. --Kipling, harp song of the Dane women
Tommy Nordgren
email@hidden




_______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: NSCalendar bug.
      • From: Deborah Goldsmith <email@hidden>
References: 
 >NSCalendar bug. (From: Half Activist <email@hidden>)

  • Prev by Date: Re: Flipping Text
  • Next by Date: Need help for promised file drag/drop
  • Previous by thread: Re: NSCalendar bug.
  • Next by thread: Re: NSCalendar bug.
  • Index(es):
    • Date
    • Thread