如何在iPhone上本地化"计时器"

Swa*_*any 8 iphone ios

我需要在iPhone上以"hh:mm:ss"格式显示一个计时器,但希望它本地化.例如,芬兰在时间成分之间使用句号而不是冒号(hh.mm.ss).如果我处理的是"时间",那么Apple的NSDateFormatter可以解决这个问题,但我需要显示大于24的小时数.

我无法使NSDate/NSDateFormatter工作,因为当你用秒制作一个...

NSDate *aDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTotalSeconds];
Run Code Online (Sandbox Code Playgroud)

...每隔86,400秒(一天的价值)NSDate自动递增日,小时,分钟和秒回零.我需要让它在任何数秒内工作而不会翻滚.例如,在86,401秒我希望显示24:00:01(或芬兰的24.00.01).

我的代码管理总秒数很好,所以我唯一的问题是显示.一个简单的...

[NSString stringWithFormat:@"%d%@%d%@%d", hours, sepString, mins, sepString, secs]
Run Code Online (Sandbox Code Playgroud)

...如果我能找到一种方法来获得本地化的"sepString"(时间组件分隔符),它会工作.NSLocale似乎没有这个.

思考?

Swa*_*any 3

这是一种公认​​的获取任何语言环境的时间分量分隔符的黑客方法。它应该适用于 iOS 3.2 及更高版本。我知道代码可以更简洁,但我打开了“最大详细程度”标志以使其尽可能具有可读性。

//------------------------------------------------------------------------------
- (NSString*)timeComponentSeparator
{
    // Make a sample date (one day, one minute, two seconds)
    NSDate *aDate = [NSDate dateWithTimeIntervalSinceReferenceDate:((24*60*60)+62)];

    // Get the localized time string
    NSDateFormatter *aFormatter = [[NSDateFormatter alloc] init];
    [aFormatter setDateStyle:NSDateFormatterNoStyle];
    [aFormatter setTimeStyle:NSDateFormatterShortStyle];
    NSString *aTimeString = [aFormatter stringFromDate:aDate]; // Not using +localizedStringFromDate... because it is iOS 4.0+

    // Get time component separator
    NSCharacterSet *aCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@":-."];
    NSRange aRange = [aTimeString rangeOfCharacterFromSet:aCharacterSet];
    NSString *aTimeComponentSeparator = [aTimeString substringWithRange:aRange];    

    // Failsafe
    if ([aTimeComponentSeparator length] != 1)
    {
        aTimeComponentSeparator = @":";
    }

    return [[aTimeComponentSeparator copy] autorelease];
}
//------------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)