diff --git a/client/helper.cpp b/client/helper.cpp index aee6481..b3ae6ff 100644 --- a/client/helper.cpp +++ b/client/helper.cpp @@ -81,7 +81,7 @@ date date::fromTimestamp(time_t timestamp, int options) date date::today() { - date r = date::fromTimestamp(time(nullptr), To_LocalTime); + date r = date::fromTimestamp(::time(nullptr), To_LocalTime); return r; } @@ -129,6 +129,36 @@ bool date::operator >= (const date& rhs) return std::tie(mYear, mMonth, mDay) >= std::tie(rhs.mYear, rhs.mMonth, rhs.mDay); } +time::time(int h, int m, int s) + :mHour(h), mMinute(m), mSecond(s) +{} + +std::string time::toString(bool showSeconds) const +{ + char buffer[32]; + if (showSeconds) + sprintf(buffer, "%02d:%02d:%02d", mHour, mMinute, mSecond); + else + sprintf(buffer, "%02d:%02d", mHour, mMinute); + + return buffer; +} + +helper::time time::fromTimestamp(time_t timestamp, int options) +{ + tm* t = nullptr; + if (options == date::To_GmtTime) + t = gmtime(×tamp); + else + t = localtime(×tamp); + + helper::time r; + r.mHour = t->tm_hour; + r.mMinute = t->tm_min; + r.mSecond = t->tm_sec; + return r; +} + std::string chrono::secondsToDisplay(int seconds, bool showSeconds) { @@ -137,9 +167,9 @@ std::string chrono::secondsToDisplay(int seconds, bool showSeconds) int secs = seconds % 60; char r[32]; if (showSeconds) - sprintf(r, "%2d:%2d:%2d", hours, minutes, secs); + sprintf(r, "%02d:%02d:%02d", hours, minutes, secs); else - sprintf(r, "%2d:%2d", hours, minutes); + sprintf(r, "%02d:%02d", hours, minutes); return r; } diff --git a/client/helper.h b/client/helper.h index fdca824..0ef8287 100644 --- a/client/helper.h +++ b/client/helper.h @@ -40,10 +40,22 @@ namespace helper bool operator >= (const date& rhs); }; + struct time + { + int mHour = 0, mMinute = 0, mSecond = 0; + + time() = default; + time(int h, int m, int s); + std::string toString(bool showSeconds = true) const; + + static time fromTimestamp(time_t timestamp, int options); + }; + class chrono { public: + // Seconds is number of seconds in a day. It is NOT a UNIX timestamp. static std::string secondsToDisplay(int seconds, bool showSeconds); static std::string timeToStr(time_t timestamp); static time_t strToTime(const std::string& s); diff --git a/client/sqlitecpp/src/Database.cpp b/client/sqlitecpp/src/Database.cpp index 11fcc8d..2b5f34c 100644 --- a/client/sqlitecpp/src/Database.cpp +++ b/client/sqlitecpp/src/Database.cpp @@ -27,7 +27,7 @@ namespace SQLite // Open the provided database UTF-8 filename with SQLITE_OPEN_xxx provided flags. Database::Database(const char* apFilename, const int aFlags /*= SQLITE_OPEN_READONLY*/, const char* apVfs /*= NULL*/) : - mpSQLite(NULL), + mpSQLite(nullptr), mFilename(apFilename) { int ret = sqlite3_open_v2(apFilename, &mpSQLite, aFlags, apVfs); @@ -41,10 +41,10 @@ Database::Database(const char* apFilename, const int aFlags /*= SQLITE_OPEN_READ // Open the provided database UTF-8 filename with SQLITE_OPEN_xxx provided flags. Database::Database(const std::string& aFilename, const int aFlags /*= SQLITE_OPEN_READONLY*/, const std::string& aVfs) : - mpSQLite(NULL), + mpSQLite(nullptr), mFilename(aFilename) { - int ret = sqlite3_open_v2(aFilename.c_str(), &mpSQLite, aFlags, aVfs.empty() ? NULL : aVfs.c_str()); + int ret = sqlite3_open_v2(aFilename.c_str(), &mpSQLite, aFlags, aVfs.empty() ? nullptr : aVfs.c_str()); if (SQLITE_OK != ret) { std::string strerr = sqlite3_errmsg(mpSQLite); @@ -64,7 +64,7 @@ Database::~Database() noexcept // nothrow // Shortcut to execute one or multiple SQL statements without results (UPDATE, INSERT, ALTER, COMMIT, CREATE...). int Database::exec(const char* apQueries) { - int ret = sqlite3_exec(mpSQLite, apQueries, NULL, NULL, NULL); + int ret = sqlite3_exec(mpSQLite, apQueries, nullptr, nullptr, nullptr); check(ret); // Return the number of rows modified by those SQL statements (INSERT, UPDATE or DELETE only) diff --git a/client/task.cpp b/client/task.cpp index 5728501..2177a39 100644 --- a/client/task.cpp +++ b/client/task.cpp @@ -206,7 +206,7 @@ void TimeLine::start() mActive = true; // Find current time in UTC format - time_t current = time(nullptr); + time_t current = ::time(nullptr); // Check if current time point does not belong to any existing time interval if (hasTimePoint(current)) @@ -446,7 +446,7 @@ void TimeLine::stop(bool updateTimeline) return; if (updateTimeline) - flush(true, time(nullptr)); + flush(true, ::time(nullptr)); mActive = false; mActiveTimeRecord = nullptr; @@ -803,7 +803,7 @@ bool TimeLine::duplicateDetected() const void TimeLine::putDebugRecord() { - time_t current = time(nullptr); + time_t current = ::time(nullptr); time_t end = current + 600; TimeRecord* r = makeNewRecord(current, end); diff --git a/client/timetreedlg.cpp b/client/timetreedlg.cpp index 020b453..220bbff 100644 --- a/client/timetreedlg.cpp +++ b/client/timetreedlg.cpp @@ -31,6 +31,8 @@ void TimeTreeDlg::addInterval() else { QDateTime current = QDateTime::currentDateTime(); + time_t t = current.toTime_t(); + //helper::time time_of_day = helper::time::fromTimestamp(t, helper::date::To_LocalTime); mTimeIntervalDlg = new TimeIntervalDlg(this, mModel, mTimeline, TimeIntervalDlg::Type::New, TimeRecord()); connect(mTimeIntervalDlg, SIGNAL(accepted()), this, SLOT(onNewIntervalAccepted())); diff --git a/client/timetreemodel.cpp b/client/timetreemodel.cpp index cc2377b..9a8c7f6 100644 --- a/client/timetreemodel.cpp +++ b/client/timetreemodel.cpp @@ -304,13 +304,18 @@ QVariant TimeTreeModel::data(const QModelIndex &index, int role) const return QString::number(day); case Level_Time: + { mTimeLine->getTime(year, month, day, &intervals); tr = intervals[index.row()]; // Intervals are in local time already // ToDo: they are in GMT! - return QString("%1 - %2").arg(QString::fromStdString(chrono::secondsToDisplay(tr.startTime(), false)), - QString::fromStdString(chrono::secondsToDisplay(tr.endTime(), false))); + helper::time start = helper::time::fromTimestamp(tr.startTime(), helper::date::To_LocalTime), + end = helper::time::fromTimestamp(tr.endTime(), helper::date::To_LocalTime); + + return QString("%1 - %2").arg(QString::fromStdString(start.toString(false)), + QString::fromStdString(start.toString(false))); + } default: return QVariant();