2 个 NSDate 应该相等不是吗?

发布于 2024-11-04 03:12:35 字数 367 浏览 1 评论 0原文

我正在使用 Stig Brautaset(http://code.google.com/p/json-framework) 的 JSON 库,我需要序列化 ​​NSDate。我正在考虑在将其 JSON化之前将其转换为字符串,但是,我遇到了这种奇怪的行为:

为什么这些 NSDates 不被认为是相等的?

NSDate *d = [[NSDate alloc] init];
NSDate *dd = [NSDate dateWithString:[d description]];

NSLog(@"%@", d);
NSLog(@"%@", dd);
if( [d isEqualToDate:dd] ){
    NSLog(@"Yay!");
}

I'm using the JSON library from Stig Brautaset(http://code.google.com/p/json-framework) and I need to serialize an NSDate. I was considering converting it into a string before JSONifying it, however, I ran into this weird behavior:

Why aren't these NSDates considered equal?

NSDate *d = [[NSDate alloc] init];
NSDate *dd = [NSDate dateWithString:[d description]];

NSLog(@"%@", d);
NSLog(@"%@", dd);
if( [d isEqualToDate:dd] ){
    NSLog(@"Yay!");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

耀眼的星火 2024-11-11 03:12:35

当您描述原始日期对象时,您会失去原始对象的一些亚秒精度 - 换句话说, -description 剃须关闭小数秒,然后返回

采用国际格式 YYYY-MM-DD HH:MM:SS ±HHMM 的接收者字符串表示形式,其中 ±HHMM 表示时区偏移量(以小时为单位)距离 GMT 时间还有分钟

当您根据描述创建新的日期对象时,您会在整秒内获得它,因为该字符串仅精确到整秒。所以 -isEqualToDate: 返回 NO 因为存在分数差异两个日期对象之间的一秒,它对此很敏感。

此方法检测日期之间的亚秒差异。如果您想比较粒度较小的日期,请使用 timeIntervalSinceDate: 来比较两个日期。

所以你应该这样做(NSTimeInterval 以秒为单位):

if ([d timeIntervalSinceDate:dd] == 0) {
    NSLog(@"Yay!");
}

When you describe the original date object you lose some sub-second precision from the original object — in other words, -description shaves off fractional seconds, and returns

A string representation of the receiver in the international format YYYY-MM-DD HH:MM:SS ±HHMM, where ±HHMM represents the time zone offset in hours and minutes from GMT

When you create a new date object based on the description, you get it in whole seconds because the string is only precise to a whole second. So -isEqualToDate: returns NO because there is a difference of a fraction of a second between your two date objects, which it's sensitive to.

This method detects sub-second differences between dates. If you want to compare dates with a less fine granularity, use timeIntervalSinceDate: to compare the two dates.

So you'd do something like this instead (NSTimeInterval measures in seconds):

if ([d timeIntervalSinceDate:dd] == 0) {
    NSLog(@"Yay!");
}
锦上情书 2024-11-11 03:12:35

isEqualToDate 检测日期之间的亚秒差异,但描述方法不包括亚秒。

isEqualToDate detects subseconds differences between dates, but the description method does not include subseconds.

怪我鬧 2024-11-11 03:12:35

因为它们不等价:

NSDate *d = [NSDate date];
NSDate *dd = [NSDate dateWithString:[d description]];
NSLog(@"%f", [d timeIntervalSinceReferenceDate]);
NSLog(@"%f", [dd timeIntervalSinceReferenceDate]);

产生:

2011-04-28 11:58:11.873 EmptyFoundation[508:903] 325709891.867788
2011-04-28 11:58:11.874 EmptyFoundation[508:903] 325709891.000000

换句话说,+dateWithString: 方法不保持亚秒精度。

Because they're not equivalent:

NSDate *d = [NSDate date];
NSDate *dd = [NSDate dateWithString:[d description]];
NSLog(@"%f", [d timeIntervalSinceReferenceDate]);
NSLog(@"%f", [dd timeIntervalSinceReferenceDate]);

Produces:

2011-04-28 11:58:11.873 EmptyFoundation[508:903] 325709891.867788
2011-04-28 11:58:11.874 EmptyFoundation[508:903] 325709891.000000

In other words, the +dateWithString: method does not maintain sub-second precision.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文