Pergunta

For example I have this code:

[self.df setDateFormat:@"z"]

which outputs with GMT I want an output of it as GMT+1

or GMT-4 to GMT-3 or GMT+9 to GMT+10

Is that possible with NSTimeZone?

Foi útil?

Solução

I've made it work by doing following.

NSTimeZone *timeZone = [NSTimeZone localTimeZone] ; 

to get my local time zone.

In NSTimeZone class, there is method called secondsFromGMT. From doc:

which returns the current difference in seconds between the receiver and Greenwich Mean Time.

I'm in GMT+02, in my case it returns 7200. As 7200 in seconds, there is two hours difference between my time zone and Greenwich.

Then, you can create a new time zone using timeZoneForSecondsFromGMT methods. As you want to add one hour, you need to add 3600 seconds to GMT. So, here is the code.

NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
NSInteger secondsFromGMT = [localTimeZone secondsFromGMT];
NSTimeZone *timeZoneYouWanted = [NSTimeZone timeZoneForSecondsFromGMT:secondsFromGMT + 3600];

I've tested timeZoneYouWanted is in GMT+03.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top