Objective C NSCalendar dateFromComponents不处理第53周 [英] Objective C NSCalendar dateFromComponents not handling week 53

查看:129
本文介绍了Objective C NSCalendar dateFromComponents不处理第53周的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码计算通过之后的给定周的上周号码。

The following code calculates the previous week number from a given week when passed year and week.

NSDateComponents *components = [[NSDateComponents alloc] init];
[components setYear:year];
[components setWeek:NSWeekCalendarUnit];
[components setWeekOfYear:(week-1)]; //Get the previous week
[components setWeekday:2]; //Monday
[components setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [calendar dateFromComponents:components];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyyww"];
NSString *newRevision = [formatter stringFromDate:date];

我很惊讶,这个处理的情况像201101(给了201052),但它没有正确处理有53周的时间。例如,对于201601,它返回201552而不是201153.(至少应该返回201153,根据 http:// en.wikipedia.org/wiki/ISO_week_date 无论如何。)

I was surprised that this handled cases like 201101 (which gives 201052), but it does not correctly handle years where there are 53 weeks. For example, for 201601 it returns 201552 instead of 201153. (At least it should return 201153 according to http://en.wikipedia.org/wiki/ISO_week_date anyway.)

我做错了吗?我已经追踪到,以确保输入是正确的。

Am I doing something wrong? I have traced through to make sure the inputs are correct.

推荐答案

我们结束了解一下这个问题。从问题上的维基百科链接,我们得到了未来400年的53周的所有年份的列表,并硬编码(包含明智的评论和正确解决方案))。

We ended up working around the issue with a bit of hack. From the wikipedia link in the question, we got the list of all years with 53 weeks for the next 400 years and hard coded them in (with judicious comments and the 'correct' solution included).

if (week == 1)
{
    year = year - 1;
    week = 52;
    if (year == 2004 || year == 2009 || year == 2015 || year == 2020 || year == 2026 || year == 2032 || year == 2037 || year == 2043 || year == 2048 || year == 2054 || year == 2060 || year == 2065 || year == 2071 || year == 2076 || year == 2082 || year == 2088 || year == 2093 || year == 2099 || year == 2105 || year == 2111 || year == 2116 || year == 2122 || year == 2128 || year == 2133 || year == 2139 || year == 2144 || year == 2150 || year == 2156 || year == 2161 || year == 2167 || year == 2172 || year == 2178 || year == 2184 || year == 2189 || year == 2195 || year == 2201 || year == 2207 || year == 2212 || year == 2218 || year == 2224 || year == 2229 || year == 2235 || year == 2240 || year == 2246 || year == 2252 || year == 2257 || year == 2263 || year == 2268 || year == 2274 || year == 2280 || year == 2285 || year == 2291 || year == 2296 || year == 2303 || year == 2308 || year == 2314 || year == 2320 || year == 2325 || year == 2331 || year == 2336 || year == 2342 || year == 2348 || year == 2353 || year == 2359 || year == 2364 || year == 2370 || year == 2376 || year == 2381 || year == 2387 || year == 2392 || year == 2398)
    {
        week = 53;
    }
}
else
{
    week = week-1;
}

这不是理想的,但是直到Apple实现ISO 8601或直到2404年。

It's not ideal, but it will do the job until either Apple implements ISO 8601 or until the year 2404.

这篇关于Objective C NSCalendar dateFromComponents不处理第53周的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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