Locale antincendio esattamente alle 17:00
-
21-12-2019 - |
Domanda
Sto usando al di sotto del codice del codice per sparare la localizzazione della località esattamente alle 17:00.Ma quando accedi al fiume, non sta visualizzando il tempo che volevo.Se sono andato storto da nessuna parte?
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
//localNotification.fireDate = myNewDate;
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
NSDate *currentDate = [NSDate date];
NSDate *fireDate = nil;
//[dateComponents setDay:3]; // ...or whatever day.
[dateComponents setHour:11];
[dateComponents setMinute:30];
fireDate = [calendar dateByAddingComponents:dateComponents
toDate:currentDate
options:0];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//Optionally for time zone converstions
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
NSString *stringFromDate = [formatter stringFromDate:fireDate];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:stringFromDate];
NSLog(@"Fire date : %@",dateFromString);
[localNotification setFireDate:dateFromString];
.
La mia output: Data antincendio : 2014-07-29 16:48:20 +0000
Soluzione
La ragione per cui non stai ottenendo le 17:00 è che non c'è nulla nel codice che tenta di impostare l'ora alle 17:00. Invece hai questo:
[dateComponents setHour:11];
[dateComponents setMinute:30];
fireDate = [calendar dateByAddingComponents:dateComponents
toDate:currentDate
options:0];
.
Cosa ti fa è un tempo 11 ore e 30 minuti in futuro. Non riceverai le 17:00 a meno che non ti capita di eseguire il codice esattamente alle 5:30 del mattino. Ma il codice non menziona IST
ovunque, quindi non riceverai le 17 in quella zona.
È anche confuso ciò che stai facendo alla fine dello snippet di codice ... convertire fireDate
in una stringa, ma quindi convertire quella stringa a destra in una data. Questo non serve alcuno scopo.
Per ottenere le 17 in un particolare fuso orario devi fare qualcosa come il seguente. Ciò ottiene le prossime 17:00 nel fuso orario richiesto, che potrebbe essere domani se è già dopo le 17:00:
NSTimeZone *zone = [NSTimeZone timeZoneWithName:@"Asia/Kolkata"];
NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setTimeZone:zone];
NSInteger targetHour = 17; // 5pm
NSDate *now = [NSDate date];
NSInteger componentFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit;
NSDateComponents *nowDateComponents = [calendar components:componentFlags fromDate:now];
if (nowDateComponents.hour >= targetHour) {
nowDateComponents.day += 1;
}
nowDateComponents.hour = targetHour;
NSDate *targetDate = [calendar dateFromComponents:nowDateComponents];
.
Se vuoi stampare questo in una forma leggibile dall'uomo, puoi fare qualcosa come il seguente. Ma NSDateFormatter
è necessario solo per presentare la data all'utente-- non fa parte di ottenere il NSDate
desiderato.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:zone];
[formatter setDateStyle:NSDateFormatterLongStyle];
[formatter setTimeStyle:NSDateFormatterLongStyle];
NSString *targetDateString = [formatter stringFromDate:targetDate];
NSLog(@"Target date: %@", targetDateString);
.