0

I am trying to find out if the recent Do Not Disturb bug (Ars Technica link) is affecting my code. I am trying to get the date and timestamp for midnight the previous Monday. Since I am writing this on a Monday, I would expect to get today's date (January 7, 2013), yet I am January 4, 2013.

I am following the guide posted by Apple, but I am trying to modify it to find the previous Monday, instead of Sunday.

+(NSTimeInterval)StartOfWeekMonday {
    NSTimeInterval finalTime = -1;

    NSInteger weekday;

    NSDate *monday  = 0;

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

    NSDateComponents *weekdays = [greg components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
    NSDateComponents *subtract = [[NSDateComponents alloc] init];
    NSDateComponents *final    = 0;

    weekday = ( weekdays.weekday == 1 ) ? 6 : weekdays.weekday;

    [subtract setDay:(0 - weekday - 1)];

    monday = [greg dateByAddingComponents:subtract toDate:[NSDate date] options:0];
    final  = [greg components:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:monday];
    monday = [greg dateFromComponents:final];

#ifdef DEBUG
    NSLog( @"%s - %d -> Weekday: %d", __FILE__, __LINE__, weekday );
    NSLog( @"%s - %d -> Monday: %@", __FILE__, __LINE__, [monday descriptionWithLocale:[NSLocale currentLocale]] );
#endif

    finalTime = [monday timeIntervalSince1970];


    return finalTime;
}

My log out put below, the weekday is correct (I am writing this on a Monday), yet the date is obviously wrong, it should be Monday: Monday, January 7, 2013, 12:00 AM

2013-01-07 15:07:33.792 MY-APP[5524:14c03] Weekday: 2
2013-01-07 15:07:36.757 MY-APP[5524:14c03] Monday: Friday, January 4, 2013, 12:00:00 AM Eastern Standard Time

This is being in the simulator right now, but all my other date calculations are giving the expected values.

Is it likely that I am being affected by the recent Do Not Disturb Bug and will have to wait for tomorrow (January 8, 2013) to see the expected results, or am I missing something entirely different?

Mike D
  • 4,938
  • 6
  • 43
  • 99
  • 3
    That article is probably not correct about the DND bug; the discrepancy beetween the ISO calendar and Gregorian calendar that the explanation relies on doesn't exist: http://www.tuaw.com/2013/01/03/a-possible-explanation-for-the-ios-new-years-do-not-disturb-bug/ (see Update second paragraph). Have you tried doing your calculations from a dummy date, or moving your system clock around? That should give you some clues. – jscs Jan 07 '13 at 20:26
  • @JoshCaswell Heh, these are the consequences of spreading FUD... *"The bug could be related to the ISO week calculation, or it might not; however the working out in this article is definitely flawed in several ways. The blogger responsible has been taken out the back and shot."* – jtbandes Jan 07 '13 at 21:34

1 Answers1

1

The issue was with how to calculate the subtraction. Either I completely mis understood the example provided by Apple, or it was not clear enough. The solution for calculating the subtraction was as follows:

weekday = ( weekdays.weekday == 1 ) ? 8 : weekdays.weekday;
toSubtract = ( 2 - weekday );

[subtract setDay:toSubtract];

Running various tests produced the proper "Monday." In the end, this had nothing to do with the date bug affecting Do Not Disturb.

Mike D
  • 4,938
  • 6
  • 43
  • 99