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

Last change on this file since 2811 was 2728, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 8.7 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// Remarke: If you encounter strange behaviour, check the casting.
35// Note, that on Linux machines ULong_t and UInt_t is the same.
36//
37// Version 1:
38// ----------
39// - first version
40//
41// Version 2:
42// ----------
43// - removed fTimeStamp[2]
44//
45// Version 3:
46// ----------
47// - removed fDurtaion - we may put it back when it is needed
48// - complete rewrite of the data members (old ones completely replaced)
49//
50/////////////////////////////////////////////////////////////////////////////
51#include "MTime.h"
52
53#include <iomanip>
54#include <sys/time.h> // struct timeval
55
56#include <TTime.h>
57
58#include "MLog.h"
59#include "MAstro.h"
60
61ClassImp(MTime);
62
63using namespace std;
64
65const UInt_t MTime::kHour = 3600000; // [ms] one hour
66const UInt_t MTime::kDay = MTime::kHour*24; // [ms] one day
67
68// --------------------------------------------------------------------------
69//
70// Return date as year(y), month(m), day(d)
71//
72void MTime::GetDate(UShort_t &y, Byte_t &m, Byte_t &d) const
73{
74 MAstro::Mjd2Ymd((Long_t)fTime<0?fMjd-1:fMjd, y, m, d);
75}
76
77// --------------------------------------------------------------------------
78//
79// Return the time in the range [0h, 24h) = [0h0m0.000s - 23h59m59.999s]
80//
81void MTime::GetTime(Byte_t &h, Byte_t &m, Byte_t &s, UShort_t &ms) const
82{
83 Long_t tm = GetTime24();
84 ms = tm%1000; // [ms]
85 tm /= 1000; // [s]
86 s = tm%60; // [s]
87 tm /= 60; // [m]
88 m = tm%60; // [m]
89 tm /= 60; // [h]
90 h = tm; // [h]
91}
92
93// --------------------------------------------------------------------------
94//
95// Return time as MJD (=JD-24000000.5)
96//
97Double_t MTime::GetMjd() const
98{
99 return fMjd+(Double_t)(fNanoSec/1e6+(Long_t)fTime)/kDay;
100}
101
102// --------------------------------------------------------------------------
103//
104// Return a time which is expressed in milliseconds since 01/01/1995 0:00h
105// This is compatible with root's definition used in gSystem->Now()
106// and TTime.
107// Note, gSystem->Now() returns local time, such that it may differ
108// from GetRootTime() (if you previously called MTime::Now())
109//
110TTime MTime::GetRootTime() const
111{
112 return (ULong_t)((GetMjd()-49718)*kDay);
113}
114
115// --------------------------------------------------------------------------
116//
117// Return a time which is expressed in seconds since 01/01/1995 0:00h
118// This is compatible with root's definition used in TAxis.
119// Note, a TAxis always displayes (automatically) given times in
120// local time (while here we return UTC) such, that you may encounter
121// strange offsets. You can get rid of this by calling:
122// TAxis::SetTimeFormat("[your-format] %F1995-01-01 00:00:00");
123//
124Double_t MTime::GetAxisTime() const
125{
126 return (GetMjd()-49718)*kDay/1000;
127}
128
129// --------------------------------------------------------------------------
130//
131// Set a time expressed in MJD, Time of Day (eg. 23:12.779h expressed
132// in milliseconds) and a nanosecond part.
133//
134Bool_t MTime::Set(UInt_t mjd, ULong_t ms, UInt_t ns)
135{
136 // [d] mjd (eg. 52320)
137 // [ms] time (eg. 17h expressed in ms)
138 // [ns] time (ns part of time)
139
140 if (ms>kDay-1 || ns>999999)
141 return kFALSE;
142
143 const Bool_t am = ms < kHour*13; // day of sunrise?
144
145 fMjd = am ? mjd : mjd + 1;
146 fTime = (Long_t)(am ? ms : ms-kDay);
147 fNanoSec = ns;
148
149 return kTRUE;
150}
151
152// --------------------------------------------------------------------------
153//
154// Set MTime to given MJD (eg. 52080.0915449892)
155//
156void MTime::SetMjd(Double_t m)
157{
158 const UInt_t mjd = (UInt_t)TMath::Floor(m);
159 const Double_t frac = fmod(m, 1)*kDay; // [ms] Fraction of day
160 const UInt_t ns = (UInt_t)fmod(frac*1e6, 1000000);
161
162 Set(mjd, (ULong_t)TMath::Floor(frac), ns);
163}
164
165// --------------------------------------------------------------------------
166//
167// Set MTime to given time and date
168//
169Bool_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)
170{
171 if (h>23 || min>59 || s>59 || ms>999 || ns>999999)
172 return kFALSE;
173
174 const Int_t mjd = MAstro::Ymd2Mjd(y, m, d);
175 if (mjd<0)
176 return kFALSE;
177
178 const ULong_t tm = ((((h*60+min)*60)+s)*1000)+ms;
179
180 return Set(mjd, tm, ns);
181}
182
183// --------------------------------------------------------------------------
184//
185// Set MTime to time expressed in a 'struct timeval'
186//
187void MTime::Set(const struct timeval &tv)
188{
189 const UInt_t mjd = 1000*tv.tv_sec/kDay + 40587;
190 const Long_t tm = tv.tv_sec%(24*3600)*1000 + tv.tv_usec/1000;
191 const UInt_t ms = tv.tv_usec%1000;
192
193 Set(mjd, tm, ms*1000);
194}
195
196// --------------------------------------------------------------------------
197//
198// Set MTime to time expressed as in CT1 PreProc files
199//
200void MTime::SetCT1Time(UInt_t mjd, UInt_t t1, UInt_t t0)
201{
202 // int isecs_since_midday; // seconds passed since midday before sunset (JD of run start)
203 // int isecfrac_200ns; // fractional part of isecs_since_midday
204 // fTime->SetTime(isecfrac_200ns, isecs_since_midday);
205 fNanoSec = (200*t1)%1000000;
206 const ULong_t ms = (200*t1)/1000000 + t0+12*kHour;
207
208 fTime = (Long_t)(ms<13*kHour ? ms : ms-kDay);
209
210 fMjd = mjd+1;
211}
212
213// --------------------------------------------------------------------------
214//
215// Update the magic time. Make sure, that the MJD is set correctly.
216// It must be the MJD of the corresponding night. You can set it
217// by Set(2003, 12, 24);
218//
219// It is highly important, that the time correspoding to the night is
220// between 13:00:00.0 (day of dawning) and 12:59:59.999 (day of sunrise)
221//
222Bool_t MTime::UpdMagicTime(Byte_t h, Byte_t m, Byte_t s, UShort_t ms)
223{
224 if (h>23 || m>59 || s>59 || ms>999)
225 return kFALSE;
226
227 const ULong_t tm = ((((h*60+m)*60)+s)*1000)+ms;
228
229 fTime = (Long_t)(tm < kHour*13 ? tm : tm-kDay); // day of sunrise?
230 //fNanoSec = ns;
231
232 return kTRUE;
233
234}
235
236// --------------------------------------------------------------------------
237//
238// Set the time to the current system time. The timezone is ignored.
239// If everything is set correctly you'll get UTC.
240//
241void MTime::Now()
242{
243#ifdef __LINUX__
244 struct timeval tv;
245 if (gettimeofday(&tv, NULL)<0)
246 Reset();
247 else
248 Set(tv);
249#else
250 Reset();
251#endif
252}
253
254// --------------------------------------------------------------------------
255//
256// Return contents as a TString of the form:
257// "yyyy/mm/dd hh:mm:ss.fff"
258//
259TString MTime::GetString() const
260{
261 UShort_t y, ms;
262 Byte_t mon, d, h, m, s;
263
264 GetDate(y, mon, d);
265 GetTime(h, m, s, ms);
266
267 return TString(Form("%4d/%02d/%02d %02d:%02d:%02d.%03d", y, mon, d, h, m, s, ms));
268}
269
270// --------------------------------------------------------------------------
271//
272// Return contents as a TString of the form:
273// "yyyymmdd_hhmmss"
274//
275TString MTime::GetFileName() const
276{
277 UShort_t y;
278 Byte_t mon, d, h, m, s;
279
280 GetDate(y, mon, d);
281 GetTime(h, m, s);
282
283 return TString(Form("%04d%02d%02d_%02d%02d%02d", y, mon, d, h, m, s));
284}
285
286// --------------------------------------------------------------------------
287//
288// Print MTime as string
289//
290void MTime::Print(Option_t *) const
291{
292 UShort_t yea, ms;
293 Byte_t mon, day, h, m, s;
294
295 GetDate(yea, mon, day);
296 GetTime(h, m, s, ms);
297
298 *fLog << GetDescriptor() << ": ";
299 *fLog << GetString() << Form(" (+%dns)", fNanoSec) << endl;
300}
301
302istream &MTime::ReadBinary(istream &fin)
303{
304 UShort_t y;
305 Byte_t mon, d, h, m, s;
306
307 fin.read((char*)&y, 2);
308 fin.read((char*)&mon, 1);
309 fin.read((char*)&d, 1);
310 fin.read((char*)&h, 1);
311 fin.read((char*)&m, 1);
312 fin.read((char*)&s, 1); // Total=7
313
314 Set(y, mon, d, h, m, s, 0);
315
316 return fin;
317}
Note: See TracBrowser for help on using the repository browser.