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

Last change on this file since 7455 was 7450, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 25.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//
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// GetMoonPhase - calculate phase of moon as a fraction:
130// Returns -1 if calculation failed
131//
132// see MAstro::GetMoonPhase
133//
134Double_t MTime::GetMoonPhase() const
135{
136 return MAstro::GetMoonPhase(GetMjd());
137}
138
139// --------------------------------------------------------------------------
140//
141// Calculate the Period to which the time belongs to. The Period is defined
142// as the number of synodic months ellapsed since the first full moon
143// after Jan 1st 1980 (which was @ MJD=44240.37917)
144//
145// see MAstro::GetMoonPeriod
146//
147Double_t MTime::GetMoonPeriod() const
148{
149 return MAstro::GetMoonPeriod(GetMjd());
150}
151
152// --------------------------------------------------------------------------
153//
154// To get the moon period as defined for MAGIC observation we take the
155// nearest integer mjd, eg:
156// 53257.8 --> 53258
157// 53258.3 --> 53258
158// Which is the time between 13h and 12:59h of the following day. To
159// this day-period we assign the moon-period at midnight. To get
160// the MAGIC definition we now substract 284.
161//
162// For MAGIC observation period do eg:
163// GetMagicPeriod(53257.91042)
164// or
165// MTime t;
166// t.SetMjd(53257.91042);
167// GetMagicPeriod(t.GetMjd());
168// or
169// MTime t;
170// t.Set(2004, 1, 1, 12, 32, 11);
171// GetMagicPeriod(t.GetMjd());
172//
173//
174// see MAstro::GetMagicPeriod
175//
176Int_t MTime::GetMagicPeriod() const
177{
178 return MAstro::GetMagicPeriod(GetMjd());
179}
180
181
182// --------------------------------------------------------------------------
183//
184// Return the time in the range [0h, 24h) = [0h0m0.000s - 23h59m59.999s]
185//
186void MTime::GetTime(Byte_t &h, Byte_t &m, Byte_t &s, UShort_t &ms) const
187{
188 Long_t tm = GetTime24();
189 ms = tm%1000; // [ms]
190 tm /= 1000; // [s]
191 s = tm%60; // [s]
192 tm /= 60; // [m]
193 m = tm%60; // [m]
194 tm /= 60; // [h]
195 h = tm; // [h]
196}
197
198// --------------------------------------------------------------------------
199//
200// Return time as MJD (=JD-24000000.5)
201//
202Double_t MTime::GetMjd() const
203{
204 return fMjd+(Double_t)(fNanoSec/1e6+(Long_t)fTime)/kDay;
205}
206
207// --------------------------------------------------------------------------
208//
209// Return a time which is expressed in milliseconds since 01/01/1995 0:00h
210// This is compatible with root's definition used in gSystem->Now()
211// and TTime.
212// Note, gSystem->Now() returns local time, such that it may differ
213// from GetRootTime() (if you previously called MTime::Now())
214//
215TTime MTime::GetRootTime() const
216{
217 return (ULong_t)((GetMjd()-49718)*kDay);
218}
219
220// --------------------------------------------------------------------------
221//
222// Return a time which is expressed in seconds since 01/01/1995 0:00h
223// This is compatible with root's definition used in TAxis.
224// Note, a TAxis always displayes (automatically) given times in
225// local time (while here we return UTC) such, that you may encounter
226// strange offsets. You can get rid of this by calling:
227// TAxis::SetTimeFormat("[your-format] %F1995-01-01 00:00:00 GMT");
228//
229Double_t MTime::GetAxisTime() const
230{
231 return (GetMjd()-49718)*kDay/1000;
232}
233
234// --------------------------------------------------------------------------
235//
236// Counterpart of GetAxisTime
237//
238void MTime::SetAxisTime(Double_t time)
239{
240 SetMjd(time*1000/kDay+49718);
241}
242
243// --------------------------------------------------------------------------
244//
245// Set this to the date of easter corresponding to the given year.
246// If calculation was not possible it is set to MTime()
247//
248// The date corresponding to the year of MTime(-1) is returned
249// if year<0
250//
251// The date corresponding to the Year() is returned if year==0.
252//
253// for more information see: GetEaster and MAstro::GetEasterOffset()
254//
255void MTime::SetEaster(Short_t year)
256{
257 *this = GetEaster(year==0 ? Year() : year);
258}
259
260// --------------------------------------------------------------------------
261//
262// Set a time expressed in MJD, Time of Day (eg. 23:12.779h expressed
263// in milliseconds) and a nanosecond part.
264//
265Bool_t MTime::SetMjd(UInt_t mjd, ULong_t ms, UInt_t ns)
266{
267 // [d] mjd (eg. 52320)
268 // [ms] time (eg. 17h expressed in ms)
269 // [ns] time (ns part of time)
270
271 if (ms>kDay-1 || ns>999999)
272 return kFALSE;
273
274 const Bool_t am = ms<kHour*13; // day of sunrise?
275
276 fMjd = am ? mjd : mjd + 1;
277 fTime = (Long_t)(am ? ms : ms-kDay);
278 fNanoSec = ns;
279
280 return kTRUE;
281}
282
283// --------------------------------------------------------------------------
284//
285// Set MTime to given MJD (eg. 52080.0915449892)
286//
287void MTime::SetMjd(Double_t m)
288{
289 const UInt_t mjd = (UInt_t)TMath::Floor(m);
290 const Double_t frac = fmod(m, 1)*kDay; // [ms] Fraction of day
291 const UInt_t ns = (UInt_t)fmod(frac*1e6, 1000000);
292
293 SetMjd(mjd, (ULong_t)TMath::Floor(frac), ns);
294}
295
296// --------------------------------------------------------------------------
297//
298// Set MTime to given time and date
299//
300Bool_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)
301{
302 if (h>23 || min>59 || s>59 || ms>999 || ns>999999)
303 return kFALSE;
304
305 const Int_t mjd = MAstro::Ymd2Mjd(y, m, d);
306 if (mjd<0)
307 return kFALSE;
308
309 const ULong_t tm = ((((h*60+min)*60)+s)*1000)+ms;
310
311 return SetMjd(mjd, tm, ns);
312}
313
314// --------------------------------------------------------------------------
315//
316// Set MTime to time expressed in a 'struct timeval'
317//
318void MTime::Set(const struct timeval &tv)
319{
320 const UInt_t mjd = (UInt_t)TMath::Floor(1000.*tv.tv_sec/kDay) + 40587;
321
322 const Long_t tm = tv.tv_sec%(24*3600)*1000 + tv.tv_usec/1000;
323 const UInt_t ms = tv.tv_usec%1000;
324
325 SetMjd(mjd, tm, ms*1000);
326}
327
328// --------------------------------------------------------------------------
329//
330// Return contents as a TString of the form:
331// "dd.mm.yyyy hh:mm:ss.fff"
332//
333Bool_t MTime::SetString(const char *str)
334{
335 if (!str)
336 return kFALSE;
337
338 UInt_t y, mon, d, h, m, s, ms;
339 const Int_t n = sscanf(str, "%02u.%02u.%04u %02u:%02u:%02u.%03u",
340 &d, &mon, &y, &h, &m, &s, &ms);
341
342 return n==7 ? Set(y, mon, d, h, m, s, ms) : kFALSE;
343}
344
345// --------------------------------------------------------------------------
346//
347// Return contents as a TString of the form:
348// "yyyy-mm-dd hh:mm:ss"
349//
350Bool_t MTime::SetSqlDateTime(const char *str)
351{
352 if (!str)
353 return kFALSE;
354
355 UInt_t y, mon, d, h, m, s;
356 const Int_t n = sscanf(str, "%04u-%02u-%02u %02u:%02u:%02u",
357 &y, &mon, &d, &h, &m, &s);
358
359 return n==6 ? Set(y, mon, d, h, m, s) : kFALSE;
360}
361
362// --------------------------------------------------------------------------
363//
364// Return contents as a TString of the form:
365// "yyyymmddhhmmss"
366//
367Bool_t MTime::SetSqlTimeStamp(const char *str)
368{
369 if (!str)
370 return kFALSE;
371
372 UInt_t y, mon, d, h, m, s;
373 const Int_t n = sscanf(str, "%04u%02u%02u%02u%02u%02u",
374 &y, &mon, &d, &h, &m, &s);
375
376 return n==6 ? Set(y, mon, d, h, m, s) : kFALSE;
377}
378
379// --------------------------------------------------------------------------
380//
381// Set MTime to time expressed as in CT1 PreProc files
382//
383void MTime::SetCT1Time(UInt_t mjd, UInt_t t1, UInt_t t0)
384{
385 // int isecs_since_midday; // seconds passed since midday before sunset (JD of run start)
386 // int isecfrac_200ns; // fractional part of isecs_since_midday
387 // fTime->SetTime(isecfrac_200ns, isecs_since_midday);
388 fNanoSec = (200*t1)%1000000;
389 const ULong_t ms = (200*t1)/1000000 + t0+12*kHour;
390
391 fTime = (Long_t)(ms<13*kHour ? ms : ms-kDay);
392
393 fMjd = mjd+1;
394}
395
396// --------------------------------------------------------------------------
397//
398// Update the magic time. Make sure, that the MJD is set correctly.
399// It must be the MJD of the corresponding night. You can set it
400// by Set(2003, 12, 24);
401//
402// It is highly important, that the time correspoding to the night is
403// between 13:00:00.0 (day of dawning) and 12:59:59.999 (day of sunrise)
404//
405Bool_t MTime::UpdMagicTime(Byte_t h, Byte_t m, Byte_t s, UInt_t ns)
406{
407 if (h>23 || m>59 || s>59 || ns>999999999)
408 return kFALSE;
409
410 const ULong_t tm = ((((h*60+m)*60)+s)*1000)+ns/1000000;
411
412 fTime = (Long_t)(tm<kHour*13 ? tm : tm-kDay); // day of sunrise?
413 fNanoSec = ns%1000000;
414
415 return kTRUE;
416}
417
418// --------------------------------------------------------------------------
419//
420// Conversion from Universal Time to Greenwich mean sidereal time,
421// with rounding errors minimized.
422//
423// The result is the Greenwich Mean Sidereal Time (radians)
424//
425// There is no restriction on how the UT is apportioned between the
426// date and ut1 arguments. Either of the two arguments could, for
427// example, be zero and the entire date+time supplied in the other.
428// However, the routine is designed to deliver maximum accuracy when
429// the date argument is a whole number and the ut argument lies in
430// the range 0 to 1, or vice versa.
431//
432// The algorithm is based on the IAU 1982 expression (see page S15 of
433// the 1984 Astronomical Almanac). This is always described as giving
434// the GMST at 0 hours UT1. In fact, it gives the difference between
435// the GMST and the UT, the steady 4-minutes-per-day drawing-ahead of
436// ST with respect to UT. When whole days are ignored, the expression
437// happens to equal the GMST at 0 hours UT1 each day.
438//
439// In this routine, the entire UT1 (the sum of the two arguments date
440// and ut) is used directly as the argument for the standard formula.
441// The UT1 is then added, but omitting whole days to conserve accuracy.
442//
443// The extra numerical precision delivered by the present routine is
444// unlikely to be important in an absolute sense, but may be useful
445// when critically comparing algorithms and in applications where two
446// sidereal times close together are differenced.
447//
448Double_t MTime::GetGmst() const
449{
450 const Double_t ut = (Double_t)(fNanoSec/1e6+(Long_t)fTime)/kDay;
451
452 // Julian centuries since J2000.
453 const Double_t t = (ut -(51544.5-fMjd)) / 36525.0;
454
455 // GMST at this UT1
456 const Double_t r1 = 24110.54841+(8640184.812866+(0.093104-6.2e-6*t)*t)*t;
457 const Double_t r2 = 86400.0*ut;
458
459 const Double_t sum = (r1+r2)/(24*3600);
460
461 return fmod(sum, 1)*TMath::TwoPi();//+TMath::TwoPi();
462}
463
464// --------------------------------------------------------------------------
465//
466// Get the day of the year represented by day, month and year.
467// Valid return values range between 1 and 366, where January 1 = 1.
468//
469UInt_t MTime::DayOfYear() const
470{
471 MTime jan1st;
472 jan1st.Set(Year(), 1, 1);
473
474 const Double_t newyear = TMath::Floor(jan1st.GetMjd());
475 const Double_t mjd = TMath::Floor(GetMjd());
476
477 return TMath::Nint(mjd-newyear)+1;
478}
479
480// --------------------------------------------------------------------------
481//
482// Get the week of the year. Valid week values are between 1 and 53.
483// 54 is returned if the day belongs to Week=1 of the next year,
484// 0 if it belongs to the Week=53 and -1 if it belongs to the Week=52
485// of the last year (In this case there is no week 53!)
486//
487Int_t MTime::Week() const
488{
489 static const Int_t kFrSaSo = BIT(kFriday) | BIT(kSaturday) | BIT(kSunday);
490 static const Int_t kTuWeTh = BIT(kTuesday) | BIT(kWednesday) | BIT(kThursday);
491
492 MTime t;
493 t.Set(Year(), 1, 1);
494
495 // The weekday of Jan 1st.
496 const Int_t day1st = 1<<t.WeekDay();
497
498 // The number of weeks since the first Monday in the year
499 Int_t week = (DayOfYear()-1 + (t.WeekDay()+6)%7) / 7 + 1;
500
501 // Correct the week number if the year has started with Fr, Sa or So
502 if (day1st & kFrSaSo)
503 week--;
504
505 // If the week number is 53 it might already belong to the next year
506 if (week == 53)
507 {
508 t.Set(Year()+1, 1, 1);
509
510 const Int_t daynext = 1<<t.WeekDay();
511 return daynext&kTuWeTh ? 54 : 53;
512 }
513
514 // If the week number is 53 it might still belong to the last year
515 if (week == 0)
516 {
517 t.Set(Year()-1, 1, 1);
518
519 const Int_t dayprev = 1<<t.WeekDay();
520 return !(dayprev&kFrSaSo) && (day1st&kFrSaSo) ? 0 : -1;
521 }
522
523 return week;
524}
525
526// --------------------------------------------------------------------------
527//
528// Is the given year a leap year.
529// The calendar year is 365 days long, unless the year is exactly divisible
530// by 4, in which case an extra day is added to February to make the year
531// 366 days long. If the year is the last year of a century, eg. 1700, 1800,
532// 1900, 2000, then it is only a leap year if it is exactly divisible by
533// 400. Therefore, 1900 wasn't a leap year but 2000 was. The reason for
534// these rules is to bring the average length of the calendar year into
535// line with the length of the Earth's orbit around the Sun, so that the
536// seasons always occur during the same months each year.
537//
538Bool_t MTime::IsLeapYear() const
539{
540 const UInt_t y = Year();
541 return (y%4==0) && !((y%100==0) && (y%400>0));
542}
543
544// --------------------------------------------------------------------------
545//
546// Set the time to the current system time. The timezone is ignored.
547// If everything is set correctly you'll get UTC.
548//
549void MTime::Now()
550{
551#ifdef __LINUX__
552 struct timeval tv;
553 if (gettimeofday(&tv, NULL)<0)
554 Clear();
555 else
556 Set(tv);
557#else
558 Clear();
559#endif
560}
561
562// --------------------------------------------------------------------------
563//
564// Return contents as a TString of the form:
565// "dd.mm.yyyy hh:mm:ss.fff"
566//
567TString MTime::GetString() const
568{
569 UShort_t y, ms;
570 Byte_t mon, d, h, m, s;
571
572 GetDate(y, mon, d);
573 GetTime(h, m, s, ms);
574
575 return TString(Form("%02d.%02d.%04d %02d:%02d:%02d.%03d", d, mon, y, h, m, s, ms));
576}
577
578// --------------------------------------------------------------------------
579//
580// Return contents as a string format'd with strftime:
581// Here is a short summary of the most important formats. For more
582// information see the man page (or any other description) of
583// strftime...
584//
585// %a The abbreviated weekday name according to the current locale.
586// %A The full weekday name according to the current locale.
587// %b The abbreviated month name according to the current locale.
588// %B The full month name according to the current locale.
589// %c The preferred date and time representation for the current locale.
590// %d The day of the month as a decimal number (range 01 to 31).
591// %e Like %d, the day of the month as a decimal number,
592// but a leading zero is replaced by a space.
593// %H The hour as a decimal number using a 24-hour clock (range 00 to 23)
594// %k The hour (24-hour clock) as a decimal number (range 0 to 23);
595// single digits are preceded by a blank.
596// %m The month as a decimal number (range 01 to 12).
597// %M The minute as a decimal number (range 00 to 59).
598// %R The time in 24-hour notation (%H:%M). For a
599// version including the seconds, see %T below.
600// %S The second as a decimal number (range 00 to 61).
601// %T The time in 24-hour notation (%H:%M:%S).
602// %x The preferred date representation for the current
603// locale without the time.
604// %X The preferred time representation for the current
605// locale without the date.
606// %y The year as a decimal number without a century (range 00 to 99).
607// %Y The year as a decimal number including the century.
608// %+ The date and time in date(1) format.
609//
610// The default is: Tuesday 16.February 2004 12:17:22
611//
612// The maximum size of the return string is 128 (incl. NULL)
613//
614// For dates before 1. 1.1902 a null string is returned
615// For dates after 31.12.2037 a null string is returned
616//
617// To change the localization use loc, eg loc = "da_DK", "de_DE".
618// Leaving the argument empty will just take the default localization.
619//
620// If loc is "", each part of the locale that should be modified is set
621// according to the environment variables. The details are implementation
622// dependent. For glibc, first (regardless of category), the environment
623// variable LC_ALL is inspected, next the environment variable with the
624// same name as the category (LC_COLLATE, LC_CTYPE, LC_MESSAGES, LC_MONE?
625// TARY, LC_NUMERIC, LC_TIME) and finally the environment variable LANG.
626// The first existing environment variable is used.
627//
628// A locale name is typically of the form language[_territory][.code?
629// set][@modifier], where language is an ISO 639 language code, territory
630// is an ISO 3166 country code, and codeset is a character set or encoding
631// identifier like ISO-8859-1 or UTF-8. For a list of all supported
632// locales, try "locale -a", cf. locale(1).
633//
634TString MTime::GetStringFmt(const char *fmt, const char *loc) const
635{
636 if (!fmt)
637 fmt = "%A %e.%B %Y %H:%M:%S";
638
639 UShort_t y, ms;
640 Byte_t mon, d, h, m, s;
641
642 GetDate(y, mon, d);
643 GetTime(h, m, s, ms);
644
645 if (y<1902 || y>2037)
646 return "";
647
648 struct tm time;
649 time.tm_sec = s;
650 time.tm_min = m;
651 time.tm_hour = h;
652 time.tm_mday = d;
653 time.tm_mon = mon-1;
654 time.tm_year = y-1900;
655 time.tm_isdst = 0;
656
657 const TString locale = setlocale(LC_TIME, 0);
658
659 setlocale(LC_TIME, loc);
660
661 // recalculate tm_yday and tm_wday
662 mktime(&time);
663
664 char ret[128];
665 const size_t rc = strftime(ret, 127, fmt, &time);
666
667 setlocale(LC_TIME, locale);
668
669 return rc ? ret : "";
670}
671
672// --------------------------------------------------------------------------
673//
674// Set the time according to the format fmt.
675// Default is "%A %e.%B %Y %H:%M:%S"
676//
677// For more information see GetStringFmt
678//
679Bool_t MTime::SetStringFmt(const char *time, const char *fmt, const char *loc)
680{
681 if (!fmt)
682 fmt = "%A %e.%B %Y %H:%M:%S";
683
684 struct tm t;
685 memset(&t, 0, sizeof(struct tm));
686
687 const TString locale = setlocale(LC_TIME, 0);
688
689 setlocale(LC_TIME, loc);
690 strptime(time, fmt, &t);
691 setlocale(LC_TIME, locale);
692
693 return Set(t.tm_year+1900, t.tm_mon+1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
694}
695
696// --------------------------------------------------------------------------
697//
698// Return contents as a TString of the form:
699// "yyyy-mm-dd hh:mm:ss"
700//
701TString MTime::GetSqlDateTime() const
702{
703 return GetStringFmt("%Y-%m-%d %H:%M:%S");
704}
705
706// --------------------------------------------------------------------------
707//
708// Return contents as a TString of the form:
709// "yyyymmddhhmmss"
710//
711TString MTime::GetSqlTimeStamp() const
712{
713 return GetStringFmt("%Y%m%d%H%M%S");
714}
715
716// --------------------------------------------------------------------------
717//
718// Return contents as a TString of the form:
719// "yyyymmdd_hhmmss"
720//
721TString MTime::GetFileName() const
722{
723 return GetStringFmt("%Y%m%d_%H%M%S");
724}
725
726// --------------------------------------------------------------------------
727//
728// Print MTime as string
729//
730void MTime::Print(Option_t *) const
731{
732 UShort_t yea, ms;
733 Byte_t mon, day, h, m, s;
734
735 GetDate(yea, mon, day);
736 GetTime(h, m, s, ms);
737
738 *fLog << all << GetDescriptor() << ": ";
739 *fLog << GetString() << Form(" (+%dns)", fNanoSec) << endl;
740}
741
742istream &MTime::ReadBinary(istream &fin)
743{
744 UShort_t y;
745 Byte_t mon, d, h, m, s;
746
747 fin.read((char*)&y, 2);
748 fin.read((char*)&mon, 1);
749 fin.read((char*)&d, 1);
750 fin.read((char*)&h, 1);
751 fin.read((char*)&m, 1);
752 fin.read((char*)&s, 1); // Total=7
753
754 Set(y, mon, d, h, m, s, 0);
755
756 return fin;
757}
758
759void MTime::AddMilliSeconds(UInt_t ms)
760{
761 fTime += ms;
762
763 fTime += 11*kHour;
764 fMjd += (Long_t)fTime/kDay;
765 fTime = (Long_t)fTime%kDay;
766 fTime -= 11*kHour;
767}
768
769void MTime::Plus1ns()
770{
771 fNanoSec++;
772
773 if (fNanoSec<1000000)
774 return;
775
776 fNanoSec = 0;
777 fTime += 1;
778
779 if ((Long_t)fTime<(Long_t)kDay*13)
780 return;
781
782 fTime = 11*kDay;
783 fMjd++;
784}
785
786void MTime::Minus1ns()
787{
788 if (fNanoSec>0)
789 {
790 fNanoSec--;
791 return;
792 }
793
794 fTime -= 1;
795 fNanoSec = 999999;
796
797 if ((Long_t)fTime>=-(Long_t)kDay*11)
798 return;
799
800 fTime = 13*kDay-1;
801 fMjd--;
802}
803
804/*
805MTime MTime::operator-(const MTime &tm1)
806{
807 const MTime &tm0 = *this;
808
809 MTime t0 = tm0>tm1 ? tm0 : tm1;
810 const MTime &t1 = tm0>tm1 ? tm1 : tm0;
811
812 if (t0.fNanoSec<t1.fNanoSec)
813 {
814 t0.fNanoSec += 1000000;
815 t0.fTime -= 1;
816 }
817
818 t0.fNanoSec -= t1.fNanoSec;
819 t0.fTime -= t1.fTime;
820
821 if ((Long_t)t0.fTime<-(Long_t)kHour*11)
822 {
823 t0.fTime += kDay;
824 t0.fMjd--;
825 }
826
827 t0.fMjd -= t1.fMjd;
828
829 return t0;
830}
831
832void MTime::operator-=(const MTime &t)
833{
834 *this = *this-t;
835}
836
837MTime MTime::operator+(const MTime &t1)
838{
839 MTime t0 = *this;
840
841 t0.fNanoSec += t1.fNanoSec;
842
843 if (t0.fNanoSec>999999)
844 {
845 t0.fNanoSec -= 1000000;
846 t0.fTime += kDay;
847 }
848
849 t0.fTime += t1.fTime;
850
851 if ((Long_t)t0.fTime>=(Long_t)kHour*13)
852 {
853 t0.fTime -= kDay;
854 t0.fMjd++;
855 }
856
857 t0.fMjd += t1.fMjd;
858
859 return t0;
860}
861
862void MTime::operator+=(const MTime &t)
863{
864 *this = *this+t;
865}
866*/
867
868void MTime::SetMean(const MTime &t0, const MTime &t1)
869{
870 // This could be an operator+
871 *this = t0;
872
873 fNanoSec += t1.fNanoSec;
874
875 if (fNanoSec>999999)
876 {
877 fNanoSec -= 1000000;
878 fTime += kDay;
879 }
880
881 fTime += t1.fTime;
882
883 if ((Long_t)fTime>=(Long_t)kHour*13)
884 {
885 fTime -= kDay;
886 fMjd++;
887 }
888
889 fMjd += t1.fMjd;
890
891 // This could be an operator/
892 if ((Long_t)fTime<0)
893 {
894 fTime += kDay;
895 fMjd--;
896 }
897
898 Int_t reminder = fMjd%2;
899 fMjd /= 2;
900
901 fTime += reminder*kDay;
902 reminder = (Long_t)fTime%2;
903 fTime /= 2;
904
905 fNanoSec += reminder*1000000;
906 fNanoSec /= 2;
907
908 fTime += 11*kHour;
909 fMjd += (Long_t)fTime/kDay;
910 fTime = (Long_t)fTime%kDay;
911 fTime -= 11*kHour;
912}
913
914void MTime::SetMean(Double_t t0, Double_t t1)
915{
916 const Double_t mean = (t0+t1)*(500./kDay);
917 SetMjd(mean);
918}
919
920void MTime::AsciiRead(istream &fin)
921{
922 fin >> *this;
923}
924
925Bool_t MTime::AsciiWrite(ostream &out) const
926{
927 out << *this;
928 return out;
929}
930
931// --------------------------------------------------------------------------
932//
933// Calculate the day of easter for the given year.
934// MTime() is returned if this was not possible.
935//
936// In case of the default argument or the year less than zero
937// the date of eastern of the current year (the year corresponding to
938// MTime(-1)) is returned.
939//
940// for more information see: MAstro::GetDayOfEaster()
941//
942MTime MTime::GetEaster(Short_t year)
943{
944 if (year<0)
945 year = MTime(-1).Year();
946
947 const Int_t day = MAstro::GetEasterOffset(year);
948 if (day<0)
949 return MTime();
950
951 MTime t;
952 t.Set(year, 3, 1);
953 t.SetMjd(t.GetMjd() + day);
954
955 return t;
956}
Note: See TracBrowser for help on using the repository browser.