views:

124

answers:

2

In my app I DO NOT want to use the local time on the phone. I want to get the current time in GMT. To do this I have a date formatter with time zone set to GMT. I then do the following to get back the current time:

NSString *dateOne = [df stringFromDate:[NSDate date]];
NSDate *date1 = [df dateFromString:dateOne];

My app gets an EXC_BAD_ACCESS though on the dateFromString call. I checked the returned value from dateOne and it's format matches the dateformatters. What am I doing wrong? Is there a better way to do this?

Thanks

EDIT:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-mm-dd HH:mm:ss"];
A: 

Hi John,

Please could you post more of surrounding code, especially where you allocate your NSDateFormatter 'df'?

EddieCatflap
Hi, I have posted the extra code.
John
That looks fine to me John, I would suggest enabling the NSZombieEnabled environment variable which will greatly help you find the real culprit of your memory woes. Use this link to help you set it up: http://www.tomwhitson.co.uk/blog/2009/04/debugging-with-nszombiesenabled/
EddieCatflap
Thanks for the help, I'll try that out.
John
A: 

An easier way perhaps:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-mm-dd HH:mm:ss"];

NSTimeZone *tz = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[dateFormatter setTimeZone:tz];

NSDate *date = [NSDate date];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(@"Time now in GMT: %@",dateString);
kharrison