Thursday 15 September 2011

ios - Why does NSDateComponents give me a different date? -



ios - Why does NSDateComponents give me a different date? -

here's code:

nsdate *datelocalnow = [self getlocaldate:[nsdate date]]; nslog(@"%@",datelocalnow); nsdatecomponents *datetocheckagainst = [[nscalendar currentcalendar] components:nsminutecalendarunit | nsyearcalendarunit|nsmonthcalendarunit|nsdaycalendarunit|nsminutecalendarunit fromdate: datelocalnow]; nslog(@"%@", datetocheckagainst);

getlocaldate gives me date in local timezone. if nslog datelocalnow outputs:

2014-10-29 01:01:55 +0000

here's getlocaldate source:

-(nsdate *)getlocaldate:(nsdate *)date { nsdate* sourcedate = date; nstimezone* sourcetimezone = [nstimezone timezonewithabbreviation:@"gmt"]; nstimezone* destinationtimezone = [nstimezone systemtimezone]; nsinteger sourcegmtoffset = [sourcetimezone secondsfromgmtfordate:sourcedate]; nsinteger destinationgmtoffset = [destinationtimezone secondsfromgmtfordate:sourcedate]; nstimeinterval interval = destinationgmtoffset - sourcegmtoffset; nsdate* destinationdate = [[nsdate alloc] initwithtimeinterval:interval sincedate:sourcedate]; homecoming destinationdate; }

why nslog datetocheck against give me following?:

calendar year: 2014 month: 10 jump month: no day: 28 minute: 1

an nsdate independent of time zone. internally, stores number of seconds have elapsed since reference point in time. reference point has many human-readable labels. here few human-readable labels cocoa's standard reference date:

1/1/01, 12:00:00 gmt 12/31/00, 6:00:00 pm cst 1/1/01, 4:00:00 gmt+4

these labels represent the same instant in time, instant can labeled in many different ways. there 1 way represent instant nsdate.

you've made new nsdate original nsdate, adjusted time zone offsets, ios sdk doesn't know or care. considers new nsdate instant in time based on offset standard reference date. new instant different original instant (unless scheme time zone happens gmt). shouldn't expect produce same result when converted human-readable string or nsdatecomponents, unless set time zone on nsdateformatter or nscalendar right—which didn't in posted code.

so do this? don't seek create nsdate offset nsdate based on time zone offsets. instead, specify time zone when converting nsdate human-readable string, setting timezone property of nsdateformatter. or set timezone property of nscalendar when inquire components of date. if you're constructing date components, can set timezone of nsdatecomponents before using calendar convert components nsdate.

thus:

#import <foundation/foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { nsdate *now = [nsdate date]; nsdateformatter *f = [[nsdateformatter alloc] init]; f.datestyle = nsdateformattershortstyle; f.timestyle = nsdateformatterlongstyle; f.timezone = [nstimezone systemtimezone]; nslog(@"from formatter = %@", [f stringfromdate:now]); nscalendar *calendar = [nscalendar currentcalendar]; calendar.timezone = [nstimezone systemtimezone]; nsdatecomponents *components = [calendar components:nsminutecalendarunit | nsyearcalendarunit|nsmonthcalendarunit|nsdaycalendarunit fromdate:now]; nslog(@"components = %@", components); } homecoming 0; }

output:

2014-10-29 00:24:19.762 commandline[28543:303] formatter = 10/29/14, 12:24:19 cdt 2014-10-29 00:24:19.763 commandline[28543:303] components = <nsdatecomponents: 0x1001027a0> calendar year: 2014 month: 10 jump month: no day: 29 minute: 24

ios objective-c

No comments:

Post a Comment