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

Last change on this file since 1759 was 1759, checked in by tbretz, 22 years ago
*** empty log message ***
File size: 2.0 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 Set(tv_sec + 40587);
25
26 fDiv = fmod((fMs+fSecs)/(60*60*24), 1.0);
27}
28
29void Timer::Set(const long mjd)
30{
31 //
32 // Express day in Gregorian calendar
33 // (taken from slalib slaDjcl)
34 //
35 fMjd = mjd;
36
37 const long jd = mjd + 2400001;
38 const long n4 = (((((jd*4-17918)/146097)*6)/4+1)/2+jd-37)*4;
39 const long nd10 = (((n4-237)%1461)/4)*10+5;
40
41 fYea = n4/1461 - 4712;
42 fMon = ((nd10/306+2)%12) + 1;
43 fDay = (nd10%306)/10 + 1;
44}
45
46void Timer::SetTimer(const 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 fMjd = t.fMjd;
75}
76
77void Timer::SetMjd(double mjd)
78{
79 Set((long)mjd);
80
81 fDiv = fmod(mjd, 1.0);
82
83 mjd -= 40587;
84
85 mjd *= 24;
86 fHor = (int)mjd;
87
88 mjd *= 60;
89 fMin = (int)mjd;
90
91 mjd *= 60;
92 fSec = (int)mjd;
93
94 fMs = mjd * 1000;
95}
96
97double Timer::Now() //[s]
98{
99 struct timeval tv;
100 gettimeofday(&tv, NULL);
101
102 SetTimer(&tv);
103
104 return fMs+fSecs;
105}
106
107Timer::operator double() //[s]
108{
109 return fMs+fSecs;
110}
111
112void Timer::Print()
113{
114 cout << fSec << "s +" << fMs << "s" << endl;
115}
116
117const char *Timer::GetTimeStr()
118{
119 sprintf(fDateStr, "%d/%02d/%02d %d:%02d:%02d.%06li",
120 fYea, fMon, fDay, fHor, fMin, fSec, (long)(1000000.0*fMs));
121
122 return fDateStr;
123}
124
Note: See TracBrowser for help on using the repository browser.