![]() |
Home · All Classes · All Functions · Overviews |
Files:
The Maemo 5 Date Time example shows how to format dates and times according to the Maemo 5 settings.
By default, Qt formats dates and times according to the current locale. However, Maemo 5 allows the user to set 24 hour or 12 hour (+ am/pm) settings independently of the current locale.
In order convert a QTime to a string that is formatted according to the system's settings, QLocale::system() can be used.
QString shortTime = QLocale::system().toString(QTime::currentTime(), QLocale::ShortFormat);
In this snippet, the current time was converted to a string using the system's default (short) time format, also honoring the system's am/pm settings.
QString amPmStrings = QLocale::system().amText() + " / " + QLocale::system().pmText();
The strings for "am" and "pm" can be overridden by Maemo 5. QLocale::amText() and QLocale::pmText() return the am/pm strings as defined by the current locale, whereas QLocale::system() returns the am/pm strings as currently set by Maemo 5.
QString longDate = QLocale::system().toString(QDate::currentDate(), QLocale::LongFormat); QString shortDate = QLocale::system().toString(QDate::currentDate(), QLocale::ShortFormat);
Here, the current date is formatted into the long and short format specified by the system.
Maemo 5 supports some custom date time formats that are not supported by QLocale. In order to format a date or time, native system calls can be used. The following code snippets show how that can be done and can be copy/pasted into your project.
// for dgettext #include <libintl.h> // for strftime #include <time.h>
The libintl.h and time.h headers are required.
static const char *getHildonTranslation(const char *string) { const char *translation = ::dgettext("hildon-libs", string); if (qstrcmp(string, translation) == 0) return 0; return translation; }
In order to obtain date/time format strings, the dgettext call must be used. Note that this function will only work after QApplication or QCoreApplication has been instantiated. Applications that do not use a QApplication object should call setlocale before the first dgettext call, e.g. setlocale(LC_ALL, "");.
const char *hildonDateDayNameShort = getHildonTranslation("wdgt_va_date_day_name_short"); const char *hildon24hFormat = getHildonTranslation("wdgt_va_24h_time");
The format strings for wdgt_va_date_day_name_short and wdgt_va_24h_time are fetched from the system. The string constants are defined in the Hildon Widget UI Specification, chapters 18.3 and 18.4. wdgt_va_date_day_name_short is documented to return a format string that contains the day name in shortened form (e.g. "Tue") and the short date (e.g. "31.03"). wdgt_va_24h_time is documented to return the 24h format string, independend of the 24h/12h settings. The returned const char array is statically allocated and must not be freed or deleted.
Note: These format strings are POSIX format strings designed to be used with the strftime system call. They are not suitable for passing to QDateTime::toString().
static QString formatHildonDate(const QDateTime &dt, const char *format) { if (!format) return QString(); char buf[255]; struct tm tm = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; if (!dt.date().isNull()) { tm.tm_wday = dt.date().dayOfWeek() % 7; tm.tm_mday = dt.date().day(); tm.tm_mon = dt.date().month() - 1; tm.tm_year = dt.date().year() - 1900; } if (!dt.time().isNull()) { tm.tm_sec = dt.time().second(); tm.tm_min = dt.time().minute(); tm.tm_hour = dt.time().hour(); } size_t resultSize = ::strftime(buf, sizeof(buf), format, &tm); if (!resultSize) return QString(); return QString::fromUtf8(buf, resultSize); }
This function uses strftime to format a QDateTime into a QString, using the native format string obtained by getHildonTranslation (see above).
In order to call strftime, the QDateTime needs to be converted into a struct tm. Full documentation about struct tm can be found in the mktime(3) man page.
QDateTime current = QDateTime::currentDateTime(); QString dayNameShort = formatHildonDate(current, hildonDateDayNameShort); QString time24h = formatHildonDate(current, hildon24hFormat);
formatHildonDate is used to convert the current date and time to Maemo's short day name format and to Maemo's 24h format. Note that depending on the format strings, it is safe to call formatHildonDate with a QDateTime containing only a QDate or a QTime component. For example, QDateTime(QDate(), QTime::currentTime()) is safe to use with Maemo's 24h format string.
Copyright © 2010 Nokia Corporation and/or its subsidiary(-ies) | Trademarks | Qt 4.6.2 |