Peter Hosey
Peter Hosey

Reputation: 96323

Reliably testing NSDateFormatter/iOS's 12-hour format-rewriting behavior

On iOS, NSDateFormatter has a “feature” where it rewrites your format string if the user has 12-hour time enabled.

I can fix that easily enough (set the formatter's locale to en_US_POSIX); that's not the difficulty.

My difficulty is that I want to create a test case that detects the 12-hour-rewriting behavior. I want to set things up so that when my formatter (which should only ever emit 24-hour date strings) spits out a 12-hour date string, at least one test case fails.

I have an iOS test bundle set for SDK current (currently 6.1), target 5.0. When I run my tests under this target, all of my times are still 24-hour.

I have at least one report of this occurring in the wild, so all I need now is to reproduce it reliably in a test case.

Upvotes: 1

Views: 760

Answers (1)

rmaddy
rmaddy

Reputation: 318794

Here's one way to check:

NSString *newFmt = [NSDateFormatter dateFormatFromTemplate:@"HH" options:0 locale:[NSLocale currentLocale]];
if ([newFmt hasPrefix:@"h"]) {
    // User in 24-hour locale has iOS device set to 12-hour time
}

This can be tested by running the Settings app and going to General, then International. Set the Region Format to the United Kingdom. Then go to General, Date & Time, and set the time format to 12-hour. Then run this code.

Upvotes: 0

Related Questions