Qt 4.8
Functions
qlocale_mac.mm File Reference
#include "qlocale_p.h"
#include "qstringlist.h"
#include "qvariant.h"
#include "qdatetime.h"
#include "private/qcore_mac_p.h"
#include <CoreFoundation/CoreFoundation.h>

Go to the source code of this file.

Functions

static QByteArray envVarLocale ()
 
static QString getCFLocaleValue (CFStringRef key)
 
QString getMacDateFormat (CFDateFormatterStyle style)
 
static QByteArray getMacLocaleName ()
 
static QString getMacTimeFormat (CFDateFormatterStyle style)
 
static QString macCurrencySymbol (QLocale::CurrencySymbolFormat format)
 
static QString macDateToString (const QDate &date, bool short_format)
 
static QString macDayName (int day, bool short_format)
 
static quint8 macFirstDayOfWeek ()
 
static QString macFormatCurrency (const QSystemLocale::CurrencyToStringArgument &arg)
 
static QLocale::MeasurementSystem macMeasurementSystem ()
 
static QString macMonthName (int month, bool short_format)
 
static QVariant macQuoteString (QSystemLocale::QueryType type, const QStringRef &str)
 
static QString macTimeToString (const QTime &time, bool short_format)
 
static QString macToQtFormat (const QString &sys_fmt)
 

Function Documentation

◆ envVarLocale()

static QByteArray envVarLocale ( )
static

Definition at line 59 of file qlocale_mac.mm.

Referenced by getMacLocaleName().

60 {
61  static QByteArray lang = 0;
62 #ifdef Q_OS_UNIX
63  lang = qgetenv("LC_ALL");
64  if (lang.isEmpty())
65  lang = qgetenv("LC_NUMERIC");
66  if (lang.isEmpty())
67 #endif
68  lang = qgetenv("LANG");
69  return lang;
70 }
Q_CORE_EXPORT QByteArray qgetenv(const char *varName)
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421

◆ getCFLocaleValue()

static QString getCFLocaleValue ( CFStringRef  key)
static

Definition at line 309 of file qlocale_mac.mm.

Referenced by macQuoteString().

310 {
311  QCFType<CFLocaleRef> locale = CFLocaleCopyCurrent();
312  CFTypeRef value = CFLocaleGetValue(locale, key);
313  return QCFString::toQString(CFStringRef(static_cast<CFTypeRef>(value)));
314 }
const struct __CFString * CFStringRef
static QString toQString(CFStringRef cfstr)
Definition: qcore_mac.cpp:47
const void * CFTypeRef
int key

◆ getMacDateFormat()

QString getMacDateFormat ( CFDateFormatterStyle  style)

Definition at line 293 of file qlocale_mac.mm.

Referenced by macQuoteString().

294 {
295  QCFType<CFLocaleRef> l = CFLocaleCopyCurrent();
296  QCFType<CFDateFormatterRef> formatter = CFDateFormatterCreate(kCFAllocatorDefault,
297  l, style, kCFDateFormatterNoStyle);
298  return macToQtFormat(QCFString::toQString(CFDateFormatterGetFormat(formatter)));
299 }
static QString toQString(CFStringRef cfstr)
Definition: qcore_mac.cpp:47
QFactoryLoader * l
static QString macToQtFormat(const QString &sys_fmt)
Definition: qlocale_mac.mm:174

◆ getMacLocaleName()

static QByteArray getMacLocaleName ( )
static

Definition at line 72 of file qlocale_mac.mm.

Referenced by macQuoteString().

