ios - Getting UTC time for any time and handling daylight saving as well -
i have written function utc time string corresponding time string passed argument function.
+(nsstring *)getutctime:(nsstring *)selectedtime { nsstring *strformat; if ([self is24hourformat:selectedtime]){ strformat = @"hh:mm"; } else{ strformat = @"hh:mm a"; } nslocale *baselocale = [[nslocale alloc] initwithlocaleidentifier:@"en_us_posix"]; nsdateformatter *dateformatterlocal = [[nsdateformatter alloc] init]; [dateformatterlocal settimezone:[nstimezone systemtimezone]]; dateformatterlocal.locale = baselocale; [dateformatterlocal setdateformat:strformat]; nsdate *selecteddate = [dateformatterlocal datefromstring:selectedtime]; nsdateformatter *dateformatterutc = [[nsdateformatter alloc] init]; [dateformatterutc settimezone:[nstimezone timezonewithname:@"utc"]]; dateformatterutc.locale = baselocale; [dateformatterutc setdateformat:strformat]; nsstring *utctimestring = [dateformatterutc stringfromdate:selecteddate]; return utctimestring; }
it working fine mumbai, india time zone. if change timezone london, returning me same time i'm passing in function argument. utc+1 in london. nsdateformatter
handle daylight saving?
you can use nstimezone methods shown below daylight saving: see nstimezone class reference
- isdaylightsavingtimefordate:// tell if day light saving exist date
- daylightsavingtimeoffset // tell how offset time daylight saving
use below code returns day light saving offset
var timezone:nstimezone = nstimezone(name:"europe/london")! print(timezone.daylightsavingtime) //this return true if day light saving present print(timezone.daylightsavingtimeoffset/(60*60))//this give number of hours day light saving offset should print("timezone: \(timezone)")//will give detail timezone
Comments
Post a Comment