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
87 public:
88 // the default constructor returns an object with the present date and time
89 Time ();
90 // Construct time with Julian day number
91 Time (double jdn);
92 // Construct Time with Gregorian calendar
93 // <ul>
94 // <li> seconds after the minute [0,59.999] (include milliseconds)
95 // <li> minutes after the hour [0,59]
96 // <li> hours after midnight [0,23]
97 // <li> day of the month [1,31]
98 // <li> month of the year [1,12]
99 // <li> year. Beware, because '94' refers to the early Christian era, not
100 // the 20th century.
101 // </ul>
103 double sec=0.0);
104
105 // Copy constructor
106 Time (const Time& time);
107
108 // return the Julian day (unit day)
109 double julianDay () const;
110 // return the modified Julian day (unit day)
111 double modifiedJulianDay () const;
112
113 // initialise the julian day data with Time class
115
116 double operator - (const Time& begin);
117 Time operator + (const double plus);
118
119 Bool operator == (const Time& other) const;
120 Bool operator != (const Time& other) const;
121 Bool operator > (const Time& other) const;
122 Bool operator < (const Time& other) const;
123
124 // if iso is True, then use ISO 8601 format
125 // otherwise, produce the string of the form
126 // Tue Mar 22 16:40:24 1994
127 // with GMT time
128 String toString(const Bool iso=False) const;
129
130 // returns a String in ISO 8601 format YYYY-MM-DDTHH:MM:SS in GMT
131 // note: for dates beyond year 9999, use more digits for year
132 const String ISODate() const
133 { return toString(True); }
134
135 // write the current time, GMT, in format
136 // Tue Mar 22 16:40:24 1994
137 friend ostream& operator<<(ostream& out, const Time& other)
138 {
139 out << other.toString(False);
140 return out;
141 }
142
143 // read in date, which must be in the following format
144 // month/day/year,hour:min:sec
145 // where month,day,year,hour,min and sec are uInt.
146 friend istream& operator >> (istream&, Time&);
147
148 // reset date to the present instant
149 void now ();
150 void setDate (uInt year, uInt month, uInt day, uInt hour=0, uInt min=0,
151 double sec=0.0);
152
153 // number of seconds which have elapsed since Time object was created
154 // or reset
155 double age ();
156
157 // Return the seconds, minutes or hour part of the time.
158 // <group>
160 double dseconds ();
163 // </group>
164
167
169
171
172
174
176
178
179 static Bool isLeapYear ();
180
182
183 // Returns the difference, in seconds, between UTC and local time.
184 // Negative values are west of GMT, positive are east.
186 // Same as timeZoneSeconds(), but returns fractional days rather
187 // than seconds.
189 // Returns a string, e.g. "EST" or "MDT", describing the current
190 // local time zone.
192
193 protected:
194
195 // Modified Julian day number
196 // 40587 modified Julian day number = 00:00:00 January 1, 1970, GMT.
198 // the fraction of the day
200
201};
202
203
204} //# NAMESPACE CASACORE - END
205
206#endif
207//# roel's original comments -- these may be useful in creating demo
208//# programs when we get some time...
209
210//# The function now () updated datas with the present time.
211//
212//# When create a object. The mJulianDay and mJulianDayfrac datas are
213//# initialise with actual modified Julian day. (<now() function).
214//# The default constructor is at preset time.
215//#
216//# i.e. 40587 modified Julian day number = 00:00:00 January 1, 1970, GMT.
217//# and 2440587.5 Julian day number = 00:00:00 January 1, 1970, GMT,
218//# then modified Julian day number = Julian day number - 2400000.5
219//#
220//# Important :We are consindered GMT time for all functions.
221//# We are considered only dates after 2400000 Julian day = 12:00:00
222//# November 15, 1858, GMT.
223//#
224//# When execute the now() function the actual mJulianDay and
225//# mJulianDayFrac datas are replace for the new modified Julian day
226//# and the fraction of the day.
227//#
228//# The function is invoked looks as follows
229//#
230//# <code>
231//#
232//# Time t; // The default constructor is at present time (now())
233//#
234//# t.now();
235//#
236//# </code>
237//#
238//# When execute the setDate() function the actual mJulianDay and
239//# mJulianDayFrac datas are replace for the new date
240//#
241//# The function is invoked looks as follows
242//#
243//# <code>
244//#
245//# Time t; // The default constructor is at present time (now())
246//#
247//# t.setDate(1915,2,21);
248//#
249//# </code>
250//#
251//# The function age() return the time in seconds between
252//# some Time object and now.
253//#
254//# The function is invoked looks as follows
255//#
256//# <code>
257//#
258//# Time t; // The default constructor is at present time (now())
259//#
260//# cout<<"time since x "<< t.age() <<"\n";
261//#
262//# </code>
263//#
264//# The function julianDay() return the julian day number.
265//#
266//# The function is invoked looks as follows
267//#
268//# <code>
269//#
270//# Time t;
271//#
272//# cout<<"Julian day number "<<t.julianDay()<<"\n";
273//#
274//# </code>
275//#
276//# The function modifiedJulianDay() return the modified julian day number.
277//#
278//# The function is invoked looks as follows
279//#
280//# <code>
281//#
282//# Time t;
283//#
284//# cout<<"Modified Julian day number "<<t.modifiedJulianDay()<<"\n";
285//#
286//# </code>
287//#
288//# The function dayOfMonth() return day of the month [1,31]
289//# Note: This function doesn't modified the actual datas (mJulianDay and '
290//# mJulianDayFrac).
291//#
292//# The function is invoked looks as follows
293//#
294//# <code>
295//#
296//# Time t;
297//#
298//#
299//# cout<<"day of month "<< t.dayOfMonth() <<"\n";
300//#
301//# </code>
302//#
303//# The function month() return month of the year [1,12]
304//# Note: This function doesn't modified the actual datas (mJulianDay and '
305//# mJulianDayFrac).
306//#
307//# The function is invoked looks as follows
308//#
309//# <code>
310//#
311//# Time t;
312//#
313//#
314//# cout<<"month "<< t.month() <<"\n";
315//#
316//# </code>
317//#
318//# The function year() return the year.
319//# Note: This function doesn't modified the actual datas (mJulianDay and '
320//# mJulianDayFrac).
321//#
322//# The function is invoked looks as follows
323//#
324//# <code>
325//#
326//# Time t;
327//#
328//#
329//# cout<<"Year "<< t.year() <<"\n";
330//#
331//# </code>
332//#
333//# The function dayOfWeek() return days since sunday [1,7].
334//# Note: This function doesn't modified the actual datas (mJulianDay and '
335//# mJulianDayFrac).
336//#
337//# The function is invoked looks as follows
338//#
339//# <code>
340//#
341//# Time t;
342//#
343//#
344//# cout<<"day of week "<< t.dayOfWeek() <<"\n";
345//#
346//# </code>
347//#
348//# The function dayOfYear() return day of the year [1,366]
349//# Note: This function doesn't modified the actual datas (mJulianDay and '
350//# mJulianDayFrac).
351//#
352//# The function is invoked looks as follows
353//#
354//# <code>
355//#
356//# Time t;
357//#
358//#
359//# cout<<"day of year "<< t.dayOfYear() <<"\n";
360//#
361//# </code>
362//#
363//# The function leapSeconds() return leap seconds.
364//# We have the next datas
365//#
366//# -Note: leapSeconds() removed 1997.10.07 by Jeff Uphoff, after
367//# -recommendation by Wim Brouw.
368//#
369//# leapsec=10;
370//#
371//# if(modified Julian Day>=41499) leapsec++; // 1 July 1972
372//# if(modified Julian Day>=41683) leapsec++; // 1 January 1973
373//# if(modified Julian Day>=42048) leapsec++; // 1 January 1974
374//# if(modified Julian Day>=42413) leapsec++; // 1 January 1975
375//# if(modified Julian Day>=42778) leapsec++; // 1 January 1976
376//# if(modified Julian Day>=43144) leapsec++; // 1 January 1977
377//# if(modified Julian Day>=43509) leapsec++; // 1 January 1978
378//# if(modified Julian Day>=43874) leapsec++; // 1 January 1979
379//# if(modified Julian Day>=44239) leapsec++; // 1 January 1980
380//# if(modified Julian Day>=44786) leapsec++; // 1 July 1981
381//# if(modified Julian Day>=45151) leapsec++; // 1 July 1982
382//# if(modified Julian Day>=45516) leapsec++; // 1 July 1983
383//# if(modified Julian Day>=46247) leapsec++; // 1 July 1985
384//# if(modified Julian Day>=47161) leapsec++; // 1 January 1988
385//# if(modified Julian Day>=47892) leapsec++; // 1 January 1990
386//# if(modified Julian Day>=48257) leapsec++; // 1 January 1991
387//# if(modified Julian Day>=48804) leapsec++; // 1 July 1992
388//# if(modified Julian Day>=49169) leapsec++; // 1 July 1993
389//#
390//# The function is invoked looks as follows
391//#
392//# <code>
393//#
394//# Time t;
395//#
396//# cout<<"Leap seconds "<< t.leapSeconds() <<"\n";
397//#
398//# </code>
399//#
400//# The function howManyDaysInMonth() return how many days are in a month.
401//#
402//# The function is invoked looks as follows
403//#
404//# <code>
405//#
406//# Time t;
407//# uInt month=1,month2=2,year=1992;
408//#
409//# cout<<"how many days are in this month "<< howManyDaysInMonth() <<"\n"
410//# cout<<"how many days are in January "<< howManyDaysInMonth(month) <<"\n";
411//# cout<<"how many days are in february of 1992 "<<
412//# howManyDaysInMonth(month,year) <<"\n"; // 1992 is a leap year
413//#
414//# </code>
415//#
416//# The function isLeapYear() return bool value. True if is a leap year
417//# and False in other case.
418//#
419//# The function is invoked looks as follows
420//#
421//# <code>
422//#
423//# Time t;
424//#
425//# uInt year=1992;
426//#
427//# if(isLeapYear(year))
428//# cout<<"Is a leap year";
429//#
430//# if(isLeapYear())
431//# cout<<"This year is a leap year";
432//#
433//# </code>
String: the storage and methods of handling collections of characters.
Definition String.h:223
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.
static uInt howManyDaysInMonth(uInt month, uInt year)
static Bool isLeapYear()
double mJulianDayfrac
the fraction of the day
Definition Time.h:199
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:137
uInt mJulianDay
Modified Julian day number 40587 modified Julian day number = 00:00:00 January 1, 1970,...
Definition Time.h:197
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:132
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
this file contains all the compiler specific defines
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
TableExprNode time(const TableExprNode &node)
Definition ExprNode.h:1584
unsigned int uInt
Definition aipstype.h:49
LatticeExprNode min(const LatticeExprNode &left, const LatticeExprNode &right)
TableExprNode day(const TableExprNode &node)
Definition ExprNode.h:1527
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