source: trunk/MagicSoft/Cosy/base/timer.cc@ 735

Last change on this file since 735 was 732, checked in by tbretz, 25 years ago
*** empty log message ***
File size: 1.7 KB
Line 
1#include "timer.h"
2
3#include <iostream.h>
4
5#include "math.h" // modf
6#include "sys/time.h" // struct timeval
7#include "slalib/slalib.h" // slacldj
8
9void Timer::SetTimer(int tv_sec, double tv_usec)
10{
11 fSecs = tv_sec;
12 fMs = tv_usec;
13
14 fSec = tv_sec%60;
15 tv_sec /= 60;
16
17 fMin = tv_sec%60;
18 tv_sec /= 60;
19
20 fHor = tv_sec%24;
21 tv_sec /= 24;
22
23 fDay = tv_sec%365;
24 tv_sec /= 365;
25
26 fYea = tv_sec+1970;
27
28 fDay -= (fYea-1972)/4;
29
30 int days = fDay;
31
32 fMon=0;
33 do fDay -= fDays[fMon++];
34 while (fDay>0);
35
36 int i=1;
37 while (i<fMon)
38 days -= fDays[i++];
39
40 fDay = days;
41
42 double dummy;
43 fDiv = modf((fMs+fSecs)/(60*60*24), &dummy);
44}
45
46void Timer::SetTimer(struct timeval *tv)
47{
48 SetTimer(tv->tv_sec, (double)tv->tv_usec/1000000.0);
49}
50
51Timer::Timer(double t)
52{
53 double inte;
54 double frac = modf(t, &inte);
55 SetTimer(inte, frac);
56}
57
58Timer::Timer(struct timeval *tv)
59{
60 SetTimer(tv);
61}
62
63Timer::Timer(Timer &t)
64{
65 fMs = t.fMs;
66 fDiv = t.fDiv;
67 fSec = t.fSec;
68 fSecs = t.fSecs;
69 fMin = t.fMin;
70 fHor = t.fHor;
71 fDay = t.fDay;
72 fMon = t.fMon;
73 fYea = t.fYea;
74}
75
76double Timer::GetTime()
77{
78 struct timeval tv;
79 gettimeofday(&tv, NULL);
80
81 SetTimer(tv.tv_sec, (double)tv.tv_usec/1000000.0);
82
83 return fMs+fSecs;
84}
85
86double Timer::GetMjd()
87{
88 int status;
89 double mjd=0;
90 slaCldj(fYea, fMon, fDay, &mjd, &status);
91
92 if (status)
93 cout << "Warning: slaCldj returned " << status << endl;
94
95 return mjd + fDiv;
96}
97
98Timer::operator double()
99{
100 return fMs+fSecs;
101}
102
103void Timer::Print()
104{
105 cout << fSec << "s +" << fMs << "s" << endl;
106}
107
108const int Timer::fDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
Note: See TracBrowser for help on using the repository browser.