]> git.lyx.org Git - lyx.git/blobdiff - src/support/Timeout.cpp
Cmake build: Adapt handling of mytheslib to automek behaviour
[lyx.git] / src / support / Timeout.cpp
index 31f1aeb231d59ac263112d257c7c6f84c311d432..449fee033b77e1a6fc698b7595ac3d432ed47ec3 100644 (file)
 
 #include <config.h>
 
+#include "support/lassert.h"
 #include "support/Timeout.h"
 #include "support/debug.h"
 
 #include <QObject>
 #include <QTimerEvent>
 
+#include <ctime>
+#include <iomanip>
+#include <sstream>
+
 using namespace std;
 
 namespace lyx {
@@ -29,7 +34,7 @@ public:
        ///
        Impl(Timeout & owner) : owner_(owner), timeout_id(-1) {}
        ///
-       bool running() const { return timeout_id != -1; } 
+       bool running() const { return timeout_id != -1; }
        /// start the timer
        void start();
        /// stop the timer
@@ -40,7 +45,7 @@ public:
        unsigned int timeout_ms() const { return owner_.timeout_ms; }
 
 protected:
-       /// 
+       ///
        void timerEvent(QTimerEvent *) { owner_.emit(); }
 
 private:
@@ -134,11 +139,96 @@ Timeout & Timeout::setType(Type t)
 Timeout & Timeout::setTimeout(unsigned int msec)
 {
        // Can't have a timeout of zero!
-       BOOST_ASSERT(msec);
+       LASSERT(msec, msec = 1000);
 
        timeout_ms = msec;
        return *this;
 }
 
 
+struct Timer::Private
+{
+       time_t start_time;
+};
+
+
+Timer::Timer() : d(new Private)
+{
+       restart();
+}
+
+
+Timer::~Timer()
+{
+       delete d;
+}
+
+
+void Timer::restart()
+{
+       time(&d->start_time);
+}
+
+
+int Timer::elapsed() const
+{
+       time_t end_time;
+       time(&end_time);
+       double diff = difftime(end_time, d->start_time);
+       return int(diff);
+}
+
+
+string Timer::timeStr(char separator) const
+{
+       tm * timeinfo = localtime(&d->start_time);
+       // With less flexibility we could also use:
+       //strftime(buffer, 10, "%X", timeinfo);
+       ostringstream out;
+       out << setw(2) << setfill('0');
+       if (separator) {
+               out << separator << setw(2) << setfill('0') << timeinfo->tm_hour
+                   << separator << setw(2) << setfill('0') << timeinfo->tm_min
+                   << separator << setw(2) << setfill('0') << timeinfo->tm_sec;
+       } else {
+               out << setw(2) << setfill('0') << timeinfo->tm_hour
+                   << setw(2) << setfill('0') << timeinfo->tm_min
+                   << setw(2) << setfill('0') << timeinfo->tm_sec;
+       }
+       return out.str();
+}
+
+
+string Timer::dateStr(char separator) const
+{
+       tm * timeinfo = localtime(&d->start_time);
+       // With less flexibility we could also use:
+       //res = strftime(buffer, 10, "%d%m%y", timeinfo);
+       ostringstream out;
+       out << setw(2) << setfill('0') << timeinfo->tm_mday;
+       if (separator)
+               out << separator;
+       out << setw(2) << setfill('0') << timeinfo->tm_mon;
+       if (separator)
+               out << separator;
+       out << setw(2) << setfill('0') << timeinfo->tm_year - 100;
+       return out.str();
+}
+
+
+string Timer::toStr() const
+{
+       tm * timeinfo = localtime(&d->start_time);
+       return asctime(timeinfo);
+}
+
+
+string Timer::currentToStr()
+{
+       time_t current_time;
+       time(&current_time);
+       tm * timeinfo = localtime(&current_time);
+       return asctime(timeinfo);
+}
+
 } // namespace lyx