NSDate earlierDate: and laterDate: for equal dates
NSDate earlierDate: and laterDate: for equal dates
- Subject: NSDate earlierDate: and laterDate: for equal dates
- From: Dado Colussi <email@hidden>
- Date: Fri, 6 Oct 2006 21:34:01 +0200
I am confused by the NSDate earlierDate: and laterDate: methods.
Apple's documentation [1] says nothing explicit about the situation
where both dates are equal. It only says that timeIntervalSinceDate:
is used for comparing the two dates. GNU documentation [2] says that
when both dates are equal, the receiver is returned. I wrote a little
test to see what is actually returned on a Mac (10.4.8). To my
surprise, I evidenced an opposite behavior on Mac than what GNU had
in documents. It seems that whenever the two dates are equal, the
argument is returned, NOT the receiver as GNU documents.
What is correct and what can I expect from this in the future on Mac?
I guess I would be on the safe side just by sticking to NSDate compare:.
Dado
[1] http://developer.apple.com/documentation/Cocoa/Reference/
Foundation/Classes/NSDate_Class/Reference/Reference.html
[2] http://www.gnu.org/software/gnustep/resources/documentation/
Developer/Base/Reference/NSDate.html
/tmp $ cat dateComparison.m
#import <Foundation/Foundation.h>
int
main(int argc, char **argv)
{
NSDate *foo;
NSDate *bar;
NSDate *d;
/*
* Make foo and bar to represent the same point in time.
*/
foo = [NSDate date];
bar = [foo copy];
NSLog(@"foo: %@\n", foo);
NSLog(@"bar: %@\n", bar);
NSLog(@"difference: %f\n", [foo timeIntervalSinceDate:bar]);
/*
* Check what earlierDate: thinks of them.
*/
NSLog(@"[foo earlierDate:bar]\n");
d = [foo earlierDate:bar];
if (d == foo) {
NSLog(@"\tfoo was earlier\n");
} else if (d == bar) {
NSLog(@"\tbar was earlier\n");
}
NSLog(@"[foo laterDate:bar]\n");
d = [foo laterDate:bar];
if (d == foo) {
NSLog(@"\tfoo was later\n");
} else if (d == bar) {
NSLog(@"\tbar was later\n");
}
NSLog(@"[bar earlierDate:foo]\n");
d = [bar earlierDate:foo];
if (d == foo) {
NSLog(@"\tfoo was earlier\n");
} else if (d == bar) {
NSLog(@"\tbar was earlier\n");
}
NSLog(@"[bar laterDate:foo]\n");
d = [bar laterDate:foo];
if (d == foo) {
NSLog(@"\tfoo was later\n");
} else if (d == bar) {
NSLog(@"\tbar was later\n");
}
/*
* Check what laterDate: thinks of them.
*/
NSLog(@"[foo laterDate:bar]\n");
d = [foo laterDate:bar];
if (d == foo) {
NSLog(@"\tfoo was later\n");
} else if (d == bar) {
NSLog(@"\tbar was later\n");
}
NSLog(@"[foo laterDate:bar]\n");
d = [foo laterDate:bar];
if (d == foo) {
NSLog(@"\tfoo was later\n");
} else if (d == bar) {
NSLog(@"\tbar was later\n");
}
NSLog(@"[bar laterDate:foo]\n");
d = [bar laterDate:foo];
if (d == foo) {
NSLog(@"\tfoo was later\n");
} else if (d == bar) {
NSLog(@"\tbar was later\n");
}
NSLog(@"[bar laterDate:foo]\n");
d = [bar laterDate:foo];
if (d == foo) {
NSLog(@"\tfoo was later\n");
} else if (d == bar) {
NSLog(@"\tbar was later\n");
}
return 0;
}
/tmp $ cc -o dateComparison -framework Foundation dateComparison.m
/tmp $ ./dateComparison
2006-10-06 21:03:58.211 dateComparison[2869] *** _NSAutoreleaseNoPool
(): Object 0x502d20 of class NSCFDate autoreleased with no pool in
place - just leaking
2006-10-06 21:03:58.219 dateComparison[2869] foo: 2006-10-06 21:03:58
+0200
2006-10-06 21:03:58.220 dateComparison[2869] bar: 2006-10-06 21:03:58
+0200
2006-10-06 21:03:58.221 dateComparison[2869] difference: 0.000000
2006-10-06 21:03:58.221 dateComparison[2869] [foo earlierDate:bar]
2006-10-06 21:03:58.222 dateComparison[2869] bar was earlier
2006-10-06 21:03:58.222 dateComparison[2869] [foo laterDate:bar]
2006-10-06 21:03:58.223 dateComparison[2869] bar was later
2006-10-06 21:03:58.223 dateComparison[2869] [bar earlierDate:foo]
2006-10-06 21:03:58.224 dateComparison[2869] foo was earlier
2006-10-06 21:03:58.224 dateComparison[2869] [bar laterDate:foo]
2006-10-06 21:03:58.224 dateComparison[2869] foo was later
2006-10-06 21:03:58.225 dateComparison[2869] [foo laterDate:bar]
2006-10-06 21:03:58.225 dateComparison[2869] bar was later
2006-10-06 21:03:58.226 dateComparison[2869] [foo laterDate:bar]
2006-10-06 21:03:58.226 dateComparison[2869] bar was later
2006-10-06 21:03:58.227 dateComparison[2869] [bar laterDate:foo]
2006-10-06 21:03:58.227 dateComparison[2869] foo was later
2006-10-06 21:03:58.227 dateComparison[2869] [bar laterDate:foo]
2006-10-06 21:03:58.228 dateComparison[2869] foo was later
/tmp $
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden