Not able to set NSTimeZone to a required format
-
06-12-2019 - |
Pregunta
I have a TimeZone as NSString value given as "GMT-09:00".
All I want is to setup my NSTimeZone with this format. I tried but wasn't able to do this. This time may vary every time so i can't set it as static in the method
eg. i can't do this -- timeZoneForSecondsFromGMT:(-9*3600).
Also, can I convert the above format (GMT-09:00) into number of seconds anyhow??
EDIT : I just want to set the time zone to a specific GMT format. It could be anything, GMT-09:00, GMT+05:30, etc.
If it was a static value I could have used the below method for NSTimeZone.
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:(-9*3600)]];
But the value "GMT-09:00" is an NSString. So everytime, I would have to break the string and get the values for Hours and minutes. Is there an easier way?????
Thanks for the help!
Solución
have you tried something like this?
NSString *tzStr = @"GMT-09:00";
NSTimeZone *tz = [[NSTimeZone alloc] initWithName:tzStr];
NSLog(@"Tz: %@", tz);
NSLog(@"Offset: %d", [tz secondsFromGMT]);
// Tz: GMT-0900 (GMT-09:00) offset -32400
// Offset: -32400
works with @"GMT+5:30"
too.
// Tz: GMT+0530 (GMT+05:30) offset 19800
and then just use [formatter setTimeZone:tz];
Otros consejos
this one is helpful to you. optimized help
float offsetSecond = ([[NSTimeZone systemTimeZone] secondsFromGMT] / 3600.0);