Pregunta
¿Cómo llego la hora GMT?
NSDate *c =[NSDate date];
da la hora del sistema, no GMT.
Solución
- (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;
}
Otros consejos
Esta es una versión más simple de la respuesta de Ramin
+ (NSDate *) GMTNow
{
NSDate *sourceDate = [NSDate date];
NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone];
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMT];
[sourceDate addTimeInterval:currentGMTOffset];
return sourceDate;
}
Si es para fines de visualización que desea mostrar, utilice NSDateFormatter este 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];
Si la realización de los cálculos de fecha, estas categorías pueden ser útiles. Después de haber convertido su fecha de ser 'normalizado' (es decir, tienen el mismo mes, el día y el año, pero en 1200 GMT), si a continuación, realiza los cálculos posteriores utilizando un NSCalendar que también está configurado a UTC (+[NSCalendar normalizedCalendar]
), es ll a cabo todo el trabajo.
@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 almacena internamente la zona horaria - hay algunas funciones que puede llamar o pasar de una zona horaria de destino si desea jsut una cadena que representa la fecha, ver documentación de apple