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

Last change on this file since 5972 was 4920, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 19.1 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#ifndef __USE_XOPEN
74#define __USE_XOPEN // on some systems needed for strptime
75#endif
76
77#include <time.h> // struct tm
78#include <sys/time.h> // struct timeval
79
80#include <TTime.h>
81
82#include "MLog.h"
83#include "MLogManip.h"
84
85#include "MAstro.h"
86
87ClassImp(MTime);
88
89using namespace std;
90
91const UInt_t MTime::kHour = 3600000; // [ms] one hour
92const UInt_t MTime::kDay = MTime::kHour*24; // [ms] one day
93
94// --------------------------------------------------------------------------
95//
96// Constructor. Calls SetMjd(d) for d>0 in all other cases the time
97// is set to the current UTC time.
98//
99MTime::MTime(Double_t d)
100{
101 Init(0, 0);
102 if (d<=0)
103 Now();
104 else
105 SetMjd(d);
106}
107
108// --------------------------------------------------------------------------
109//
110// Return date as year(y), month(m), day(d)
111//
112void MTime::GetDate(UShort_t &y, Byte_t &m, Byte_t &d) const
113{
114 MAstro::Mjd2Ymd((Long_t)fTime<0?fMjd-1:fMjd, y, m, d);
115}
116
117// --------------------------------------------------------------------------
118//
119// Return date as year(y), month(m), day(d). If the time is afternoon
120// (>=13:00:00) the date of the next day is returned.
121//
122void MTime::GetDateOfSunrise(UShort_t &y, Byte_t &m, Byte_t &d) const
123{
124 MAstro::Mjd2Ymd(fMjd, y, m, d);
125}
126
127// --------------------------------------------------------------------------
128//
129// Return the time in the range [0h, 24h) = [0h0m0.000s - 23h59m59.999s]
130//
131void MTime::GetTime(Byte_t &h, Byte_t &m, Byte_t &s, UShort_t &ms) const
132{
133 Long_t tm = GetTime24();
134 ms = tm%1000; // [ms]
135 tm /= 1000; // [s]
136 s = tm%60; // [s]
137 tm /= 60; // [m]
138 m = tm%60; // [m]
139 tm /= 60; // [h]
140 h = tm; // [h]
141}
142
143// --------------------------------------------------------------------------
144//
145// Return time as MJD (=JD-24000000.5)
146//
147Double_t MTime::GetMjd() const
148{
149 return fMjd+(Double_t)(fNanoSec/1e6+(Long_t)fTime)/kDay;
150}
151
152// --------------------------------------------------------------------------
153//
154// Return a time which is expressed in milliseconds since 01/01/1995 0:00h
155// This is compatible with root's definition used in gSystem->Now()
156// and TTime.
157// Note, gSystem->Now() returns local time, such that it may differ
158// from GetRootTime() (if you previously called MTime::Now())
159//
160TTime MTime::GetRootTime() const
161{
162 return (ULong_t)((GetMjd()-49718)*kDay);
163}
164
165// --------------------------------------------------------------------------
166//
167// Return a time which is expressed in seconds since 01/01/1995 0:00h
168// This is compatible with root's definition used in TAxis.
169// Note, a TAxis always displayes (automatically) given times in
170// local time (while here we return UTC) such, that you may encounter
171// strange offsets. You can get rid of this by calling:
172// TAxis::SetTimeFormat("[your-format] %F1995-01-01 00:00:00 GMT");
173//
174Double_t MTime::GetAxisTime() const
175{
176 return (GetMjd()-49718)*kDay/1000;
177}
178
179// --------------------------------------------------------------------------
180//
181// Set a time expressed in MJD, Time of Day (eg. 23:12.779h expressed
182// in milliseconds) and a nanosecond part.
183//
184Bool_t MTime::SetMjd(UInt_t mjd, ULong_t ms, UInt_t ns)
185{
186 // [d] mjd (eg. 52320)
187 // [ms] time (eg. 17h expressed in ms)
188 // [ns] time (ns part of time)
189
190 if (ms>kDay-1 || ns>999999)
191 return kFALSE;
192
193 const Bool_t am = ms<kHour*13; // day of sunrise?
194
195 fMjd = am ? mjd : mjd + 1;
196 fTime = (Long_t)(am ? ms : ms-kDay);
197 fNanoSec = ns;
198
199 return kTRUE;
200}
201
202// --------------------------------------------------------------------------
203//
204// Set MTime to given MJD (eg. 52080.0915449892)
205//
206void MTime::SetMjd(Double_t m)
207{
208 const UInt_t mjd = (UInt_t)TMath::Floor(m);
209 const Double_t frac = fmod(m, 1)*kDay; // [ms] Fraction of day
210 const UInt_t ns = (UInt_t)fmod(frac*1e6, 1000000);
211
212 SetMjd(mjd, (ULong_t)TMath::Floor(frac), ns);
213}
214
215// --------------------------------------------------------------------------
216//
217// Set MTime to given time and date
218//
219Bool_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)
220{
221 if (h>23 || min>59 || s>59 || ms>999 || ns>999999)
222 return kFALSE;
223
224 const Int_t mjd = MAstro::Ymd2Mjd(y, m, d);
225 if (mjd<0)
226 return kFALSE;
227
228 const ULong_t tm = ((((h*60+min)*60)+s)*1000)+ms;
229
230 return SetMjd(mjd, tm, ns);
231}
232
233// --------------------------------------------------------------------------
234//
235// Set MTime to time expressed in a 'struct timeval'
236//
237void MTime::Set(const struct timeval &tv)
238{
239 const UInt_t mjd = (UInt_t)TMath::Floor(1000.*tv.tv_sec/kDay) + 40587;
240
241 const Long_t tm = tv.tv_sec%(24*3600)*1000 + tv.tv_usec/1000;
242 const UInt_t ms = tv.tv_usec%1000;
243
244 SetMjd(mjd, tm, ms*1000);
245}
246
247// --------------------------------------------------------------------------
248//
249// Return contents as a TString of the form:
250// "dd.mm.yyyy hh:mm:ss.fff"
251//
252Bool_t MTime::SetString(const char *str)
253{
254 if (!str)
255 return kFALSE;
256
257 UInt_t y, mon, d, h, m, s, ms;
258 const Int_t n = sscanf(str, "%02u.%02u.%04u %02u:%02u:%02u.%03u",
259 &d, &mon, &y, &h, &m, &s, &ms);
260
261 return n==7 ? Set(y, mon, d, h, m, s, ms) : kFALSE;
262}
263
264// --------------------------------------------------------------------------
265//
266// Return contents as a TString of the form:
267// "yyyy-mm-dd hh:mm:ss"
268//
269Bool_t MTime::SetSqlDateTime(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// Return contents as a TString of the form:
284// "yyyymmddhhmmss"
285//
286Bool_t MTime::SetSqlTimeStamp(const char *str)
287{
288 if (!str)
289 return kFALSE;
290
291 UInt_t y, mon, d, h, m, s;
292 const Int_t n = sscanf(str, "%04u%02u%02u%02u%02u%02u",
293 &y, &mon, &d, &h, &m, &s);
294
295 return n==6 ? Set(y, mon, d, h, m, s) : kFALSE;
296}
297
298// --------------------------------------------------------------------------
299//
300// Set MTime to time expressed as in CT1 PreProc files
301//
302void MTime::SetCT1Time(UInt_t mjd, UInt_t t1, UInt_t t0)
303{
304 // int isecs_since_midday; // seconds passed since midday before sunset (JD of run start)
305 // int isecfrac_200ns; // fractional part of isecs_since_midday
306 // fTime->SetTime(isecfrac_200ns, isecs_since_midday);
307 fNanoSec = (200*t1)%1000000;
308 const ULong_t ms = (200*t1)/1000000 + t0+12*kHour;
309
310 fTime = (Long_t)(ms<13*kHour ? ms : ms-kDay);
311
312 fMjd = mjd+1;
313}
314
315// --------------------------------------------------------------------------
316//
317// Update the magic time. Make sure, that the MJD is set correctly.
318// It must be the MJD of the corresponding night. You can set it
319// by Set(2003, 12, 24);
320//
321// It is highly important, that the time correspoding to the night is
322// between 13:00:00.0 (day of dawning) and 12:59:59.999 (day of sunrise)
323//
324Bool_t MTime::UpdMagicTime(Byte_t h, Byte_t m, Byte_t s, UInt_t ns)
325{
326 if (h>23 || m>59 || s>59 || ns>999999999)
327 return kFALSE;
328
329 const ULong_t tm = ((((h*60+m)*60)+s)*1000)+ns/1000000;
330
331 fTime = (Long_t)(tm<kHour*13 ? tm : tm-kDay); // day of sunrise?
332 fNanoSec = ns%1000000;
333
334 return kTRUE;
335}
336
337// --------------------------------------------------------------------------
338//
339// Conversion from Universal Time to Greenwich mean sidereal time,
340// with rounding errors minimized.
341//
342// The result is the Greenwich Mean Sidereal Time (radians)
343//
344// There is no restriction on how the UT is apportioned between the
345// date and ut1 arguments. Either of the two arguments could, for
346// example, be zero and the entire date+time supplied in the other.
347// However, the routine is designed to deliver maximum accuracy when
348// the date argument is a whole number and the ut argument lies in
349// the range 0 to 1, or vice versa.
350//
351// The algorithm is based on the IAU 1982 expression (see page S15 of
352// the 1984 Astronomical Almanac). This is always described as giving
353// the GMST at 0 hours UT1. In fact, it gives the difference between
354// the GMST and the UT, the steady 4-minutes-per-day drawing-ahead of
355// ST with respect to UT. When whole days are ignored, the expression
356// happens to equal the GMST at 0 hours UT1 each day.
357//
358// In this routine, the entire UT1 (the sum of the two arguments date
359// and ut) is used directly as the argument for the standard formula.
360// The UT1 is then added, but omitting whole days to conserve accuracy.
361//
362// The extra numerical precision delivered by the present routine is
363// unlikely to be important in an absolute sense, but may be useful
364// when critically comparing algorithms and in applications where two
365// sidereal times close together are differenced.
366//
367Double_t MTime::GetGmst() const
368{
369 const Double_t ut = (Double_t)(fNanoSec/1e6+(Long_t)fTime)/kDay;
370
371 // Julian centuries since J2000.
372 const Double_t t = (ut -(51544.5-fMjd)) / 36525.0;
373
374 // GMST at this UT1
375 const Double_t r1 = 24110.54841+(8640184.812866+(0.093104-6.2e-6*t)*t)*t;
376 const Double_t r2 = 86400.0*ut;
377
378 const Double_t sum = (r1+r2)/(24*3600);
379
380 return fmod(sum, 1)*TMath::TwoPi();//+TMath::TwoPi();
381}
382
383// --------------------------------------------------------------------------
384//
385// Set the time to the current system time. The timezone is ignored.
386// If everything is set correctly you'll get UTC.
387//
388void MTime::Now()
389{
390#ifdef __LINUX__
391 struct timeval tv;
392 if (gettimeofday(&tv, NULL)<0)
393 Clear();
394 else
395 Set(tv);
396#else
397 Clear();
398#endif
399}
400
401// --------------------------------------------------------------------------
402//
403// Return contents as a TString of the form:
404// "dd.mm.yyyy hh:mm:ss.fff"
405//
406TString MTime::GetString() const
407{
408 UShort_t y, ms;
409 Byte_t mon, d, h, m, s;
410
411 GetDate(y, mon, d);
412 GetTime(h, m, s, ms);
413
414 return TString(Form("%02d.%02d.%04d %02d:%02d:%02d.%03d", d, mon, y, h, m, s, ms));
415}
416
417// --------------------------------------------------------------------------
418//
419// Return contents as a string format'd with strftime:
420// Here is a short summary of the most important formats. For more
421// information see the man page (or any other description) of
422// strftime...
423//
424// %a The abbreviated weekday name according to the current locale.
425// %A The full weekday name according to the current locale.
426// %b The abbreviated month name according to the current locale.
427// %B The full month name according to the current locale.
428// %c The preferred date and time representation for the current locale.
429// %d The day of the month as a decimal number (range 01 to 31).
430// %e Like %d, the day of the month as a decimal number,
431// but a leading zero is replaced by a space.
432// %H The hour as a decimal number using a 24-hour clock (range 00 to 23)
433// %k The hour (24-hour clock) as a decimal number (range 0 to 23);
434// single digits are preceded by a blank.
435// %m The month as a decimal number (range 01 to 12).
436// %M The minute as a decimal number (range 00 to 59).
437// %R The time in 24-hour notation (%H:%M). For a
438// version including the seconds, see %T below.
439// %S The second as a decimal number (range 00 to 61).
440// %T The time in 24-hour notation (%H:%M:%S).
441// %x The preferred date representation for the current
442// locale without the time.
443// %X The preferred time representation for the current
444// locale without the date.
445// %y The year as a decimal number without a century (range 00 to 99).
446// %Y The year as a decimal number including the century.
447// %+ The date and time in date(1) format.
448//
449// The default is: Tuesday 16.February 2004 12:17:22
450//
451// The maximum size of the return string is 128 (incl. NULL)
452//
453TString MTime::GetStringFmt(const char *fmt) const
454{
455 if (!fmt)
456 fmt = "%A %e.%B %Y %H:%M:%S";
457
458 UShort_t y, ms;
459 Byte_t mon, d, h, m, s;
460
461 GetDate(y, mon, d);
462 GetTime(h, m, s, ms);
463
464 struct tm time;
465 time.tm_sec = s;
466 time.tm_min = m;
467 time.tm_hour = h;
468 time.tm_mday = d;
469 time.tm_mon = mon-1;
470 time.tm_year = y-1900;
471 time.tm_isdst = 0;
472
473 // recalculate tm_yday and tm_wday
474 mktime(&time);
475
476 char ret[128];
477 return TString(strftime(ret, 127, fmt, &time) ? ret : "");
478}
479
480// --------------------------------------------------------------------------
481//
482// Set the time according to the format fmt.
483// Default is "%A %e.%B %Y %H:%M:%S"
484//
485// For more information see GetStringFmt
486//
487Bool_t MTime::SetStringFmt(const char *time, const char *fmt)
488{
489 if (!fmt)
490 fmt = "%A %e.%B %Y %H:%M:%S";
491
492 struct tm t;
493 memset(&t, 0, sizeof(struct tm));
494 strptime(time, fmt, &t);
495
496 return Set(t.tm_year+1900, t.tm_mon+1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
497}
498
499// --------------------------------------------------------------------------
500//
501// Return contents as a TString of the form:
502// "yyyy-mm-dd hh:mm:ss"
503//
504TString MTime::GetSqlDateTime() const
505{
506 return GetStringFmt("%Y-%m-%d %H:%M:%S");
507}
508
509// --------------------------------------------------------------------------
510//
511// Return contents as a TString of the form:
512// "yyyymmddhhmmss"
513//
514TString MTime::GetSqlTimeStamp() const
515{
516 return GetStringFmt("%Y%m%d%H%M%S");
517}
518
519// --------------------------------------------------------------------------
520//
521// Return contents as a TString of the form:
522// "yyyymmdd_hhmmss"
523//
524TString MTime::GetFileName() const
525{
526 return GetStringFmt("%Y%m%d_%H%M%S");
527}
528
529// --------------------------------------------------------------------------
530//
531// Print MTime as string
532//
533void MTime::Print(Option_t *) const
534{
535 UShort_t yea, ms;
536 Byte_t mon, day, h, m, s;
537
538 GetDate(yea, mon, day);
539 GetTime(h, m, s, ms);
540
541 *fLog << all << GetDescriptor() << ": ";
542 *fLog << GetString() << Form(" (+%dns)", fNanoSec) << endl;
543}
544
545istream &MTime::ReadBinary(istream &fin)
546{
547 UShort_t y;
548 Byte_t mon, d, h, m, s;
549
550 fin.read((char*)&y, 2);
551 fin.read((char*)&mon, 1);
552 fin.read((char*)&d, 1);
553 fin.read((char*)&h, 1);
554 fin.read((char*)&m, 1);
555 fin.read((char*)&s, 1); // Total=7
556
557 Set(y, mon, d, h, m, s, 0);
558
559 return fin;
560}
561
562void MTime::AddMilliSeconds(UInt_t ms)
563{
564 fTime += ms;
565
566 fTime += 11*kHour;
567 fMjd += (Long_t)fTime/kDay;
568 fTime = (Long_t)fTime%kDay;
569 fTime -= 11*kHour;
570}
571
572void MTime::Plus1ns()
573{
574 fNanoSec++;
575
576 if (fNanoSec<1000000)
577 return;
578
579 fNanoSec = 0;
580 fTime += 1;
581
582 if ((Long_t)fTime<(Long_t)kDay*13)
583 return;
584
585 fTime = 11*kDay;
586 fMjd++;
587}
588
589void MTime::Minus1ns()
590{
591 if (fNanoSec>0)
592 {
593 fNanoSec--;
594 return;
595 }
596
597 fTime -= 1;
598 fNanoSec = 999999;
599
600 if ((Long_t)fTime>=-(Long_t)kDay*11)
601 return;
602
603 fTime = 13*kDay-1;
604 fMjd--;
605}
606
607/*
608MTime MTime::operator-(const MTime &tm1)
609{
610 const MTime &tm0 = *this;
611
612 MTime t0 = tm0>tm1 ? tm0 : tm1;
613 const MTime &t1 = tm0>tm1 ? tm1 : tm0;
614
615 if (t0.fNanoSec<t1.fNanoSec)
616 {
617 t0.fNanoSec += 1000000;
618 t0.fTime -= 1;
619 }
620
621 t0.fNanoSec -= t1.fNanoSec;
622 t0.fTime -= t1.fTime;
623
624 if ((Long_t)t0.fTime<-(Long_t)kHour*11)
625 {
626 t0.fTime += kDay;
627 t0.fMjd--;
628 }
629
630 t0.fMjd -= t1.fMjd;
631
632 return t0;
633}
634
635void MTime::operator-=(const MTime &t)
636{
637 *this = *this-t;
638}
639
640MTime MTime::operator+(const MTime &t1)
641{
642 MTime t0 = *this;
643
644 t0.fNanoSec += t1.fNanoSec;
645
646 if (t0.fNanoSec>999999)
647 {
648 t0.fNanoSec -= 1000000;
649 t0.fTime += kDay;
650 }
651
652 t0.fTime += t1.fTime;
653
654 if ((Long_t)t0.fTime>=(Long_t)kHour*13)
655 {
656 t0.fTime -= kDay;
657 t0.fMjd++;
658 }
659
660 t0.fMjd += t1.fMjd;
661
662 return t0;
663}
664
665void MTime::operator+=(const MTime &t)
666{
667 *this = *this+t;
668}
669*/
670
671void MTime::SetMean(const MTime &t0, const MTime &t1)
672{
673 // This could be an operator+
674 *this = t0;
675
676 fNanoSec += t1.fNanoSec;
677
678 if (fNanoSec>999999)
679 {
680 fNanoSec -= 1000000;
681 fTime += kDay;
682 }
683
684 fTime += t1.fTime;
685
686 if ((Long_t)fTime>=(Long_t)kHour*13)
687 {
688 fTime -= kDay;
689 fMjd++;
690 }
691
692 fMjd += t1.fMjd;
693
694 // This could be an operator/
695 if ((Long_t)fTime<0)
696 {
697 fTime += kDay;
698 fMjd--;
699 }
700
701 Int_t reminder = fMjd%2;
702 fMjd /= 2;
703
704 fTime += reminder*kDay;
705 reminder = (Long_t)fTime%2;
706 fTime /= 2;
707
708 fNanoSec += reminder*1000000;
709 fNanoSec /= 2;
710
711 fTime += 11*kHour;
712 fMjd += (Long_t)fTime/kDay;
713 fTime = (Long_t)fTime%kDay;
714 fTime -= 11*kHour;
715}
716
717void MTime::SetMean(Double_t t0, Double_t t1)
718{
719 const Double_t mean = (t0+t1)*(500./kDay);
720 SetMjd(mean);
721}
722
723void MTime::AsciiRead(istream &fin)
724{
725 fin >> *this;
726}
727
728Bool_t MTime::AsciiWrite(ostream &out) const
729{
730 out << *this;
731 return out;
732}
Note: See TracBrowser for help on using the repository browser.