2012-02-16 4 views
1

ここでアラームを設定しようとしています。私はモントリオールにありますので、ESTタイムゾーンにあります。 私が使用しているコードでは、現在の日付を取得して、それを数分後に呼び出します。 コードは正常に動作し、アラームは期待どおりに鳴ります。UILocalNotification&NSTimeZone、タイムゾーンが正しく表示されない

ここが問題です:今は12.41です。アラームは12.43で鳴ります。 しかし、私のNSLogで、時間が出力されます。fireDate:2012-02-16夜05時43分00秒0000

それはそれが動作するので、大きな問題ではないのですが、なぜそれがで表示されて上の任意のアイデアその時、まだ動作しますか?どのようにそれを修正する上で任意のアイデア?ありがとう! (空)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]; 

}

は、私は基本的にどこでもタイムゾーンを置く

、ここで私が使用していたコードですありがとう!

答えて

2

最後に+0000があります。これがタイムゾーンです。それは17:43 GMTに達したことを伝えています。まず、あなたのタイムゾーンを設定するには、「アメリカ/モントリオール」(ESTではなく)を使用するか、ESTにtimeZoneWithAbbreviation:を使用する必要があります。 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]); 
+0

私は+0000についてはわかりませんでした。しかし、私は_dateComps.timeZone = [NSTimeZone timeZoneWithName:@ "EST"]を追加しました。dateComps initの直後に_まだ取得しました** fireDate:2012-02-16 18:07:00 + 0000 ** –

+0

申し訳ありません、EST有効な名前ではありません。 "America/Montreal"(解答で編集)を試してみてください。 [NSTimeZone knownTimeZoneNames]を使用して有効な名前の配列を取得できます。 – MGA

+0

便利です! [NSTimeZone knownTimeZoneNames]を使って見つかったものをどこにコピーして貼り付けたのですか?しかし、私は奇妙なことに+0000を持っています。私はすべての変数を設定する前にsetTimeZonesを移動し、それでも取得します! –

関連する問題