- fixes for time related operations

- cleanups for compiler warnings
This commit is contained in:
dmytro.bogovych 2019-05-11 22:52:59 +03:00
parent 53ff5a63dd
commit a1bd0c3d37
6 changed files with 61 additions and 12 deletions

View File

@ -81,7 +81,7 @@ date date::fromTimestamp(time_t timestamp, int options)
date date::today() date date::today()
{ {
date r = date::fromTimestamp(time(nullptr), To_LocalTime); date r = date::fromTimestamp(::time(nullptr), To_LocalTime);
return r; 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); 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(&timestamp);
else
t = localtime(&timestamp);
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) std::string chrono::secondsToDisplay(int seconds, bool showSeconds)
{ {
@ -137,9 +167,9 @@ std::string chrono::secondsToDisplay(int seconds, bool showSeconds)
int secs = seconds % 60; int secs = seconds % 60;
char r[32]; char r[32];
if (showSeconds) if (showSeconds)
sprintf(r, "%2d:%2d:%2d", hours, minutes, secs); sprintf(r, "%02d:%02d:%02d", hours, minutes, secs);
else else
sprintf(r, "%2d:%2d", hours, minutes); sprintf(r, "%02d:%02d", hours, minutes);
return r; return r;
} }

View File

@ -40,10 +40,22 @@ namespace helper
bool operator >= (const date& rhs); 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 class chrono
{ {
public: 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 secondsToDisplay(int seconds, bool showSeconds);
static std::string timeToStr(time_t timestamp); static std::string timeToStr(time_t timestamp);
static time_t strToTime(const std::string& s); static time_t strToTime(const std::string& s);

View File

@ -27,7 +27,7 @@ namespace SQLite
// Open the provided database UTF-8 filename with SQLITE_OPEN_xxx provided flags. // 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*/) : Database::Database(const char* apFilename, const int aFlags /*= SQLITE_OPEN_READONLY*/, const char* apVfs /*= NULL*/) :
mpSQLite(NULL), mpSQLite(nullptr),
mFilename(apFilename) mFilename(apFilename)
{ {
int ret = sqlite3_open_v2(apFilename, &mpSQLite, aFlags, apVfs); 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. // 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) : Database::Database(const std::string& aFilename, const int aFlags /*= SQLITE_OPEN_READONLY*/, const std::string& aVfs) :
mpSQLite(NULL), mpSQLite(nullptr),
mFilename(aFilename) 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) if (SQLITE_OK != ret)
{ {
std::string strerr = sqlite3_errmsg(mpSQLite); 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...). // Shortcut to execute one or multiple SQL statements without results (UPDATE, INSERT, ALTER, COMMIT, CREATE...).
int Database::exec(const char* apQueries) 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); check(ret);
// Return the number of rows modified by those SQL statements (INSERT, UPDATE or DELETE only) // Return the number of rows modified by those SQL statements (INSERT, UPDATE or DELETE only)

View File

@ -206,7 +206,7 @@ void TimeLine::start()
mActive = true; mActive = true;
// Find current time in UTC format // 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 // Check if current time point does not belong to any existing time interval
if (hasTimePoint(current)) if (hasTimePoint(current))
@ -446,7 +446,7 @@ void TimeLine::stop(bool updateTimeline)
return; return;
if (updateTimeline) if (updateTimeline)
flush(true, time(nullptr)); flush(true, ::time(nullptr));
mActive = false; mActive = false;
mActiveTimeRecord = nullptr; mActiveTimeRecord = nullptr;
@ -803,7 +803,7 @@ bool TimeLine::duplicateDetected() const
void TimeLine::putDebugRecord() void TimeLine::putDebugRecord()
{ {
time_t current = time(nullptr); time_t current = ::time(nullptr);
time_t end = current + 600; time_t end = current + 600;
TimeRecord* r = makeNewRecord(current, end); TimeRecord* r = makeNewRecord(current, end);

View File

@ -31,6 +31,8 @@ void TimeTreeDlg::addInterval()
else else
{ {
QDateTime current = QDateTime::currentDateTime(); 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()); mTimeIntervalDlg = new TimeIntervalDlg(this, mModel, mTimeline, TimeIntervalDlg::Type::New, TimeRecord());
connect(mTimeIntervalDlg, SIGNAL(accepted()), this, SLOT(onNewIntervalAccepted())); connect(mTimeIntervalDlg, SIGNAL(accepted()), this, SLOT(onNewIntervalAccepted()));

View File

@ -304,13 +304,18 @@ QVariant TimeTreeModel::data(const QModelIndex &index, int role) const
return QString::number(day); return QString::number(day);
case Level_Time: case Level_Time:
{
mTimeLine->getTime(year, month, day, &intervals); mTimeLine->getTime(year, month, day, &intervals);
tr = intervals[index.row()]; tr = intervals[index.row()];
// Intervals are in local time already // Intervals are in local time already
// ToDo: they are in GMT! // ToDo: they are in GMT!
return QString("%1 - %2").arg(QString::fromStdString(chrono::secondsToDisplay(tr.startTime(), false)), helper::time start = helper::time::fromTimestamp(tr.startTime(), helper::date::To_LocalTime),
QString::fromStdString(chrono::secondsToDisplay(tr.endTime(), false))); 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: default:
return QVariant(); return QVariant();