casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Time.h
Go to the documentation of this file.
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: Time.h 20652 2009-07-06 05:04:32Z Malte.Marquarding $
00028 
00029 #ifndef CASA_TIME_H
00030 #define CASA_TIME_H
00031 
00032 #include <casa/aips.h>
00033 #include <casa/BasicSL/String.h>
00034 #include <casa/iosfwd.h>
00035 
00036 namespace casa { //# NAMESPACE CASA - 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
00112    double julianDay () const;
00113      // return the modified Julian 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    uInt seconds ();
00161 
00162    uInt minutes ();
00163 
00164    uInt hours ();
00165 
00166    uInt dayOfMonth ();
00167    uInt month ();
00168 
00169    uInt year ();
00170 
00171    uInt dayOfWeek ();
00172 
00173 
00174    uInt dayOfYear ();
00175 
00176    static uInt howManyDaysInMonth ();
00177 
00178    static uInt howManyDaysInMonth (uInt month,uInt year);
00179 
00180    static Bool isLeapYear ();
00181 
00182    static Bool isLeapYear (uInt year);
00183 
00184      // Returns the difference, in seconds, between UTC and local time.
00185      // Negative values are west of GMT, positive are east.
00186    static Int timeZoneSeconds ();
00187      // Same as timeZoneSeconds(), but returns fractional days rather
00188      // than seconds.
00189    static Double timeZoneDays ();
00190      // Returns a string, e.g. "EST" or "MDT", describing the current
00191      // local time zone.
00192    static String timeZoneName ();
00193 
00194  protected:
00195 
00196      // Modified Julian day number
00197      // 40587 modified Julian day number = 00:00:00 January 1, 1970, GMT.
00198    uInt mJulianDay;
00199      // the fraction of the day
00200    double mJulianDayfrac;
00201 
00202 };
00203 
00204 
00205 } //# NAMESPACE CASA - END
00206 
00207 #endif
00208 //# roel's original comments -- these may be useful in creating demo
00209 //# programs when we get some time...
00210 
00211 //# The function now () updated datas with the present time.
00212 //
00213 //# When create a object. The mJulianDay and  mJulianDayfrac datas are
00214 //# initialise with actual modified Julian day. (<now() function).
00215 //# The default constructor is at preset time.
00216 //#
00217 //# i.e. 40587 modified Julian day number = 00:00:00 January 1, 1970, GMT.
00218 //# and 2440587.5 Julian day number = 00:00:00 January 1, 1970, GMT,
00219 //# then modified Julian day number = Julian day number - 2400000.5
00220 //#
00221 //# Important :We are consindered GMT time for all functions.
00222 //# We are considered only dates after 2400000 Julian day = 12:00:00
00223 //# November 15, 1858, GMT.
00224 //#
00225 //# When execute the now() function the actual mJulianDay and 
00226 //# mJulianDayFrac datas are replace for the new modified Julian day
00227 //# and the fraction of the day.
00228 //#
00229 //# The function is invoked looks as follows
00230 //#
00231 //# <code>
00232 //#
00233 //# Time t;   // The default constructor is at present time (now())
00234 //#
00235 //# t.now();
00236 //#
00237 //# </code>
00238 //#
00239 //# When execute the setDate() function the actual mJulianDay and 
00240 //# mJulianDayFrac datas are replace for the new date
00241 //#
00242 //# The function is invoked looks as follows
00243 //#
00244 //# <code>
00245 //#
00246 //# Time t;   // The default constructor is at present time (now())
00247 //#
00248 //# t.setDate(1915,2,21);
00249 //#
00250 //# </code>
00251 //#
00252 //# The function age() return the time in seconds between
00253 //# some Time object and now.
00254 //#
00255 //# The function is invoked looks as follows
00256 //#
00257 //# <code>
00258 //#
00259 //# Time t;   // The default constructor is at present time (now())
00260 //#
00261 //# cout<<"time since x "<< t.age() <<"\n";
00262 //#
00263 //# </code>
00264 //#
00265 //# The function julianDay() return the julian day number.
00266 //#
00267 //# The function is invoked looks as follows
00268 //#
00269 //# <code>
00270 //#
00271 //# Time t;
00272 //#
00273 //# cout<<"Julian day number "<<t.julianDay()<<"\n";
00274 //#
00275 //# </code>
00276 //#
00277 //# The function modifiedJulianDay() return the modified julian day number.
00278 //#
00279 //# The function is invoked looks as follows
00280 //#
00281 //# <code>
00282 //#
00283 //# Time t;
00284 //#
00285 //# cout<<"Modified Julian day number "<<t.modifiedJulianDay()<<"\n";
00286 //#
00287 //# </code>
00288 //#
00289 //# The function dayOfMonth() return day of the month [1,31]
00290 //# Note: This function doesn't modified the actual datas (mJulianDay and  '
00291 //# mJulianDayFrac).
00292 //#
00293 //# The function is invoked looks as follows
00294 //#
00295 //# <code>
00296 //#
00297 //# Time t;
00298 //#
00299 //#
00300 //# cout<<"day of month "<< t.dayOfMonth() <<"\n";
00301 //#
00302 //# </code>
00303 //#
00304 //# The function month() return month of the year [1,12]
00305 //# Note: This function doesn't modified the actual datas (mJulianDay and   '
00306 //# mJulianDayFrac).
00307 //#
00308 //# The function is invoked looks as follows
00309 //#
00310 //# <code>
00311 //#
00312 //# Time t;
00313 //#
00314 //#
00315 //# cout<<"month "<< t.month() <<"\n";
00316 //#
00317 //# </code>
00318 //#
00319 //# The function year() return the year.
00320 //# Note: This function doesn't modified the actual datas (mJulianDay and   '
00321 //# mJulianDayFrac).
00322 //#
00323 //# The function is invoked looks as follows
00324 //#
00325 //# <code>
00326 //#
00327 //# Time t;
00328 //#
00329 //#
00330 //# cout<<"Year "<< t.year() <<"\n";
00331 //#
00332 //# </code>
00333 //#
00334 //# The function dayOfWeek() return days since sunday [1,7].
00335 //# Note: This function doesn't modified the actual datas (mJulianDay and   '
00336 //# mJulianDayFrac).
00337 //#
00338 //# The function is invoked looks as follows
00339 //#
00340 //# <code>
00341 //#
00342 //# Time t;
00343 //#
00344 //#
00345 //# cout<<"day of week "<< t.dayOfWeek() <<"\n";
00346 //#
00347 //# </code>
00348 //#
00349 //# The function dayOfYear() return day  of the year [1,366]
00350 //# Note: This function doesn't modified the actual datas (mJulianDay and   '
00351 //# mJulianDayFrac).
00352 //#
00353 //# The function is invoked looks as follows
00354 //#
00355 //# <code>
00356 //#
00357 //# Time t;
00358 //#
00359 //#
00360 //# cout<<"day of year "<< t.dayOfYear() <<"\n";
00361 //#
00362 //# </code>
00363 //#
00364 //# The function leapSeconds() return leap seconds.
00365 //# We have the next datas
00366 //#
00367 //# -Note: leapSeconds() removed 1997.10.07 by Jeff Uphoff, after
00368 //# -recommendation by Wim Brouw.
00369 //#
00370 //#  leapsec=10;
00371 //#
00372 //#  if(modified Julian Day>=41499) leapsec++;    // 1 July      1972
00373 //#  if(modified Julian Day>=41683) leapsec++;    // 1 January   1973
00374 //#  if(modified Julian Day>=42048) leapsec++;    // 1 January   1974
00375 //#  if(modified Julian Day>=42413) leapsec++;    // 1 January   1975
00376 //#  if(modified Julian Day>=42778) leapsec++;    // 1 January   1976
00377 //#  if(modified Julian Day>=43144) leapsec++;    // 1 January   1977
00378 //#  if(modified Julian Day>=43509) leapsec++;    // 1 January   1978
00379 //#  if(modified Julian Day>=43874) leapsec++;    // 1 January   1979
00380 //#  if(modified Julian Day>=44239) leapsec++;    // 1 January   1980
00381 //#  if(modified Julian Day>=44786) leapsec++;    // 1 July      1981
00382 //#  if(modified Julian Day>=45151) leapsec++;    // 1 July      1982
00383 //#  if(modified Julian Day>=45516) leapsec++;    // 1 July      1983
00384 //#  if(modified Julian Day>=46247) leapsec++;    // 1 July      1985
00385 //#  if(modified Julian Day>=47161) leapsec++;    // 1 January   1988
00386 //#  if(modified Julian Day>=47892) leapsec++;    // 1 January   1990
00387 //#  if(modified Julian Day>=48257) leapsec++;    // 1 January   1991
00388 //#  if(modified Julian Day>=48804) leapsec++;    // 1 July      1992
00389 //#  if(modified Julian Day>=49169) leapsec++;    // 1 July      1993
00390 //#
00391 //# The function is invoked looks as follows
00392 //#
00393 //# <code>
00394 //#
00395 //# Time t;
00396 //#
00397 //# cout<<"Leap seconds "<< t.leapSeconds() <<"\n";
00398 //#
00399 //# </code>
00400 //#
00401 //# The function howManyDaysInMonth() return how many days are in a month.
00402 //#
00403 //# The function is invoked looks as follows
00404 //#
00405 //# <code>
00406 //#
00407 //# Time t;
00408 //# uInt month=1,month2=2,year=1992;
00409 //#
00410 //# cout<<"how many days are in this month "<< howManyDaysInMonth() <<"\n"
00411 //# cout<<"how many days are in January "<< howManyDaysInMonth(month) <<"\n";
00412 //# cout<<"how many days are in february of 1992 "<< 
00413 //#        howManyDaysInMonth(month,year) <<"\n"; // 1992 is a leap year
00414 //#
00415 //# </code>
00416 //#
00417 //# The function isLeapYear() return bool value. True if is a leap year
00418 //# and False in other case.
00419 //#
00420 //# The function is invoked looks as follows
00421 //#
00422 //# <code>
00423 //#
00424 //# Time t;
00425 //#
00426 //# uInt year=1992;
00427 //#
00428 //# if(isLeapYear(year))
00429 //#   cout<<"Is a leap year";
00430 //#
00431 //# if(isLeapYear())
00432 //#   cout<<"This year is a leap year";
00433 //#
00434 //# </code>