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 <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2014-06-27 12:04:54 -07:00
parent e88f37d327
commit a8a9deb262

View file

@ -29,7 +29,6 @@
#include <QSettings> #include <QSettings>
#include <QDesktopWidget> #include <QDesktopWidget>
#include <QStyle> #include <QStyle>
#include <QDebug>
#include <QMap> #include <QMap>
#include <QMultiMap> #include <QMultiMap>
#include <QNetworkProxy> #include <QNetworkProxy>
@ -48,6 +47,9 @@ static MainWindow *window = NULL;
int error_count; int error_count;
const char *existing_filename; const char *existing_filename;
static QString shortDateFormat;
static QString dateFormat;
static QString timeFormat;
#if defined(Q_OS_WIN) && QT_VERSION < 0x050000 #if defined(Q_OS_WIN) && QT_VERSION < 0x050000
static QByteArray encodeUtf8(const QString &fname) static QByteArray encodeUtf8(const QString &fname)
@ -88,6 +90,14 @@ QString uiLanguage(QLocale *callerLoc)
if (callerLoc) if (callerLoc)
*callerLoc = loc; *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; return uiLang;
} }
@ -380,27 +390,16 @@ int parseTemperatureToMkelvin(const QString &text)
QString get_dive_date_string(timestamp_t when) QString get_dive_date_string(timestamp_t when)
{ {
struct tm tm; QDateTime ts;
utc_mkdate(when, &tm); ts.setMSecsSinceEpoch(when * 1000);
return translate("gettextFromC", "%1, %2 %3, %4 %5:%6") return ts.toUTC().toString(dateFormat + " " + timeFormat);
.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'));
} }
QString get_short_dive_date_string(timestamp_t when) QString get_short_dive_date_string(timestamp_t when)
{ {
struct tm tm; QDateTime ts;
utc_mkdate(when, &tm); ts.setMSecsSinceEpoch(when * 1000);
return translate("gettextFromC", "%1 %2, %3\n%4:%5") return ts.toUTC().toString(shortDateFormat + " " + timeFormat);
.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'));
} }
QString get_trip_date_string(timestamp_t when, int nr) QString get_trip_date_string(timestamp_t when, int nr)