73 {
74  QByteArray result = envVarLocale();
75 
76  QString lang, script, cntry;
77  if (result.isEmpty() || result != "C"
78  && !qt_splitLocaleName(QString::fromLocal8Bit(result), lang, script, cntry)) {
79  QCFType<CFLocaleRef> l = CFLocaleCopyCurrent();
80  CFStringRef locale = CFLocaleGetIdentifier(l);
81  result = QCFString::toQString(locale).toUtf8();
82  }
83  return result;
84 }
const struct __CFString * CFStringRef
static QString fromLocal8Bit(const char *, int size=-1)
Returns a QString initialized with the first size characters of the 8-bit string str.
Definition: qstring.cpp:4245
QByteArray toUtf8() const Q_REQUIRED_RESULT
Returns a UTF-8 representation of the string as a QByteArray.
Definition: qstring.cpp:4074
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
static QByteArray envVarLocale()
Definition: qlocale_mac.mm:59
static QString toQString(CFStringRef cfstr)
Definition: qcore_mac.cpp:47
bool qt_splitLocaleName(const QString &name, QString &lang, QString &script, QString &cntry)
Definition: qlocale.cpp:302
The QString class provides a Unicode character string.
Definition: qstring.h:83
Q_CORE_EXPORT int QT_FASTCALL script(uint ucs4)
QFactoryLoader * l
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421

◆ getMacTimeFormat()

static QString getMacTimeFormat ( CFDateFormatterStyle  style)
static

Definition at line 301 of file qlocale_mac.mm.

Referenced by macQuoteString().

302 {
303  QCFType<CFLocaleRef> l = CFLocaleCopyCurrent();
304  QCFType<CFDateFormatterRef> formatter = CFDateFormatterCreate(kCFAllocatorDefault,
305  l, kCFDateFormatterNoStyle, style);
306  return macToQtFormat(QCFString::toQString(CFDateFormatterGetFormat(formatter)));
307 }
static QString toQString(CFStringRef cfstr)
Definition: qcore_mac.cpp:47
QFactoryLoader * l
static QString macToQtFormat(const QString &sys_fmt)
Definition: qlocale_mac.mm:174

◆ macCurrencySymbol()

static QString macCurrencySymbol ( QLocale::CurrencySymbolFormat  format)
static

Definition at line 337 of file qlocale_mac.mm.

Referenced by macQuoteString().

338 {
339  QCFType<CFLocaleRef> locale = CFLocaleCopyCurrent();
340  switch (format) {
342  return QCFString::toQString(static_cast<CFStringRef>(CFLocaleGetValue(locale, kCFLocaleCurrencyCode)));
344  return QCFString::toQString(static_cast<CFStringRef>(CFLocaleGetValue(locale, kCFLocaleCurrencySymbol)));
346  CFStringRef code = static_cast<CFStringRef>(CFLocaleGetValue(locale, kCFLocaleCurrencyCode));
347  QCFType<CFStringRef> value = CFLocaleCopyDisplayNameForPropertyValue(locale, kCFLocaleCurrencyCode, code);
348  return QCFString::toQString(value);
349  }
350  default:
351  break;
352  }
353  return QString();
354 }
const struct __CFString * CFStringRef
static QString toQString(CFStringRef cfstr)
Definition: qcore_mac.cpp:47
The QString class provides a Unicode character string.
Definition: qstring.h:83

◆ macDateToString()

static QString macDateToString ( const QDate date,
bool  short_format 
)
static

Definition at line 124 of file qlocale_mac.mm.

Referenced by macQuoteString().

125 {
126  CFGregorianDate macGDate;
127  macGDate.year = date.year();
128  macGDate.month = date.month();
129  macGDate.day = date.day();
130  macGDate.hour = 0;
131  macGDate.minute = 0;
132  macGDate.second = 0.0;
133  QCFType<CFDateRef> myDate
134  = CFDateCreate(0, CFGregorianDateGetAbsoluteTime(macGDate,
135  QCFType<CFTimeZoneRef>(CFTimeZoneCopyDefault())));
136  QCFType<CFLocaleRef> mylocale = CFLocaleCopyCurrent();
137  CFDateFormatterStyle style = short_format ? kCFDateFormatterShortStyle : kCFDateFormatterLongStyle;
138  QCFType<CFDateFormatterRef> myFormatter
139  = CFDateFormatterCreate(kCFAllocatorDefault,
140  mylocale, style,
141  kCFDateFormatterNoStyle);
142  return QCFString(CFDateFormatterCreateStringWithDate(0, myFormatter, myDate));
143 }
int month() const
Returns the number corresponding to the month of this date, using the following convention: ...
Definition: qdatetime.cpp:382
int day() const
Returns the day of the month (1 to 31) of this date.
Definition: qdatetime.cpp:395
int year() const
Returns the year of this date.
Definition: qdatetime.cpp:353

