Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSDateComponents week component of this last week of the year

how many weeks there are in a year? because i have searched on the web and i found there is in total 52.1 week in a year, but if i do this in Xcode:

NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];

[gregorian setFirstWeekday:2];

NSDate* sourceDate = [NSDate date];

NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];

NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;

NSDate* today = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];

NSDateComponents *todaysComponents = [gregorian components:NSWeekCalendarUnit fromDate:today];

NSUInteger todaysWeek = [todaysComponents week];

the todaysWeek value is: 53, how it's possible? why is not 52?

like image 676
Piero Avatar asked Nov 13 '22 14:11

Piero


1 Answers

They converted 52.1 to 53... Its better to show more than to truncate the decimal part.

It also depends on the starting day of the week.

Europe and ISO Standard (from Monday to Sunday) -- 52 full weeks, a short (1 day) week to account for Sunday, January 1st, and a short (1 day) week to account for Monday, December 31st, for a total of 54 weeks.

U.S. Standard (from Sunday to Saturday) -- 52 full weeks, and a short (2 day) week to account for December 30th and 31st, for a total of 53 weeks.

From Saturday to Friday -- 51 full weeks, a short (6 day) week to account for January 1st through January 6th, and a short (3 day) week to account for December 29th through December 31st, for a total of 53 weeks.

EDIT:

Since iOS NSCalender dosent seem to be defining accordingly to ISO 8601 as default. You need to set your calender with method "setMinimumDaysInFirstWeek" to get expected week number result.

Please add this code and check

    [gregorian setMinimumDaysInFirstWeek:5]; //"5" means Thursday in this case.

http://en.wikipedia.org/wiki/ISO_week_date "The first week of a year is the week that contains the first Thursday of the year."

EDIT 2:

For checking the minimum/maximum number of weeks possible in a year

NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
int minWeeks, maxWeek;
minWeeks=maxWeek=0;
for (int weekCounter=1; weekCounter<=7; weekCounter++) {
    [gregorian setMinimumDaysInFirstWeek:weekCounter];
    [gregorian setFirstWeekday:2];
    NSDate* sourceDate = [NSDate date];
    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
    NSDate* today = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];
    NSDateComponents *todaysComponents = [gregorian components:NSWeekCalendarUnit fromDate:today];
    NSUInteger todaysWeek = [todaysComponents week];

    if (todaysWeek>maxWeek) {
        maxWeek=todaysWeek;
    }
    if (todaysWeek<minWeeks || minWeeks==0) {
        minWeeks=todaysWeek;
    }
}
NSLog(@"min:%d, max:%d", minWeeks, maxWeek);
like image 73
Anoop Vaidya Avatar answered Nov 15 '22 07:11

Anoop Vaidya