Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find if user prefers 12 / 24 Hour Clock?

I've got a drawRect that makes a timeline a bit like iCal. I use a for loop to write the times along a scroll view. I was wondering if A) theres a way of determining whether the user has chosen a 12 or 24 hour clock in the system settings and B) if there is a more efficient way of changing the time labels then calling an 'if' query every pass of the 'for' loop. Cheers

like image 465
sobox studio Avatar asked Dec 15 '11 12:12

sobox studio


1 Answers

The earlier answers assume that the "AM" and "PM" symbols are represented in roman characters. This code adapted from keyur bhalodiya does a better job at handling languages like Chinese, by using the AMSymbol and PMSymbol methods of NSDateFormatter.

-(BOOL)uses24hourTime
{
     NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
     [formatter setLocale:[NSLocale currentLocale]];
     [formatter setDateStyle:NSDateFormatterNoStyle];
     [formatter setTimeStyle:NSDateFormatterShortStyle];

     NSString *dateString = [formatter stringFromDate:[NSDate date]];
     NSRange amRange = [dateString rangeOfString:[formatter AMSymbol]];
     NSRange pmRange = [dateString rangeOfString:[formatter PMSymbol]];

     return (amRange.location == NSNotFound && pmRange.location == NSNotFound);
}
like image 181
William Denniss Avatar answered Nov 15 '22 13:11

William Denniss