如何将CFAbsoluteTime转换为YYYY-MM-DD HH:MM:SS格式?

时间:2013-06-21 13:53:06

标签: c++ macos core-foundation

如何将CFAbsoluteTime结构转换为YYYY-MM-DD HH:MM:SS格式?

1 个答案:

答案 0 :(得分:4)

你可以使用这段代码(纯CoreFoundation):

CFAbsoluteTime cfTime = CFAbsoluteTimeGetCurrent();
CFDateRef cfDate = CFDateCreate(kCFAllocatorDefault, cfTime);
CFDateFormatterRef dateFormatter = CFDateFormatterCreate(kCFAllocatorDefault, CFLocaleCopyCurrent(), kCFDateFormatterFullStyle, kCFDateFormatterFullStyle);
CFStringRef newString = CFDateFormatterCreateStringWithDate(kCFAllocatorDefault, dateFormatter, cfDate);
CFRelease(dateFormatter);
CFRelease(cfDate);

kCFDateFormatterFullStyle表征格式。选择一个更适合你的方式:)