29.07.2011 - fundamental new
This commit is contained in:
52
pitimer.cpp
52
pitimer.cpp
@@ -4,19 +4,20 @@
|
||||
PITimer::PITimer(TimerEvent slot, void * data_) {
|
||||
ret_func = slot;
|
||||
data = data_;
|
||||
#ifndef __WIN32__
|
||||
#ifndef WINDOWS
|
||||
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;
|
||||
lockRun = false;
|
||||
#endif
|
||||
reset();
|
||||
}
|
||||
|
||||
|
||||
#ifndef __WIN32__
|
||||
#ifndef WINDOWS
|
||||
void PITimer::start(double msecs) {
|
||||
spec.it_interval.tv_nsec = ((int)(msecs * 1000) % 1000000) * 1000;
|
||||
spec.it_interval.tv_sec = (time_t)(msecs / 1000);
|
||||
@@ -29,13 +30,17 @@ void PITimer::start(double msecs) {
|
||||
|
||||
void PITimer::timer_event(sigval e) {
|
||||
PITimer * ct = (PITimer * )e.sival_ptr;
|
||||
if (ct->ret_func != 0) ct->ret_func(ct->data);
|
||||
if (ct->ret_func != 0) {
|
||||
if (ct->lockRun) ct->lock();
|
||||
ct->ret_func(ct->data);
|
||||
if (ct->lockRun) ct->unlock();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
double PITimer::elapsed_n() {
|
||||
#ifdef __WIN32__
|
||||
#ifdef WINDOWS
|
||||
t_cur = GetCurrentTime();
|
||||
return (t_cur * 1000000. - t_st * 1000000.);
|
||||
#else
|
||||
@@ -47,7 +52,7 @@ double PITimer::elapsed_n() {
|
||||
|
||||
|
||||
double PITimer::elapsed_u() {
|
||||
#ifdef __WIN32__
|
||||
#ifdef WINDOWS
|
||||
t_cur = GetCurrentTime();
|
||||
return (t_cur * 1000. - t_st * 1000.);
|
||||
#else
|
||||
@@ -59,7 +64,7 @@ double PITimer::elapsed_u() {
|
||||
|
||||
|
||||
double PITimer::elapsed_m() {
|
||||
#ifdef __WIN32__
|
||||
#ifdef WINDOWS
|
||||
t_cur = GetCurrentTime();
|
||||
return (double)(t_cur - t_st);
|
||||
#else
|
||||
@@ -71,7 +76,7 @@ double PITimer::elapsed_m() {
|
||||
|
||||
|
||||
double PITimer::elapsed_s() {
|
||||
#ifdef __WIN32__
|
||||
#ifdef WINDOWS
|
||||
t_cur = GetCurrentTime();
|
||||
return (t_cur / 1000. - t_st / 1000.);
|
||||
#else
|
||||
@@ -104,25 +109,26 @@ PIDate currentDate() {
|
||||
}
|
||||
|
||||
|
||||
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));
|
||||
PIString time2string(const PITime & time, const PIString & format) {
|
||||
PIString ts = format;
|
||||
ts.replace("hh", PIString::fromNumber(time.hours).expandLeftTo(2, '0'));
|
||||
ts.replace("h", PIString::fromNumber(time.hours));
|
||||
ts.replace("mm", PIString::fromNumber(time.minutes).expandLeftTo(2, '0'));
|
||||
ts.replace("m", PIString::fromNumber(time.minutes));
|
||||
ts.replace("ss", PIString::fromNumber(time.seconds).expandLeftTo(2, '0'));
|
||||
ts.replace("s", PIString::fromNumber(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));
|
||||
PIString date2string(const PIDate & date, const PIString & format) {
|
||||
PIString ts = format;
|
||||
ts.replace("yyyy", PIString::fromNumber(date.year).expandLeftTo(4, '0'));
|
||||
ts.replace("yy", PIString::fromNumber(date.year).expandLeftTo(2, '0'));
|
||||
ts.replace("y", PIString::fromNumber(date.year));
|
||||
ts.replace("mm", PIString::fromNumber(date.month).expandLeftTo(2, '0'));
|
||||
ts.replace("m", PIString::fromNumber(date.month));
|
||||
ts.replace("dd", PIString::fromNumber(date.day).expandLeftTo(2, '0'));
|
||||
ts.replace("d", PIString::fromNumber(date.day));
|
||||
return ts;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user