views:

49

answers:

1

First one:

+ (NSDate*)convertToUTC:(NSDate*)sourceDate
{
    NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone];
    NSTimeZone* utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];

    NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:sourceDate];
    NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:sourceDate];
    NSTimeInterval gmtInterval = gmtOffset - currentGMTOffset;

    return [NSDate dateWithTimeInterval:gmtInterval sinceDate:sourceDate];
}

Yes, I know this next one is strange but my server gives me a whack date format

+(NSDate *)getDateFromString:(NSString *)dtStr
{
 NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
 NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
 [inputFormatter setLocale:locale];
 [locale release];
 [inputFormatter setDateFormat:@"MMMM, dd yyyy HH:mm:ss"];
 NSDate *formatterDate = [[inputFormatter dateFromString:dtStr] copy];
 [inputFormatter release];
 return formatterDate;
}
+3  A: 

The first one doesn't, but the second one does, because you created a copy and didn't autorelease it. If you don't release it later, it will be leaked.

I don't see why you're even copying the date in the second method. Just cut that out and that will fix the leak.

You really should read (or re-read) the Memory Management Programming Guide for Cocoa, as it seems you need to refine your understanding of the memory-management rules.

Peter Hosey