#include "timer.h" #include #include #include // modf #include "sys/time.h" // struct timeval #include "slalib/slalib.h" // slacldj void Timer::SetTimer(int tv_sec, double tv_usec) { fSecs = tv_sec; fMs = tv_usec; fSec = tv_sec%60; tv_sec /= 60; fMin = tv_sec%60; tv_sec /= 60; fHor = tv_sec%24; tv_sec /= 24; Set(tv_sec + 40587); fDiv = fmod((fMs+fSecs)/(60*60*24), 1.0); } void Timer::Set(const long mjd) { // // Express day in Gregorian calendar // (taken from slalib slaDjcl) // fMjd = mjd; const long jd = mjd + 2400001; const long n4 = (((((jd*4-17918)/146097)*6)/4+1)/2+jd-37)*4; const long nd10 = (((n4-237)%1461)/4)*10+5; fYea = n4/1461 - 4712; fMon = ((nd10/306+2)%12) + 1; fDay = (nd10%306)/10 + 1; } void Timer::SetTimer(const struct timeval *tv) { SetTimer(tv->tv_sec, (double)tv->tv_usec/1000000.0); } Timer::Timer(double t) { double inte; double frac = modf(t, &inte); SetTimer(inte, frac); } Timer::Timer(struct timeval *tv) { SetTimer(tv); } Timer::Timer(Timer &t) { fMs = t.fMs; fDiv = t.fDiv; fSec = t.fSec; fSecs = t.fSecs; fMin = t.fMin; fHor = t.fHor; fDay = t.fDay; fMon = t.fMon; fYea = t.fYea; fMjd = t.fMjd; } void Timer::SetMjd(double mjd) { Set((long)mjd); fDiv = fmod(mjd, 1.0); mjd -= 40587; mjd *= 24; fHor = (int)mjd; mjd *= 60; fMin = (int)mjd; mjd *= 60; fSec = (int)mjd; fMs = mjd * 1000; } double Timer::Now() //[s] { struct timeval tv; gettimeofday(&tv, NULL); SetTimer(&tv); return fMs+fSecs; } Timer::operator double() //[s] { return fMs+fSecs; } void Timer::Print() { cout << fSec << "s +" << fMs << "s" << endl; } const char *Timer::GetTimeStr() { sprintf(fDateStr, "%d/%02d/%02d %d:%02d:%02d.%06li", fYea, fMon, fDay, fHor, fMin, fSec, (long)(1000000.0*fMs)); return fDateStr; }