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

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