casacore
Loading...
Searching...
No Matches
Time.h
Go to the documentation of this file.
1// # Time.h: enquiry functions for calendar and clock time, with some operations
2// # Copyright (C) 1994,1995,1999,2000,2001
3// # Associated Universities, Inc. Washington DC, USA.
4// #
5// # This library is free software; you can redistribute it and/or modify it
6// # under the terms of the GNU Library General Public License as published by
7// # the Free Software Foundation; either version 2 of the License, or (at your
8// # option) any later version.
9// #
10// # This library is distributed in the hope that it will be useful, but WITHOUT
11// # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12// # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13// # License for more details.
14// #
15// # You should have received a copy of the GNU Library General Public License
16// # along with this library; if not, write to the Free Software Foundation,
17// # Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18// #
19// # Correspondence concerning AIPS++ should be addressed as follows:
20// # Internet email: casa-feedback@nrao.edu.
21// # Postal address: AIPS++ Project Office
22// # National Radio Astronomy Observatory
23// # 520 Edgemont Road
24// # Charlottesville, VA 22903-2475 USA
25
26#ifndef CASA_TIME_H
27#define CASA_TIME_H
28
29#include <casacore/casa/aips.h>
30#include <casacore/casa/BasicSL/String.h>
31#include <casacore/casa/iosfwd.h>
32
33namespace casacore { // # NAMESPACE CASACORE - BEGIN
34
35// <Summary> date and time enquiry functions, with some operations.
36// </summary>
37//
38// <use visibility=export>
39//
40// <reviewed reviewer="Paul Shannon" date="1995/03/01" tests="tTime" demos="">
41// This class might be better named a Date object, especially given that
42// more accurate Time classes are going to be required.
43// </reviewed>
44
45// <prerequisite>
46// <li> you should understand the difference between "Julian" and
47// "modified Julian" date
48// </prerequisite>
49
50// <synopsis>
51// This class provides convenient date objects for the programmer.
52// Once constructed, they may be compared, read and written, and
53// queried for a wide variety of re-expressions. In a typical (?) use
54// you might create a Time object, and then query it to find out
55// the current month, day of the week, and whether it is a leap
56// year. You can also find out the number of seconds which have elapsed
57// since a specific Time.
58//
59// <note role=caution> This class should not be used for very high precision
60// work. The time from epoch (1970.0) in seconds is
61// interconverted between computer "double" values, and
62// some loss of accuracy might result.
63// </note>
64// </synopsis>
65
66// <example>
67// <srcblock>
68// Time startTime;
69// Time moonLanding (1969,7,14);
70// cout << "date and time of moon landing: " << moonLanding << endl;
71// cout << "day of week: " << moonLanding.dayOfWeek () << endl;
72// cout << "day of year: " << moonLanding.dayOfYear () << endl;
73// cout << "seconds since moon landing: " << moonLanding.age () << endl;
74// cout << "weeks since moon landing: " <<
75// moonLanding.age () / (60 * 60 * 24 * 7) << endl;
76// cout << "seconds elapsed since start: " << startTime.age () << endl;
77// </srcblock>
78// </example>
79//
80
81// <todo asof="1995/03/23">
82// <li> member function 'age' might be renamed 'elapsedTime'
83// <li> A reference to the source of each algorithm should be provided.
84// </todo>
85class Time {
86 public:
87 // the default constructor returns an object with the present date and time
89 // Construct time with Julian day number
90 Time(double jdn);
91 // Construct Time with Gregorian calendar
92 // <ul>
93 // <li> seconds after the minute [0,59.999] (include milliseconds)
94 // <li> minutes after the hour [0,59]
95 // <li> hours after midnight [0,23]
96 // <li> day of the month [1,31]
97 // <li> month of the year [1,12]
98 // <li> year. Beware, because '94' refers to the early Christian era, not
99 // the 20th century.
100 // </ul>
101 Time(uInt year, uInt month, uInt day, uInt hour = 0, uInt min = 0, double sec = 0.0);
102
103 // Copy constructor
104 Time(const Time& time);
105
106 // return the Julian day (unit day)
107 double julianDay() const;
108 // return the modified Julian day (unit day)
109 double modifiedJulianDay() const;
110
111 // initialise the julian day data with Time class
113
114 double operator-(const Time& begin);
115 Time operator+(const double plus);
116
117 Bool operator==(const Time& other) const;
118 Bool operator!=(const Time& other) const;
119 Bool operator>(const Time& other) const;
120 Bool operator<(const Time& other) const;
121
122 // if iso is True, then use ISO 8601 format
123 // otherwise, produce the string of the form
124 // Tue Mar 22 16:40:24 1994
125 // with GMT time
126 String toString(const Bool iso = False) const;
127
128 // returns a String in ISO 8601 format YYYY-MM-DDTHH:MM:SS in GMT
129 // note: for dates beyond year 9999, use more digits for year
130 const String ISODate() const { return toString(True); }
131
132 // write the current time, GMT, in format
133 // Tue Mar 22 16:40:24 1994
134 friend ostream& operator<<(ostream& out, const Time& other) {
135 out << other.toString(False);
136 return out;
137 }
138
139 // read in date, which must be in the following format
140 // month/day/year,hour:min:sec
141 // where month,day,year,hour,min and sec are uInt.
142 friend istream& operator>>(istream&, Time&);
143
144 // reset date to the present instant
145 void now();
146 void setDate(uInt year, uInt month, uInt day, uInt hour = 0, uInt min = 0, double sec = 0.0);
147
148 // number of seconds which have elapsed since Time object was created
149 // or reset
150 double age();
151
152 // Return the seconds, minutes or hour part of the time.
153 // <group>
155 double dseconds();
158 // </group>
159
162
164
166
168
170
172
173 static Bool isLeapYear();
174
176
177 // Returns the difference, in seconds, between UTC and local time.
178 // Negative values are west of GMT, positive are east.
180 // Same as timeZoneSeconds(), but returns fractional days rather
181 // than seconds.
183 // Returns a string, e.g. "EST" or "MDT", describing the current
184 // local time zone.
186
187 protected:
188 // Modified Julian day number
189 // 40587 modified Julian day number = 00:00:00 January 1, 1970, GMT.
191 // the fraction of the day
193};
194
195} // namespace casacore
196
197#endif
198// # roel's original comments -- these may be useful in creating demo
199// # programs when we get some time...
200
201// # The function now () updated datas with the present time.
202//
203// # When create a object. The mJulianDay and mJulianDayfrac datas are
204// # initialise with actual modified Julian day. (<now() function).
205// # The default constructor is at preset time.
206// #
207// # i.e. 40587 modified Julian day number = 00:00:00 January 1, 1970, GMT.
208// # and 2440587.5 Julian day number = 00:00:00 January 1, 1970, GMT,
209// # then modified Julian day number = Julian day number - 2400000.5
210// #
211// # Important :We are consindered GMT time for all functions.
212// # We are considered only dates after 2400000 Julian day = 12:00:00
213// # November 15, 1858, GMT.
214// #
215// # When execute the now() function the actual mJulianDay and
216// # mJulianDayFrac datas are replace for the new modified Julian day
217// # and the fraction of the day.
218// #
219// # The function is invoked looks as follows
220// #
221// # <code>
222// #
223// # Time t; // The default constructor is at present time (now())
224// #
225// # t.now();
226// #
227// # </code>
228// #
229// # When execute the setDate() function the actual mJulianDay and
230// # mJulianDayFrac datas are replace for the new date
231// #
232// # The function is invoked looks as follows
233// #
234// # <code>
235// #
236// # Time t; // The default constructor is at present time (now())
237// #
238// # t.setDate(1915,2,21);
239// #
240// # </code>
241// #
242// # The function age() return the time in seconds between
243// # some Time object and now.
244// #
245// # The function is invoked looks as follows
246// #
247// # <code>
248// #
249// # Time t; // The default constructor is at present time (now())
250// #
251// # cout<<"time since x "<< t.age() <<"\n";
252// #
253// # </code>
254// #
255// # The function julianDay() return the julian day number.
256// #
257// # The function is invoked looks as follows
258// #
259// # <code>
260// #
261// # Time t;
262// #
263// # cout<<"Julian day number "<<t.julianDay()<<"\n";
264// #
265// # </code>
266// #
267// # The function modifiedJulianDay() return the modified julian day number.
268// #
269// # The function is invoked looks as follows
270// #
271// # <code>
272// #
273// # Time t;
274// #
275// # cout<<"Modified Julian day number "<<t.modifiedJulianDay()<<"\n";
276// #
277// # </code>
278// #
279// # The function dayOfMonth() return day of the month [1,31]
280// # Note: This function doesn't modified the actual datas (mJulianDay and '
281// # mJulianDayFrac).
282// #
283// # The function is invoked looks as follows
284// #
285// # <code>
286// #
287// # Time t;
288// #
289// #
290// # cout<<"day of month "<< t.dayOfMonth() <<"\n";
291// #
292// # </code>
293// #
294// # The function month() return month of the year [1,12]
295// # Note: This function doesn't modified the actual datas (mJulianDay and '
296// # mJulianDayFrac).
297// #
298// # The function is invoked looks as follows
299// #
300// # <code>
301// #
302// # Time t;
303// #
304// #
305// # cout<<"month "<< t.month() <<"\n";
306// #
307// # </code>
308// #
309// # The function year() return the year.
310// # Note: This function doesn't modified the actual datas (mJulianDay and '
311// # mJulianDayFrac).
312// #
313// # The function is invoked looks as follows
314// #
315// # <code>
316// #
317// # Time t;
318// #
319// #
320// # cout<<"Year "<< t.year() <<"\n";
321// #
322// # </code>
323// #
324// # The function dayOfWeek() return days since sunday [1,7].
325// # Note: This function doesn't modified the actual datas (mJulianDay and '
326// # mJulianDayFrac).
327// #
328// # The function is invoked looks as follows
329// #
330// # <code>
331// #
332// # Time t;
333// #
334// #
335// # cout<<"day of week "<< t.dayOfWeek() <<"\n";
336// #
337// # </code>
338// #
339// # The function dayOfYear() return day of the year [1,366]
340// # Note: This function doesn't modified the actual datas (mJulianDay and '
341// # mJulianDayFrac).
342// #
343// # The function is invoked looks as follows
344// #
345// # <code>
346// #
347// # Time t;
348// #
349// #
350// # cout<<"day of year "<< t.dayOfYear() <<"\n";
351// #
352// # </code>
353// #
354// # The function leapSeconds() return leap seconds.
355// # We have the next datas
356// #
357// # -Note: leapSeconds() removed 1997.10.07 by Jeff Uphoff, after
358// # -recommendation by Wim Brouw.
359// #
360// # leapsec=10;
361// #
362// # if(modified Julian Day>=41499) leapsec++; // 1 July 1972
363// # if(modified Julian Day>=41683) leapsec++; // 1 January 1973
364// # if(modified Julian Day>=42048) leapsec++; // 1 January 1974
365// # if(modified Julian Day>=42413) leapsec++; // 1 January 1975
366// # if(modified Julian Day>=42778) leapsec++; // 1 January 1976
367// # if(modified Julian Day>=43144) leapsec++; // 1 January 1977
368// # if(modified Julian Day>=43509) leapsec++; // 1 January 1978
369// # if(modified Julian Day>=43874) leapsec++; // 1 January 1979
370// # if(modified Julian Day>=44239) leapsec++; // 1 January 1980
371// # if(modified Julian Day>=44786) leapsec++; // 1 July 1981
372// # if(modified Julian Day>=45151) leapsec++; // 1 July 1982
373// # if(modified Julian Day>=45516) leapsec++; // 1 July 1983
374// # if(modified Julian Day>=46247) leapsec++; // 1 July 1985
375// # if(modified Julian Day>=47161) leapsec++; // 1 January 1988
376// # if(modified Julian Day>=47892) leapsec++; // 1 January 1990
377// # if(modified Julian Day>=48257) leapsec++; // 1 January 1991
378// # if(modified Julian Day>=48804) leapsec++; // 1 July 1992
379// # if(modified Julian Day>=49169) leapsec++; // 1 July 1993
380// #
381// # The function is invoked looks as follows
382// #
383// # <code>
384// #
385// # Time t;
386// #
387// # cout<<"Leap seconds "<< t.leapSeconds() <<"\n";
388// #
389// # </code>
390// #
391// # The function howManyDaysInMonth() return how many days are in a month.
392// #
393// # The function is invoked looks as follows
394// #
395// # <code>
396// #
397// # Time t;
398// # uInt month=1,month2=2,year=1992;
399// #
400// # cout<<"how many days are in this month "<< howManyDaysInMonth() <<"\n"
401// # cout<<"how many days are in January "<< howManyDaysInMonth(month) <<"\n";
402// # cout<<"how many days are in february of 1992 "<<
403// # howManyDaysInMonth(month,year) <<"\n"; // 1992 is a leap year
404// #
405// # </code>
406// #
407// # The function isLeapYear() return bool value. True if is a leap year
408// # and False in other case.
409// #
410// # The function is invoked looks as follows
411// #
412// # <code>
413// #
414// # Time t;
415// #
416// # uInt year=1992;
417// #
418// # if(isLeapYear(year))
419// # cout<<"Is a leap year";
420// #
421// # if(isLeapYear())
422// # cout<<"This year is a leap year";
423// #
424// # </code>
String: the storage and methods of handling collections of characters.
Definition String.h:355
void now()
reset date to the present instant
Time(uInt year, uInt month, uInt day, uInt hour=0, uInt min=0, double sec=0.0)
Construct Time with Gregorian calendar.
double age()
number of seconds which have elapsed since Time object was created or reset
double operator-(const Time &begin)
double dseconds()
Time(double jdn)
Construct time with Julian day number.
Bool operator<(const Time &other) const
static uInt howManyDaysInMonth(uInt month, uInt year)
static Bool isLeapYear()
double mJulianDayfrac
the fraction of the day
Definition Time.h:192
Bool operator>(const Time &other) const
friend istream & operator>>(istream &, Time &)
read in date, which must be in the following format month/day/year,hour:min:sec where month,...
static Double timeZoneDays()
Same as timeZoneSeconds(), but returns fractional days rather than seconds.
Time operator+(const double plus)
String toString(const Bool iso=False) const
if iso is True, then use ISO 8601 format otherwise, produce the string of the form Tue Mar 22 16:40:2...
static uInt howManyDaysInMonth()
void setDate(uInt year, uInt month, uInt day, uInt hour=0, uInt min=0, double sec=0.0)
double julianDay() const
return the Julian day (unit day)
static Bool isLeapYear(uInt year)
friend ostream & operator<<(ostream &out, const Time &other)
write the current time, GMT, in format Tue Mar 22 16:40:24 1994
Definition Time.h:134
uInt mJulianDay
Modified Julian day number 40587 modified Julian day number = 00:00:00 January 1, 1970,...
Definition Time.h:190
Time & operator=(const Time &time)
initialise the julian day data with Time class
double modifiedJulianDay() const
return the modified Julian day (unit day)
static Int timeZoneSeconds()
Returns the difference, in seconds, between UTC and local time.
const String ISODate() const
returns a String in ISO 8601 format YYYY-MM-DDTHH:MM:SS in GMT note: for dates beyond year 9999,...
Definition Time.h:130
Time()
the default constructor returns an object with the present date and time
uInt seconds()
Return the seconds, minutes or hour part of the time.
Time(const Time &time)
Copy constructor.
Bool operator==(const Time &other) const
static String timeZoneName()
Returns a string, e.g.
Bool operator!=(const Time &other) const
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
TableExprNode time(const TableExprNode &node)
Definition ExprNode.h:1368
unsigned int uInt
Definition aipstype.h:49
LatticeExprNode min(const LatticeExprNode &left, const LatticeExprNode &right)
TableExprNode day(const TableExprNode &node)
Definition ExprNode.h:1329
iterator begin()
Get the begin and end iterator object for this block.
Definition Block.h:599
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
const Bool True
Definition aipstype.h:41
double Double
Definition aipstype.h:53