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

Last change on this file since 1752 was 926, checked in by tbretz, 23 years ago
*** empty log message ***
File size: 1.9 KB
Line 
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
10void Timer::SetTimer(int tv_sec, double tv_usec)
11{
12 fSecs = tv_sec;
13 fMs = tv_usec;
14
15 fSec = tv_sec%60;
16 tv_sec /= 60;
17
18 fMin = tv_sec%60;
19 tv_sec /= 60;
20
21 fHor = tv_sec%24;
22 tv_sec /= 24;
23
24 fDay = tv_sec%365;
25 tv_sec /= 365;
26
27 fYea = tv_sec+1970;
28
29 fDay -= (fYea-1972)/4;
30
31 int days = fDay;
32
33 fMon=0;
34 do fDay -= fDays[fMon++];
35 while (fDay>0);
36
37 int i=1;
38 while (i<fMon)
39 days -= fDays[i++];
40
41 fDay = days;
42
43 double dummy;
44 fDiv = modf((fMs+fSecs)/(60*60*24), &dummy);
45}
46
47void Timer::SetTimer(const struct timeval *tv)
48{
49 SetTimer(tv->tv_sec, (double)tv->tv_usec/1000000.0);
50}
51
52Timer::Timer(double t)
53{
54 double inte;
55 double frac = modf(t, &inte);
56 SetTimer(inte, frac);
57}
58
59Timer::Timer(struct timeval *tv)
60{
61 SetTimer(tv);
62}
63
64Timer::Timer(Timer &t)
65{
66 fMs = t.fMs;
67 fDiv = t.fDiv;
68 fSec = t.fSec;
69 fSecs = t.fSecs;
70 fMin = t.fMin;
71 fHor = t.fHor;
72 fDay = t.fDay;
73 fMon = t.fMon;
74 fYea = t.fYea;
75}
76
77double Timer::Now() //[s]
78{
79 struct timeval tv;
80 gettimeofday(&tv, NULL);
81
82 SetTimer(&tv);
83
84 return fMs+fSecs;
85}
86
87double Timer::CalcMjd()
88{
89 int status;
90 double mjd=0;
91 slaCldj(fYea, fMon, fDay, &mjd, &status);
92
93 if (status)
94 cout << "Warning: slaCldj returned " << status << endl;
95
96 return mjd + fDiv;
97}
98
99Timer::operator double() //[s]
100{
101 return fMs+fSecs;
102}
103
104void Timer::Print()
105{
106 cout << fSec << "s +" << fMs << "s" << endl;
107}
108
109const char *Timer::GetTimeStr()
110{
111 sprintf(fDateStr, "%d/%02d/%02d %d:%02d:%02d.%06li",
112 fYea, fMon, fDay, fHor, fMin, fSec, (long)(1000000.0*fMs));
113
114 return fDateStr;
115}
116
117const 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.