Alex Rivera | Logout

How To Localize a "Timer" on iPhone

Asked 2010-11-05T20:04:00.503
9

I need to display a timer in "hh:mm:ss" format on the iPhone, but want it localized. Finland, for example uses a period instead of a colon between the time components (hh.mm.ss). Apple's NSDateFormatter would do the trick if I was dealing with a "time" but I need to display hours much greater than 24.

I have not been able to make an NSDate/NSDateFormatter work because when you make one with seconds...

NSDate *aDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTotalSeconds];

... every 86,400 seconds (one day's worth) NSDate automatically increments the day and hours, minutes, and seconds go back to zero. I need to make it work on an any number of seconds without rolling over. For example, with 86,401 seconds I want to display 24:00:01 (or 24.00.01 in Finland).

My code manages total seconds fine, so the only problem I have is the display. A simple...

[NSString stringWithFormat:@"%d%@%d%@%d", hours, sepString, mins, sepString, secs]

... would work if I could find a way to get at a localized "sepString" (the time component separator). NSLocale does not seem to have this.

Thoughts?

Edit
Report

1 Answer

4

Here is information on this topic for others who might be trying to figure out what character different areas of the world use to separate time components. I went through all of the "Region Format" settings on my iPhone - iOS version 4.1 (8B117) and I found all but seven regions use a colon (hh:mm). Here are the others (their NSLocaleCountryCode is in curly braces).

Use a period, hh.mm

• Danish (Denmark) {DK}
• Finnish (Finland) {FI}
• Serbian Montenegro (Cyrillic) {ME}
• Serbian Montenegro (Latin) {ME}
• Serbian (Cyrillic) {RS}
• Serbian (Latin) {RS}

Use a dash, hh-mm

• Marathi (India) {IN}

Note that India (not Marathi-India) also has the country code IN and it uses a colon. So just checking the country code is not enough, you will have to look at other NSLocale fields. I haven't gone through all the fields yet, but I suspect NSLocaleScriptCode or NSLocaleVariantCode might differentiate them. This complexity is why Apple should expose this character to us in the SDK. I did log it in Radar, so if you agree please request it too, quantity of requests matters a lot in what they decide to do.

answered 2010-11-07T03:30:54.070

Your Answer