Friday, May 3, 2013

NSTimeZone timeZoneForSecondsFromGMT example ios


timeZoneForSecondsFromGMT:

Returns a time zone object offset from Greenwich Mean Time by a given number of seconds.
+ (id)timeZoneForSecondsFromGMT:(NSInteger)seconds
Parameters
seconds
The number of seconds by which the new time zone is offset from GMT.
Return Value of [NSTimeZone timeZoneForSecondsFromGMT]
A time zone object offset from Greenwich Mean Time by seconds.
Discussion
The name of the new time zone is GMT +/– the offset, in hours and minutes. Time zones created with this method never have daylight savings, and the offset is constant no matter the date.
Example of [NSTimeZone timeZoneForSecondsFromGMT]
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:3600*2]]; // GMT+2
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:-3600*6]]; // GMT-6
Example of [NSTimeZone timeZoneForSecondsFromGMT]
NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *now = [NSDate date];
NSTimeZone *tz = [NSTimeZone timeZoneForSecondsFromGMT:(-8 * 3600)]; // for PST
NSDateComponents *dc = [cal components: NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:now];
[cal setTimeZone:tz];
NSDate *newDate = [cal dateFromComponents:dc];