From a8a9deb262dfe9af158e0faefabc4166d310d1b9 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Fri, 27 Jun 2014 12:04:54 -0700 Subject: Better date localization Instead on relying on the translators to correctly format date and time we should be able to get all we need from QLocale. Sadly this takes a bit of hacky post-processing, but in my limited testing the results look good. Signed-off-by: Dirk Hohndel --- qt-gui.cpp | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/qt-gui.cpp b/qt-gui.cpp index 52b26febd..eeb43b899 100644 --- a/qt-gui.cpp +++ b/qt-gui.cpp @@ -29,7 +29,6 @@ #include #include #include -#include #include #include #include @@ -48,6 +47,9 @@ static MainWindow *window = NULL; int error_count; const char *existing_filename; +static QString shortDateFormat; +static QString dateFormat; +static QString timeFormat; #if defined(Q_OS_WIN) && QT_VERSION < 0x050000 static QByteArray encodeUtf8(const QString &fname) @@ -88,6 +90,14 @@ QString uiLanguage(QLocale *callerLoc) if (callerLoc) *callerLoc = loc; + // the short format is fine + // the long format uses long weekday and month names, so replace those with the short ones + // for time we don't want the time zone designator and don't want leading zeroes on the hours + shortDateFormat = loc.dateFormat(QLocale::ShortFormat); + dateFormat = loc.dateFormat(QLocale::LongFormat); + dateFormat.replace("dddd,", "ddd").replace("dddd", "ddd").replace("MMMM", "MMM"); + timeFormat = loc.timeFormat(); + timeFormat.replace(" t", "").replace("t", "").replace("hh", "h").replace("HH", "H"); return uiLang; } @@ -380,27 +390,16 @@ int parseTemperatureToMkelvin(const QString &text) QString get_dive_date_string(timestamp_t when) { - struct tm tm; - utc_mkdate(when, &tm); - return translate("gettextFromC", "%1, %2 %3, %4 %5:%6") - .arg(weekday(tm.tm_wday)) - .arg(monthname(tm.tm_mon)) - .arg(tm.tm_mday) - .arg(tm.tm_year + 1900) - .arg(tm.tm_hour, 2, 10, QChar('0')) - .arg(tm.tm_min, 2, 10, QChar('0')); + QDateTime ts; + ts.setMSecsSinceEpoch(when * 1000); + return ts.toUTC().toString(dateFormat + " " + timeFormat); } QString get_short_dive_date_string(timestamp_t when) { - struct tm tm; - utc_mkdate(when, &tm); - return translate("gettextFromC", "%1 %2, %3\n%4:%5") - .arg(monthname(tm.tm_mon)) - .arg(tm.tm_mday) - .arg(tm.tm_year + 1900) - .arg(tm.tm_hour, 2, 10, QChar('0')) - .arg(tm.tm_min, 2, 10, QChar('0')); + QDateTime ts; + ts.setMSecsSinceEpoch(when * 1000); + return ts.toUTC().toString(shortDateFormat + " " + timeFormat); } QString get_trip_date_string(timestamp_t when, int nr) -- cgit v1.2.3-70-g09d2