1 | #include "timer.h"
|
---|
2 |
|
---|
3 | #include <stdio.h>
|
---|
4 | #include <iostream.h>
|
---|
5 |
|
---|
6 | #include <math.h> // modf
|
---|
7 | #include "sys/time.h" // struct timeval
|
---|
8 | #include "slalib/slalib.h" // slacldj
|
---|
9 |
|
---|
10 | void Timer::SetTimer(int tv_sec, double tv_usec)
|
---|
11 | {
|
---|
12 | fSecs = tv_sec;
|
---|
13 | fMs = tv_usec;
|
---|
14 |
|
---|
15 | SetMjd((fMs+fSecs)/(60*60*24) + 40587);
|
---|
16 |
|
---|
17 | fSec = tv_sec%60;
|
---|
18 | tv_sec /= 60;
|
---|
19 |
|
---|
20 | fMin = tv_sec%60;
|
---|
21 | tv_sec /= 60;
|
---|
22 |
|
---|
23 | fHor = tv_sec%24;
|
---|
24 | tv_sec /= 24;
|
---|
25 |
|
---|
26 | // fDiv = fmod((fMs+fSecs)/(60*60*24), 1.0);
|
---|
27 | }
|
---|
28 |
|
---|
29 | void Timer::GetTimeval(struct timeval *tv) const
|
---|
30 | {
|
---|
31 | tv->tv_sec = fSecs;
|
---|
32 | tv->tv_usec = (__suseconds_t)fMs;
|
---|
33 | }
|
---|
34 |
|
---|
35 | /*
|
---|
36 | void Timer::Set(const long mjd)
|
---|
37 | {
|
---|
38 | SetMjd(mjd);
|
---|
39 |
|
---|
40 | //
|
---|
41 | // Express day in Gregorian calendar
|
---|
42 | // (taken from slalib slaDjcl)
|
---|
43 | //
|
---|
44 | const long jd = mjd + 2400001;
|
---|
45 | const long n4 = (((((jd*4-17918)/146097)*6)/4+1)/2+jd-37)*4;
|
---|
46 | const long nd10 = (((n4-237)%1461)/4)*10+5;
|
---|
47 |
|
---|
48 | fYea = n4/1461 - 4712;
|
---|
49 | fMon = ((nd10/306+2)%12) + 1;
|
---|
50 | fDay = (nd10%306)/10 + 1;
|
---|
51 | }
|
---|
52 | */
|
---|
53 | void Timer::SetTimer(const struct timeval *tv)
|
---|
54 | {
|
---|
55 | SetTimer(tv->tv_sec, (double)tv->tv_usec/1000000.0);
|
---|
56 | }
|
---|
57 |
|
---|
58 | Timer::Timer(double t)
|
---|
59 | {
|
---|
60 | double inte;
|
---|
61 | double frac = modf(t, &inte);
|
---|
62 | SetTimer(inte, frac);
|
---|
63 | }
|
---|
64 |
|
---|
65 | Timer::Timer(struct timeval *tv)
|
---|
66 | {
|
---|
67 | SetTimer(tv);
|
---|
68 | }
|
---|
69 |
|
---|
70 | Timer::Timer(const Timer &t)
|
---|
71 | {
|
---|
72 | fMs = t.fMs;
|
---|
73 | fDiv = t.fDiv;
|
---|
74 | fSec = t.fSec;
|
---|
75 | fSecs = t.fSecs;
|
---|
76 | fMin = t.fMin;
|
---|
77 | fHor = t.fHor;
|
---|
78 | fDay = t.fDay;
|
---|
79 | fMon = t.fMon;
|
---|
80 | fYea = t.fYea;
|
---|
81 | fMjd = t.fMjd;
|
---|
82 | }
|
---|
83 |
|
---|
84 | void Timer::SetMjd(double mjd)
|
---|
85 | {
|
---|
86 | //
|
---|
87 | // Express day in Gregorian calendar
|
---|
88 | // (taken from slalib slaDjcl)
|
---|
89 | //
|
---|
90 | fMjd = mjd;
|
---|
91 |
|
---|
92 | const long jd = (long)mjd + 2400001;
|
---|
93 | const long n4 = (((((jd*4-17918)/146097)*6)/4+1)/2+jd-37)*4;
|
---|
94 | const long nd10 = (((n4-237)%1461)/4)*10+5;
|
---|
95 |
|
---|
96 | fYea = n4/1461 - 4712;
|
---|
97 | fMon = ((nd10/306+2)%12) + 1;
|
---|
98 | fDay = (nd10%306)/10 + 1;
|
---|
99 |
|
---|
100 | fDiv = fmod(mjd, 1.0);
|
---|
101 |
|
---|
102 | mjd -= 40587;
|
---|
103 |
|
---|
104 | mjd *= 24;
|
---|
105 | fHor = (int)fmod(mjd, 24);
|
---|
106 |
|
---|
107 | mjd *= 60;
|
---|
108 | fMin = (int)fmod(mjd, 60);
|
---|
109 |
|
---|
110 | mjd *= 60;
|
---|
111 | fSec = (int)fmod(mjd, 60);
|
---|
112 |
|
---|
113 | fMs = fmod(mjd * 1000, 1000)/1000;
|
---|
114 | }
|
---|
115 |
|
---|
116 | double Timer::Now() //[s]
|
---|
117 | {
|
---|
118 | struct timeval tv;
|
---|
119 | gettimeofday(&tv, NULL);
|
---|
120 |
|
---|
121 | SetTimer(&tv);
|
---|
122 |
|
---|
123 | return fMs+fSecs;
|
---|
124 | }
|
---|
125 |
|
---|
126 | Timer::operator double() const //[s]
|
---|
127 | {
|
---|
128 | return fMs+fSecs;
|
---|
129 | }
|
---|
130 |
|
---|
131 | void Timer::Print()
|
---|
132 | {
|
---|
133 | cout << fSec << "s +" << fMs << "s" << endl;
|
---|
134 | }
|
---|
135 |
|
---|
136 | const char *Timer::GetTimeStr()
|
---|
137 | {
|
---|
138 | sprintf(fDateStr, "%d/%02d/%02d %d:%02d:%02d.%06li",
|
---|
139 | fYea, fMon, fDay, fHor, fMin, fSec, (long)(1000000.0*fMs));
|
---|
140 |
|
---|
141 | return fDateStr;
|
---|
142 | }
|
---|
143 |
|
---|
144 | ostream &operator<<(ostream &out, Timer &t)
|
---|
145 | {
|
---|
146 | char text[256];
|
---|
147 |
|
---|
148 | sprintf(text, "%d/%02d/%02d %d:%02d:%02d.%01li",
|
---|
149 | t.fYea, t.fMon, t.fDay, t.fHor, t.fMin, t.fSec, (long)(10.0*t.fMs));
|
---|
150 |
|
---|
151 | out << text;
|
---|
152 | return out;
|
---|
153 | }
|
---|