• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: CoreFoundation and CFDate functions - question on memory de-allocation
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

  • Prev by Date: Re: Thinking about shareware prices...
  • Next by Date: Running an SQLite application on another machine
  • Previous by thread: call it software (was Re: Thinking about shareware prices...)
  • Next by thread: NSView rotation confusion
  • Index(es):
    • Date
    • Thread