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

Last change on this file since 7458 was 7458, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 26.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// Return Mjd of the first day (a monday) which belongs to week 1 of
483// the year give as argument. The returned Mjd might be a date in the
484// year before.
485//
486// see also MTime::Week()
487//
488Int_t MTime::GetMjdWeek1(Short_t year)
489{
490 MTime t;
491 t.Set(year, 1, 4);
492
493 return (Int_t)t.GetMjd() + t.WeekDay() - 6;
494}
495
496// --------------------------------------------------------------------------
497//
498// Get the week of the year. Valid week values are between 1 and 53.
499// If for a january date a week number above 50 is returned the
500// week belongs to the previous year. If for a december data 1 is
501// returned the week already belongs to the next year.
502//
503// The year to which the week belongs is returned in year.
504//
505// Die Kalenderwochen werden für Jahre ab 1976 berechnet, da mit
506// Geltung vom 1. Januar 1976 der Wochenbeginn auf Montag festgelegt
507// wurde. Die erste Woche ist definiert als die Woche, in der
508// mindestens 4 der ersten 7 Januartage fallen (also die Woche, in der
509// der 4. Januar liegt). Beides wurde damals festgelegt in der DIN 1355
510// (1974). Inhaltlich gleich regelt das die Internationale Norm
511// ISO 8601 (1988), die von der Europäischen Union als EN 28601 (1992)
512// übernommen und in Deutschland als DIN EN 28601 (1993) umgesetzt
513// wurde.
514//
515Int_t MTime::Week(Short_t &year) const
516{
517 // Possibilities for Week 1:
518 //
519 // Mo 4.Jan: Mo 4. - So 10. -0 6-6
520 // Di 4.Jan: Mo 3. - So 9. -1 6-5
521 // Mi 4.Jan: Mo 2. - So 8. -2 6-4
522 // Do 4.Jan: Mo 1. - So 7. -3 6-3
523 // Fr 4.Jan: Mo 31. - So 6. -4 6-2
524 // Sa 4.Jan: Mo 30. - So 5. -5 6-1
525 // So 4.Jan: Mo 29. - So 4. -6 6-0
526 //
527 const Int_t mjd2 = GetMjdWeek1(Year()-1);
528 const Int_t mjd0 = GetMjdWeek1(Year());
529 const Int_t mjd3 = GetMjdWeek1(Year()+1);
530
531 // Today
532 const Int_t mjd = (Int_t)GetMjd();
533
534 // Week belongs to last year, return week of last year
535 if (mjd<mjd0)
536 {
537 year = Year()-1;
538 return (mjd-mjd2)/7 + 1;
539 }
540
541 // Check if Week belongs to next year (can only be week 1)
542 if ((mjd3-mjd)/7==1)
543 {
544 year = Year()+1;
545 return 1;
546 }
547
548 // Return calculated Week
549 year = Year();
550 return (mjd-mjd0)/7 + 1;
551}
552
553// --------------------------------------------------------------------------
554//
555// Is the given year a leap year.
556// The calendar year is 365 days long, unless the year is exactly divisible
557// by 4, in which case an extra day is added to February to make the year
558// 366 days long. If the year is the last year of a century, eg. 1700, 1800,
559// 1900, 2000, then it is only a leap year if it is exactly divisible by
560// 400. Therefore, 1900 wasn't a leap year but 2000 was. The reason for
561// these rules is to bring the average length of the calendar year into
562// line with the length of the Earth's orbit around the Sun, so that the
563// seasons always occur during the same months each year.
564//
565Bool_t MTime::IsLeapYear() const
566{
567 const UInt_t y = Year();
568 return (y%4==0) && !((y%100==0) && (y%400>0));
569}
570
571// --------------------------------------------------------------------------
572//
573// Set the time to the current system time. The timezone is ignored.
574// If everything is set correctly you'll get UTC.
575//
576void MTime::Now()
577{
578#ifdef __LINUX__
579 struct timeval tv;
580 if (gettimeofday(&tv, NULL)<0)
581 Clear();
582 else
583 Set(tv);
584#else
585 Clear();
586#endif
587}
588
589// --------------------------------------------------------------------------
590//
591// Return contents as a TString of the form:
592// "dd.mm.yyyy hh:mm:ss.fff"
593//
594TString MTime::GetString() const
595{
596 UShort_t y, ms;
597 Byte_t mon, d, h, m, s;
598
599 GetDate(y, mon, d);
600 GetTime(h, m, s, ms);
601
602 return TString(Form("%02d.%02d.%04d %02d:%02d:%02d.%03d", d, mon, y, h, m, s, ms));
603}
604
605// --------------------------------------------------------------------------
606//
607// Return contents as a string format'd with strftime:
608// Here is a short summary of the most important formats. For more
609// information see the man page (or any other description) of
610// strftime...
611//
612// %a The abbreviated weekday name according to the current locale.
613// %A The full weekday name according to the current locale.
614// %b The abbreviated month name according to the current locale.
615// %B The full month name according to the current locale.
616// %c The preferred date and time representation for the current locale.
617// %d The day of the month as a decimal number (range 01 to 31).
618// %e Like %d, the day of the month as a decimal number,
619// but a leading zero is replaced by a space.
620// %H The hour as a decimal number using a 24-hour clock (range 00 to 23)
621// %k The hour (24-hour clock) as a decimal number (range 0 to 23);
622// single digits are preceded by a blank.
623// %m The month as a decimal number (range 01 to 12).
624// %M The minute as a decimal number (range 00 to 59).
625// %R The time in 24-hour notation (%H:%M). For a
626// version including the seconds, see %T below.
627// %S The second as a decimal number (range 00 to 61).
628// %T The time in 24-hour notation (%H:%M:%S).
629// %x The preferred date representation for the current
630// locale without the time.
631// %X The preferred time representation for the current
632// locale without the date.
633// %y The year as a decimal number without a century (range 00 to 99).
634// %Y The year as a decimal number including the century.
635// %+ The date and time in date(1) format.
636//
637// The default is: Tuesday 16.February 2004 12:17:22
638//
639// The maximum size of the return string is 128 (incl. NULL)
640//
641// For dates before 1. 1.1902 a null string is returned
642// For dates after 31.12.2037 a null string is returned
643//
644// To change the localization use loc, eg loc = "da_DK", "de_DE".
645// Leaving the argument empty will just take the default localization.
646//
647// If loc is "", each part of the locale that should be modified is set
648// according to the environment variables. The details are implementation
649// dependent. For glibc, first (regardless of category), the environment
650// variable LC_ALL is inspected, next the environment variable with the
651// same name as the category (LC_COLLATE, LC_CTYPE, LC_MESSAGES, LC_MONE?
652// TARY, LC_NUMERIC, LC_TIME) and finally the environment variable LANG.
653// The first existing environment variable is used.
654//
655// A locale name is typically of the form language[_territory][.code?
656// set][@modifier], where language is an ISO 639 language code, territory
657// is an ISO 3166 country code, and codeset is a character set or encoding
658// identifier like ISO-8859-1 or UTF-8. For a list of all supported
659// locales, try "locale -a", cf. locale(1).
660//
661TString MTime::GetStringFmt(const char *fmt, const char *loc) const
662{
663 if (!fmt)
664 fmt = "%A %e.%B %Y %H:%M:%S";
665
666 UShort_t y, ms;
667 Byte_t mon, d, h, m, s;
668
669 GetDate(y, mon, d);
670 GetTime(h, m, s, ms);
671
672 if (y<1902 || y>2037)
673 return "";
674
675 struct tm time;
676 time.tm_sec = s;
677 time.tm_min = m;
678 time.tm_hour = h;
679 time.tm_mday = d;
680 time.tm_mon = mon-1;
681 time.tm_year = y-1900;
682 time.tm_isdst = 0;
683
684 const TString locale = setlocale(LC_TIME, 0);
685
686 setlocale(LC_TIME, loc);
687
688 // recalculate tm_yday and tm_wday
689 mktime(&time);
690
691 char ret[128];
692 const size_t rc = strftime(ret, 127, fmt, &time);
693
694 setlocale(LC_TIME, locale);
695
696 return rc ? ret : "";
697}
698
699// --------------------------------------------------------------------------
700//
701// Set the time according to the format fmt.
702// Default is "%A %e.%B %Y %H:%M:%S"
703//
704// For more information see GetStringFmt
705//
706Bool_t MTime::SetStringFmt(const char *time, const char *fmt, const char *loc)
707{
708 if (!fmt)
709 fmt = "%A %e.%B %Y %H:%M:%S";
710
711 struct tm t;
712 memset(&t, 0, sizeof(struct tm));
713
714 const TString locale = setlocale(LC_TIME, 0);
715
716 setlocale(LC_TIME, loc);
717 strptime(time, fmt, &t);
718 setlocale(LC_TIME, locale);
719
720 return Set(t.tm_year+1900, t.tm_mon+1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
721}
722
723// --------------------------------------------------------------------------
724//
725// Return contents as a TString of the form:
726// "yyyy-mm-dd hh:mm:ss"
727//
728TString MTime::GetSqlDateTime() const
729{
730 return GetStringFmt("%Y-%m-%d %H:%M:%S");
731}
732
733// --------------------------------------------------------------------------
734//
735// Return contents as a TString of the form:
736// "yyyymmddhhmmss"
737//
738TString MTime::GetSqlTimeStamp() const
739{
740 return GetStringFmt("%Y%m%d%H%M%S");
741}
742
743// --------------------------------------------------------------------------
744//
745// Return contents as a TString of the form:
746// "yyyymmdd_hhmmss"
747//
748TString MTime::GetFileName() const
749{
750 return GetStringFmt("%Y%m%d_%H%M%S");
751}
752
753// --------------------------------------------------------------------------
754//
755// Print MTime as string
756//
757void MTime::Print(Option_t *) const
758{
759 UShort_t yea, ms;
760 Byte_t mon, day, h, m, s;
761
762 GetDate(yea, mon, day);
763 GetTime(h, m, s, ms);
764
765 *fLog << all << GetDescriptor() << ": ";
766 *fLog << GetString() << Form(" (+%dns)", fNanoSec) << endl;
767}
768
769istream &MTime::ReadBinary(istream &fin)
770{
771 UShort_t y;
772 Byte_t mon, d, h, m, s;
773
774 fin.read((char*)&y, 2);
775 fin.read((char*)&mon, 1);
776 fin.read((char*)&d, 1);
777 fin.read((char*)&h, 1);
778 fin.read((char*)&m, 1);
779 fin.read((char*)&s, 1); // Total=7
780
781 Set(y, mon, d, h, m, s, 0);
782
783 return fin;
784}
785
786void MTime::AddMilliSeconds(UInt_t ms)
787{
788 fTime += ms;
789
790 fTime += 11*kHour;
791 fMjd += (Long_t)fTime/kDay;
792 fTime = (Long_t)fTime%kDay;
793 fTime -= 11*kHour;
794}
795
796void MTime::Plus1ns()
797{
798 fNanoSec++;
799
800 if (fNanoSec<1000000)
801 return;
802
803 fNanoSec = 0;
804 fTime += 1;
805
806 if ((Long_t)fTime<(Long_t)kDay*13)
807 return;
808
809 fTime = 11*kDay;
810 fMjd++;
811}
812
813void MTime::Minus1ns()
814{
815 if (fNanoSec>0)
816 {
817 fNanoSec--;
818 return;
819 }
820
821 fTime -= 1;
822 fNanoSec = 999999;
823
824 if ((Long_t)fTime>=-(Long_t)kDay*11)
825 return;
826
827 fTime = 13*kDay-1;
828 fMjd--;
829}
830
831/*
832MTime MTime::operator-(const MTime &tm1)
833{
834 const MTime &tm0 = *this;
835
836 MTime t0 = tm0>tm1 ? tm0 : tm1;
837 const MTime &t1 = tm0>tm1 ? tm1 : tm0;
838
839 if (t0.fNanoSec<t1.fNanoSec)
840 {
841 t0.fNanoSec += 1000000;
842 t0.fTime -= 1;
843 }
844
845 t0.fNanoSec -= t1.fNanoSec;
846 t0.fTime -= t1.fTime;
847
848 if ((Long_t)t0.fTime<-(Long_t)kHour*11)
849 {
850 t0.fTime += kDay;
851 t0.fMjd--;
852 }
853
854 t0.fMjd -= t1.fMjd;
855
856 return t0;
857}
858
859void MTime::operator-=(const MTime &t)
860{
861 *this = *this-t;
862}
863
864MTime MTime::operator+(const MTime &t1)
865{
866 MTime t0 = *this;
867
868 t0.fNanoSec += t1.fNanoSec;
869
870 if (t0.fNanoSec>999999)
871 {
872 t0.fNanoSec -= 1000000;
873 t0.fTime += kDay;
874 }
875
876 t0.fTime += t1.fTime;
877
878 if ((Long_t)t0.fTime>=(Long_t)kHour*13)
879 {
880 t0.fTime -= kDay;
881 t0.fMjd++;
882 }
883
884 t0.fMjd += t1.fMjd;
885
886 return t0;
887}
888
889void MTime::operator+=(const MTime &t)
890{
891 *this = *this+t;
892}
893*/
894
895void MTime::SetMean(const MTime &t0, const MTime &t1)
896{
897 // This could be an operator+
898 *this = t0;
899
900 fNanoSec += t1.fNanoSec;
901
902 if (fNanoSec>999999)
903 {
904 fNanoSec -= 1000000;
905 fTime += kDay;
906 }
907
908 fTime += t1.fTime;
909
910 if ((Long_t)fTime>=(Long_t)kHour*13)
911 {
912 fTime -= kDay;
913 fMjd++;
914 }
915
916 fMjd += t1.fMjd;
917
918 // This could be an operator/
919 if ((Long_t)fTime<0)
920 {
921 fTime += kDay;
922 fMjd--;
923 }
924
925 Int_t reminder = fMjd%2;
926 fMjd /= 2;
927
928 fTime += reminder*kDay;
929 reminder = (Long_t)fTime%2;
930 fTime /= 2;
931
932 fNanoSec += reminder*1000000;
933 fNanoSec /= 2;
934
935 fTime += 11*kHour;
936 fMjd += (Long_t)fTime/kDay;
937 fTime = (Long_t)fTime%kDay;
938 fTime -= 11*kHour;
939}
940
941void MTime::SetMean(Double_t t0, Double_t t1)
942{
943 const Double_t mean = (t0+t1)*(500./kDay);
944 SetMjd(mean);
945}
946
947void MTime::AsciiRead(istream &fin)
948{
949 fin >> *this;
950}
951
952Bool_t MTime::AsciiWrite(ostream &out) const
953{
954 out << *this;
955 return out;
956}
957
958// --------------------------------------------------------------------------
959//
960// Calculate the day of easter for the given year.
961// MTime() is returned if this was not possible.
962//
963// In case of the default argument or the year less than zero
964// the date of eastern of the current year (the year corresponding to
965// MTime(-1)) is returned.
966//
967// for more information see: MAstro::GetDayOfEaster()
968//
969MTime MTime::GetEaster(Short_t year)
970{
971 if (year<0)
972 year = MTime(-1).Year();
973
974 const Int_t day = MAstro::GetEasterOffset(year);
975 if (day<0)
976 return MTime();
977
978 MTime t;
979 t.Set(year, 3, 1);
980 t.SetMjd(t.GetMjd() + day);
981
982 return t;
983}
Note: See TracBrowser for help on using the repository browser.