Re: CoreFoundation and CFDate functions - question on memory de-allocation
Re: CoreFoundation and CFDate functions - question on memory de-allocation
- Subject: Re: CoreFoundation and CFDate functions - question on memory de-allocation
- From: Chris Kane <email@hidden>
- Date: Thu, 8 Jan 2004 16:16:31 -0800
On Jan 7, 2004, at 10:47 PM, Kent Behrends wrote:
// Get current CFDate
CFDateRef aCFDate = CFDateCreate(kCFAllocatorDefault,
CFAbsoluteTimeGetCurrent());
// Create a date formatter and set its format string
CFDateFormatterRef dateFormatter1 = CFDateFormatterCreate(NULL, NULL,
kCFDateFormatterShortStyle, kCFDateFormatterShortStyle);
CFDateFormatterSetFormat(dateFormatter1, CFSTR("yyyy-MM-dd HH:mm:ss"));
CFStringRef dateStr = CFDateFormatterCreateStringWithDate(NULL,
dateFormatter1, aCFDate);
// Copy formatted string into loopData struct
strncpy(loopData->dateTime,CFStringGetCStringPtr(dateStr,
CFStringGetSystemEncoding()),19); // yyyy-mm-dd hh:mm:ss'\0'
In addition to the "Create functions' return values have to be
CFRelease()'d by you" replies, I'd add that this last line is
problematic:
- CFStringGetCStringPtr() can return NULL;
- I wouldn't hard-code the length to 19 either.
For non-localized date formatting into Gregorian calendar with a fixed
format, as in this case, CFDateFormatter is overkill. You can use the
functions in CFDate.h to convert the current CFAbsoluteTime to a
CFGregorianDate struct, then format the pieces yourself (ie, more under
your control) into a C char buffer using snprintf(). You also avoid
creating the temporary CF objects.
Chris Kane
Cocoa Frameworks, Apple
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.