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

Last change on this file since 9017 was 9017, 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 if (mktime(&time)<0)
784 return "";
785
786 char ret[128];
787 const size_t rc = strftime(ret, 127, fmt, &time);
788
789 setlocale(LC_TIME, locale);
790
791 return rc ? ret : "";
792}
793
794// --------------------------------------------------------------------------
795//
796// Set the time according to the format fmt.
797// Default is "%A %e.%B %Y %H:%M:%S"
798//
799// For more information see GetStringFmt
800//
801Bool_t MTime::SetStringFmt(const char *time, const char *fmt, const char *loc)
802{
803 if (!fmt)
804 fmt = "%A %e.%B %Y %H:%M:%S";
805
806 struct tm t;
807 memset(&t, 0, sizeof(struct tm));
808
809 const TString locale = setlocale(LC_TIME, 0);
810
811 setlocale(LC_TIME, loc);
812 strptime(time, fmt, &t);
813 setlocale(LC_TIME, locale);
814
815 return Set(t.tm_year+1900, t.tm_mon+1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
816}
817
818// --------------------------------------------------------------------------
819//
820// Return contents as a TString of the form:
821// "yyyy-mm-dd hh:mm:ss"
822//
823TString MTime::GetSqlDateTime() 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// "yyyymmddhhmmss"
832//
833TString MTime::GetSqlTimeStamp() const
834{
835 return GetStringFmt("%Y%m%d%H%M%S");
836}
837
838// --------------------------------------------------------------------------
839//
840// Return contents as a TString of the form:
841// "yyyymmdd_hhmmss"
842//
843TString MTime::GetFileName() const
844{
845 return GetStringFmt("%Y%m%d_%H%M%S");
846}
847
848// --------------------------------------------------------------------------
849//
850// Print MTime as string
851//
852void MTime::Print(Option_t *) const
853{
854 UShort_t yea, ms;
855 Byte_t mon, day, h, m, s;
856
857 GetDate(yea, mon, day);
858 GetTime(h, m, s, ms);
859
860 *fLog << all << GetDescriptor() << ": ";
861 *fLog << GetString() << Form(" (+%dns)", fNanoSec) << endl;
862}
863
864Bool_t MTime::SetBinary(const UInt_t t[6])
865{
866 return Set(t[0], t[1], t[2], t[3], t[4], t[5], 0);
867}
868
869istream &MTime::ReadBinary(istream &fin)
870{
871 UShort_t y;
872 Byte_t mon, d, h, m, s;
873
874 fin.read((char*)&y, 2);
875 fin.read((char*)&mon, 1);
876 fin.read((char*)&d, 1);
877 fin.read((char*)&h, 1);
878 fin.read((char*)&m, 1);
879 fin.read((char*)&s, 1); // Total=7
880
881 Set(y, mon, d, h, m, s, 0);
882
883 return fin;
884}
885
886void MTime::AddMilliSeconds(UInt_t ms)
887{
888 fTime += ms;
889
890 fTime += 11*kHour;
891 fMjd += (Long_t)fTime/kDay;
892 fTime = (Long_t)fTime%kDay;
893 fTime -= 11*kHour;
894}
895
896void MTime::Plus1ns()
897{
898 fNanoSec++;
899
900 if (fNanoSec<1000000)
901 return;
902
903 fNanoSec = 0;
904 fTime += 1;
905
906 if ((Long_t)fTime<(Long_t)kDay*13)
907 return;
908
909 fTime = 11*kDay;
910 fMjd++;
911}
912
913void MTime::Minus1ns()
914{
915 if (fNanoSec>0)
916 {
917 fNanoSec--;
918 return;
919 }
920
921 fTime -= 1;
922 fNanoSec = 999999;
923
924 if ((Long_t)fTime>=-(Long_t)kDay*11)
925 return;
926
927 fTime = 13*kDay-1;
928 fMjd--;
929}
930
931/*
932MTime MTime::operator-(const MTime &tm1)
933{
934 const MTime &tm0 = *this;
935
936 MTime t0 = tm0>tm1 ? tm0 : tm1;
937 const MTime &t1 = tm0>tm1 ? tm1 : tm0;
938
939 if (t0.fNanoSec<t1.fNanoSec)
940 {
941 t0.fNanoSec += 1000000;
942 t0.fTime -= 1;
943 }
944
945 t0.fNanoSec -= t1.fNanoSec;
946 t0.fTime -= t1.fTime;
947
948 if ((Long_t)t0.fTime<-(Long_t)kHour*11)
949 {
950 t0.fTime += kDay;
951 t0.fMjd--;
952 }
953
954 t0.fMjd -= t1.fMjd;
955
956 return t0;
957}
958
959void MTime::operator-=(const MTime &t)
960{
961 *this = *this-t;
962}
963
964MTime MTime::operator+(const MTime &t1)
965{
966 MTime t0 = *this;
967
968 t0.fNanoSec += t1.fNanoSec;
969
970 if (t0.fNanoSec>999999)
971 {
972 t0.fNanoSec -= 1000000;
973 t0.fTime += kDay;
974 }
975
976 t0.fTime += t1.fTime;
977
978 if ((Long_t)t0.fTime>=(Long_t)kHour*13)
979 {
980 t0.fTime -= kDay;
981 t0.fMjd++;
982 }
983
984 t0.fMjd += t1.fMjd;
985
986 return t0;
987}
988
989void MTime::operator+=(const MTime &t)
990{
991 *this = *this+t;
992}
993*/
994
995void MTime::SetMean(const MTime &t0, const MTime &t1)
996{
997 // This could be an operator+
998 *this = t0;
999
1000 fNanoSec += t1.fNanoSec;
1001
1002 if (fNanoSec>999999)
1003 {
1004 fNanoSec -= 1000000;
1005 fTime += kDay;
1006 }
1007
1008 fTime += t1.fTime;
1009
1010 if ((Long_t)fTime>=(Long_t)kHour*13)
1011 {
1012 fTime -= kDay;
1013 fMjd++;
1014 }
1015
1016 fMjd += t1.fMjd;
1017
1018 // This could be an operator/
1019 if ((Long_t)fTime<0)
1020 {
1021 fTime += kDay;
1022 fMjd--;
1023 }
1024
1025 Int_t reminder = fMjd%2;
1026 fMjd /= 2;
1027
1028 fTime += reminder*kDay;
1029 reminder = (Long_t)fTime%2;
1030 fTime /= 2;
1031
1032 fNanoSec += reminder*1000000;
1033 fNanoSec /= 2;
1034
1035 fTime += 11*kHour;
1036 fMjd += (Long_t)fTime/kDay;
1037 fTime = (Long_t)fTime%kDay;
1038 fTime -= 11*kHour;
1039}
1040
1041void MTime::SetMean(Double_t t0, Double_t t1)
1042{
1043 const Double_t mean = (t0+t1)*(0.5/kDaySec);
1044 SetMjd(mean);
1045}
1046
1047void MTime::AsciiRead(istream &fin)
1048{
1049 fin >> *this;
1050}
1051
1052Bool_t MTime::AsciiWrite(ostream &out) const
1053{
1054 out << *this;
1055 return out;
1056}
1057
1058// --------------------------------------------------------------------------
1059//
1060// Calculate the day of easter for the given year.
1061// MTime() is returned if this was not possible.
1062//
1063// In case of the default argument or the year less than zero
1064// the date of eastern of the current year (the year corresponding to
1065// MTime(-1)) is returned.
1066//
1067// for more information see: MAstro::GetDayOfEaster()
1068//
1069MTime MTime::GetEaster(Short_t year)
1070{
1071 if (year<0)
1072 year = MTime(-1).Year();
1073
1074 const Int_t day = MAstro::GetEasterOffset(year);
1075 if (day<0)
1076 return MTime();
1077
1078 MTime t;
1079 t.Set(year, 3, 1);
1080 t.SetMjd(t.GetMjd() + day);
1081
1082 return t;
1083}
Note: See TracBrowser for help on using the repository browser.