129 lines
2.8 KiB
C++
129 lines
2.8 KiB
C++
#include "pitimer.h"
|
|
|
|
|
|
PITimer::PITimer(TimerEvent slot, void * data_) {
|
|
ret_func = slot;
|
|
data = data_;
|
|
#ifndef __WIN32__
|
|
ti = -1;
|
|
running = false;
|
|
se.sigev_notify = SIGEV_THREAD;
|
|
se.sigev_value.sival_ptr = this;
|
|
se.sigev_notify_function = timer_event;
|
|
se.sigev_notify_attributes = 0;
|
|
#endif
|
|
reset();
|
|
}
|
|
|
|
|
|
#ifndef __WIN32__
|
|
void PITimer::start(double msecs) {
|
|
spec.it_interval.tv_nsec = ((int)(msecs * 1000) % 1000000) * 1000;
|
|
spec.it_interval.tv_sec = (time_t)(msecs / 1000);
|
|
spec.it_value = spec.it_interval;
|
|
ti = timer_create(CLOCK_REALTIME, &se, &timer);
|
|
timer_settime(timer, 0, &spec, 0);
|
|
running = true;
|
|
}
|
|
|
|
|
|
void PITimer::timer_event(sigval e) {
|
|
PITimer * ct = (PITimer * )e.sival_ptr;
|
|
if (ct->ret_func != 0) ct->ret_func(ct->data);
|
|
}
|
|
#endif
|
|
|
|
|
|
double PITimer::elapsed_n() {
|
|
#ifdef __WIN32__
|
|
t_cur = GetCurrentTime();
|
|
return (t_cur * 1000000. - t_st * 1000000.);
|
|
#else
|
|
clock_gettime(0, &t_cur);
|
|
return (t_cur.tv_sec * 1.e+9 + t_cur.tv_nsec) -
|
|
(t_st.tv_sec * 1.e+9 + t_st.tv_nsec);
|
|
#endif
|
|
}
|
|
|
|
|
|
double PITimer::elapsed_u() {
|
|
#ifdef __WIN32__
|
|
t_cur = GetCurrentTime();
|
|
return (t_cur * 1000. - t_st * 1000.);
|
|
#else
|
|
clock_gettime(0, &t_cur);
|
|
return (t_cur.tv_sec * 1.e+6 + (t_cur.tv_nsec / 1.e+3)) -
|
|
(t_st.tv_sec * 1.e+6 + (t_st.tv_nsec / 1.e+3));
|
|
#endif
|
|
}
|
|
|
|
|
|
double PITimer::elapsed_m() {
|
|
#ifdef __WIN32__
|
|
t_cur = GetCurrentTime();
|
|
return (double)(t_cur - t_st);
|
|
#else
|
|
clock_gettime(0, &t_cur);
|
|
return (t_cur.tv_sec * 1.e+3 + (t_cur.tv_nsec / 1.e+6)) -
|
|
(t_st.tv_sec * 1.e+3 + (t_st.tv_nsec / 1.e+6));
|
|
#endif
|
|
}
|
|
|
|
|
|
double PITimer::elapsed_s() {
|
|
#ifdef __WIN32__
|
|
t_cur = GetCurrentTime();
|
|
return (t_cur / 1000. - t_st / 1000.);
|
|
#else
|
|
clock_gettime(0, &t_cur);
|
|
return (t_cur.tv_sec + (t_cur.tv_nsec / 1.e+9)) -
|
|
(t_st.tv_sec + (t_st.tv_nsec / 1.e+9));
|
|
#endif
|
|
}
|
|
|
|
|
|
PITime currentTime() {
|
|
time_t rt = time(0);
|
|
tm * pt = localtime(&rt);
|
|
PITime t;
|
|
t.seconds = pt->tm_sec;
|
|
t.minutes = pt->tm_min;
|
|
t.hours = pt->tm_hour;
|
|
return t;
|
|
}
|
|
|
|
|
|
PIDate currentDate() {
|
|
time_t rt = time(0);
|
|
tm * pt = localtime(&rt);
|
|
PIDate d;
|
|
d.day = pt->tm_mday;
|
|
d.month = pt->tm_mon + 1;
|
|
d.year = pt->tm_year + 1900;
|
|
return d;
|
|
}
|
|
|
|
|
|
string time2string(const PITime & time, const string & format) {
|
|
string ts = format;
|
|
int i = ts.find_first_of('h');
|
|
if (i >= 0) ts = ts.replace(i, 1, itos(time.hours));
|
|
i = ts.find_first_of('m');
|
|
if (i >= 0) ts = ts.replace(i, 1, itos(time.minutes));
|
|
i = ts.find_first_of('s');
|
|
if (i >= 0) ts = ts.replace(i, 1, itos(time.seconds));
|
|
return ts;
|
|
}
|
|
|
|
|
|
string date2string(const PIDate & date, const string & format) {
|
|
string ts = format;
|
|
int i = ts.find_first_of('y');
|
|
if (i >= 0) ts = ts.replace(i, 1, itos(date.year));
|
|
i = ts.find_first_of('m');
|
|
if (i >= 0) ts = ts.replace(i, 1, itos(date.month));
|
|
i = ts.find_first_of('d');
|
|
if (i >= 0) ts = ts.replace(i, 1, itos(date.day));
|
|
return ts;
|
|
}
|