Skip to content

Instantly share code, notes, and snippets.

@klaaspieter
Created July 19, 2010 11:17
Show Gist options
  • Save klaaspieter/481288 to your computer and use it in GitHub Desktop.
Save klaaspieter/481288 to your computer and use it in GitHub Desktop.
[[CPDate dateWithTimeIntervalSince1970:seconds] isEqualToDate:[CPDate dateWithTimeIntervalSince1970:seconds]]
// initWithTimeIntervalSince1970 is defined as
- (id)initWithTimeIntervalSince1970:(CPTimeInterval)seconds
{
self = new Date(seconds * 1000);
return self;
}
// equalToDate was defined as (not working)
- (BOOL)isEqualToDate:(CPDate)aDate
{
if (!aDate)
return NO;
return self === aDate;
}
// equalToDate is now defined as (working)
- (BOOL)isEqualToDate:(CPDate)aDate
{
if (!aDate)
return NO;
return !(self < aDate || self > aDate);
}
@alloy
Copy link

alloy commented Jul 19, 2010

I have no experience with obj-j & cappuccino, but I wonder; is the `self === aDate' part pure JS? (It seems so).

Because in that case, it will never work. First of all, === is the identity equality operator, which means only if both are the exact same objects will it return `true'. However, even with the value equality operator (==) you cannot compare dates, you will have to compare the values:

% /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc
> d1 = new Date(1234)
Thu Jan 01 1970 01:00:01 GMT+0100 (CET)
> d2 = new Date(1234)
Thu Jan 01 1970 01:00:01 GMT+0100 (CET)
> d1 === d2
false
> d1 == d2
false
> d1.valueOf() == d2.valueOf()
true

@klaaspieter
Copy link
Author

Yeah I figured that out. But I'm unsure if d1.getTime() or d1.valueOf() actually work properly. I haven't found any conclusive answer for this on Google/Stackoverflow.

@alloy
Copy link

alloy commented Jul 19, 2010

Why would you wonder that? Afaik, the valueOf method is implemented by all, as it is the way to get a primitive value out of objects. For instance: http://msdn.microsoft.com/en-us/library/ftx8swz5(v=VS.85).aspx, https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Object/valueOf etc.

@klaaspieter
Copy link
Author

I'm not wondering about browser implementation, more about if they will always return the same value for the same time. I've read several comments on Stackoverflow which said they were unsure about it.

If it's guaranteed to always be equal than the question is which of these two methods is faster. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment