From 75c0a9950a3436ed322fa0e04923b5053c5a1dcc Mon Sep 17 00:00:00 2001 From: Seeker4 Date: Tue, 6 Aug 2019 10:51:14 -0700 Subject: [PATCH] Format dates per national settings. --- oscar/Graphs/gXAxis.cpp | 6 ++++-- oscar/SleepLib/common.cpp | 30 +++++++++++++++++++++++++++++- oscar/SleepLib/common.h | 4 ++++ oscar/docs/release_notes.html | 4 +++- oscar/main.cpp | 2 ++ oscar/mainwindow.cpp | 2 +- oscar/overview.cpp | 2 +- oscar/statistics.cpp | 19 +++++++++++-------- 8 files changed, 55 insertions(+), 14 deletions(-) diff --git a/oscar/Graphs/gXAxis.cpp b/oscar/Graphs/gXAxis.cpp index 29a29d8d..7239da73 100644 --- a/oscar/Graphs/gXAxis.cpp +++ b/oscar/Graphs/gXAxis.cpp @@ -13,6 +13,7 @@ #include "Graphs/gXAxis.h" #include "SleepLib/profiles.h" +#include "SleepLib/common.h" #include "Graphs/glcommon.h" #include "Graphs/gGraph.h" #include "Graphs/gGraphView.h" @@ -303,7 +304,8 @@ void gXAxis::paint(QPainter &painter, gGraph &w, const QRegion ®ion) //dt.toString("MMM dd"); // Doing it this way instead because it's MUUUUUUCH faster - tmpstr = QString("%1 %2").arg(months[date.month() - 1]).arg(date.day()); + tmpstr = QString(dayFirst?"%1 %2":"%2 %1").arg(date.day()).arg(months[date.month() - 1]); + //} else if (fitmode==0) { // tmpstr=QString("%1 %2:%3").arg(dow[d]).arg(h,2,10,QChar('0')).arg(m,2,10,QChar('0')); } else if (fitmode == 1) { // minute @@ -435,7 +437,7 @@ void gXAxisDay::paint(QPainter &painter, gGraph &graph, const QRegion ®ion) if ((lastx + barw) > (left + width + 1)) break; - QString tmpstr = QString("%1 %2").arg(months[date.month() - 1]).arg(date.day(), 2, 10, QChar('0')); + QString tmpstr = QString(dayFirst?"%2 %1":"%1 %2").arg(months[date.month() - 1]).arg(date.day(), 2, 10, QChar('0')); float x1 = lastx + xpos; //lines.append(QLine(lastx, top, lastx, top+6)); diff --git a/oscar/SleepLib/common.cpp b/oscar/SleepLib/common.cpp index 380ec5f3..78947c21 100644 --- a/oscar/SleepLib/common.cpp +++ b/oscar/SleepLib/common.cpp @@ -37,8 +37,36 @@ extern MainWindow * mainwin; -// Used by internal settings +QString MedDateFormat = "ddd MMM d yyyy"; // QT default value, which we override if we can +bool dayFirst = false; +// System locale and regional settings support only a "short" date (m/d/yyy) and a "long" +// date (day of week, month, day, year -- all spelled out fully). We get the formatting +// for the long format, shorten day and month name, and remove excess commas. +void SetDateFormat () { + QLocale sysLocale = QLocale::system(); + QString dfmt = sysLocale.dateFormat(); + qDebug() << "system locale date format" << dfmt; + + QString s = dfmt.replace("dddd", "ddd"); + if (!s.isEmpty()) s = s.replace("MMMM", "MMM"); + if (!s.isEmpty()) s = s.replace(",", ""); + if (!s.isEmpty()) { + QDate testDate (2018, 12, 31); + QString testresult = testDate.toString(s); + if (!testresult.isEmpty()) // make sure we can translate a date + MedDateFormat = s; // If we can, save the format for future use + } + + // Record whether month or day is first in the formatting + QString s2 = MedDateFormat; + s2 = s2.replace("ddd",""); + int monthidx = s2.indexOf("MMM"); + if (s2.indexOf("d") < monthidx) + dayFirst = true; + + qDebug() << "shortened date format" << MedDateFormat << "dayFirst" << dayFirst; +} const QString & gitRevision() { diff --git a/oscar/SleepLib/common.h b/oscar/SleepLib/common.h index b2f1eb94..523d4757 100644 --- a/oscar/SleepLib/common.h +++ b/oscar/SleepLib/common.h @@ -27,6 +27,8 @@ const QString CSTR_GFX_ANGLE = "ANGLE"; const QString CSTR_GFX_OpenGL = "OpenGL"; const QString CSTR_GFX_BrokenGL = "LegacyGFX"; +extern QString MedDateFormat; +extern bool dayFirst; //! \brief Gets the first day of week from the system locale, to show in the calendars. Qt::DayOfWeek firstDayOfWeekFromLocale(); @@ -49,6 +51,8 @@ QStringList makeBuildInfo(QString relinfo, QString forcedEngine); QStringList getBuildInfo(); QStringList addBuildInfo (QString value); +void SetDateFormat (); + QByteArray gCompress(const QByteArray& data); QByteArray gUncompress(const QByteArray &data); diff --git a/oscar/docs/release_notes.html b/oscar/docs/release_notes.html index aaffeb05..6dc66e92 100644 --- a/oscar/docs/release_notes.html +++ b/oscar/docs/release_notes.html @@ -13,8 +13,10 @@ Which was written and copyright 2011-2018 © Mark Watkins
  • Portions of OSCAR are © 2019 by The OSCAR Team
  • [new] DreamStation BiPAP S/T and AVAPS ventilators (1030X and 1130X) are now supported. The settings aren't yet displayed correctly, but therapy events and graphs should now display properly.
  • [new] View/Reset Graphs organizes graphs in their original order
  • +
  • [fix] Format dates for the national region as reported by the operating system
  • [fix] Improve progress bar for statistics cache update
  • -
  • [fix] Correct calculation of seven-day AHI in Records tab of right sidebar
  • +
  • [fix] Correct calculations of seven-day AHI and leak rate on Welcome page
  • +
  • [fix] Clarify AHI and hours labels on Records tab of right sidebar
  • [fix] Correct import error resulting in invalid elapsed times and impossibly high AHI values
  • diff --git a/oscar/main.cpp b/oscar/main.cpp index 1d7b7c96..fd903259 100644 --- a/oscar/main.cpp +++ b/oscar/main.cpp @@ -346,6 +346,8 @@ int main(int argc, char *argv[]) { qDebug() << "OSCAR starting" << QDateTime::currentDateTime(); qDebug().noquote() << STR_AppName << VersionString << relinfo << "Built with Qt" << QT_VERSION_STR << __DATE__ << __TIME__; + SetDateFormat(); + //////////////////////////////////////////////////////////////////////////////////////////// // Language Selection //////////////////////////////////////////////////////////////////////////////////////////// diff --git a/oscar/mainwindow.cpp b/oscar/mainwindow.cpp index e4888edf..b420d57a 100644 --- a/oscar/mainwindow.cpp +++ b/oscar/mainwindow.cpp @@ -1221,7 +1221,7 @@ void MainWindow::updateFavourites() if (notes.size() > 0) { tmp += QString("%2
    ") .arg(date.toString(Qt::ISODate)) - .arg(date.toString()); + .arg(date.toString(MedDateFormat)); tmp += ""; diff --git a/oscar/overview.cpp b/oscar/overview.cpp index 3d8e1bb6..36c3a236 100644 --- a/oscar/overview.cpp +++ b/oscar/overview.cpp @@ -469,6 +469,7 @@ void Overview::on_rangeCombo_activated(int index) setRange(start, end); } + void Overview::setRange(QDate start, QDate end) { ui->dateEnd->blockSignals(true); @@ -481,7 +482,6 @@ void Overview::setRange(QDate start, QDate end) ui->dateStart->blockSignals(false); this->on_toolButton_clicked(); updateGraphCombo(); - } void Overview::on_graphCombo_activated(int index) diff --git a/oscar/statistics.cpp b/oscar/statistics.cpp index be659679..b8b96ee8 100644 --- a/oscar/statistics.cpp +++ b/oscar/statistics.cpp @@ -20,6 +20,7 @@ #include "mainwindow.h" #include "statistics.h" #include "CProgressBar.h" +#include "SleepLib/common.h" extern MainWindow *mainwin; @@ -613,7 +614,7 @@ QString Statistics::getUserInfo () { if (!p_profile->user->firstName().isEmpty()) { userinfo = tr("Name: %1, %2").arg(p_profile->user->lastName()).arg(p_profile->user->firstName()) + "
    "; if (!p_profile->user->DOB().isNull()) { - userinfo += tr("DOB: %1").arg(p_profile->user->DOB().toString()) + "
    "; + userinfo += tr("DOB: %1").arg(p_profile->user->DOB().toString(MedDateFormat)) + "
    "; } if (!p_profile->user->phone().isEmpty()) { userinfo += tr("Phone: %1").arg(p_profile->user->phone()) + "
    "; @@ -897,8 +898,10 @@ QString Statistics::GenerateMachineList() .arg(m->model() + (mn.isEmpty() ? "" : QString(" (") + mn + QString(")"))) .arg(m->serial()) - .arg(d1.toString(Qt::SystemLocaleShortDate)) - .arg(d2.toString(Qt::SystemLocaleShortDate)); + .arg(d1.toString(MedDateFormat)) + .arg(d2.toString(MedDateFormat)); +// .arg(d1.toString(Qt::SystemLocaleShortDate)) +// .arg(d2.toString(Qt::SystemLocaleShortDate)); } @@ -984,8 +987,8 @@ QString Statistics::GenerateRXChanges() double ahi = rdi ? (double(rx.rdi) / rx.hours) : (double(rx.ahi) /rx.hours); double fli = double(rx.count(CPAP_FlowLimit)) / rx. hours; - html += QString("%1").arg(rx.start.toString())+ - QString("%1").arg(rx.end.toString())+ + html += QString("%1").arg(rx.start.toString(MedDateFormat))+ + QString("%1").arg(rx.end.toString(MedDateFormat))+ QString("%1").arg(rx.days)+ QString("%1").arg(ahi, 0, 'f', 2)+ QString("%1").arg(fli, 0, 'f', 2)+ @@ -1174,14 +1177,14 @@ QString Statistics::GenerateCPAPUsage() arg(tr("%1 day of %2 Data on %3") .arg(value) .arg(machine) - .arg(last.toString())); + .arg(last.toString(MedDateFormat))); } else { html+=QString("%2\n").arg(periods.size()+1). arg(tr("%1 days of %2 Data, between %3 and %4") .arg(value) .arg(machine) - .arg(first.toString()) - .arg(last.toString())); + .arg(first.toString(MedDateFormat)) + .arg(last.toString(MedDateFormat))); } continue; } else if (row.calc == SC_SUBHEADING) { // subheading..