objective c - Is current NSDate in local timezone within range of start and end NSDates in another timezone? -
i need determine if user's current nsdate (local time zone) falls in range of 2 nsdates belonging other time zones.
for example:
i'm in california, , it's 20:00 pst. coffee shop in new york open between 08:00 , 00:00 est. coffee shop stored exact values , sent in xml along tz field indicating america/new_york.
i need able determine if coffee shop open or not in world currently. in example, since it's 23:00 est in new york, open still.
i've tried using nsdate, nstimezone, nscalendar, nsdatecomponents construct algorithm convert, i've been unsuccessful, , when think understand it, confused again. nsdate has no concept of timezone, can't figure out real value exists in it, since need pass time zones nsdateformatter see.
how create method determine if [nsdate date] in [nstimezone localtimezone] between 2 nsdates in other time zone?
nsdate represents single instance in time, irrespective of time zones, calendars, etc.
all need compare date 2 other dates see if lies between them.
it sounds problem getting actual dates compare, isn't hard. top of head sample.
nscalendar *calendar = [[[nscalendar alloc] initwithcalendaridentifier:nsgregoriancalendar] autorelease]; calendar.timezone = [nstimezone timezonewithname:@"america/new_york"]; nsdatecomponents *components = [[[nsdatecomponents alloc] init] autorelease]; components.year = 2011; ... components.hour = 8; nsdate *opening = [calendar datefromcomponents:components]; components.day = components.day + 1; components.hour = 0; nsdate *closing = [calendar datefromcomponents:components]; nsdate *now = [nsdate date]; if ([opening compare:now] == nsorderedascending && [now compare:closing] == nsorderedascending) { // stuff }
alternatively, might easier if convert current time target time zone's date components , examine hour component. way don't need deal making sure values in date components not out of range. plus, not straightforward compute opening , closing date components in general case.
nscalendar *calendar = [[[nscalendar alloc] initwithcalendaridentifier:nsgregoriancalendar] autorelease]; calendar.timezone = [nstimezone timezonewithname:@"america/new_york"]; nsdate *now = [nsdate date]; nsdatecomponents *components = [calendar components:nshourcalendarunit fromdate:now]; if (components.hour > 0 && components.hour < 8) { // stuff }
Comments
Post a Comment