NSLocale is mostly empty - where are the attributes?
-
22-07-2019 - |
Question
I really just want to know whether the user has set the iPhone to 12 hour mode for time display.
The NSLocale
class seemed to be describing (in vague unspecific terms without any examples or apparently useful methods) what I was after.
So I have created some NSLocale
objects like so:
NSLocale *systemLocaleApparently = [NSLocale systemLocale];
NSLocale *currentLocaleWithLotsOfGoodies = [NSLocale autoupdatingCurrentLocale];
When I inspect these objects using the debugger or NSLog()
, the best I can get is something along these lines:
<CFLocale 0x1278e0 [0x382084f8]>{type = system, identifier = ''}
The current locale has an ID string in it, but nothing else.
From the documentation, I can lookup values from the locale, and one of these is an NSCalendar
object. So I get that back and have a look at it, and it tells me it is "gregorian".
All of which is apparently of use to somebody... but what I would really like is a nice big dictionary of attributes showing all of the actual system properties, mainly so that my NSDateFormatters
don't keep blowing up when a user chooses 12 hour format even though I have forced the formatters to use en_US_POSIX
locale and a fixed setDate
format.
(I'm sure NSCalendarDate
never used to have these problems...)
Hopefully I am missing something blatantly (and probably embarrassingly) obvious, but perhaps someone kind would share it with me. Please :)
Solution
Here is one way to tell if the user has set their time format to a 12- or 24-hour format:
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setTimeStyle:NSDateFormatterMediumStyle];
// Look for H (24-hour format) vs. h (12-hour format) in the date format.
NSString* dateFormat = [formatter dateFormat];
BOOL using24HourClock = [dateFormat rangeOfString:@"h"].location == NSNotFound;