1

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

問題は次のとおりです。現在、午前 12 時 41 分です。アラームは 12.43 に鳴ります。ただし、私の NSLog では、時刻が出力されます: fireDate : 2012-02-16 17:43:00 +0000

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

私は基本的にタイムゾーンをどこにでも置きます。ここに私が使用しているコードがあります:

-(void)scheduleNotificationWithInterval:(int)minutesBefore {

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

}

ありがとう!

4

1 に答える 1

2

末尾の +0000 に注目してください。それがタイムゾーンです。17:43 GMT に鳴ったことを伝えています。まず、タイム ゾーンを設定するには、"America/Montreal" (EST ではなく) を使用するかtimeZoneWithAbbreviation:、EST と共に使用する必要があります。dateComponent は、設定した時間とカレンダーのタイムゾーンで構成されます。そのため、日付は正しいのですが、正しいタイム ゾーンで表示されていません。それを変更するには、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]);
于 2012-02-16T17:55:59.370 に答える