Files
pip/pitimer.h
2010-12-10 23:42:53 +03:00

70 lines
1.3 KiB
C++

#ifndef PITIMER_H
#define PITIMER_H
#include <ctime>
#include <csignal>
#include "pithread.h"
typedef void (*TimerEvent)(void * );
struct PITime {
int seconds;
int minutes;
int hours;
};
struct PIDate {
int day;
int month;
int year; // since 1900
};
class PITimer
#ifdef __WIN32__
: public PIThread
#endif
{
public:
PITimer(TimerEvent slot = 0, void * data = 0);
~PITimer() {stop();}
#ifdef __WIN32__
void reset() {t_st = GetCurrentTime();}
#else
void reset() {clock_gettime(0, &t_st);}
void start(double msecs);
void stop() {if (ti == 0) timer_delete(timer); ti = -1; running = false;}
bool isRunning() const {return running;}
#endif
double elapsed_n(); // nanoseconds
double elapsed_u(); // microseconds
double elapsed_m(); // miliseconds
double elapsed_s(); // seconds
private:
#ifdef __WIN32__
void run() {if (ret_func != 0) ret_func(data);}
long int t_st, t_cur;
#else
static void timer_event(sigval e);
bool running;
int ti;
itimerspec spec;
timespec t_st, t_cur;
timer_t timer;
sigevent se;
#endif
void * data;
TimerEvent ret_func;
};
PITime currentTime();
PIDate currentDate();
string time2string(const PITime & time, const string & format = "h:m:s");
string date2string(const PIDate & date, const string & format = "d.m.y");
#endif // PITIMER_H