开发者

Absolute GMT date on iPhone

开发者 https://www.devze.com 2023-01-15 20:28 出处:网络
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.

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开发者_StackOverflow中文版: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];


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];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号