source: trunk/Mars/mbase/MTime.cc@ 11550

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