Skip to content
Snippets Groups Projects
Commit e0ff769e authored by Alexandre Lision's avatar Alexandre Lision Committed by gerrit2
Browse files

history: fix time calculation

We worked with periods of 24 consecutive hours to define 'Today',
'Yesterday'... (c.f: https://en.wikipedia.org/wiki/Nychthemeron)
This was misleading for the user.

We now integrate with the local system time to define these terms.

Issue: #76563
Change-Id: Ifc642499dbcfd050cce8e3b05acec15dc639fb38
parent 6ccd9726
Branches
Tags
No related merge requests found
/****************************************************************************
* Copyright (C) 2012-2015 by Savoir-Faire Linux *
* Copyright (C) 2012-2015 by Savoir-faire Linux *
* Author : Emmanuel Lepage Vallee <emmanuel.lepage@savoirfairelinux.com> *
* Author : Alexandre Lision <alexandre.lision@savoirfairelinux.com> *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
......@@ -18,6 +19,7 @@
#include "historytimecategorymodel.h"
#include <QtCore/QDate>
#include <time.h>
class HistoryTimeCategoryModelPrivate
{
......@@ -28,7 +30,7 @@ public:
HistoryTimeCategoryModel* HistoryTimeCategoryModelPrivate::instance()
{
static HistoryTimeCategoryModel* m_spInstance = new HistoryTimeCategoryModel();
static auto m_spInstance = new HistoryTimeCategoryModel();
return m_spInstance;
}
......@@ -45,21 +47,22 @@ d_ptr(new HistoryTimeCategoryModelPrivate)
d_ptr->m_lCategories << tr("Last week") ;//7
d_ptr->m_lCategories << tr("Two weeks ago") ;//8
d_ptr->m_lCategories << tr("Three weeks ago") ;//9
d_ptr->m_lCategories << tr("Last month") ;//10
d_ptr->m_lCategories << tr("Two months ago") ;//11
d_ptr->m_lCategories << tr("Three months ago") ;//12
d_ptr->m_lCategories << tr("Four months ago") ;//13
d_ptr->m_lCategories << tr("Five months ago") ;//14
d_ptr->m_lCategories << tr("Six months ago") ;//15
d_ptr->m_lCategories << tr("Seven months ago") ;//16
d_ptr->m_lCategories << tr("Eight months ago") ;//17
d_ptr->m_lCategories << tr("Nine months ago") ;//18
d_ptr->m_lCategories << tr("Ten months ago") ;//19
d_ptr->m_lCategories << tr("Eleven months ago") ;//20
d_ptr->m_lCategories << tr("Twelve months ago") ;//21
d_ptr->m_lCategories << tr("Last year") ;//22
d_ptr->m_lCategories << tr("Very long time ago") ;//23
d_ptr->m_lCategories << tr("Never") ;//24
d_ptr->m_lCategories << tr("Four weeks ago") ;//10
d_ptr->m_lCategories << tr("Last month") ;//11
d_ptr->m_lCategories << tr("Two months ago") ;//12
d_ptr->m_lCategories << tr("Three months ago") ;//13
d_ptr->m_lCategories << tr("Four months ago") ;//14
d_ptr->m_lCategories << tr("Five months ago") ;//15
d_ptr->m_lCategories << tr("Six months ago") ;//16
d_ptr->m_lCategories << tr("Seven months ago") ;//17
d_ptr->m_lCategories << tr("Eight months ago") ;//18
d_ptr->m_lCategories << tr("Nine months ago") ;//19
d_ptr->m_lCategories << tr("Ten months ago") ;//20
d_ptr->m_lCategories << tr("Eleven months ago") ;//21
d_ptr->m_lCategories << tr("Twelve months ago") ;//22
d_ptr->m_lCategories << tr("Last year") ;//23
d_ptr->m_lCategories << tr("Very long time ago") ;//24
d_ptr->m_lCategories << tr("Never") ;//25
}
HistoryTimeCategoryModel::~HistoryTimeCategoryModel()
......@@ -112,50 +115,52 @@ bool HistoryTimeCategoryModel::setData(const QModelIndex& index, const QVariant
QString HistoryTimeCategoryModel::timeToHistoryCategory(const time_t time)
{
static int categoriesSize = HistoryTimeCategoryModelPrivate::instance()->d_ptr->m_lCategories.size();
int period = (int)HistoryTimeCategoryModel::timeToHistoryConst(time);
if (period >= 0 && period <= 24)
if (period >= 0 && period < categoriesSize)
return HistoryTimeCategoryModelPrivate::instance()->d_ptr->m_lCategories[period];
else
return HistoryTimeCategoryModelPrivate::instance()->d_ptr->m_lCategories[24];
return HistoryTimeCategoryModelPrivate::instance()->d_ptr->m_lCategories[categoriesSize - 1];
}
HistoryTimeCategoryModel::HistoryConst HistoryTimeCategoryModel::timeToHistoryConst(const time_t time)
{
time_t time2 = time;
time_t currentTime;
::time(&currentTime);
if (!time || time < 0)
return HistoryTimeCategoryModel::HistoryConst::Never;
//Check if part if the current Nychthemeron
if (currentTime - time <= 3600*24) //The future case would be a bug, but it have to be handled anyway or it will appear in "very long time ago"
return HistoryConst::Today;
time2 -= time%(3600*24); //Reset to midnight
currentTime -= currentTime%(3600*24); //Reset to midnight
//Check for last week
if (currentTime-(6)*3600*24 < time2) {
for (int i=1;i<7;i++) {
if (currentTime-((i)*3600*24) == time2)
return (HistoryTimeCategoryModel::HistoryConst)(i); //Yesterday to Six_days_ago
}
time_t currentTime;
::time(&currentTime);
/*
* Struct tm description of fields used below:
* tm_mday int day of the month 1-31
* tm_mon int months since January 0-11
* tm_year int years since 1900
* tm_wday int days since Sunday 0-6
*/
struct tm localCurrentTime = {};
struct tm localPastTime = {};
::localtime_r(&currentTime, &localCurrentTime);
::localtime_r(&time, &localPastTime);
int diffYears = localCurrentTime.tm_year - localPastTime.tm_year;
int diffMonths = localCurrentTime.tm_mon - localPastTime.tm_mon;
int diffDays = localCurrentTime.tm_mday - localPastTime.tm_mday;
//Check for past days, stopping at Monday
if (diffYears == 0 && diffMonths == 0 && diffDays < 7 && localPastTime.tm_wday <= localCurrentTime.tm_wday) {
return (HistoryTimeCategoryModel::HistoryConst)(diffDays); //Today to Six_days_ago
}
//Check for last month
else if (currentTime - ((4)*7*24*3600) < time2) {
for (int i=1;i<4;i++) {
if (currentTime - ((i+1)*7*24*3600) < time2)
return (HistoryTimeCategoryModel::HistoryConst)(i+((int)HistoryTimeCategoryModel::HistoryConst::Last_week)-1); //Last_week to Three_weeks_ago
}
else if (diffYears == 0 && diffMonths == 0) {
return (HistoryTimeCategoryModel::HistoryConst)(diffDays / 7 + ((int)HistoryTimeCategoryModel::HistoryConst::Last_week)); //Last_week to Three_weeks_ago
}
//Check for last year
else if (currentTime-(12)*30.4f*24*3600 < time2) {
for (int i=1;i<12;i++) {
if (currentTime-(i+1)*30.4f*24*3600 < time2) //Not exact, but faster
return (HistoryTimeCategoryModel::HistoryConst)(i+((int)HistoryTimeCategoryModel::HistoryConst::Last_month)-1); //Last_month to Twelve_months ago
}
else if (diffYears == 0 && diffMonths > 0) {
return (HistoryTimeCategoryModel::HistoryConst)(diffMonths + ((int)HistoryTimeCategoryModel::HistoryConst::Last_month) - 1); //Last_month to Twelve_months ago
}
//if (QDate::currentDate().addYears(-1) >= date && QDate::currentDate().addYears(-2) < date)
else if (currentTime-365*24*3600 < time2)
else if (diffYears == 1)
return HistoryConst::Last_year;
//Every other senario
......@@ -164,6 +169,8 @@ HistoryTimeCategoryModel::HistoryConst HistoryTimeCategoryModel::timeToHistoryCo
QString HistoryTimeCategoryModel::indexToName(int idx)
{
if (idx > 24) return HistoryTimeCategoryModelPrivate::instance()->d_ptr->m_lCategories[24];
static int size = HistoryTimeCategoryModelPrivate::instance()->d_ptr->m_lCategories.size();
if (idx >= size)
return HistoryTimeCategoryModelPrivate::instance()->d_ptr->m_lCategories.last();
return HistoryTimeCategoryModelPrivate::instance()->d_ptr->m_lCategories[idx];
}
......@@ -41,21 +41,22 @@ public:
Last_week = 7 ,
Two_weeks_ago = 8 ,
Three_weeks_ago = 9 ,
Last_month = 10 ,
Two_months_ago = 11 ,
Three_months_ago = 12 ,
Four_months_ago = 13 ,
Five_months_ago = 14 ,
Six_months_ago = 15 ,
Seven_months_ago = 16 ,
Eight_months_ago = 17 ,
Nine_months_ago = 18 ,
Ten_months_ago = 19 ,
Eleven_months_ago = 20 ,
Twelve_months_ago = 21 ,
Last_year = 22 ,
Very_long_time_ago= 23 ,
Never = 24 ,
Four_weeks_ago = 10 ,
Last_month = 11 ,
Two_months_ago = 12 ,
Three_months_ago = 13 ,
Four_months_ago = 14 ,
Five_months_ago = 15 ,
Six_months_ago = 16 ,
Seven_months_ago = 17 ,
Eight_months_ago = 18 ,
Nine_months_ago = 19 ,
Ten_months_ago = 20 ,
Eleven_months_ago = 21 ,
Twelve_months_ago = 22 ,
Last_year = 23 ,
Very_long_time_ago= 24 ,
Never = 25 ,
};
Q_ENUMS(HistoryConst)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment