Domanda
Come faccio ad avere il tempo GMT?
NSDate *c =[NSDate date];
dà il tempo di sistema, non GMT.
Soluzione
- (NSDate*) convertToUTC:(NSDate*)sourceDate
{
NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone];
NSTimeZone* utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:sourceDate];
NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval gmtInterval = gmtOffset - currentGMTOffset;
NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:sourceDate] autorelease];
return destinationDate;
}
Altri suggerimenti
Questa è una versione più semplice di risposta di Ramin
+ (NSDate *) GMTNow
{
NSDate *sourceDate = [NSDate date];
NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone];
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMT];
[sourceDate addTimeInterval:currentGMTOffset];
return sourceDate;
}
Se è per scopi di visualizzazione che si desidera visualizzare, utilizzare NSDateFormatter in questo modo:
NSDate *myDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
// Set date style:
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *GMTDateString = [dateFormatter stringFromDate: myDate];
Se l'esecuzione di calcoli di data, queste categorie possono essere utili. Dopo aver convertito la data ad essere 'normalizzato' (vale a dire, avendo lo stesso mese, il giorno e l'anno ma a 1200 UTC), se quindi si esegue calcoli successivi utilizzando un NSCalendar che è anche impostato su UTC (+[NSCalendar normalizedCalendar]
), è' ll tutto bene.
@implementation NSDate (NormalizedAdditions)
+ (NSDate *)normalizedDateFromDateInCurrentCalendar:(NSDate *)inDate
{
NSDateComponents *todayComponents = [[NSCalendar currentCalendar] components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
fromDate:inDate];
[todayComponents setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[todayComponents setHour:12];
return [[NSCalendar normalizedCalendar] dateFromComponents:todayComponents];
}
+ (NSDate *)normalizedDate
{
return [self normalizedDateFromDateInCurrentCalendar:[NSDate date]];
}
@end
@implementation NSCalendar (NormalizedAdditions)
+ (NSCalendar *)normalizedCalendar
{
static NSCalendar *gregorian = nil;
if (!gregorian) {
gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
}
return gregorian;
}
@end
+ (NSDate*) convertToGMT:(NSDate*)sourceDate
{
NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone];
NSTimeInterval gmtInterval = [currentTimeZone secondsFromGMTForDate:sourceDate];
NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:sourceDate] autorelease];
return destinationDate;
}
NSDate memorizza il fuso orario internamente - ci sono alcune funzioni è possibile chiamare e passare in un fuso orario di destinazione se si desidera che sono finite una rappresentazione di stringa della data, vedi la documentazione di apple