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 NSDate
object directly, you will get whatever the description
method returns, which is the GMT representation of the date (but that is not carved in stone).
NSDate
objects are not subject to timezones. A NSDate
represents an absolute instant in time measured from a reference date. For example, at the time of writing, the current date is something like 337035053.199801
(seconds since reference date), which can be represented as 2011-09-06 20:50:53 GMT
or 2011-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 NSDateFormatter
to get a string representation of your NSDate
in any timezone you like.