Friday, May 3, 2013

NSTimeZone secondsFromGMT example ios


secondsFromGMT

Returns the current difference in seconds between the receiver and Greenwich Mean Time.
- (NSInteger)secondsFromGMT
Return Value
The current difference in seconds between the receiver and Greenwich Mean Time.
Example of [NSTimeZone secondsFromGMT]
- (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;
}
Example of [NSTimeZone secondsFromGMT]
NSDate *sourceDate = [NSDate dateWithTimeIntervalSinceNow:3600 * 24 * 60];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
int timeZoneOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate] / 3600;
NSLog(@"sourceDate=%@ timeZoneOffset=%d", sourceDate, timeZoneOffset);