This code
NSCalendar *calendar =[NSCalendar currentCalendar];
[gregorian setTimeZone:tz];
NSLog(@"%@", [gregorian timeZone]);
NSDateComponents *comp = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit) fromDate:[NSDate date]];
[comp setDay:info.day];
[comp setMonth:info.month];
[comp setYear:info.year];
[comp setHour:info.hour];
[comp setMinute:info.minute];
[comp setSecond:info.second];
[comp setTimeZone:tz];
NSLog(@"%@", [comp timeZone]);
NSLog(@"%@", [gregorian dateFromComponents:comp]);
shows the following in console:
Europe/Moscow (MSKS) offset 14400 (Daylight)
Europe/Moscow (MSKS) offset 14400 (Daylight)
2011-08-31 20:00:00 +0000
So, the timezone for calendar and components is specified correctly, but [gregorian dateFromComponents:comp] returns NSDate value with wrong time zone (GMT).
What do I need to correct to get proper timezone?
The output you are seeing is perfectly normal. If you NSLog a
NSDateobject directly, you will get whatever thedescriptionmethod returns, which is the GMT representation of the date (but that is not carved in stone).NSDateobjects are not subject to timezones. ANSDaterepresents an absolute instant in time measured from a reference date. For example, at the time of writing, the current date is something like337035053.199801(seconds since reference date), which can be represented as2011-09-06 20:50:53 GMTor2011-09-06 22:50:53 CEST. Both are different human readable representations of the same date.In conclusion, what do you need to get the proper timezone? You need to use
NSDateFormatterto get a string representation of yourNSDatein any timezone you like.