iPhone OS4.3:NSCalendar 的 [ordinalityOfUnit:inUnit:forDate:] 方法返回不同/错误的结果

发布于 2024-10-31 07:56:25 字数 487 浏览 2 评论 0原文

我有以下代码片段,我用它来推断一周的第一天,以显示在我的日历视图中。这段代码一直运行没有任何问题,直到我在 iPhone OS 4.3 及以上版本上对其进行测试。

int firstDOW = [m_calendar ordinalityOfUnit:NSWeekdayCalendarUnit inUnit:NSWeekCalendarUnit forDate:fom]%7;

其中“fom”是该月的第一个日期。
调试代码时,在 4.3 之前的 iOS 上,“ordinality”方法的返回值似乎返回正确的值(例如,如果“fom”日期是星期五,则上述方法返回的值为“5”)。但在 iOS >= 4.3 上,返回值在某种程度上不是正确的工作日(例如,如果“fom”日期是星期五,则上述方法返回的值为“6”!)。
我不明白,是否是我的代码有问题,或者确实是上述方法的错误。
还有其他人在 iOS >= 4.3 中遇到过这个吗?

谢谢和问候。

I've following code snippet which I'm using to deduce the first day of the week, to display in my calendar view. This code has been working without any issue, till I tested it on iPhone OS 4.3 onwards.

int firstDOW = [m_calendar ordinalityOfUnit:NSWeekdayCalendarUnit inUnit:NSWeekCalendarUnit forDate:fom]%7;

where 'fom' is the first date of the month.
When debugged the code, on iOS prior to 4.3, return value for 'ordinality' method seems to return correct value( for example if the 'fom' date falls on Friday, the value returned from above method is '5'). But on iOS >= 4.3, the return value is somehow not the correct weekday( for example, if the 'fom' date falls on Friday, the value returned from above method is '6'!).
I don't understand, whether there is any issue with my code, or it really is a bug in the above method.
Has anyone else faced this in iOS >= 4.3??

Thanks and Regards.

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

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

发布评论

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

评论(1

梦过后 2024-11-07 07:56:25

我对这个方法调用有了更多的了解。看来ordinalityOfUnit:inUnit:forDate: 方法关注当前区域设置是什么以及该区域设置认为一周的第一天是什么。

例如。在美国语言环境中,一周的第一天被视为星期日,而在爱尔兰语言环境中,一周的第一天被视为星期一。

如果您位于美国区域,并询问星期二的序数,它将返回 2(0 表示星期日,1 表示星期一,2 表示星期二)。然而,在爱尔兰语言环境中,它将返回 1(周一为 0,周二为 1)。

简而言之,一周内一天的顺序取决于哪一天被视为一周的开始。相反,如果您使用 NSDateComponents API 并从那里获取星期几,您总是会获得相对于星期日 === 0 的一天。例如

NSDateComponents *components = [[self calendar] components: NSWeekdayCalendarUnit fromDate: [self startingDay]];
NSUInteger weekdayIndex = [components weekday];

I learned a bit more about this method call. It appears that the ordinalityOfUnit:inUnit:forDate: method pays attention to what current locale is and what that locale considers the first day of the week to be.

For example. In the United States locale the first day of the week is considered to be Sunday while in the Ireland locale, the first day of the week is considered to be Monday.

If you are in the US locale, and ask for the ordinality of Tuesday, it will return 2 (0 for Sunday, 1 for Monday and 2 for Tuesday). In the Ireland locale, however, it will return 1 (0 for Monday and 1 for Tuesday).

In short, the ordinality of the day within the week depends on which day is considered to begin the week. In contrast, if you use the NSDateComponents API and get the day of the week from there, you always get a day relative to Sunday === 0. e.g.

NSDateComponents *components = [[self calendar] components: NSWeekdayCalendarUnit fromDate: [self startingDay]];
NSUInteger weekdayIndex = [components weekday];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文