• 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
Does a given locale use a 12 or 24-hour clock? (Re: 24-hour default)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Does a given locale use a 12 or 24-hour clock? (Re: 24-hour default)


  • Subject: Does a given locale use a 12 or 24-hour clock? (Re: 24-hour default)
  • From: Chris Kane <email@hidden>
  • Date: Mon, 12 Dec 2005 14:13:42 -0800

On Oct 19, 2005, at 12:08 AM, Chuck Soper wrote:

The first time my application launches I need to determine if the user uses a 12 or 24 hour time format to be able to display the time appropriately.

I believe that I need to determine if the current locale uses a 24- hour time format or not. Does anyone know how to do this for 10.4.x? I've looked at NSLocale but I don't see this information.

Here's a belated response for the archives ...


The trouble here is, there isn't really just a single per-locale "uses 24-hour clock" state. Each (Short, Medium, Long, Full) time style may use either a 12 or 24-hour clock variant by default in each locale. If both are in use in a locale, it's hard to say that one way or the other is the better choice.

As others posted at the time, you perhaps want to look at the current user's locale instead of a particular named locale, so you pick up the user's preferences. However, this has the same issue, in that the user can cusomize each of the style separately. For example, I may have en_US as my general locale setting, but I may think that "13:25" is a better Short time style than the default "1:25 PM" (for en_US), but I may leave Medium, Long, and Full alone. In fact there are 8 choices the user has for how hours can be display in the base en_US locale. Other locales could have yet other choices even!

In 10.4+, a ... technique ... to resolve the original question would be to use an NSDateFormatter, configured to the 10_4 behavior, GMT time zone, Gregorian calendar, user's locale ([NSLocale currentLocale]), and one of the time styles (date style of None). Perhaps pick the time style most like the times you're going to be displaying, if that makes sense. Then, format a fixed reference time interval, like 79200.0 (2001-01-01 22:00), and test to see if the string contains "22" or not. If not, assume 12-hour.

Of course, it is quite possible that locales may have calendar- specific suites of formatting defaults ("if the calendar is Gregorian do this for Short, if the calendar is Hebrew do this for Short, ..."), so forcing the calendar to a particular one, to gain some predictability so that you can programmatically analyze the string, may still end up giving you something different than what the user typically sees. And if the hour "22" is indeed in the string, but using Unicode code points other than ASCII "2" and "2", you're also out of luck.

Better is to just use NSDateFormatter whenever possible to produce a string from a date for you. But if you're drawing graphics based on the time (like your own clock) rather than a text string, that of course doesn't work -- you can only use NSDateFormatter to guess.

Best is to give the user a preference (or several) that they can set in the application, for that application. For example, when I want dates formatted, I may be generally OK with what en_US gives me, but in the menu bar clock I want something different and specific, and I don't want to have to change my app global settings to get it. Having added the app-specific settings, you can either have them default to something at first run, or try to guess a good starting value using the previous heuristic.


Chris Kane
Cocoa Frameworks, Apple

_______________________________________________
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
  • Prev by Date: Re: Ignore hide messages sent to my application?
  • Next by Date: RE: NSObject Exercise comments?
  • Previous by thread: Re: Getting the arguments of methods in other frameworks (from GDB)
  • Next by thread: Shadowed text view
  • Index(es):
    • Date
    • Thread