質問

ここでアラームを設定しようとしています。私はモントリオールにいるので、ESTタイムゾーンにあります。私が使用しているコードでは、現在の日付を取得し、数分後にそれを鳴らそうとします。コードは完全に正常に機能し、アラームは予想どおりに鳴ります。

問題は次のとおりです。今の午前12時41分です。アラームは12.43に鳴ります。しかし、私のnslogでは、時間が印刷されます。 Firedate:2012-02-16 17:43:00 +0000

それは機能しているので、それは大きな問題ではありませんが、なぜそれがその時に表示されていて、それでも機能している理由についての考えはありますか?それを修正する方法について何かアイデアはありますか?ありがとう!

私は基本的にTimeZoneをどこにでも置きました。ここに私が使用しているコードがあります。

- (void)scheduleNotificationWithInterval:(int)minutes befer {

// Current date
NSDate *now = [NSDate date];
// Specify which units we would like to use
unsigned units = NSTimeZoneCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSTimeZone* zone = [NSTimeZone timeZoneWithName:@"EST"];
[calendar setTimeZone:zone];
NSDateComponents *components = [calendar components:units fromDate:now];
[components setTimeZone:[NSTimeZone timeZoneWithName:@"EST"]];

NSInteger year = [components year];
NSInteger month = [components month];
NSInteger day = [components day];
NSInteger hour = [components hour];
NSInteger minute = [components minute];

NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setYear:year];
[dateComps setMonth:month];
[dateComps setDay:day];
[dateComps setHour:hour];
[dateComps setMinute:minute+2]; // Temporary
NSDate *itemDate = [calendar dateFromComponents:dateComps];

NSLog(@"fireDate : %@", itemDate);

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
    return;
localNotif.fireDate = itemDate;
//localNotif.timeZone = zone;
localNotif.timeZone = [NSTimeZone timeZoneWithName:@"EST"];

minutesBefore = 15; // Temporary
localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(@"%@ in %i minutes.", nil),
                        @"Blabla", minutesBefore];
localNotif.alertAction = NSLocalizedString(@"See Foo", nil);

localNotif.soundName = UILocalNotificationDefaultSoundName;

NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"LastCall" forKey:@"lastcall"];
localNotif.userInfo = infoDict;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 

}

ありがとう!

役に立ちましたか?

解決

最後の+0000に気づきましたか?それがタイムゾーンです。 17:43 GMTで鳴ったことを伝えています。まず、タイムゾーンを設定するには、「アメリカ/モントリオール」(ESTではない)を使用するか、使用する必要があります timeZoneWithAbbreviation: ESTで。 Date -Componentは、設定された時期とカレンダーのタイムゾーンで構成されます。そのため、日付が正しいのは、適切なタイムゾーンに表示されないだけです。使用する必要があることを変更するには NSDateFormatter. 。どのように以下の例をご覧ください!

 NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSTimeZone* zone = [NSTimeZone timeZoneWithAbbreviation:@"EST"];
[calendar setTimeZone:zone];

NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setHour:16];
[dateComps setYear:2001];
[dateComps setMinute:30];
NSDate *itemDate = [calendar dateFromComponents:dateComps];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeStyle:NSDateFormatterFullStyle];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"EST"]];

NSLog(@"fireDate : %@", [formatter stringFromDate:itemDate]);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top