Re: International date formatting
Re: International date formatting
- Subject: Re: International date formatting
- From: "Sean McBride" <email@hidden>
- Date: Fri, 20 May 2005 13:01:31 -0400
- Organization: Rogue Research
On 20.05.2005 1:45 Uhr, Ben Kazez said:
>I'm new to Cocoa internationalization. Is there a way to format an
>NSDate according to the current locale so that it displays the
>weekday, full month name, and date but no year? In other words, in US
>English I want:
>
> Thursday, May 19
>
>In French from France, this would be (I think):
>
> vendredi 19 mai
How do you know May 19 is a Thursday if you don't know the year?
I also needed this (but without the day of week), and found no built in
way to do it.
>Right now I'm using the following code, but it doesn't include the
>year and worse, it's not changing based on System Preferences >
>International:
I haven't played with 10.4, but in 10.3 and earlier Cocoa is maddeningly
annoying when it comes to making localised NSStrings from NSDates. You
have to go fetching things from NSUserDefaults yourself, etc. In Mac OS
7 it was 1 function call. sigh.
Anyway, I whipped up this solution which seems to work well, but please
tell me if you find any problems.
NSString* sdf = [[NSUserDefaults standardUserDefaults] stringForKey:
NSDateFormatString];
// There doesn't seem to be any perfect way to have a localised format string
// for the case of a date without a year. So we try our best. We
assume that
// the month (%b, %B, %m, %1m) and day (%e, %d) will be adjacent. We
find each
// in the format string, figure out which is first, then keep everything
// in between the two. We fall back to some sane default if something
goes wrong.
NSString* tempStr = nil;
NSRange monthRange = [stdDateFormatStr rangeOfString:@"%B"
options:NSCaseInsensitiveSearch];
if (monthRange.length == 0) {
monthRange = [stdDateFormatStr rangeOfString:@"%m"];
}
if (monthRange.length == 0) {
monthRange = [stdDateFormatStr rangeOfString:@"%1m"];
}
if (monthRange.length != 0) {
NSRange dayRange = [stdDateFormatStr rangeOfString:@"%e"];
if (dayRange.length == 0) {
dayRange = [stdDateFormatStr rangeOfString:@"%d"];
}
if (dayRange.length != 0) {
// Which of day or month is first...
unsigned int firstLoc, lastLoc, lastLen;
if (monthRange.location < dayRange.location) {
firstLoc = monthRange.location;
lastLoc = dayRange.location;
lastLen = dayRange.length;
}
else {
firstLoc = dayRange.location;
lastLoc = monthRange.location;
lastLen = monthRange.length;
}
NSRange subRange = NSMakeRange(firstLoc,
(lastLoc-firstLoc+lastLen));
tempStr = [stdDateFormatStr substringWithRange:subRange];
}
}
if (tempStr == nil) {
tempStr = @"%B %e"; // reasonable default
}
hth,
--
____________________________________________________________
Sean McBride, B. Eng email@hidden
Rogue Research www.rogue-research.com
Mac Software Developer Montréal, Québec, Canada
_______________________________________________
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