ios - How to offset NSDate with UTC timezone offset without hardcoded manual calculation -
imagine current local time being 15:11
utc. retrieve data set server showing opening closing time of business displayed so:
{ close = { day = 3; time = 0200; }; open = { day = 2; time = 1700; };
i receive utc-offset property exposed so: "utc_offset" = "-420”;
imagine minute offset giving hour offset of 7 hours seems right considering timezone i'm in utc , business location's opening hours information i'm receiving business in los angeles 7 hours behind.
how use property able time calculations on want determine whether current local time falls between open , close time bit have figured out calculations come out wrong considering time comparison done in local timezone when needs offset before calculating against time range.
i'm trying avoid doing things like
psuedocode: nsdate.date hour componenent + (utc_offset / 60 = -7 hours)
update: here's how i'm checking if business open right now
if currentarmytime.compare(string(openinfo.time)) != .orderedascending && currentarmytime.compare(string(closeinfo.time)) != .ordereddescending { //the business open right now, though not take consideration business's time zone offset. }
is easier offset current time?
before can use 'open' , 'close' times in date operations need create nsdate calendar has been set time zone times. here's example:
// create calendar time zone nsinteger timeoffsetinseconds = -420 * 60; nstimezone *tz = [nstimezone timezoneforsecondsfromgmt:timeoffsetinseconds]; nscalendar *calendar = [[nscalendar alloc] initwithcalendaridentifier:nscalendaridentifiergregorian]; calendar.timezone = tz; // create nsdate source data nsdatecomponents *comps = [[nsdatecomponents alloc] init]; comps.day = 1; comps.month = 1; comps.year = 2016; comps.hour = 8; comps.minute = 0; nsdate *opentime = [calendar datefromcomponents:comps]; // 'opentime' can compared local time. nslog(@"opentime = %@", opentime); // result opentime = 2016-01-01 15:00:00 +0000
you should put above code method takes in raw time , time offset apply.
Comments
Post a Comment