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

Last change on this file since 4308 was 3969, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 15.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//
32// We do not use floating point values here, because of several reasons:
33// - having the times stored in integers only is more accurate and
34// more reliable in comparison conditions
35// - storing only integers gives similar bit-pattern for similar times
36// which makes compression (eg gzip algorithm in TFile) more
37// successfull
38//
39// Note, that there are many conversion function converting the day time
40// into a readable string. Also a direct interface to SQL time strings
41// is available.
42//
43// If you are using MTime containers as axis lables in root histograms
44// use GetAxisTime(). Make sure that you use the correct TimeFormat
45// on your TAxis (see GetAxisTime())
46//
47//
48// WARNING: Be carefull changing this class. It is also used in the
49// MAGIC drive software cosy as VERY IMPORTANT stuff!
50//
51// Remarke: If you encounter strange behaviour, check the casting.
52// Note, that on Linux machines ULong_t and UInt_t is the same.
53//
54//
55// Version 1:
56// ----------
57// - first version
58//
59// Version 2:
60// ----------
61// - removed fTimeStamp[2]
62//
63// Version 3:
64// ----------
65// - removed fDurtaion - we may put it back when it is needed
66// - complete rewrite of the data members (old ones completely replaced)
67//
68/////////////////////////////////////////////////////////////////////////////
69#include "MTime.h"
70
71#include <iomanip>
72
73#include <time.h> // struct tm
74#include <sys/time.h> // struct timeval
75
76#include <TTime.h>
77
78#include "MLog.h"
79#include "MAstro.h"
80
81ClassImp(MTime);
82
83using namespace std;
84
85const UInt_t MTime::kHour = 3600000; // [ms] one hour
86const UInt_t MTime::kDay = MTime::kHour*24; // [ms] one day
87
88// --------------------------------------------------------------------------
89//
90// Constructor. Calls SetMjd(d) for d>0 in all other cases the time
91// is set to the current UTC time.
92//
93MTime::MTime(Double_t d)
94{
95 Init(0, 0);
96 if (d<=0)
97 Now();
98 else
99 SetMjd(d);
100}
101
102// --------------------------------------------------------------------------
103//
104// Return date as year(y), month(m), day(d)
105//
106void MTime::GetDate(UShort_t &y, Byte_t &m, Byte_t &d) const
107{
108 MAstro::Mjd2Ymd((Long_t)fTime<0?fMjd-1:fMjd, y, m, d);
109}
110
111// --------------------------------------------------------------------------
112//
113// Return the time in the range [0h, 24h) = [0h0m0.000s - 23h59m59.999s]
114//
115void MTime::GetTime(Byte_t &h, Byte_t &m, Byte_t &s, UShort_t &ms) const
116{
117 Long_t tm = GetTime24();
118 ms = tm%1000; // [ms]
119 tm /= 1000; // [s]
120 s = tm%60; // [s]
121 tm /= 60; // [m]
122 m = tm%60; // [m]
123 tm /= 60; // [h]
124 h = tm; // [h]
125}
126
127// --------------------------------------------------------------------------
128//
129// Return time as MJD (=JD-24000000.5)
130//
131Double_t MTime::GetMjd() const
132{
133 return fMjd+(Double_t)(fNanoSec/1e6+(Long_t)fTime)/kDay;
134}
135
136// --------------------------------------------------------------------------
137//
138// Return a time which is expressed in milliseconds since 01/01/1995 0:00h
139// This is compatible with root's definition used in gSystem->Now()
140// and TTime.
141// Note, gSystem->Now() returns local time, such that it may differ
142// from GetRootTime() (if you previously called MTime::Now())
143//
144TTime MTime::GetRootTime() const
145{
146 return (ULong_t)((GetMjd()-49718)*kDay);
147}
148
149// --------------------------------------------------------------------------
150//
151// Return a time which is expressed in seconds since 01/01/1995 0:00h
152// This is compatible with root's definition used in TAxis.
153// Note, a TAxis always displayes (automatically) given times in
154// local time (while here we return UTC) such, that you may encounter
155// strange offsets. You can get rid of this by calling:
156// TAxis::SetTimeFormat("[your-format] %F1995-01-01 00:00:00");
157//
158Double_t MTime::GetAxisTime() const
159{
160 return (GetMjd()-49718)*kDay/1000;
161}
162
163// --------------------------------------------------------------------------
164//
165// Set a time expressed in MJD, Time of Day (eg. 23:12.779h expressed
166// in milliseconds) and a nanosecond part.
167//
168Bool_t MTime::SetMjd(UInt_t mjd, ULong_t ms, UInt_t ns)
169{
170 // [d] mjd (eg. 52320)
171 // [ms] time (eg. 17h expressed in ms)
172 // [ns] time (ns part of time)
173
174 if (ms>kDay-1 || ns>999999)
175 return kFALSE;
176
177 const Bool_t am = ms<kHour*13; // day of sunrise?
178
179 fMjd = am ? mjd : mjd + 1;
180 fTime = (Long_t)(am ? ms : ms-kDay);
181 fNanoSec = ns;
182
183 return kTRUE;
184}
185
186// --------------------------------------------------------------------------
187//
188// Set MTime to given MJD (eg. 52080.0915449892)
189//
190void MTime::SetMjd(Double_t m)
191{
192 const UInt_t mjd = (UInt_t)TMath::Floor(m);
193 const Double_t frac = fmod(m, 1)*kDay; // [ms] Fraction of day
194 const UInt_t ns = (UInt_t)fmod(frac*1e6, 1000000);
195
196 SetMjd(mjd, (ULong_t)TMath::Floor(frac), ns);
197}
198
199// --------------------------------------------------------------------------
200//
201// Set MTime to given time and date
202//
203Bool_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)
204{
205 if (h>23 || min>59 || s>59 || ms>999 || ns>999999)
206 return kFALSE;
207
208 const Int_t mjd = MAstro::Ymd2Mjd(y, m, d);
209 if (mjd<0)
210 return kFALSE;
211
212 const ULong_t tm = ((((h*60+min)*60)+s)*1000)+ms;
213
214 return SetMjd(mjd, tm, ns);
215}
216
217// --------------------------------------------------------------------------
218//
219// Set MTime to time expressed in a 'struct timeval'
220//
221void MTime::Set(const struct timeval &tv)
222{
223 const UInt_t mjd = 1000*tv.tv_sec/kDay + 40587;
224 const Long_t tm = tv.tv_sec%(24*3600)*1000 + tv.tv_usec/1000;
225 const UInt_t ms = tv.tv_usec%1000;
226
227 SetMjd(mjd, tm, ms*1000);
228}
229
230// --------------------------------------------------------------------------
231//
232// Return contents as a TString of the form:
233// "dd.mm.yyyy hh:mm:ss.fff"
234//
235Bool_t MTime::SetString(const char *str)
236{
237 if (!str)
238 return kFALSE;
239
240 UInt_t y, mon, d, h, m, s, ms;
241 const Int_t n = sscanf(str, "%02u.%02u.%04u %02u:%02u:%02u.%03u",
242 &d, &mon, &y, &h, &m, &s, &ms);
243
244 return n==7 ? Set(y, mon, d, h, m, s, ms) : kFALSE;
245}
246
247// --------------------------------------------------------------------------
248//
249// Return contents as a TString of the form:
250// "yyyy-mm-dd hh:mm:ss"
251//
252Bool_t MTime::SetSqlDateTime(const char *str)
253{
254 if (!str)
255 return kFALSE;
256
257 UInt_t y, mon, d, h, m, s;
258 const Int_t n = sscanf(str, "%04u-%02u-%02u %02u:%02u:%02u",
259 &y, &mon, &d, &h, &m, &s);
260
261 return n==6 ? Set(y, mon, d, h, m, s) : kFALSE;
262}
263
264// --------------------------------------------------------------------------
265//
266// Return contents as a TString of the form:
267// "yyyymmddhhmmss"
268//
269Bool_t MTime::SetSqlTimeStamp(const char *str)
270{
271 if (!str)
272 return kFALSE;
273
274 UInt_t y, mon, d, h, m, s;
275 const Int_t n = sscanf(str, "%04u%02u%02u%02u%02u%02u",
276 &y, &mon, &d, &h, &m, &s);
277
278 return n==6 ? Set(y, mon, d, h, m, s) : kFALSE;
279}
280
281// --------------------------------------------------------------------------
282//
283// Set MTime to time expressed as in CT1 PreProc files
284//
285void MTime::SetCT1Time(UInt_t mjd, UInt_t t1, UInt_t t0)
286{
287 // int isecs_since_midday; // seconds passed since midday before sunset (JD of run start)
288 // int isecfrac_200ns; // fractional part of isecs_since_midday
289 // fTime->SetTime(isecfrac_200ns, isecs_since_midday);
290 fNanoSec = (200*t1)%1000000;
291 const ULong_t ms = (200*t1)/1000000 + t0+12*kHour;
292
293 fTime = (Long_t)(ms<13*kHour ? ms : ms-kDay);
294
295 fMjd = mjd+1;
296}
297
298// --------------------------------------------------------------------------
299//
300// Update the magic time. Make sure, that the MJD is set correctly.
301// It must be the MJD of the corresponding night. You can set it
302// by Set(2003, 12, 24);
303//
304// It is highly important, that the time correspoding to the night is
305// between 13:00:00.0 (day of dawning) and 12:59:59.999 (day of sunrise)
306//
307Bool_t MTime::UpdMagicTime(Byte_t h, Byte_t m, Byte_t s, UInt_t ns)
308{
309 if (h>23 || m>59 || s>59 || ns>999999999)
310 return kFALSE;
311
312 const ULong_t tm = ((((h*60+m)*60)+s)*1000)+ns/1000000;
313
314 fTime = (Long_t)(tm<kHour*13 ? tm : tm-kDay); // day of sunrise?
315 fNanoSec = ns%1000000;
316
317 return kTRUE;
318
319}
320
321// --------------------------------------------------------------------------
322//
323// Conversion from Universal Time to Greenwich mean sidereal time,
324// with rounding errors minimized.
325//
326// The result is the Greenwich Mean Sidereal Time (radians)
327//
328// There is no restriction on how the UT is apportioned between the
329// date and ut1 arguments. Either of the two arguments could, for
330// example, be zero and the entire date+time supplied in the other.
331// However, the routine is designed to deliver maximum accuracy when
332// the date argument is a whole number and the ut argument lies in
333// the range 0 to 1, or vice versa.
334//
335// The algorithm is based on the IAU 1982 expression (see page S15 of
336// the 1984 Astronomical Almanac). This is always described as giving
337// the GMST at 0 hours UT1. In fact, it gives the difference between
338// the GMST and the UT, the steady 4-minutes-per-day drawing-ahead of
339// ST with respect to UT. When whole days are ignored, the expression
340// happens to equal the GMST at 0 hours UT1 each day.
341//
342// In this routine, the entire UT1 (the sum of the two arguments date
343// and ut) is used directly as the argument for the standard formula.
344// The UT1 is then added, but omitting whole days to conserve accuracy.
345//
346// The extra numerical precision delivered by the present routine is
347// unlikely to be important in an absolute sense, but may be useful
348// when critically comparing algorithms and in applications where two
349// sidereal times close together are differenced.
350//
351Double_t MTime::GetGmst() const
352{
353 const Double_t ut = (Double_t)(fNanoSec/1e6+(Long_t)fTime)/kDay;
354
355 // Julian centuries since J2000.
356 const Double_t t = (ut -(51544.5-fMjd)) / 36525.0;
357
358 // GMST at this UT1
359 const Double_t r1 = 24110.54841+(8640184.812866+(0.093104-6.2e-6*t)*t)*t;
360 const Double_t r2 = 86400.0*ut;
361
362 const Double_t sum = (r1+r2)/(24*3600);
363
364 return fmod(sum, 1)*TMath::TwoPi();//+TMath::TwoPi();
365}
366
367// --------------------------------------------------------------------------
368//
369// Set the time to the current system time. The timezone is ignored.
370// If everything is set correctly you'll get UTC.
371//
372void MTime::Now()
373{
374#ifdef __LINUX__
375 struct timeval tv;
376 if (gettimeofday(&tv, NULL)<0)
377 Clear();
378 else
379 Set(tv);
380#else
381 Clear();
382#endif
383}
384
385// --------------------------------------------------------------------------
386//
387// Return contents as a TString of the form:
388// "dd.mm.yyyy hh:mm:ss.fff"
389//
390TString MTime::GetString() const
391{
392 UShort_t y, ms;
393 Byte_t mon, d, h, m, s;
394
395 GetDate(y, mon, d);
396 GetTime(h, m, s, ms);
397
398 return TString(Form("%02d.%02d.%04d %02d:%02d:%02d.%03d", d, mon, y, h, m, s, ms));
399}
400
401// --------------------------------------------------------------------------
402//
403// Return contents as a string format'd with strftime:
404// Here is a short summary of the most important formats. For more
405// information see the man page (or any other description) of
406// strftime...
407//
408// %a The abbreviated weekday name according to the current locale.
409// %A The full weekday name according to the current locale.
410// %b The abbreviated month name according to the current locale.
411// %B The full month name according to the current locale.
412// %c The preferred date and time representation for the current locale.
413// %d The day of the month as a decimal number (range 01 to 31).
414// %e Like %d, the day of the month as a decimal number,
415// but a leading zero is replaced by a space.
416// %H The hour as a decimal number using a 24-hour clock (range 00 to 23)
417// %k The hour (24-hour clock) as a decimal number (range 0 to 23);
418// single digits are preceded by a blank.
419// %m The month as a decimal number (range 01 to 12).
420// %M The minute as a decimal number (range 00 to 59).
421// %R The time in 24-hour notation (%H:%M). For a
422// version including the seconds, see %T below.
423// %S The second as a decimal number (range 00 to 61).
424// %T The time in 24-hour notation (%H:%M:%S).
425// %x The preferred date representation for the current
426// locale without the time.
427// %X The preferred time representation for the current
428// locale without the date.
429// %y The year as a decimal number without a century (range 00 to 99).
430// %Y The year as a decimal number including the century.
431// %+ The date and time in date(1) format.
432//
433// The default is: Tuesday 16.February 2004 12:17:22
434//
435// The maximum size of the return string is 128 (incl. NULL)
436//
437TString MTime::GetStringFmt(const char *fmt) const
438{
439 if (!fmt)
440 fmt = "%A %e.%B %Y %H:%M:%S";
441
442 UShort_t y, ms;
443 Byte_t mon, d, h, m, s;
444
445 GetDate(y, mon, d);
446 GetTime(h, m, s, ms);
447
448 struct tm time;
449 time.tm_sec = s;
450 time.tm_min = m;
451 time.tm_hour = h;
452 time.tm_mday = d;
453 time.tm_mon = mon-1;
454 time.tm_year = y-1900;
455 time.tm_isdst = 0;
456
457 // recalculate tm_yday and tm_wday
458 mktime(&time);
459
460 char ret[128];
461 return TString(strftime(ret, 127, fmt, &time) ? ret : "");
462}
463
464// --------------------------------------------------------------------------
465//
466// Return contents as a TString of the form:
467// "yyyy-mm-dd hh:mm:ss"
468//
469TString MTime::GetSqlDateTime() const
470{
471 return GetStringFmt("%Y-%m-%d %H:%M:%S");
472}
473
474// --------------------------------------------------------------------------
475//
476// Return contents as a TString of the form:
477// "yyyymmddhhmmss"
478//
479TString MTime::GetSqlTimeStamp() const
480{
481 return GetStringFmt("%Y%m%d%H%M%S");
482}
483
484// --------------------------------------------------------------------------
485//
486// Return contents as a TString of the form:
487// "yyyymmdd_hhmmss"
488//
489TString MTime::GetFileName() const
490{
491 return GetStringFmt("%Y%m%d_%H%M%S");
492}
493
494// --------------------------------------------------------------------------
495//
496// Print MTime as string
497//
498void MTime::Print(Option_t *) const
499{
500 UShort_t yea, ms;
501 Byte_t mon, day, h, m, s;
502
503 GetDate(yea, mon, day);
504 GetTime(h, m, s, ms);
505
506 *fLog << GetDescriptor() << ": ";
507 *fLog << GetString() << Form(" (+%dns)", fNanoSec) << endl;
508}
509
510istream &MTime::ReadBinary(istream &fin)
511{
512 UShort_t y;
513 Byte_t mon, d, h, m, s;
514
515 fin.read((char*)&y, 2);
516 fin.read((char*)&mon, 1);
517 fin.read((char*)&d, 1);
518 fin.read((char*)&h, 1);
519 fin.read((char*)&m, 1);
520 fin.read((char*)&s, 1); // Total=7
521
522 Set(y, mon, d, h, m, s, 0);
523
524 return fin;
525}
Note: See TracBrowser for help on using the repository browser.