简体   繁体   中英

Does the iPhone iOS support UNIX C locales?

All I really want to do is format a date using strftime("%x") in the right order. On most platforms a call to setlocale("") is enough. On iPhone iOS CI keep getting !@#$ US dates.

So, does iPhone iOS C not support locales?

See similar question: Does the Android NDK support locales?

No. setlocale() and strftime() does not work in IPhone iOS C, just NULL.

And the ANSI C localeconv() does not work in IPhone iOS C:

struct lconv *localeconv(void);

Also the standard Unix way of getting user name, does not work in Android.

char *p = getenv("USER");

So you need to use the Swift side to get the locale information.

let RFC3339DateFormatter = DateFormatter()
RFC3339DateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
RFC3339DateFormatter.dateFormat = "yyyy-MM-dd"
let LocaleDateFormatter = DateFormatter()
LocaleDateFormatter.locale = Locale(identifier: Locale.preferredLanguages[0]) //NSLocale.current.identifier)
LocaleDateFormatter.dateStyle = .short
let DateString = LocaleDateFormatter.string(from: RFC3339DateFormatter.date(from: "3333-11-22")!)
let LocaleCountry = NSLocale.current.regionCode
let LocaleLanguage = Locale.preferredLanguages[0]
let LocaleCurrency = NSLocale.current.currencyCode
let LocaleShortDateTimePattern = DateString
let LocaleDecimalSeparator = NSLocale.current.decimalSeparator
let LocaleThousandSeparator = NSLocale.current.groupingSeparator
let LocaleThousandGrouping =  "3;0",/* ThousandGrouping locale looks not be implemented by Apple*/
let LocaleNegativePrefix = NumberFormatter().negativePrefix
let LocaleNegativeSuffix = NumberFormatter().negativeSuffix
let Operator = NSUserName()

And then to be used in the C-code, call a C-function with the Swift locale data as parameters and store them there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM