Re: Getting the NSDateFormatter strings from the user's preferences
Re: Getting the NSDateFormatter strings from the user's preferences
- Subject: Re: Getting the NSDateFormatter strings from the user's preferences
- From: James Bucanek <email@hidden>
- Date: Thu, 19 May 2005 16:17:13 -0700
James Bucanek wrote on Wednesday, May 18, 2005:
>Here's my second, annoying, question for the day. Where I can get the date
>and time formatting strings set by the user in the International Preferences
>Pane?
OK, I solved this -- although I think there should have been a more straightforward way of doing this for OS X 10.2/10.3.
What I was looking for was the local's date formatting that is set by the user in the International Preferences. It turns out that the CFDateFormatter... functions can use this with no problem. But NSDateFormatter (pre-10.4) doesn't understand these formats, nor will it work with CFDateFormatter. Even though NSDateFormatter is clearly built off of CFDateFormatter (NSDateFormatter's only instance variable is a CFDateFormatterRef), I find bizarre that NSDateFormatter reimplements the format strings in an incompatible way.
My solution, for anyone who needs something similar in the future, was to simply create my own NSDateFormatter that uses a CFDateFormatter to do its work. I can then initialize CFDateFormatter with the style and local I was looking for, and everything is peaches and cream.
Enjoy,
//
// CompositeOutlineDateFormatter.h
// Quantum Recall
//
// Created by James Bucanek on 5/19/05.
//
#import <Cocoa/Cocoa.h>
@interface CompositeOutlineDateFormatter : NSFormatter
{
CFDateFormatterRef cfDateFormatter;
}
- (id)initWithDateFormatterStyle:(CFDateFormatterStyle)dateStyle
timeStyle:(CFDateFormatterStyle)timeStyle;
- (void)dealloc;
- (NSString *)stringForObjectValue:(id)anObject;
- (BOOL)getObjectValue:(id*)anObject forString:(NSString*)string
errorDescription:(NSString**)error;
//- (NSAttributedString *)attributedStringForObjectValue:(id)anObject
withDefaultAttributes:(NSDictionary *)attributes;
@end
@implementation CompositeOutlineDateFormatter
- (id)initWithDateFormatterStyle:(CFDateFormatterStyle)dateStyle
timeStyle:(CFDateFormatterStyle)timeStyle
{
if ( self = [super init] )
{
CFLocaleRef currentLocalRef = CFLocaleCopyCurrent();
cfDateFormatter = CFDateFormatterCreate
(NULL,currentLocalRef,dateStyle,timeStyle);
CFRelease(currentLocalRef);
}
return (self);
}
- (void)dealloc
{
CFRelease(cfDateFormatter);
[super dealloc];
}
- (NSString *)stringForObjectValue:(id)object
{
if ([object isMemberOfClass:[NSObject class]])
{
// If this appears to be an NSObject, test to see if it's an
NSDate
if ([object isMemberOfClass:[NSDate class]])
return ([(NSString*)CFDateFormatterCreateStringWithDate
(NULL,cfDateFormatter,(CFDateRef)object) autorelease]);
}
else
{
// The "toll-free-bridge" objects don't respond to
isMemberOfClass: try getting the CFType of the object instead
if (CFGetTypeID((CFTypeRef)object)==CFDateGetTypeID())
return ([(NSString*)CFDateFormatterCreateStringWithDate
(NULL,cfDateFormatter,(CFDateRef)object) autorelease]);
}
// If it's anything other than a NSDate or CFDate object, just
convert it to a string
return ([object description]);
}
- (BOOL)getObjectValue:(id*)anObject forString:(NSString*)string
errorDescription:(NSString **)error
{
#pragma unused(anObject)
#pragma unused(string)
if (error!=NULL)
*error = @"unimplemented";
return (NO);
}
@end
--
James Bucanek <mailto:email@hidden>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden