Note that there are some explanatory texts on larger screens.

plurals
  1. PO(iOS, Objective-C) Handling different time zones when calculating dates/times
    primarykey
    data
    text
    <p><strong>Edited 07/08/13: Apple has an excellent set of WWDC videos that really helped me understand the various date and time classes in Objective-C, and how to correctly perform time calculations/manipulations with them.</strong></p> <p><strong>"Solutions to Common Date and Time Challenges" (<a href="http://devstreaming.apple.com/videos/wwdc/2013/227xax5xif2s7s531dsmfs1afo2/227/227-HD.mov?dl=1" rel="nofollow">HD video</a>, <a href="http://devstreaming.apple.com/videos/wwdc/2013/227xax5xif2s7s531dsmfs1afo2/227/227-SD.mov?dl=1" rel="nofollow">SD video</a>, <a href="http://devstreaming.apple.com/videos/wwdc/2013/227xax5xif2s7s531dsmfs1afo2/227/227.pdf?dl=1" rel="nofollow">slides (PDF)</a>) (WWDC 2013)</strong><br> <strong>"Performing Calendar Calculations" (<a href="https://developer.apple.com/videos/wwdc/2011/includes/performing-calendar-calculations.html#performing-calendar-calculations" rel="nofollow">SD video</a>, <a href="https://developer.apple.com/devcenter/download.action?path=/wwdc_2011/adc_on_itunes__wwdc11_sessions__pdf/117performing_calendar_calculations.pdf" rel="nofollow">slides (PDF)</a>) (WWDC 2011)</strong></p> <p><strong>Note: links require a free Apple Developer membership.</strong></p> <p>In <A HREF="http://stackoverflow.com/questions/12829705/programmatically-getting-the-date-next-sunday-at-5pm">this SO question</A>, I asked how I might go about calculating a certain date and time ("next Sunday at 5 PM"). Thanks to the answers I got, I came up with the following code:</p> <pre><code> - (NSDate *) toPacificTime { NSTimeZone *tz = [NSTimeZone timeZoneWithName:@"America/Los_Angeles"]; NSInteger seconds = [tz secondsFromGMTForDate: self]; return [NSDate dateWithTimeInterval: seconds sinceDate: self]; } - (void)handleLiveShowReminders { NSDate *gmtNow = [NSDate date]; NSDate *now = [gmtNow toPacificTime]; NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; [calendar setTimeZone:[NSTimeZone timeZoneWithName:@"America/Los_Angeles"]]; NSDateComponents *dateComponents = [calendar components:NSWeekdayCalendarUnit fromDate:now]; NSInteger weekday = [dateComponents weekday]; NSInteger daysTillNextSunday = 8 - weekday; int secondsInDay = 86400; // 24 * 60 * 60 NSDate *nextSunday = [now dateByAddingTimeInterval:secondsInDay * daysTillNextSunday]; NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:nextSunday]; [components setHour:17]; [components setMinute:00]; [components setTimeZone:[NSTimeZone timeZoneWithName:@"America/Los_Angeles"]]; NSDate *nextSunday5PM = [calendar dateFromComponents:components]; warningInterval = -300; // we want the notification to fire 5 minutes beforehand NSDate *alertDate = [nextSunday5PM dateByAddingTimeInterval:(NSTimeInterval)warningInterval]; UILocalNotification* notifyAlarm = [[[UILocalNotification alloc] init] autorelease]; if (notifyAlarm) { notifyAlarm.fireDate = alertDate; notifyAlarm.timeZone = [NSTimeZone timeZoneWithName:@"America/Los_Angeles"]; notifyAlarm.repeatInterval = NSWeekCalendarUnit; notifyAlarm.soundName = @"alert.aif"; notifyAlarm.alertBody = @"LIVE SHOW REMINDER: The live show is about to start!"; [[UIApplication sharedApplication] scheduleLocalNotification:notifyAlarm]; } } </code></pre> <p>The problem is that, although this code works for me, it's not working for anyone who's not in the PST timezone, as can be seen by this debug output I received from a user in EST:</p> <pre><code>number of notifications = 1 Notification #1 =================== Body: LIVE SHOW REMINDER: The live show is about to start! Details: &lt;UIConcreteLocalNotification: 0x1d5f2200&gt; {fire date = Sunday, December 9, 2012, 4:30:00 PM Pacific Standard Time, time zone = America/Los_Angeles (PST) offset -28800, repeat interval = NSWeekCalendarUnit repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Sunday, December 9, 2012, 4:30:00 PM Eastern Standard Time, user info = (null)} </code></pre> <p>What am I doing wrong here?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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