0

I'm trying to get a date that represents noon GMT for a recurring local notification.

I need this local notification to fire at the exact moment to anyone using the app.

So far, I'm trying this:

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setHour:12];
[dateComps setMinute:0];
[dateComps setMonth:1];
[dateComps setDay:1];
[dateComps setYear:2010];

NSDate *localDate = [calendar dateFromComponents:dateComps];
NSTimeInterval timeZoneOffset = [[NSTimeZone defaultTimeZone] secondsFromGMT];
NSTimeInterval gmtTimeInterval = [localDate timeIntervalSinceReferenceDate] - timeZoneOffset;
NSDate *gmtDate = [NSDate dateWithTimeIntervalSinceReferenceDate:gmtTimeInterval];

localNotification.fireDate = gmtDate;

This isn't working, however. Any suggestions?

Edit:: Will this work if I don't specify a time zone on the notfication:

localNotification.fireDate = [NSDate dateWithTimeIntervalSince1970:1284483600];
Dexter
  • 5,666
  • 6
  • 33
  • 45

1 Answers1

1

Does setting the calendar's timezone to GMT work?

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setHour:12];
[dateComps setMinute:0];
[dateComps setMonth:1];
[dateComps setDay:1];
[dateComps setYear:2010]; 

[calendar setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

NSDate *gmtDate = [calendar dateFromComponents:dateComps];
Graham Perks
  • 23,007
  • 8
  • 61
  • 83
  • Is this similar to my edit above, wherein you just create a date with the seconds from epoch that represent this time? Damn, who would have thought this date stuff could be so tricky? – Dexter Sep 14 '10 at 01:30
  • Yes, I think your edit will work too. Everyone will get the same offset from 1970 in GMT, so everyone will fire at the same time. Either should work (I would think...!) – Graham Perks Sep 14 '10 at 01:39