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

Last change on this file since 9148 was 9027, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 29.8 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, ms;
406
407 if (7==sscanf(str, "%04u-%02u-%02u %02u:%02u:%02u.%u", &y, &mon, &d, &h, &m, &s, &ms))
408 return Set(y, mon, d, h, m, s, ms);
409
410 if (6==sscanf(str, "%04u-%02u-%02u %02u:%02u:%02u", &y, &mon, &d, &h, &m, &s))
411 return Set(y, mon, d, h, m, s);
412
413 if (5==sscanf(str, "%04u-%02u-%02u %02u:%02u", &y, &mon, &d, &h, &m))
414 return Set(y, mon, d, h, m);
415
416 if (4==sscanf(str, "%04u-%02u-%02u %02u", &y, &mon, &d, &h))
417 return Set(y, mon, d, h);
418
419 if (3==sscanf(str, "%04u-%02u-%02u", &y, &mon, &d))
420 return Set(y, mon, d);
421
422 return kFALSE;
423}
424
425// --------------------------------------------------------------------------
426//
427// Return contents as a TString of the form:
428// "yyyymmddhhmmss"
429//
430Bool_t MTime::SetSqlTimeStamp(const char *str)
431{
432 if (!str)
433 return kFALSE;
434
435 UInt_t y, mon, d, h, m, s;
436 const Int_t n = sscanf(str, "%04u%02u%02u%02u%02u%02u",
437 &y, &mon, &d, &h, &m, &s);
438
439 return n==6 ? Set(y, mon, d, h, m, s) : kFALSE;
440}
441
442// --------------------------------------------------------------------------
443//
444// Set MTime to time expressed as in CT1 PreProc files
445//
446void MTime::SetCT1Time(UInt_t mjd, UInt_t t1, UInt_t t0)
447{
448 // int isecs_since_midday; // seconds passed since midday before sunset (JD of run start)
449 // int isecfrac_200ns; // fractional part of isecs_since_midday
450 // fTime->SetTime(isecfrac_200ns, isecs_since_midday);
451 fNanoSec = (200*t1)%1000000;
452 const ULong_t ms = (200*t1)/1000000 + t0+12*kHour;
453
454 fTime = (Long_t)(ms<13*kHour ? ms : ms-kDay);
455
456 fMjd = mjd+1;
457}
458
459// --------------------------------------------------------------------------
460//
461// Set MTime to time expressed as float (yymmdd.ffff)
462// for details see MAstro::Yymmdd2Mjd
463//
464void MTime::SetCorsikaTime(Float_t t)
465{
466 const UInt_t yymmdd = (UInt_t)TMath::Floor(t);
467 const UInt_t mjd = MAstro::Yymmdd2Mjd(yymmdd);
468 const Double_t frac = fmod(t, 1)*kDay; // [ms] Fraction of day
469 const UInt_t ns = (UInt_t)fmod(frac*1e6, 1000000);
470
471 SetMjd(mjd, (ULong_t)TMath::Floor(frac), ns);
472}
473
474// --------------------------------------------------------------------------
475//
476// Update the magic time. Make sure, that the MJD is set correctly.
477// It must be the MJD of the corresponding night. You can set it
478// by Set(2003, 12, 24);
479//
480// It is highly important, that the time correspoding to the night is
481// between 13:00:00.0 (day of dawning) and 12:59:59.999 (day of sunrise)
482//
483Bool_t MTime::UpdMagicTime(Byte_t h, Byte_t m, Byte_t s, UInt_t ns)
484{
485 if (h>23 || m>59 || s>59 || ns>999999999)
486 return kFALSE;
487
488 const ULong_t tm = ((((h*60+m)*60)+s)*1000)+ns/1000000;
489
490 fTime = (Long_t)(tm<kHour*13 ? tm : tm-kDay); // day of sunrise?
491 fNanoSec = ns%1000000;
492
493 return kTRUE;
494}
495
496// --------------------------------------------------------------------------
497//
498// Conversion from Universal Time to Greenwich mean sidereal time,
499// with rounding errors minimized.
500//
501// The result is the Greenwich Mean Sidereal Time (radians)
502//
503// There is no restriction on how the UT is apportioned between the
504// date and ut1 arguments. Either of the two arguments could, for
505// example, be zero and the entire date+time supplied in the other.
506// However, the routine is designed to deliver maximum accuracy when
507// the date argument is a whole number and the ut argument lies in
508// the range 0 to 1, or vice versa.
509//
510// The algorithm is based on the IAU 1982 expression (see page S15 of
511// the 1984 Astronomical Almanac). This is always described as giving
512// the GMST at 0 hours UT1. In fact, it gives the difference between
513// the GMST and the UT, the steady 4-minutes-per-day drawing-ahead of
514// ST with respect to UT. When whole days are ignored, the expression
515// happens to equal the GMST at 0 hours UT1 each day.
516//
517// In this routine, the entire UT1 (the sum of the two arguments date
518// and ut) is used directly as the argument for the standard formula.
519// The UT1 is then added, but omitting whole days to conserve accuracy.
520//
521// The extra numerical precision delivered by the present routine is
522// unlikely to be important in an absolute sense, but may be useful
523// when critically comparing algorithms and in applications where two
524// sidereal times close together are differenced.
525//
526Double_t MTime::GetGmst() const
527{
528 const Double_t ut = (Double_t)(fNanoSec/1e6+(Long_t)fTime)/kDay;
529
530 // Julian centuries since J2000.
531 const Double_t t = (ut -(51544.5-fMjd)) / 36525.0;
532
533 // GMST at this UT1
534 const Double_t r1 = 24110.54841+(8640184.812866+(0.093104-6.2e-6*t)*t)*t;
535 const Double_t r2 = 86400.0*ut;
536
537 const Double_t sum = (r1+r2)/kDaySec;
538
539 return fmod(sum, 1)*TMath::TwoPi();//+TMath::TwoPi();
540}
541
542// --------------------------------------------------------------------------
543//
544// Return Day of the week: Sun=0, Mon=1, ..., Sat=6
545//
546Byte_t MTime::WeekDay() const
547{
548 return TMath::FloorNint(GetMjd()+3)%7;
549}
550
551// --------------------------------------------------------------------------
552//
553// Get the day of the year represented by day, month and year.
554// Valid return values range between 1 and 366, where January 1 = 1.
555//
556UInt_t MTime::DayOfYear() const
557{
558 MTime jan1st;
559 jan1st.Set(Year(), 1, 1);
560
561 const Double_t newyear = TMath::Floor(jan1st.GetMjd());
562 const Double_t mjd = TMath::Floor(GetMjd());
563
564 return TMath::Nint(mjd-newyear)+1;
565}
566
567// --------------------------------------------------------------------------
568//
569// Return Mjd of the first day (a monday) which belongs to week 1 of
570// the year give as argument. The returned Mjd might be a date in the
571// year before.
572//
573// see also MTime::Week()
574//
575Int_t MTime::GetMjdWeek1(Short_t year)
576{
577 MTime t;
578 t.Set(year, 1, 4);
579
580 return (Int_t)t.GetMjd() + t.WeekDay() - 6;
581}
582
583// --------------------------------------------------------------------------
584//
585// Get the week of the year. Valid week values are between 1 and 53.
586// If for a january date a week number above 50 is returned the
587// week belongs to the previous year. If for a december data 1 is
588// returned the week already belongs to the next year.
589//
590// The year to which the week belongs is returned in year.
591//
592// Die Kalenderwochen werden für Jahre ab 1976 berechnet, da mit
593// Geltung vom 1. Januar 1976 der Wochenbeginn auf Montag festgelegt
594// wurde. Die erste Woche ist definiert als die Woche, in der
595// mindestens 4 der ersten 7 Januartage fallen (also die Woche, in der
596// der 4. Januar liegt). Beides wurde damals festgelegt in der DIN 1355
597// (1974). Inhaltlich gleich regelt das die Internationale Norm
598// ISO 8601 (1988), die von der Europäischen Union als EN 28601 (1992)
599// übernommen und in Deutschland als DIN EN 28601 (1993) umgesetzt
600// wurde.
601//
602Int_t MTime::Week(Short_t &year) const
603{
604 // Possibilities for Week 1:
605 //
606 // Mo 4.Jan: Mo 4. - So 10. -0 6-6
607 // Di 4.Jan: Mo 3. - So 9. -1 6-5
608 // Mi 4.Jan: Mo 2. - So 8. -2 6-4
609 // Do 4.Jan: Mo 1. - So 7. -3 6-3
610 // Fr 4.Jan: Mo 31. - So 6. -4 6-2
611 // Sa 4.Jan: Mo 30. - So 5. -5 6-1
612 // So 4.Jan: Mo 29. - So 4. -6 6-0
613 //
614 const Int_t mjd2 = GetMjdWeek1(Year()-1);
615 const Int_t mjd0 = GetMjdWeek1(Year());
616 const Int_t mjd3 = GetMjdWeek1(Year()+1);
617
618 // Today
619 const Int_t mjd = (Int_t)GetMjd();
620
621 // Week belongs to last year, return week of last year
622 if (mjd<mjd0)
623 {
624 year = Year()-1;
625 return (mjd-mjd2)/7 + 1;
626 }
627
628 // Check if Week belongs to next year (can only be week 1)
629 if ((mjd3-mjd)/7==1)
630 {
631 year = Year()+1;
632 return 1;
633 }
634
635 // Return calculated Week
636 year = Year();
637 return (mjd-mjd0)/7 + 1;
638}
639
640// --------------------------------------------------------------------------
641//
642// Is the given year a leap year.
643// The calendar year is 365 days long, unless the year is exactly divisible
644// by 4, in which case an extra day is added to February to make the year
645// 366 days long. If the year is the last year of a century, eg. 1700, 1800,
646// 1900, 2000, then it is only a leap year if it is exactly divisible by
647// 400. Therefore, 1900 wasn't a leap year but 2000 was. The reason for
648// these rules is to bring the average length of the calendar year into
649// line with the length of the Earth's orbit around the Sun, so that the
650// seasons always occur during the same months each year.
651//
652Bool_t MTime::IsLeapYear() const
653{
654 const UInt_t y = Year();
655 return (y%4==0) && !((y%100==0) && (y%400>0));
656}
657
658// --------------------------------------------------------------------------
659//
660// Set the time to the current system time. The timezone is ignored.
661// If everything is set correctly you'll get UTC.
662//
663void MTime::Now()
664{
665#ifdef __LINUX__
666 struct timeval tv;
667 if (gettimeofday(&tv, NULL)<0)
668 Clear();
669 else
670 Set(tv);
671#else
672 Clear();
673#endif
674}
675
676// --------------------------------------------------------------------------
677//
678// Return contents as a TString of the form:
679// "dd.mm.yyyy hh:mm:ss.fff"
680//
681TString MTime::GetString() const
682{
683 UShort_t y, ms;
684 Byte_t mon, d, h, m, s;
685
686 GetDate(y, mon, d);
687 GetTime(h, m, s, ms);
688
689 return TString(Form("%02d.%02d.%04d %02d:%02d:%02d.%03d", d, mon, y, h, m, s, ms));
690}
691
692// --------------------------------------------------------------------------
693//
694// Return contents as a string format'd with strftime:
695// Here is a short summary of the most important formats. For more
696// information see the man page (or any other description) of
697// strftime...
698//
699// %a The abbreviated weekday name according to the current locale.
700// %A The full weekday name according to the current locale.
701// %b The abbreviated month name according to the current locale.
702// %B The full month name according to the current locale.
703// %c The preferred date and time representation for the current locale.
704// %d The day of the month as a decimal number (range 01 to 31).
705// %e Like %d, the day of the month as a decimal number,
706// but a leading zero is replaced by a space.
707// %H The hour as a decimal number using a 24-hour clock (range 00 to 23)
708// %k The hour (24-hour clock) as a decimal number (range 0 to 23);
709// single digits are preceded by a blank.
710// %m The month as a decimal number (range 01 to 12).
711// %M The minute as a decimal number (range 00 to 59).
712// %R The time in 24-hour notation (%H:%M). For a
713// version including the seconds, see %T below.
714// %S The second as a decimal number (range 00 to 61).
715// %T The time in 24-hour notation (%H:%M:%S).
716// %x The preferred date representation for the current
717// locale without the time.
718// %X The preferred time representation for the current
719// locale without the date.
720// %y The year as a decimal number without a century (range 00 to 99).
721// %Y The year as a decimal number including the century.
722// %+ The date and time in date(1) format.
723//
724// The default is: Tuesday 16.February 2004 12:17:22
725//
726// The maximum size of the return string is 128 (incl. NULL)
727//
728// For dates before 1. 1.1902 a null string is returned
729// For dates after 31.12.2037 a null string is returned
730//
731// To change the localization use loc, eg loc = "da_DK", "de_DE".
732// Leaving the argument empty will just take the default localization.
733//
734// If loc is "", each part of the locale that should be modified is set
735// according to the environment variables. The details are implementation
736// dependent. For glibc, first (regardless of category), the environment
737// variable LC_ALL is inspected, next the environment variable with the
738// same name as the category (LC_COLLATE, LC_CTYPE, LC_MESSAGES, LC_MONE?
739// TARY, LC_NUMERIC, LC_TIME) and finally the environment variable LANG.
740// The first existing environment variable is used.
741//
742// A locale name is typically of the form language[_territory][.code?
743// set][@modifier], where language is an ISO 639 language code, territory
744// is an ISO 3166 country code, and codeset is a character set or encoding
745// identifier like ISO-8859-1 or UTF-8. For a list of all supported
746// locales, try "locale -a", cf. locale(1).
747//
748TString MTime::GetStringFmt(const char *fmt, const char *loc) const
749{
750 if (!fmt)
751 fmt = "%A %e.%B %Y %H:%M:%S";
752
753 UShort_t y, ms;
754 Byte_t mon, d, h, m, s;
755
756 GetDate(y, mon, d);
757 GetTime(h, m, s, ms);
758
759 // If date<1902 strftime crahses on my (tbretz) laptop
760 // it doesn't crash in the DC.
761 // if (y<1902 || y>2037)
762 // return "";
763
764 struct tm time;
765 time.tm_sec = s;
766 time.tm_min = m;
767 time.tm_hour = h;
768 time.tm_mday = d;
769 time.tm_mon = mon-1;
770 time.tm_year = y-1900;
771 time.tm_isdst = -1;
772
773 // -1: If dst, isdst is set to 1 but hour is not changed
774 // 0: If dst, hour is changed
775
776 // Get old local
777 const TString locale = setlocale(LC_TIME, 0);
778
779 // Set new local (e.g. Montag instead of Monday)
780 setlocale(LC_TIME, loc);
781
782 // recalculate tm_yday and tm_wday
783 mktime(&time);
784
785 // We get errors for example for 1910-01-01
786 // if (mktime(&time)<0)
787 // return "";
788
789 char ret[128];
790 const size_t rc = strftime(ret, 127, fmt, &time);
791
792 setlocale(LC_TIME, locale);
793
794 return rc ? ret : "";
795}
796
797// --------------------------------------------------------------------------
798//
799// Set the time according to the format fmt.
800// Default is "%A %e.%B %Y %H:%M:%S"
801//
802// For more information see GetStringFmt
803//
804Bool_t MTime::SetStringFmt(const char *time, const char *fmt, const char *loc)
805{
806 if (!fmt)
807 fmt = "%A %e.%B %Y %H:%M:%S";
808
809 struct tm t;
810 memset(&t, 0, sizeof(struct tm));
811
812 const TString locale = setlocale(LC_TIME, 0);
813
814 setlocale(LC_TIME, loc);
815 strptime(time, fmt, &t);
816 setlocale(LC_TIME, locale);
817
818 return Set(t.tm_year+1900, t.tm_mon+1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
819}
820
821// --------------------------------------------------------------------------
822//
823// Return contents as a TString of the form:
824// "yyyy-mm-dd hh:mm:ss"
825//
826TString MTime::GetSqlDateTime() const
827{
828 return GetStringFmt("%Y-%m-%d %H:%M:%S");
829}
830
831// --------------------------------------------------------------------------
832//
833// Return contents as a TString of the form:
834// "yyyymmddhhmmss"
835//
836TString MTime::GetSqlTimeStamp() const
837{
838 return GetStringFmt("%Y%m%d%H%M%S");
839}
840
841// --------------------------------------------------------------------------
842//
843// Return contents as a TString of the form:
844// "yyyymmdd_hhmmss"
845//
846TString MTime::GetFileName() const
847{
848 return GetStringFmt("%Y%m%d_%H%M%S");
849}
850
851// --------------------------------------------------------------------------
852//
853// Print MTime as string
854//
855void MTime::Print(Option_t *) const
856{
857 UShort_t yea, ms;
858 Byte_t mon, day, h, m, s;
859
860 GetDate(yea, mon, day);
861 GetTime(h, m, s, ms);
862
863 *fLog << all << GetDescriptor() << ": ";
864 *fLog << GetString() << Form(" (+%dns)", fNanoSec) << endl;
865}
866
867Bool_t MTime::SetBinary(const UInt_t t[6])
868{
869 return Set(t[0], t[1], t[2], t[3], t[4], t[5], 0);
870}
871
872istream &MTime::ReadBinary(istream &fin)
873{
874 UShort_t y;
875 Byte_t mon, d, h, m, s;
876
877 fin.read((char*)&y, 2);
878 fin.read((char*)&mon, 1);
879 fin.read((char*)&d, 1);
880 fin.read((char*)&h, 1);
881 fin.read((char*)&m, 1);
882 fin.read((char*)&s, 1); // Total=7
883
884 Set(y, mon, d, h, m, s, 0);
885
886 return fin;
887}
888
889void MTime::AddMilliSeconds(UInt_t ms)
890{
891 fTime += ms;
892
893 fTime += 11*kHour;
894 fMjd += (Long_t)fTime/kDay;
895 fTime = (Long_t)fTime%kDay;
896 fTime -= 11*kHour;
897}
898
899void MTime::Plus1ns()
900{
901 fNanoSec++;
902
903 if (fNanoSec<1000000)
904 return;
905
906 fNanoSec = 0;
907 fTime += 1;
908
909 if ((Long_t)fTime<(Long_t)kDay*13)
910 return;
911
912 fTime = 11*kDay;
913 fMjd++;
914}
915
916void MTime::Minus1ns()
917{
918 if (fNanoSec>0)
919 {
920 fNanoSec--;
921 return;
922 }
923
924 fTime -= 1;
925 fNanoSec = 999999;
926
927 if ((Long_t)fTime>=-(Long_t)kDay*11)
928 return;
929
930 fTime = 13*kDay-1;
931 fMjd--;
932}
933
934/*
935MTime MTime::operator-(const MTime &tm1)
936{
937 const MTime &tm0 = *this;
938
939 MTime t0 = tm0>tm1 ? tm0 : tm1;
940 const MTime &t1 = tm0>tm1 ? tm1 : tm0;
941
942 if (t0.fNanoSec<t1.fNanoSec)
943 {
944 t0.fNanoSec += 1000000;
945 t0.fTime -= 1;
946 }
947
948 t0.fNanoSec -= t1.fNanoSec;
949 t0.fTime -= t1.fTime;
950
951 if ((Long_t)t0.fTime<-(Long_t)kHour*11)
952 {
953 t0.fTime += kDay;
954 t0.fMjd--;
955 }
956
957 t0.fMjd -= t1.fMjd;
958
959 return t0;
960}
961
962void MTime::operator-=(const MTime &t)
963{
964 *this = *this-t;
965}
966
967MTime MTime::operator+(const MTime &t1)
968{
969 MTime t0 = *this;
970
971 t0.fNanoSec += t1.fNanoSec;
972
973 if (t0.fNanoSec>999999)
974 {
975 t0.fNanoSec -= 1000000;
976 t0.fTime += kDay;
977 }
978
979 t0.fTime += t1.fTime;
980
981 if ((Long_t)t0.fTime>=(Long_t)kHour*13)
982 {
983 t0.fTime -= kDay;
984 t0.fMjd++;
985 }
986
987 t0.fMjd += t1.fMjd;
988
989 return t0;
990}
991
992void MTime::operator+=(const MTime &t)
993{
994 *this = *this+t;
995}
996*/
997
998void MTime::SetMean(const MTime &t0, const MTime &t1)
999{
1000 // This could be an operator+
1001 *this = t0;
1002
1003 fNanoSec += t1.fNanoSec;
1004
1005 if (fNanoSec>999999)
1006 {
1007 fNanoSec -= 1000000;
1008 fTime += kDay;
1009 }
1010
1011 fTime += t1.fTime;
1012
1013 if ((Long_t)fTime>=(Long_t)kHour*13)
1014 {
1015 fTime -= kDay;
1016 fMjd++;
1017 }
1018
1019 fMjd += t1.fMjd;
1020
1021 // This could be an operator/
1022 if ((Long_t)fTime<0)
1023 {
1024 fTime += kDay;
1025 fMjd--;
1026 }
1027
1028 Int_t reminder = fMjd%2;
1029 fMjd /= 2;
1030
1031 fTime += reminder*kDay;
1032 reminder = (Long_t)fTime%2;
1033 fTime /= 2;
1034
1035 fNanoSec += reminder*1000000;
1036 fNanoSec /= 2;
1037
1038 fTime += 11*kHour;
1039 fMjd += (Long_t)fTime/kDay;
1040 fTime = (Long_t)fTime%kDay;
1041 fTime -= 11*kHour;
1042}
1043
1044void MTime::SetMean(Double_t t0, Double_t t1)
1045{
1046 const Double_t mean = (t0+t1)*(0.5/kDaySec);
1047 SetMjd(mean);
1048}
1049
1050void MTime::AsciiRead(istream &fin)
1051{
1052 fin >> *this;
1053}
1054
1055Bool_t MTime::AsciiWrite(ostream &out) const
1056{
1057 out << *this;
1058 return out;
1059}
1060
1061// --------------------------------------------------------------------------
1062//
1063// Calculate the day of easter for the given year.
1064// MTime() is returned if this was not possible.
1065//
1066// In case of the default argument or the year less than zero
1067// the date of eastern of the current year (the year corresponding to
1068// MTime(-1)) is returned.
1069//
1070// for more information see: MAstro::GetDayOfEaster()
1071//
1072MTime MTime::GetEaster(Short_t year)
1073{
1074 if (year<0)
1075 year = MTime(-1).Year();
1076
1077 const Int_t day = MAstro::GetEasterOffset(year);
1078 if (day<0)
1079 return MTime();
1080
1081 MTime t;
1082 t.Set(year, 3, 1);
1083 t.SetMjd(t.GetMjd() + day);
1084
1085 return t;
1086}
Note: See TracBrowser for help on using the repository browser.