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

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