Update all calls to fromMSecsSinceEpoch to specify UTC

QT 5 changed the behavior of QDateTime::fromMSecsSinceEpoch to return the result in local time zone, whereas in QT 4, it returned UTC.  On systems that do not support time zones, the QT 5 version still returns UTC.  But for all other systems, the api change causes confusion in the SH UI because some date and time values are displayed in UTC instead of the local time zone.  This manifests itself when the user is in USA which has a negative UTC offset.  When selecting a date range to display in the overview screen, the displayed dates appear to be one day behind the selected date range.

For consistently, SH should always use UTC internally and only convert to the local time zone when displaying data to the user.  This will ensure that the time zone information is preserved correctly when the UTC offset of the user's machine changes due to DST changes or traveling.

There are a few calls to fromMSecsSinceEpoch which should be using local time, and those will be updated in future commits.
This commit is contained in:
reznet 2015-08-11 15:01:24 -05:00
parent b0bc8af718
commit 59a0823d20
6 changed files with 26 additions and 29 deletions

View File

@ -470,7 +470,7 @@ void gGraphView::dumpInfo()
Day * day = p_profile->GetGoodDay(date, MT_CPAP);
if (day) {
QDateTime dt=QDateTime::fromMSecsSinceEpoch(day->first());
QDateTime dt=QDateTime::fromMSecsSinceEpoch(day->first(), Qt::UTC);
mainwin->log(QString("Available Channels for %1").arg(dt.toString("MMM dd yyyy")));
QHash<schema::ChanType, QList<schema::Channel *> > list;
@ -1397,8 +1397,8 @@ QString gGraphView::getRangeString()
qint64 maxx = minx + 86400000L * qint64(days)-1;
QDateTime st = QDateTime::fromMSecsSinceEpoch(minx);
QDateTime et = QDateTime::fromMSecsSinceEpoch(maxx);
QDateTime st = QDateTime::fromMSecsSinceEpoch(minx, Qt::UTC);
QDateTime et = QDateTime::fromMSecsSinceEpoch(maxx, Qt::UTC);
QString txt = st.toString("d MMM") + " - " + et.addDays(-1).toString("d MMM yyyy");
return txt;
@ -1407,8 +1407,8 @@ QString gGraphView::getRangeString()
} else {
fmt = "HH:mm:ss:zzz";
}
QDateTime st = QDateTime::fromMSecsSinceEpoch(m_minx);
QDateTime et = QDateTime::fromMSecsSinceEpoch(m_maxx);
QDateTime st = QDateTime::fromMSecsSinceEpoch(m_minx, Qt::UTC);
QDateTime et = QDateTime::fromMSecsSinceEpoch(m_maxx, Qt::UTC);
QString txt = st.toString(QObject::tr("d MMM [ %1 - %2 ]").arg(fmt).arg(et.toString(fmt))) ;
@ -2125,7 +2125,7 @@ void gGraphView::onSnapshotGraphToggle()
QString basename = name+";";
if (graph->m_day) {
QDateTime date = QDateTime::fromMSecsSinceEpoch(graph->min_x);
QDateTime date = QDateTime::fromMSecsSinceEpoch(graph->min_x, Qt::UTC);
basename += date.date().toString(Qt::SystemLocaleLongDate);
}
QString newname;
@ -2968,8 +2968,7 @@ void gGraphView::keyPressEvent(QKeyEvent *event)
m_metaselect=false;
qint64 start,end;
getSelectionTimes(start,end);
QDateTime d1 = QDateTime::fromMSecsSinceEpoch(start);
// QDateTime d2 = QDateTime::fromMSecsSinceEpoch(end);
QDateTime d1 = QDateTime::fromMSecsSinceEpoch(start, Qt::UTC);
mainwin->getDaily()->addBookmark(start, end, QString("Bookmark at %1").arg(d1.time().toString("HH:mm:ss")));
m_graphs[m_graph_index]->cancelSelection();

View File

