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