00001 //# Time.h: enquiry functions for calendar and clock time, with some operations 00002 //# Copyright (C) 1994,1995,1999,2000,2001 00003 //# Associated Universities, Inc. Washington DC, USA. 00004 //# 00005 //# This library is free software; you can redistribute it and/or modify it 00006 //# under the terms of the GNU Library General Public License as published by 00007 //# the Free Software Foundation; either version 2 of the License, or (at your 00008 //# option) any later version. 00009 //# 00010 //# This library is distributed in the hope that it will be useful, but WITHOUT 00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00012 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00013 //# License for more details. 00014 //# 00015 //# You should have received a copy of the GNU Library General Public License 00016 //# along with this library; if not, write to the Free Software Foundation, 00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. 00018 //# 00019 //# Correspondence concerning AIPS++ should be addressed as follows: 00020 //# Internet email: aips2-request@nrao.edu. 00021 //# Postal address: AIPS++ Project Office 00022 //# National Radio Astronomy Observatory 00023 //# 520 Edgemont Road 00024 //# Charlottesville, VA 22903-2475 USA 00025 //# 00026 00027 //# $Id$ 00028 00029 #ifndef CASA_TIME_H 00030 #define CASA_TIME_H 00031 00032 #include <casacore/casa/aips.h> 00033 #include <casacore/casa/BasicSL/String.h> 00034 #include <casacore/casa/iosfwd.h> 00035 00036 namespace casacore { //# NAMESPACE CASACORE - BEGIN 00037 00038 // <Summary> date and time enquiry functions, with some operations. 00039 // </summary> 00040 // 00041 // <use visibility=export> 00042 // 00043 // <reviewed reviewer="Paul Shannon" date="1995/03/01" tests="tTime" demos=""> 00044 // This class might be better named a Date object, especially given that 00045 // more accurate Time classes are going to be required. 00046 // </reviewed> 00047 00048 // <prerequisite> 00049 // <li> you should understand the difference between "Julian" and 00050 // "modified Julian" date 00051 // </prerequisite> 00052 00053 // <synopsis> 00054 // This class provides convenient date objects for the programmer. 00055 // Once constructed, they may be compared, read and written, and 00056 // queried for a wide variety of re-expressions. In a typical (?) use 00057 // you might create a Time object, and then query it to find out 00058 // the current month, day of the week, and whether it is a leap 00059 // year. You can also find out the number of seconds which have elapsed 00060 // since a specific Time. 00061 // 00062 // <note role=caution> This class should not be used for very high precision 00063 // work. The time from epoch (1970.0) in seconds is 00064 // interconverted between computer "double" values, and 00065 // some loss of accuracy might result. 00066 // </note> 00067 // </synopsis> 00068 00069 // <example> 00070 // <srcblock> 00071 // Time startTime; 00072 // Time moonLanding (1969,7,14); 00073 // cout << "date and time of moon landing: " << moonLanding << endl; 00074 // cout << "day of week: " << moonLanding.dayOfWeek () << endl; 00075 // cout << "day of year: " << moonLanding.dayOfYear () << endl; 00076 // cout << "seconds since moon landing: " << moonLanding.age () << endl; 00077 // cout << "weeks since moon landing: " << 00078 // moonLanding.age () / (60 * 60 * 24 * 7) << endl; 00079 // cout << "seconds elapsed since start: " << startTime.age () << endl; 00080 // </srcblock> 00081 // </example> 00082 // 00083 00084 // <todo asof="1995/03/23"> 00085 // <li> member function 'age' might be renamed 'elapsedTime' 00086 // <li> A reference to the source of each algorithm should be provided. 00087 // </todo> 00088 class Time { 00089 00090 public: 00091 // the default constructor returns an object with the present date and time 00092 Time (); 00093 // Construct time with Julian day number 00094 Time (double jdn); 00095 // Construct Time with Gregorian calendar 00096 // <ul> 00097 // <li> seconds after the minute [0,59.999] (include milliseconds) 00098 // <li> minutes after the hour [0,59] 00099 // <li> hours after midnight [0,23] 00100 // <li> day of the month [1,31] 00101 // <li> month of the year [1,12] 00102 // <li> year. Beware, because '94' refers to the early Christian era, not 00103 // the 20th century. 00104 // </ul> 00105 Time (uInt year, uInt month, uInt day, uInt hour=0, uInt min=0, 00106 double sec=0.0); 00107 00108 // Copy constructor 00109 Time (const Time& time); 00110 00111 // return the Julian day (unit day) 00112 double julianDay () const; 00113 // return the modified Julian day (unit day) 00114 double modifiedJulianDay () const; 00115 00116 // initialise the julian day data with Time class 00117 Time& operator = (const Time& time); 00118 00119 double operator - (const Time& begin); 00120 Time operator + (const double plus); 00121 00122 Bool operator == (const Time& other) const; 00123 Bool operator != (const Time& other) const; 00124 Bool operator > (const Time& other) const; 00125 Bool operator < (const Time& other) const; 00126 00127 // if iso is True, then use ISO 8601 format 00128 // otherwise, produce the string of the form 00129 // Tue Mar 22 16:40:24 1994 00130 // with GMT time 00131 String toString(const Bool iso=False) const; 00132 00133 // returns a String in ISO 8601 format YYYY-MM-DDTHH:MM:SS in GMT 00134 // note: for dates beyond year 9999, use more digits for year 00135 const String ISODate() const 00136 { return toString(True); } 00137 00138 // write the current time, GMT, in format 00139 // Tue Mar 22 16:40:24 1994 00140 friend ostream& operator<<(ostream& out, const Time& other) 00141 { 00142 out << other.toString(False); 00143 return out; 00144 } 00145 00146 // read in date, which must be in the following format 00147 // month/day/year,hour:min:sec 00148 // where month,day,year,hour,min and sec are uInt. 00149 friend istream& operator >> (istream&, Time&); 00150 00151 // reset date to the present instant 00152 void now (); 00153 void setDate (uInt year, uInt month, uInt day, uInt hour=0, uInt min=0, 00154 double sec=0.0); 00155 00156 // number of seconds which have elapsed since Time object was created 00157 // or reset 00158 double age (); 00159 00160 // Return the seconds, minutes or hour part of the time. 00161 // <group> 00162 uInt seconds (); 00163 double dseconds (); 00164 uInt minutes (); 00165 uInt hours (); 00166 // </group> 00167 00168 uInt dayOfMonth (); 00169 uInt month (); 00170 00171 uInt year (); 00172 00173 uInt dayOfWeek (); 00174 00175 00176 uInt dayOfYear (); 00177 00178 static uInt howManyDaysInMonth (); 00179 00180 static uInt howManyDaysInMonth (uInt month,uInt year); 00181 00182 static Bool isLeapYear (); 00183 00184 static Bool isLeapYear (uInt year); 00185 00186 // Returns the difference, in seconds, between UTC and local time. 00187 // Negative values are west of GMT, positive are east. 00188 static Int timeZoneSeconds (); 00189 // Same as timeZoneSeconds(), but returns fractional days rather 00190 // than seconds. 00191 static Double timeZoneDays (); 00192 // Returns a string, e.g. "EST" or "MDT", describing the current 00193 // local time zone. 00194 static String timeZoneName (); 00195 00196 protected: 00197 00198 // Modified Julian day number 00199 // 40587 modified Julian day number = 00:00:00 January 1, 1970, GMT. 00200 uInt mJulianDay; 00201 // the fraction of the day 00202 double mJulianDayfrac; 00203 00204 }; 00205 00206 00207 } //# NAMESPACE CASACORE - END 00208 00209 #endif 00210 //# roel's original comments -- these may be useful in creating demo 00211 //# programs when we get some time... 00212 00213 //# The function now () updated datas with the present time. 00214 // 00215 //# When create a object. The mJulianDay and mJulianDayfrac datas are 00216 //# initialise with actual modified Julian day. (<now() function). 00217 //# The default constructor is at preset time. 00218 //# 00219 //# i.e. 40587 modified Julian day number = 00:00:00 January 1, 1970, GMT. 00220 //# and 2440587.5 Julian day number = 00:00:00 January 1, 1970, GMT, 00221 //# then modified Julian day number = Julian day number - 2400000.5 00222 //# 00223 //# Important :We are consindered GMT time for all functions. 00224 //# We are considered only dates after 2400000 Julian day = 12:00:00 00225 //# November 15, 1858, GMT. 00226 //# 00227 //# When execute the now() function the actual mJulianDay and 00228 //# mJulianDayFrac datas are replace for the new modified Julian day 00229 //# and the fraction of the day. 00230 //# 00231 //# The function is invoked looks as follows 00232 //# 00233 //# <code> 00234 //# 00235 //# Time t; // The default constructor is at present time (now()) 00236 //# 00237 //# t.now(); 00238 //# 00239 //# </code> 00240 //# 00241 //# When execute the setDate() function the actual mJulianDay and 00242 //# mJulianDayFrac datas are replace for the new date 00243 //# 00244 //# The function is invoked looks as follows 00245 //# 00246 //# <code> 00247 //# 00248 //# Time t; // The default constructor is at present time (now()) 00249 //# 00250 //# t.setDate(1915,2,21); 00251 //# 00252 //# </code> 00253 //# 00254 //# The function age() return the time in seconds between 00255 //# some Time object and now. 00256 //# 00257 //# The function is invoked looks as follows 00258 //# 00259 //# <code> 00260 //# 00261 //# Time t; // The default constructor is at present time (now()) 00262 //# 00263 //# cout<<"time since x "<< t.age() <<"\n"; 00264 //# 00265 //# </code> 00266 //# 00267 //# The function julianDay() return the julian day number. 00268 //# 00269 //# The function is invoked looks as follows 00270 //# 00271 //# <code> 00272 //# 00273 //# Time t; 00274 //# 00275 //# cout<<"Julian day number "<<t.julianDay()<<"\n"; 00276 //# 00277 //# </code> 00278 //# 00279 //# The function modifiedJulianDay() return the modified julian day number. 00280 //# 00281 //# The function is invoked looks as follows 00282 //# 00283 //# <code> 00284 //# 00285 //# Time t; 00286 //# 00287 //# cout<<"Modified Julian day number "<<t.modifiedJulianDay()<<"\n"; 00288 //# 00289 //# </code> 00290 //# 00291 //# The function dayOfMonth() return day of the month [1,31] 00292 //# Note: This function doesn't modified the actual datas (mJulianDay and ' 00293 //# mJulianDayFrac). 00294 //# 00295 //# The function is invoked looks as follows 00296 //# 00297 //# <code> 00298 //# 00299 //# Time t; 00300 //# 00301 //# 00302 //# cout<<"day of month "<< t.dayOfMonth() <<"\n"; 00303 //# 00304 //# </code> 00305 //# 00306 //# The function month() return month of the year [1,12] 00307 //# Note: This function doesn't modified the actual datas (mJulianDay and ' 00308 //# mJulianDayFrac). 00309 //# 00310 //# The function is invoked looks as follows 00311 //# 00312 //# <code> 00313 //# 00314 //# Time t; 00315 //# 00316 //# 00317 //# cout<<"month "<< t.month() <<"\n"; 00318 //# 00319 //# </code> 00320 //# 00321 //# The function year() return the year. 00322 //# Note: This function doesn't modified the actual datas (mJulianDay and ' 00323 //# mJulianDayFrac). 00324 //# 00325 //# The function is invoked looks as follows 00326 //# 00327 //# <code> 00328 //# 00329 //# Time t; 00330 //# 00331 //# 00332 //# cout<<"Year "<< t.year() <<"\n"; 00333 //# 00334 //# </code> 00335 //# 00336 //# The function dayOfWeek() return days since sunday [1,7]. 00337 //# Note: This function doesn't modified the actual datas (mJulianDay and ' 00338 //# mJulianDayFrac). 00339 //# 00340 //# The function is invoked looks as follows 00341 //# 00342 //# <code> 00343 //# 00344 //# Time t; 00345 //# 00346 //# 00347 //# cout<<"day of week "<< t.dayOfWeek() <<"\n"; 00348 //# 00349 //# </code> 00350 //# 00351 //# The function dayOfYear() return day of the year [1,366] 00352 //# Note: This function doesn't modified the actual datas (mJulianDay and ' 00353 //# mJulianDayFrac). 00354 //# 00355 //# The function is invoked looks as follows 00356 //# 00357 //# <code> 00358 //# 00359 //# Time t; 00360 //# 00361 //# 00362 //# cout<<"day of year "<< t.dayOfYear() <<"\n"; 00363 //# 00364 //# </code> 00365 //# 00366 //# The function leapSeconds() return leap seconds. 00367 //# We have the next datas 00368 //# 00369 //# -Note: leapSeconds() removed 1997.10.07 by Jeff Uphoff, after 00370 //# -recommendation by Wim Brouw. 00371 //# 00372 //# leapsec=10; 00373 //# 00374 //# if(modified Julian Day>=41499) leapsec++; // 1 July 1972 00375 //# if(modified Julian Day>=41683) leapsec++; // 1 January 1973 00376 //# if(modified Julian Day>=42048) leapsec++; // 1 January 1974 00377 //# if(modified Julian Day>=42413) leapsec++; // 1 January 1975 00378 //# if(modified Julian Day>=42778) leapsec++; // 1 January 1976 00379 //# if(modified Julian Day>=43144) leapsec++; // 1 January 1977 00380 //# if(modified Julian Day>=43509) leapsec++; // 1 January 1978 00381 //# if(modified Julian Day>=43874) leapsec++; // 1 January 1979 00382 //# if(modified Julian Day>=44239) leapsec++; // 1 January 1980 00383 //# if(modified Julian Day>=44786) leapsec++; // 1 July 1981 00384 //# if(modified Julian Day>=45151) leapsec++; // 1 July 1982 00385 //# if(modified Julian Day>=45516) leapsec++; // 1 July 1983 00386 //# if(modified Julian Day>=46247) leapsec++; // 1 July 1985 00387 //# if(modified Julian Day>=47161) leapsec++; // 1 January 1988 00388 //# if(modified Julian Day>=47892) leapsec++; // 1 January 1990 00389 //# if(modified Julian Day>=48257) leapsec++; // 1 January 1991 00390 //# if(modified Julian Day>=48804) leapsec++; // 1 July 1992 00391 //# if(modified Julian Day>=49169) leapsec++; // 1 July 1993 00392 //# 00393 //# The function is invoked looks as follows 00394 //# 00395 //# <code> 00396 //# 00397 //# Time t; 00398 //# 00399 //# cout<<"Leap seconds "<< t.leapSeconds() <<"\n"; 00400 //# 00401 //# </code> 00402 //# 00403 //# The function howManyDaysInMonth() return how many days are in a month. 00404 //# 00405 //# The function is invoked looks as follows 00406 //# 00407 //# <code> 00408 //# 00409 //# Time t; 00410 //# uInt month=1,month2=2,year=1992; 00411 //# 00412 //# cout<<"how many days are in this month "<< howManyDaysInMonth() <<"\n" 00413 //# cout<<"how many days are in January "<< howManyDaysInMonth(month) <<"\n"; 00414 //# cout<<"how many days are in february of 1992 "<< 00415 //# howManyDaysInMonth(month,year) <<"\n"; // 1992 is a leap year 00416 //# 00417 //# </code> 00418 //# 00419 //# The function isLeapYear() return bool value. True if is a leap year 00420 //# and False in other case. 00421 //# 00422 //# The function is invoked looks as follows 00423 //# 00424 //# <code> 00425 //# 00426 //# Time t; 00427 //# 00428 //# uInt year=1992; 00429 //# 00430 //# if(isLeapYear(year)) 00431 //# cout<<"Is a leap year"; 00432 //# 00433 //# if(isLeapYear()) 00434 //# cout<<"This year is a leap year"; 00435 //# 00436 //# </code>