@ -136,7 +136,7 @@ bool gSummaryChart::mouseReleaseEvent(QMouseEvent *event, gGraph *graph)
graph->roundY(miny, maxy);
QDate date = QDateTime::fromMSecsSinceEpoch(m_minx).date();
QDate date = QDateTime::fromMSecsSinceEpoch(m_minx, Qt::UTC).date();
int days = ceil(double(m_maxx - m_minx) / 86400000.0);
@ -404,8 +404,8 @@ void gSummaryChart::paint(QPainter &painter, gGraph &graph, const QRegion &regio
m_maxx = graph.max_x;
QDateTime date2 = QDateTime::fromMSecsSinceEpoch(m_minx);
QDateTime enddate2 = QDateTime::fromMSecsSinceEpoch(m_maxx);
QDateTime date2 = QDateTime::fromMSecsSinceEpoch(m_minx, Qt::UTC);
QDateTime enddate2 = QDateTime::fromMSecsSinceEpoch(m_maxx, Qt::UTC);
QDate date = date2.date();
QDate enddate = enddate2.date();
@ -819,8 +819,8 @@ void gSessionTimesChart::paint(QPainter &painter, gGraph &graph, const QRegion &
m_minx = graph.min_x;
m_maxx = graph.max_x;
QDateTime date2 = QDateTime::fromMSecsSinceEpoch(m_minx);
QDateTime enddate2 = QDateTime::fromMSecsSinceEpoch(m_maxx);
QDateTime date2 = QDateTime::fromMSecsSinceEpoch(m_minx, Qt::UTC);
QDateTime enddate2 = QDateTime::fromMSecsSinceEpoch(m_maxx, Qt::UTC);
QDate date = date2.date();
QDate enddate = enddate2.date();
@ -900,7 +900,7 @@ void gSessionTimesChart::paint(QPainter &painter, gGraph &graph, const QRegion &
// segments
for (int j=0; j<slize; ++j) {
const SessionSlice & slice = sess->m_slices.at(j);
QDateTime st = QDateTime::fromMSecsSinceEpoch(slice.start);
QDateTime st = QDateTime::fromMSecsSinceEpoch(slice.start, Qt::UTC);
float s1 = float(splittime.secsTo(st)) / 3600.0;
@ -915,7 +915,7 @@ void gSessionTimesChart::paint(QPainter &painter, gGraph &graph, const QRegion &
} else {
// otherwise just show session duration
qint64 sf = sess->first();
QDateTime st = QDateTime::fromMSecsSinceEpoch(sf);
QDateTime st = QDateTime::fromMSecsSinceEpoch(sf, Qt::UTC);
float s1 = float(splittime.secsTo(st)) / 3600.0;
float s2 = sess->hours();

View File

@ -144,7 +144,7 @@ void ResmedLoader::ParseSTR(Machine *mach, QStringList strfiles)
continue;
}
QDateTime start = QDateTime::fromMSecsSinceEpoch(str.startdate);
QDateTime start = QDateTime::fromMSecsSinceEpoch(str.startdate, Qt::UTC);
QDate date = start.date();
qDebug() << "Parsing" << *it << date << str.GetNumDataRecords() << str.GetNumSignals();
@ -444,6 +444,7 @@ void ResmedLoader::ParseSTR(Machine *mach, QStringList strfiles)
laston = ontime;
// TODO UTC
QDateTime dontime = QDateTime::fromTime_t(ontime);
date = dontime.date();
R.date = date;
@ -2017,7 +2018,7 @@ int ResmedLoader::Open(QString path)
QFile::copy(path + RMS9_STR_idfile + STR_ext_TGT, backup_path + RMS9_STR_idfile + STR_ext_TGT);
QFile::copy(path + RMS9_STR_idfile + STR_ext_CRC, backup_path + RMS9_STR_idfile + STR_ext_CRC);
QDateTime dts = QDateTime::fromMSecsSinceEpoch(stredf.startdate);
QDateTime dts = QDateTime::fromMSecsSinceEpoch(stredf.startdate, Qt::UTC);
dir.mkpath(backup_path + "STR_Backup");
QString strmonthly = backup_path + "STR_Backup/STR-" + dts.toString("yyyyMM") + "." + STR_ext_EDF;

View File

@ -150,9 +150,6 @@ int SomnoposeLoader::OpenFile(QString filename)
sess->set_last(time);
ev_orientation->AddEvent(time, orientation);
ev_inclination->AddEvent(time, inclination);
// QDateTime dt=QDateTime::fromMSecsSinceEpoch(time);
// qDebug() << dt << orientation << inclination;
}
sess->setMin(POS_Orientation, ev_orientation->Min());

View File

@ -668,7 +668,7 @@ void Daily::UpdateEventsTree(QTreeWidget *tree,Day *day)
t-=float(ev.raw(o)/2.0)*1000.0;
}
QStringList a;
QDateTime d=QDateTime::fromMSecsSinceEpoch(t);
QDateTime d=QDateTime::fromMSecsSinceEpoch(t, Qt::UTC);
QString s=QString("#%1: %2 (%3)").arg((int)(++mccnt[code]),(int)3,(int)10,QChar('0')).arg(d.toString("HH:mm:ss")).arg(m.value()[z]->raw(o));
a.append(s);
QTreeWidgetItem *item=new QTreeWidgetItem(a);
@ -690,8 +690,8 @@ void Daily::UpdateEventsTree(QTreeWidget *tree,Day *day)
tree->insertTopLevelItem(cnt++ , start);
tree->insertTopLevelItem(cnt++ , end);
for (QList<Session *>::iterator s=day->begin(); s!=day->end(); ++s) {
QDateTime st = QDateTime::fromMSecsSinceEpoch((*s)->first());
QDateTime et = QDateTime::fromMSecsSinceEpoch((*s)->last());
QDateTime st = QDateTime::fromMSecsSinceEpoch((*s)->first(), Qt::UTC);
QDateTime et = QDateTime::fromMSecsSinceEpoch((*s)->last(), Qt::UTC);
QTreeWidgetItem * item = new QTreeWidgetItem(QStringList(st.toString("HH:mm:ss")));
item->setData(0,Qt::UserRole, (*s)->first());
@ -1998,7 +1998,7 @@ void Daily::RedrawGraphs()
void Daily::on_LineCursorUpdate(double time)
{
if (time > 1) {
QDateTime dt = QDateTime::fromMSecsSinceEpoch(time);
QDateTime dt = QDateTime::fromMSecsSinceEpoch(time, Qt::UTC);
QString txt = dt.toString("MMM dd HH:mm:ss.zzz");
dateDisplay->setText(txt);
} else dateDisplay->setText(QString(GraphView->emptyText()));
@ -2215,7 +2215,7 @@ void Daily::on_bookmarkTable_itemClicked(QTableWidgetItem *item)
void Daily::addBookmark(qint64 st, qint64 et, QString text)
{
ui->bookmarkTable->blockSignals(true);
QDateTime d=QDateTime::fromTime_t(st/1000L);
QDateTime d=QDateTime::fromTime_t(st/1000L, Qt::UTC);
int row=ui->bookmarkTable->rowCount();
ui->bookmarkTable->insertRow(row);
QTableWidgetItem *tw=new QTableWidgetItem(text);

View File

@ -549,7 +549,7 @@ void OximeterImport::on_calendarWidget_clicked(const QDate &date)
sessbar->clear();
if (day) {
QDateTime time=QDateTime::fromMSecsSinceEpoch(day->first());
QDateTime time=QDateTime::fromMSecsSinceEpoch(day->first(), Qt::UTC);
sessbar->clear();
QList<QColor> colors;
colors.push_back("#ffffe0");
@ -582,7 +582,7 @@ void OximeterImport::on_calendarWidget_selectionChanged()
}
void OximeterImport::onSessionSelected(Session * session)
{
QDateTime time=QDateTime::fromMSecsSinceEpoch(session->first());
QDateTime time=QDateTime::fromMSecsSinceEpoch(session->first(), Qt::UTC);
ui->dateTimeEdit->setDateTime(time);
}
@ -591,7 +591,7 @@ void OximeterImport::on_sessionBackButton_clicked()
int idx = (sessbar->selected()-1);
if (idx >= 0) {
sessbar->setSelected(idx);
QDateTime datetime = QDateTime::fromMSecsSinceEpoch(sessbar->session(idx)->first());
QDateTime datetime = QDateTime::fromMSecsSinceEpoch(sessbar->session(idx)->first(), Qt::UTC);
ui->dateTimeEdit->setDateTime(datetime);
sessbar->update();
}
@ -602,7 +602,7 @@ void OximeterImport::on_sessionForwardButton_clicked()
int idx = (sessbar->selected()+1);
if (idx < sessbar->count()) {
sessbar->setSelected(idx);
QDateTime datetime = QDateTime::fromMSecsSinceEpoch(sessbar->session(idx)->first());
QDateTime datetime = QDateTime::fromMSecsSinceEpoch(sessbar->session(idx)->first(), Qt::UTC);
ui->dateTimeEdit->setDateTime(datetime);
sessbar->update();
}