◆ macDayName()

static QString macDayName ( int  day,
bool  short_format 
)
static

Definition at line 106 of file qlocale_mac.mm.

Referenced by macQuoteString().

107 {
108  if (day < 1 || day > 7)
109  return QString();
110 
112  = CFDateFormatterCreate(0, QCFType<CFLocaleRef>(CFLocaleCopyCurrent()),
113  kCFDateFormatterNoStyle, kCFDateFormatterNoStyle);
114  QCFType<CFArrayRef> values = static_cast<CFArrayRef>(CFDateFormatterCopyProperty(formatter,
115  short_format ? kCFDateFormatterShortWeekdaySymbols
116  : kCFDateFormatterWeekdaySymbols));
117  if (values != 0) {
118  CFStringRef cfstring = static_cast<CFStringRef>(CFArrayGetValueAtIndex(values, day % 7));
119  return QCFString::toQString(cfstring);
120  }
121  return QString();
122 }
const struct __CFString * CFStringRef
static QString toQString(CFStringRef cfstr)
Definition: qcore_mac.cpp:47
The QString class provides a Unicode character string.
Definition: qstring.h:83
quint16 values[128]
const struct __CFArray * CFArrayRef

◆ macFirstDayOfWeek()

static quint8 macFirstDayOfWeek ( )
static

Definition at line 328 of file qlocale_mac.mm.

Referenced by macQuoteString().

329 {
330  QCFType<CFCalendarRef> calendar = CFCalendarCopyCurrent();
331  quint8 day = static_cast<quint8>(CFCalendarGetFirstWeekday(calendar))-1;
332  if (day == 0)
333  day = 7;
334  return day;
335 }
unsigned char quint8
Definition: qglobal.h:934

◆ macFormatCurrency()

static QString macFormatCurrency ( const QSystemLocale::CurrencyToStringArgument arg)
static

Definition at line 357 of file qlocale_mac.mm.

Referenced by macQuoteString().

