NSDate earlierDate: and laterDate: for equal dates

  • 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 $
previous month october 2006 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today