Friday, May 3, 2013

NSTimeZone resetSystemTimeZone example ios


Resets the system time zone object cached by the application, if any.
+ (void)resetSystemTimeZone
Discussion of [NSTimeZone resetSystemTimeZone]
If the application has cached the system time zone, this method clears that cached object. If you subsequently invoke systemTimeZoneNSTimeZone will attempt to redetermine the system time zone and a new object will be created and cached (see systemTimeZone).
Example of [NSTimeZone resetSystemTimeZone]
NSLog(@"Time Str = %@",Time);

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd-MM-yyyy hh:mm a"];

[NSTimeZone resetSystemTimeZone];

NSLog(@"system time zone = %@",[NSTimeZone systemTimeZone]);

[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];

NSDate *date = [dateFormat dateFromString:Time];

[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
NSLog(@"date from string = %@",date);
NSLog(@"string from date = %@",[dateFormat stringFromDate:date]);
Example of [NSTimeZone resetSystemTimeZone]
+ (NSDate*) convertToUTC:(NSDate*)sourceDate {
    [NSTimeZone resetSystemTimeZone];
    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;