source: trunk/MagicSoft/Mars/mbase/MTime.cc@ 2678

Last change on this file since 2678 was 2678, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 7.9 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz 12/2000 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2003
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MTime
28//
29// A generalized MARS time stamp
30//
31// WARNING: Be carefull changing this class. It is also used in the
32// MAGIC drive software cosy!
33//
34// Version 1:
35// ----------
36// - first version
37//
38// Version 2:
39// ----------
40// - removed fTimeStamp[2]
41//
42// Version 3:
43// ----------
44// - removed fDurtaion - we may put it back when it is needed
45// - complete rewrite of the data members (old ones completely replaced)
46//
47/////////////////////////////////////////////////////////////////////////////
48#include "MTime.h"
49
50#include <iomanip>
51#include <sys/time.h> // struct timeval
52
53#include <TTime.h>
54
55#include "MLog.h"
56#include "MAstro.h"
57
58ClassImp(MTime);
59
60using namespace std;
61
62const UInt_t MTime::kHour = 3600000; // [ms] one hour
63const UInt_t MTime::kDay = MTime::kHour*24; // [ms] one day
64
65// --------------------------------------------------------------------------
66//
67// Return date as year(y), month(m), day(d)
68//
69void MTime::GetDate(UShort_t &y, Byte_t &m, Byte_t &d) const
70{
71 MAstro::Mjd2Ymd((Long_t)fTime<0?fMjd-1:fMjd, y, m, d);
72}
73
74// --------------------------------------------------------------------------
75//
76// Return the time in the range [0h, 24h) = [0h0m0.000s - 23h59m59.999s]
77//
78void MTime::GetTime(Byte_t &h, Byte_t &m, Byte_t &s, UShort_t &ms) const
79{
80 Long_t tm = GetTime24();
81 ms = tm%1000; // [ms]
82 tm /= 1000; // [s]
83 s = tm%60; // [s]
84 tm /= 60; // [m]
85 m = tm%60; // [m]
86 tm /= 60; // [h]
87 h = tm; // [h]
88}
89
90// --------------------------------------------------------------------------
91//
92// Return time as MJD (=JD-24000000.5)
93//
94Double_t MTime::GetMjd() const
95{
96 return fMjd+(Double_t)(fNanoSec/1e6+(Long_t)fTime)/kDay;
97}
98
99// --------------------------------------------------------------------------
100//
101// Return a time which is expressed in milliseconds since 01/01/1995 0:00h
102// This is compatible with root's definition used in gSystem->Now()
103// and TTime.
104// Note, gSystem->Now() returns local time, such that it may differ
105// from GetRootTime() (if you previously called MTime::Now())
106//
107TTime MTime::GetRootTime() const
108{
109 return (ULong_t)((GetMjd()-49718)*kDay);
110}
111
112// --------------------------------------------------------------------------
113//
114// Return a time which is expressed in seconds since 01/01/1995 0:00h
115// This is compatible with root's definition used in TAxis.
116// Note, a TAxis always displayes (automatically) given times in
117// local time (while here we return UTC) such, that you may encounter
118// strange offsets. You can get rid of this by calling:
119// TAxis::SetTimeFormat("[your-format] %F1995-01-01 00:00:00");
120//
121Double_t MTime::GetAxisTime() const
122{
123 return (GetMjd()-49718)*kDay/1000;
124}
125
126// --------------------------------------------------------------------------
127//
128// Set a time expressed in MJD, Time of Day (eg. 23:12.779h expressed
129// in milliseconds) and a nanosecond part.
130//
131Bool_t MTime::Set(UInt_t mjd, ULong_t ms, UInt_t ns)
132{
133 // [d] mjd (eg. 52320)
134 // [ms] time (eg. 17h expressed in ms)
135 // [ns] time (ns part of time)
136
137 if (ms>kDay-1 || ns>999999)
138 return kFALSE;
139
140 const Bool_t am = ms < kHour*13; // day of sunrise?
141
142 fMjd = am ? mjd : mjd + 1;
143 fTime = (Long_t)(am ? ms : ms-kDay);
144 fNanoSec = ns;
145
146 return kTRUE;
147}
148
149// --------------------------------------------------------------------------
150//
151// Set MTime to given MJD (eg. 52080.0915449892)
152//
153void MTime::SetMjd(Double_t m)
154{
155 const UInt_t mjd = (UInt_t)TMath::Floor(m);
156 const Double_t frac = fmod(m, 1)*kDay; // [ms] Fraction of day
157 const UInt_t ns = (UInt_t)fmod(frac*1e6, 1000000);
158
159 Set(mjd, (ULong_t)TMath::Floor(frac), ns);
160}
161
162// --------------------------------------------------------------------------
163//
164// Set MTime to given time and date
165//
166Bool_t MTime::Set(UShort_t y, Byte_t m, Byte_t d, Byte_t h, Byte_t min, Byte_t s, UShort_t ms, UInt_t ns)
167{
168 if (h>23 || min>59 || s>59 || ms>999 || ns>999999)
169 return kFALSE;
170
171 const Int_t mjd = MAstro::Ymd2Mjd(y, m, d);
172 if (mjd<0)
173 return kFALSE;
174
175 const ULong_t tm = ((((h*60+min)*60)+s)*1000)+ms;
176
177 return Set(mjd, tm, ns);
178}
179
180// --------------------------------------------------------------------------
181//
182// Set MTime to time expressed in a 'struct timeval'
183//
184void MTime::Set(const struct timeval &tv)
185{
186 const UInt_t mjd = 1000*tv.tv_sec/kDay + 40587;
187 const Long_t tm = tv.tv_sec%(24*3600)*1000 + tv.tv_usec/1000;
188 const UInt_t ms = tv.tv_usec%1000;
189
190 Set(mjd, tm, ms*1000);
191}
192
193// --------------------------------------------------------------------------
194//
195// Set MTime to time expressed as in CT1 PreProc files
196//
197void MTime::SetCT1Time(UInt_t mjd, UInt_t t1, UInt_t t0)
198{
199 // int isecs_since_midday; // seconds passed since midday before sunset (JD of run start)
200 // int isecfrac_200ns; // fractional part of isecs_since_midday
201 // fTime->SetTime(isecfrac_200ns, isecs_since_midday);
202 fNanoSec = (200*t1)%1000000;
203 const ULong_t ms = (200*t1)/1000000 + t0+12*kHour;
204
205 fTime = (Long_t)(ms<13*kHour ? ms : ms-kDay);
206
207 fMjd = mjd+1;
208}
209
210// --------------------------------------------------------------------------
211//
212// Set the time to the current system time. The timezone is ignored.
213// If everything is set correctly you'll get UTC.
214//
215void MTime::Now()
216{
217#ifdef __LINUX__
218 struct timeval tv;
219 if (gettimeofday(&tv, NULL)<0)
220 Reset();
221 else
222 Set(tv);
223#else
224 Reset();
225#endif
226}
227
228// --------------------------------------------------------------------------
229//
230// Return contents as a TString of the form:
231// "yyyy/mm/dd hh:mm:ss.fff"
232//
233TString MTime::GetString() const
234{
235 UShort_t y, ms;
236 Byte_t mon, d, h, m, s;
237
238 GetDate(y, mon, d);
239 GetTime(h, m, s, ms);
240
241 return TString(Form("%4d/%02d/%02d %02d:%02d:%02d.%03d", y, mon, d, h, m, s, ms));
242}
243
244// --------------------------------------------------------------------------
245//
246// Return contents as a TString of the form:
247// "yyyymmdd_hhmmss"
248//
249TString MTime::GetFileName() const
250{
251 UShort_t y;
252 Byte_t mon, d, h, m, s;
253
254 GetDate(y, mon, d);
255 GetTime(h, m, s);
256
257 return TString(Form("%04d%02d%02d_%02d%02d%02d", y, mon, d, h, m, s));
258}
259
260// --------------------------------------------------------------------------
261//
262// Print MTime as string
263//
264void MTime::Print(Option_t *) const
265{
266 UShort_t yea, ms;
267 Byte_t mon, day, h, m, s;
268
269 GetDate(yea, mon, day);
270 GetTime(h, m, s, ms);
271
272 *fLog << GetDescriptor() << ": ";
273 *fLog << GetString() << Form(" (+%dns)", fNanoSec) << endl;
274}
275
276istream &MTime::ReadBinary(istream &fin)
277{
278 UShort_t y;
279 Byte_t mon, d, h, m, s;
280
281 fin.read((char*)&y, 2);
282 fin.read((char*)&mon, 1);
283 fin.read((char*)&d, 1);
284 fin.read((char*)&h, 1);
285 fin.read((char*)&m, 1);
286 fin.read((char*)&s, 1); // Total=7
287
288 Set(y, mon, d, h, m, s, 0);
289
290 return fin;
291}
Note: See TracBrowser for help on using the repository browser.