Note that there are some explanatory texts on larger screens.

plurals
  1. POSetting a desired time for NSDate object fails when adding a week component to it
    primarykey
    data
    text
    <p>I'm trying to schedule notifications on every 2 weeks at 12:00 (noon) by looping the following code...</p> <pre><code>unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit; // Allocate new temporary calendar object with default timezone NSCalendar *tmpCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; [tmpCalendar setTimeZone:[NSTimeZone defaultTimeZone]]; // Allocate new temporary date components from the date to fire NSDateComponents *tmpDateComps = [tmpCalendar components:unitFlags fromDate:dateToFire]; // Set time to '12.00' [tmpDateComps setHour:12]; [tmpDateComps setMinute:0]; // Get new date to fire object based on date components dateToFire = [tmpCalendar dateFromComponents:tmpDateComps]; NSLog(@"dateToFire %@", dateToFire); // On 6th iteration = xx-xx-xxxx 09:00 +00:00 // On 11th iteration = xx-xx-xxxx 10:00 +00:00 // Allocate new temporary offset date components NSDateComponents *tmpOffsetComps = [[NSDateComponents alloc] init]; // Add a number of weeks [tmpOffsetComps setWeek:2]; // Get new date to fire object based on offset date components dateToFire = [tmpCalendar dateByAddingComponents:tmpOffsetComps toDate:dateToFire options:0]; NSLog(@"dateToFire %@", dateToFire); //On 6th iteration = xx-xx-xxxx 10:00 +00:00 //On 11th iteration = xx-xx-xxxx 09:00 +00:00 </code></pre> <p>Somehow when I log the <code>dateToFire</code> object inside the loop, on the 6th iteration I first get the correct <code>xx-xx-xxxx 09:00 +00:00</code> but on the second log after I've added the offset components, I get <code>xx-xx-xxxx 10:00 +00:00</code>. The strange thing is that it gets back to correct time inside 11th iteration.</p> <p>What am I doing wrong and how to fix this strange behavior?</p> <p><strong>UPDATE</strong><br> As UIAdam stated, this was happening due to daylight saving so this is a normal behavior.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload