日本日历下的核心数据错误地比较了日期 [英] Core Data under Japanese Calendar incorrectly comparing dates

查看:117
本文介绍了日本日历下的核心数据错误地比较了日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以再次,我似乎对iPhone的日历支持感到不高兴.我遇到了核心数据比较日期的方式的问题.核心数据似乎使用公历存储日期,因为它们以20XX-MM-dd格式出现.但是当我以这种方式构建NSPredicate

So I seem to be getting burned by the iPhone's calendar support again. I've come across an issue with the way core data compares dates. Core data appears to be storing dates using the Gregorian calendar as they come out in a 20XX-MM-dd format. But when I build an NSPredicate this way

[nsRequest setPredicate:[NSPredicate predicateWithFormat:@"publish <= %@ AND expires >= %@", [NSDate date], [NSDate date]]]

如果未将用户设备设置为公历,则不会出现任何项目,因为在与输入日期进行比较之前,核心数据似乎未对它的NSDates进行本地化.

if the user's device isn't set to a Gregorian calendar then no items will appear, as core data doesn't seem to localizing it's NSDates before comparing against the input dates.

因此,如果日本日历模式中的日期为今天的"2011-05-31",则该日期使用的是"0023-05-31",因为它是当前天皇的第23年,因此将输入日期设置为比它早2000年.

So it would appear that if the date were today '2011-05-31' in Japanese calendar mode core data is using '0023-05-31' since it's the 23rd year of the current emperor, placing the input date about 2000 years before what it's being compared against.

我发现我可以像这样解决此问题

I've found I can work around this like so

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:[[NSLocale preferredLanguages] objectAtIndex:0]] autorelease]];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZZ"];
NSString *todayGregorian = [[dateFormat stringFromDate:[NSDate date]] retain];
[dateFormat release];

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZZ"];
[nsRequest setPredicate:[NSPredicate predicateWithFormat:@"publish <= %@ AND expires >= %@", [df dateFromString:todayGregorian], [df dateFromString:todayGregorian]]];

但是我希望我可能是个白痴,并且有更好的方法.有什么建议?

but I'm hoping that maybe I'm being an idiot and there's a better way. Any suggestions?

请注意:

  • 要求我的日语用户进行更改 他们使用的日期格式不是 选项.
  • 我还需要存储 核心数据以公历记录 格式,如这些用户有时所做的 改回他们的日历,
  • Asking my Japanese users to change the date format they use isn't an option.
  • Also, I still need to store the core data dates in a Gregorian format, as these users do sometimes change their calendars back and forth.

实际问题 我上面的解决方案是错误的. TechZen的回答激起了我的注意,我希望我能投票给他100次,最终解决了这个问题.

Real problem My solution above is wrong. TechZen's answer kicked my head in to gear and I wish I could up vote him 100 times, I ended up solving the issue.

我的问题是,进入核心数据的数据是由GMT中的服务器发送的,而格式表解析该数据可能不一定位于GMT中,因此我需要正确调整进入核心数据的时间.

My problem is data going in to Core Data is being sent by the server in GMT and the formmater parsing that data might not necessarily be in GMT so I need to adjust the time going in to Core Data correctly.

推荐答案

您真的别无选择,只能从NSDate转换为各种日历,尤其是在您的用户使用多个日历的情况下.

You really have no choice but to translate from NSDate to various calendars, especially if your users employ multiple calendars.

日历对象本身不是日期,而是专门的格式化程序.实际上,所有日期都以GMT标准格式存储,而不管它们源自哪个日历.然后根据需要将它们翻译/格式化为适当的日历.我们不会在针对西方人的应用程序中立即看到这一点,因为当代西方日历基于格林尼治标准时间(GMT),因此不需要翻译.

Calendar objects are not dates themselves but rather specialized formatters. All dates are actually stored in the GMT standard format regardless of the calendar they originated with. Then they are translated/formatted to the appropriate calendar as needed. We don't see this immediately in apps targeted towards Westerners because contemporary Western calendars are based on the GMT so no translation appears needed.

日期和时间编程看似复杂.我们不觉得它很复杂,因为通常需要我们处理很多繁琐的工作.但是,当您开始做一些不寻常的事情时,复杂性因为显而易见.

Date and time programming is deceptively complex. We don't intuitive feel it is complex because so much of the grunt work is usually handled for us. However, when you start doing something unusual, the complexity because readily apparent.

根据经验,数据和时间编程的时间不是在代码看起来很复杂的时候,而是在看起来简单的时候.

As a rule of thumb, the time to worry with data and time programming is not when the code seems to complex but when it seems to simple.

这篇关于日本日历下的核心数据错误地比较了日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