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

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