358 {
359  QCFType<CFNumberRef> value;
360  switch (arg.value.type()) {
361  case QVariant::Int:
362  case QVariant::UInt: {
363  int v = arg.value.toInt();
364  value = CFNumberCreate(NULL, kCFNumberIntType, &v);
365  break;
366  }
367  case QVariant::Double: {
368  double v = arg.value.toDouble();
369  value = CFNumberCreate(NULL, kCFNumberDoubleType, &v);
370  break;
371  }
372  case QVariant::LongLong:
373  case QVariant::ULongLong: {
374  qint64 v = arg.value.toLongLong();
375  value = CFNumberCreate(NULL, kCFNumberLongLongType, &v);
376  break;
377  }
378  default:
379  return QString();
380  }
381 
382  QCFType<CFLocaleRef> locale = CFLocaleCopyCurrent();
383  QCFType<CFNumberFormatterRef> currencyFormatter =
384  CFNumberFormatterCreate(NULL, locale, kCFNumberFormatterCurrencyStyle);
385  if (!arg.symbol.isEmpty()) {
386  CFNumberFormatterSetProperty(currencyFormatter, kCFNumberFormatterCurrencySymbol,
388  }
389  QCFType<CFStringRef> result = CFNumberFormatterCreateStringWithNumber(NULL, currencyFormatter, value);
390  return QCFString::toQString(result);
391 }
static CFStringRef toCFStringRef(const QString &str)
Definition: qcore_mac.cpp:69
static QString toQString(CFStringRef cfstr)
Definition: qcore_mac.cpp:47
The QString class provides a Unicode character string.
Definition: qstring.h:83
int toInt(bool *ok=0) const
Returns the variant as an int if the variant has type() Int , Bool , ByteArray , Char ...
Definition: qvariant.cpp:2625
qlonglong toLongLong(bool *ok=0) const
Returns the variant as a long long int if the variant has type() LongLong , Bool , ByteArray , Char , Double , Int , String , UInt , or ULongLong ; otherwise returns 0.
Definition: qvariant.cpp:2659
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
__int64 qint64
Definition: qglobal.h:942
Type type() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1901
double toDouble(bool *ok=0) const
Returns the variant as a double if the variant has type() Double , QMetaType::Float ...
Definition: qvariant.cpp:2710

◆ macMeasurementSystem()

static QLocale::MeasurementSystem macMeasurementSystem ( )
static

Definition at line 316 of file qlocale_mac.mm.

Referenced by macQuoteString().

317 {
318  QCFType<CFLocaleRef> locale = CFLocaleCopyCurrent();
319  CFStringRef system = static_cast<CFStringRef>(CFLocaleGetValue(locale, kCFLocaleMeasurementSystem));
320  if (QCFString::toQString(system) == QLatin1String("Metric")) {
321  return QLocale::MetricSystem;
322  } else {
324  }
325 }
const struct __CFString * CFStringRef
static QString toQString(CFStringRef cfstr)
Definition: qcore_mac.cpp:47
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString

◆ macMonthName()

static QString macMonthName ( int  month,
bool  short_format 
)
static

Definition at line 86 of file qlocale_mac.mm.

Referenced by macQuoteString().

87 {
88  month -= 1;
89  if (month < 0 || month > 11)
90  return QString();
91 
93  = CFDateFormatterCreate(0, QCFType<CFLocaleRef>(CFLocaleCopyCurrent()),
94  kCFDateFormatterNoStyle, kCFDateFormatterNoStyle);
96  = static_cast<CFArrayRef>(CFDateFormatterCopyProperty(formatter,
97  short_format ? kCFDateFormatterShortMonthSymbols
98  : kCFDateFormatterMonthSymbols));
99  if (values != 0) {
100  CFStringRef cfstring = static_cast<CFStringRef>(CFArrayGetValueAtIndex(values, month));
101  return QCFString::toQString(cfstring);
102  }
103  return QString();
104 }
const struct __CFString * CFStringRef
static QString toQString(CFStringRef cfstr)
Definition: qcore_mac.cpp:47
The QString class provides a Unicode character string.
Definition: qstring.h:83
quint16 values[128]
const struct __CFArray * CFArrayRef

◆ macQuoteString()

static QVariant macQuoteString ( QSystemLocale::QueryType  type,
const QStringRef str 
)
static

Definition at line 393 of file qlocale_mac.mm.

394 {
395 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6 && !defined(Q_OS_IOS)
397  return QVariant();
398 
399  QString begin, end;
400  QCFType<CFLocaleRef> locale = CFLocaleCopyCurrent();
401  switch (type) {
403  begin = QCFString::toQString(static_cast<CFStringRef>(CFLocaleGetValue(locale, kCFLocaleQuotationBeginDelimiterKey)));
404  end = QCFString::toQString(static_cast<CFStringRef>(CFLocaleGetValue(locale, kCFLocaleQuotationEndDelimiterKey)));
405  return QString(begin % str % end);
407  begin = QCFString::toQString(static_cast<CFStringRef>(CFLocaleGetValue(locale, kCFLocaleAlternateQuotationBeginDelimiterKey)));
408  end = QCFString::toQString(static_cast<CFStringRef>(CFLocaleGetValue(locale, kCFLocaleAlternateQuotationEndDelimiterKey)));
409  return QString(begin % str % end);
410  default:
411  break;
412  }
413 #endif
414  return QVariant();
415 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
int type
Definition: qmetatype.cpp:239
static QString toQString(CFStringRef cfstr)
Definition: qcore_mac.cpp:47
The QString class provides a Unicode character string.
Definition: qstring.h:83
static const MacVersion MacintoshVersion
the version of the Macintosh operating system on which the application is run (Mac only)...
Definition: qglobal.h:1646
static const KeyPair *const end

◆ macTimeToString()

static QString macTimeToString ( const QTime time,
bool  short_format 
)
static

Definition at line 145 of file qlocale_mac.mm.

Referenced by macQuoteString().

146 {
147  CFGregorianDate macGDate;
148  // Assume this is local time and the current date
149  QDate dt = QDate::currentDate();
150  macGDate.year = dt.year();
151  macGDate.month = dt.month();
152  macGDate.day = dt.day();
153  macGDate.hour = time.hour();
154  macGDate.minute = time.minute();
155  macGDate.second = time.second();
156  QCFType<CFDateRef> myDate
157  = CFDateCreate(0, CFGregorianDateGetAbsoluteTime(macGDate,
158  QCFType<CFTimeZoneRef>(CFTimeZoneCopyDefault())));
159 
160  QCFType<CFLocaleRef> mylocale = CFLocaleCopyCurrent();
161  CFDateFormatterStyle style = short_format ? kCFDateFormatterShortStyle : kCFDateFormatterLongStyle;
162  QCFType<CFDateFormatterRef> myFormatter = CFDateFormatterCreate(kCFAllocatorDefault,
163  mylocale,
164  kCFDateFormatterNoStyle,
165  style);
166  return QCFString(CFDateFormatterCreateStringWithDate(0, myFormatter, myDate));
167 }
int month() const
Returns the number corresponding to the month of this date, using the following convention: ...
Definition: qdatetime.cpp:382
int day() const
Returns the day of the month (1 to 31) of this date.
Definition: qdatetime.cpp:395
The QDate class provides date functions.
Definition: qdatetime.h:55
static QDate currentDate()
Returns the current date, as reported by the system clock.
Definition: qdatetime.cpp:3115
int second() const
Returns the second part (0 to 59) of the time.
Definition: qdatetime.cpp:1600
int minute() const
Returns the minute part (0 to 59) of the time.
Definition: qdatetime.cpp:1589
int year() const
Returns the year of this date.
Definition: qdatetime.cpp:353
int hour() const
Returns the hour part (0 to 23) of the time.
Definition: qdatetime.cpp:1578

◆ macToQtFormat()

static QString macToQtFormat ( const QString sys_fmt)
static

Definition at line 174 of file qlocale_mac.mm.

Referenced by getMacDateFormat(), and getMacTimeFormat().

175 {
176  QString result;
177  int i = 0;
178 
179  while (i < sys_fmt.size()) {
180  if (sys_fmt.at(i).unicode() == '\'') {
181  QString text = qt_readEscapedFormatString(sys_fmt, &i);
182  if (text == QLatin1String("'"))
183  result += QLatin1String("''");
184  else
185  result += QLatin1Char('\'') + text + QLatin1Char('\'');
186  continue;
187  }
188 
189  QChar c = sys_fmt.at(i);
190  int repeat = qt_repeatCount(sys_fmt, i);
191 
192  switch (c.unicode()) {
193  // Qt does not support the following options
194  case 'G': // Era (1..5): 4 = long, 1..3 = short, 5 = narrow
195  case 'Y': // Year of Week (1..n): 1..n = padded number
196  case 'U': // Cyclic Year Name (1..5): 4 = long, 1..3 = short, 5 = narrow
197  case 'Q': // Quarter (1..4): 4 = long, 3 = short, 1..2 = padded number
198  case 'q': // Standalone Quarter (1..4): 4 = long, 3 = short, 1..2 = padded number
199  case 'w': // Week of Year (1..2): 1..2 = padded number
200  case 'W': // Week of Month (1): 1 = number
201  case 'D': // Day of Year (1..3): 1..3 = padded number
202  case 'F': // Day of Week in Month (1): 1 = number
203  case 'g': // Modified Julian Day (1..n): 1..n = padded number
204  case 'A': // Milliseconds in Day (1..n): 1..n = padded number
205  break;
206 
207  case 'y': // Year (1..n): 2 = short year, 1 & 3..n = padded number
208  case 'u': // Extended Year (1..n): 2 = short year, 1 & 3..n = padded number
209  // Qt only supports long (4) or short (2) year, use long for all others
210  if (repeat == 2)
211  result += QLatin1String("yy");
212  else
213  result += QLatin1String("yyyy");
214  break;
215  case 'M': // Month (1..5): 4 = long, 3 = short, 1..2 = number, 5 = narrow
216  case 'L': // Standalone Month (1..5): 4 = long, 3 = short, 1..2 = number, 5 = narrow
217  // Qt only supports long, short and number, use short for narrow
218  if (repeat == 5)
219  result += QLatin1String("MMM");
220  else
221  result += QString(repeat, QLatin1Char('M'));
222  break;
223  case 'd': // Day of Month (1..2): 1..2 padded number
224  result += QString(repeat, c);
225  break;
226  case 'E': // Day of Week (1..6): 4 = long, 1..3 = short, 5..6 = narrow
227  // Qt only supports long, short and padded number, use short for narrow
228  if (repeat == 4)
229  result += QLatin1String("dddd");
230  else
231  result += QLatin1String("ddd");
232  break;
233  case 'e': // Local Day of Week (1..6): 4 = long, 3 = short, 5..6 = narrow, 1..2 padded number
234  case 'c': // Standalone Local Day of Week (1..6): 4 = long, 3 = short, 5..6 = narrow, 1..2 padded number
235  // Qt only supports long, short and padded number, use short for narrow
236  if (repeat >= 5)
237  result += QLatin1String("ddd");
238  else
239  result += QString(repeat, QLatin1Char('d'));
240  break;
241  case 'a': // AM/PM (1): 1 = short
242  // Translate to Qt uppercase AM/PM
243  result += QLatin1String("AP");
244  break;
245  case 'h': // Hour [1..12] (1..2): 1..2 = padded number
246  case 'K': // Hour [0..11] (1..2): 1..2 = padded number
247  case 'j': // Local Hour [12 or 24] (1..2): 1..2 = padded number
248  // Qt h is local hour
249  result += QString(repeat, QLatin1Char('h'));
250  break;
251  case 'H': // Hour [0..23] (1..2): 1..2 = padded number
252  case 'k': // Hour [1..24] (1..2): 1..2 = padded number
253  // Qt H is 0..23 hour
254  result += QString(repeat, QLatin1Char('H'));
255  break;
256  case 'm': // Minutes (1..2): 1..2 = padded number
257  case 's': // Seconds (1..2): 1..2 = padded number
258  result += QString(repeat, c);
259  break;
260  case 'S': // Fractional second (1..n): 1..n = truncates to decimal places
261  // Qt uses msecs either unpadded or padded to 3 places
262  if (repeat < 3)
263  result += QLatin1Char('z');
264  else
265  result += QLatin1String("zzz");
266  break;
267  case 'z': // Time Zone (1..4)
268  case 'Z': // Time Zone (1..5)
269  case 'O': // Time Zone (1, 4)
270  case 'v': // Time Zone (1, 4)
271  case 'V': // Time Zone (1..4)
272  case 'X': // Time Zone (1..5)
273  case 'x': // Time Zone (1..5)
274  result += QLatin1Char('t');
275  break;
276  default:
277  // a..z and A..Z are reserved for format codes, so any occurrence of these not
278  // already processed are not known and so unsupported formats to be ignored.
279  // All other chars are allowed as literals.
280  if (c < QLatin1Char('A') || c > QLatin1Char('z') ||
281  (c > QLatin1Char('Z') && c < QLatin1Char('a'))) {
282  result += QString(repeat, c);
283  }
284  break;
285  }
286 
287  i += repeat;
288  }
289 
290  return result;
291 }
unsigned char c[8]
Definition: qnumeric_p.h:62
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
ushort unicode() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qchar.h:251
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
int qt_repeatCount(const QString &s, int i)
Definition: qlocale.cpp:421
QString qt_readEscapedFormatString(const QString &format, int *idx)
Definition: qlocale.cpp:387
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
#define text
Definition: qobjectdefs.h:80