2

I'm having trouble getting my head around time zones and can't seem to solve it.

The user should enter a time for an alarm to go off. So they choose 6:00am Sunday (using a pickerView) while in Sydney Australia's time zone.

Then when their device changes time zone to Los Angeles, USA, the alarm should still go off at 6:00am in LA's time (which is now 1:00AM or something in Sydney's time).

Setting the notification:

UILocalNotification *localNotification;
[localNotification setTimeZone:[NSTimeZone localTimeZone]];

Reading the notification to display in a TableView:

NSDate *fd = <the firedate of the scheduled notification>
NSDateFormatter* df_local = [[[NSDateFormatter alloc] init] autorelease];
//[df_local setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
[df_local setTimeZone:[NSTimeZone localTimeZone]];
[df_local setDateFormat:@"hh:mm"];
NSString* ts_local_string = [df_local stringFromDate:fd];

Using that code I set the time to 6:00am in Sydney, but when viewing it in Los Angeles, the time is now 1:00pm.

What should I set the timezone to when Setting the UILocalNotification, and what I should set the timezone to when reading the Notification, so that the time is 6:00am localtime when viewed in both Sydney and Los Angeles.

Thanks

user3015717
  • 226
  • 2
  • 7
  • "timeZone - The date specified in fireDate is interpreted according to the value of this property. If you specify nil (the default), the fire date is interpreted as an absolute GMT time, which is suitable for cases such as countdown timers. If you assign a valid NSTimeZone object to this property, the fire date is interpreted as a wall-clock time that is automatically adjusted when there are changes in time zones; an example suitable for this case is an an alarm clock." – Hot Licks Dec 06 '13 at 03:13
  • (This is one the rare cases where you should "fudge" the NSDate timezone -- set it using a formatter set to GMT, so that NSDate will reflect local time vs GMT.) – Hot Licks Dec 06 '13 at 03:16
  • So you're saying I should fudge the displayed time? I did some testing, and the notification fires at the correct time (ie. 6:00am in every time zone's local time) it's just that it displays the wrong time with the dateFormatter – user3015717 Dec 06 '13 at 04:57
  • Because you're not setting the dateFormatter to GMT. – Hot Licks Dec 06 '13 at 18:02
  • Setting the dateFormatter to GMT still made the displayed time change when switching timezones. Which was then not accurate to when the notification actually fired (relative to the user's time zone at the time) – user3015717 Dec 08 '13 at 04:33
  • NSLog the NSDate (unformatted) version fireDate with the phone set in different timezones. Does the time change, or does it remain fixed? Is it the time you set? – Hot Licks Dec 08 '13 at 13:32

1 Answers1

0

I think I've manage to figure it out.

UILocalNotification *localNotification;
[localNotification setTimeZone:[NSTimeZone localTimeZone]];
    [localNotification setFireDate:fireDate];

This will make whatever Date is in fireDate the time/Date that the notification will go off, in the user's current timezone. So it will go off at 6:00am in Sydney, then again at 6:00am in Los Angeles, etc.

So that part was working as intended, where I had made the mistake was in displaying the time.

Instead of the code in my question I needed to set the timeZone of the NSDateFormatter to the timeZone of the UILocalNotification:

UILocalNotification *myLN = <The UILocalNotification we've retrieved from somewhere>;
NSDateFormatter* df_local = [[[NSDateFormatter alloc] init] autorelease];
[df_local setTimeZone:myLN.timeZone];
[df_local setDateFormat:@"hh:mm"];
NSString* ts_local_string = [df_local stringFromDate:fd];
user3015717
  • 226
  • 2
  • 7