Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

lkdatetime.h

00001 /* liblookdb: lkdatetime.h - Date, Time, Date-Time classes
00002     Copyright (C) 1998-2001 LOOK Systems
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Lesser General Public
00006     License as published by the Free Software Foundation; either
00007     version 2.1 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Lesser General Public License for more details.
00013 
00014     You should have received a copy of the GNU Lesser General Public
00015     License along with this library; if not, write to the Free Software
00016     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 */
00018 #ifndef LIBLOOKDB_LKDATETIME_H
00019 #define LIBLOOKDB_LKDATETIME_H
00020 
00021 // $Id: lkdatetime.h,v 1.5 2001/07/04 15:53:17 clive Exp $
00022 
00023 /* Date class (julian dates) Time class (secs since midnight) Date-Time (both)
00024 */
00025 
00026 #include "looktypes.h"
00027 #include "lktypebase.h"
00028 
00029 #include "lkstring.h"
00030 
00031 namespace look {
00032 
00033 // *****************************************************
00034 // LookDateTime class
00035 // *****************************************************
00036 
00038 
00049 class LOOKTYPES_DLL LookDate : public LookTypeBase
00050 {
00051 public:
00052 
00053 // Constructors.
00054         LookDate( unsigned long julianDate = 0UL );
00055         LookDate( const LookDate& theOther );
00056 
00057         LookDate( unsigned int theDay, unsigned int theMonth, unsigned int theYear );
00058         LookDate( unsigned int theDay, const LookString& theMonth, unsigned int theYear );
00059 
00060         ~LookDate( void );
00061 
00062 // get a copy
00063         LookTypeBase* GetNewCopy( void ) const;
00064 
00065 // get my id
00066         long GetTypeID( void ) const;
00067 
00068 // arithmetic operators
00069 // these are all changes in days
00070         LookDate& operator++();
00071         LookDate operator++( int );
00072 
00073         LookDate& operator--();
00074         LookDate operator--( int );
00075 
00076         LookDate& operator+=( long theDays );
00077         LookDate operator+( long theDays ) const;
00078 
00079         LookDate& operator-=( long theDays );
00080         LookDate operator-( long theDays ) const;
00081 // this one is the exception - you can get ndays from two dates
00082         long operator-( const LookDate& theDate ) const;
00083 
00084         // add months like ingres does
00085         // we can take + or - months
00086         void AddMonths( int theMonths );
00087 
00088 // comparison
00089         bool operator==( const LookDate& theOther ) const;
00090         bool operator!=( const LookDate& theOther ) const;
00091         bool operator<( const LookDate& theOther ) const;
00092         bool operator>( const LookDate& theOther ) const;
00093         bool operator<=( const LookDate& theOther ) const;
00094         bool operator>=( const LookDate& theOther ) const;
00095 
00096 // is this date valid?
00097         bool IsSet( void ) const;
00098 
00099 // various day, month, week, year queries
00100         unsigned int DayOfMonth( void ) const;
00101         unsigned int Month( void ) const;       // 1-12
00102         unsigned int Year( void ) const;        // no BC support
00103 
00104         unsigned int DayOfYear( void ) const;
00105 
00106         // day of week is 1 = Monday, 7 = Sunday, just like RW.
00107         // people seem to do it many ways, so i've stuck with what i'm replacing
00108         unsigned int DayOfWeek( void ) const;
00109 
00110         // convenience methods
00111         LookString DayName( void ) const;
00112         LookString MonthName( void ) const;
00113 
00114 // the previous method (get the previous monday or whatever)
00115 // and next
00116         LookDate Previous( unsigned int dayNumber ) const;
00117         LookDate Next( unsigned int dayNumber ) const;
00118 
00119 // are we a leap year?
00120 // (notice the static one too)
00121         bool IsLeap( void ) const;
00122 
00123 // string access
00124         bool Set( LookString theDateString );
00125 
00126 // set format is one of these
00127 // also is the getstring parameters
00128 //                separator  order    monthnum   yeardigits
00129 // dd-mmm-yyyy     -          dmy       true        4
00130 // dd/mm/yy        /          dmy       false       2
00131 // yymmdd          '\0'       ymd       true        2
00132 // yyyy-mm-dd      -          ymd       true        4
00133 // dd.mm.yyyy      .          dmy       true        4
00134 // yyyy-mmm-dd     -          ymd       false       4
00135 // dd-mmm-yyyy     -          dmy       false       4
00136 // mmm-dd-yyyy     -          mdy       false       4
00137 // dd/mm/yyyy      /          dmy       true        4
00138 
00139         // this one is easiest to program
00140         LookString GetString( const LookString& theSeparator = "/",
00141                 const LookString& theOrder = "dmy", 
00142                 bool monthIsNumber = true,
00143                 int yearDigits = 4 ) const;
00144 
00145 // static methods
00146 // today
00147         static LookDate Now( void );
00148 
00149 // conversions. fromstring takes short or long form
00150         static unsigned int DayFromName( LookString theDayName );
00151         static LookString NameFromDay( unsigned int theDayNumber );
00152         static LookString ShortNameFromDay( unsigned int theDayNumber );
00153 
00154         static unsigned int MonthFromName( LookString theMonthName );
00155         static LookString NameFromMonth( unsigned int theMonthNumber );
00156         static LookString ShortNameFromMonth( unsigned int theMonthNumber );
00157 
00158         static unsigned int DaysInMonth( unsigned int theMonthNumber,
00159                 unsigned int theYear );
00160 
00161         static bool IsLeap( unsigned int theYear );
00162 
00163         static void ConvertFromJulian( unsigned long theJulianDate,
00164                 unsigned int& year, unsigned int& month, unsigned int& day ); 
00165 
00166         static unsigned long ConvertToJulian( unsigned int year,
00167                 unsigned int month, unsigned int day ); 
00168 
00169         static void SetCenturyBoundary( int theBoundary );
00170         static int GetCenturyBoundary( void );
00171 
00172 protected:
00173 
00175         unsigned long itsJulianDate;
00176 
00178 
00183         static unsigned int itsCenturyBoundary;
00184 };
00185 
00186 // ===========================================================
00187 // time
00188 // ===========================================================
00190 
00195 class LOOKTYPES_DLL LookTime : public LookTypeBase
00196 {
00197 public:
00198 // Constructors.
00199         LookTime( unsigned int theTime = 0 );
00200 
00201         LookTime( unsigned int theHours, unsigned int theMinutes,
00202                 unsigned int theSeconds = 0 );
00203 
00204         LookTime( bool isPM, unsigned int theHours, unsigned int theMinutes = 0,
00205                 unsigned int theSeconds = 0 );
00206 
00207         LookTime( const LookTime& theOther );
00208 
00209         ~LookTime();
00210 
00211         // get a copy
00212         LookTypeBase* GetNewCopy( void ) const;
00213 
00214 // get my id
00215         long GetTypeID( void ) const;
00216 
00217         LookTime& operator=( const LookTime& theOther );
00218 
00219         // comparison
00220         bool operator==( const LookTime& theOther ) const;
00221         bool operator!=( const LookTime& theOther ) const;
00222         bool operator<( const LookTime& theOther ) const;
00223         bool operator>( const LookTime& theOther ) const;
00224         bool operator<=( const LookTime& theOther ) const;
00225         bool operator>=( const LookTime& theOther ) const;
00226 
00227         // differences are always in seconds
00228         LookTime& operator+=( int theChange );
00229         LookTime operator+( int theChange ) const;
00230 
00231         // difference between two times is in seconds
00232         int operator-( LookTime& theOther ) const;
00233 
00234         // difference between a time and some seconds is a time
00235         LookTime operator-( int theChange ) const;
00236         LookTime& operator-=( int theChange );
00237 
00238         // normalise us to a time of day, returning the change in days
00239         // if if itsTime = 100000, normalise will set time to
00240         // 13600 and return 1
00241         // -100000 -> 72800, -2 days
00242         int Normalise( void );
00243 
00244         bool Set( LookString theTimeString );
00245 
00246         // precision is 3 for hms, 2 for hm and 1 for h
00247         LookString GetString( const LookString& theSeparator = ":",
00248                 int thePrecision = 3,
00249                 bool hours24 = true ) const;
00250 
00251         // if the time is -ve these won't work
00252         unsigned int Hours( void ) const;
00253         unsigned int Hours12( void ) const;
00254         unsigned int Minutes( void ) const;
00255         unsigned int Seconds( void ) const;
00256         bool isPM( void ) const;
00257 
00258         void SetTime( unsigned int theTime )
00259         {
00260                 itsTime = theTime;
00261         }
00262 
00263         unsigned int GetTime( void ) const
00264         {
00265                 return itsTime;
00266         }
00267 
00268         // get current date/time
00269         static LookTime Now( void );
00270 
00271 protected:
00272 
00273         // allowed to be negative before normalising if you _really_ want
00275         int itsTime;
00276 };
00277 
00278 // ===========================================================
00279 // date/time
00280 // ===========================================================
00282 
00287 class LOOKTYPES_DLL LookDateTime : public LookTypeBase
00288 {
00289 public:
00290 // Constructors.
00291         LookDateTime();
00292         LookDateTime( const LookDate& theDate, const LookTime& theTime = 0 );
00293 
00294         LookDateTime( const LookDateTime& theOther );
00295 
00296         // destructor
00297         ~LookDateTime();
00298 
00299 // get a copy
00300         LookTypeBase* GetNewCopy( void ) const;
00301 
00302 // get my id
00303         long GetTypeID( void ) const;
00304 
00305         LookDateTime& operator=( const LookDateTime& theOther );
00306 
00307         // comparison
00308         bool operator==( const LookDateTime& theOther ) const;
00309         bool operator!=( const LookDateTime& theOther ) const;
00310         bool operator<( const LookDateTime& theOther ) const;
00311         bool operator>( const LookDateTime& theOther ) const;
00312         bool operator<=( const LookDateTime& theOther ) const;
00313         bool operator>=( const LookDateTime& theOther ) const;
00314 
00315         // differences are always in seconds
00316         LookDateTime& operator+=( long theChange );
00317         LookDateTime operator+( long theChange ) const;
00318 
00319         // difference between two dates is in seconds
00320         long operator-( LookDateTime& theOther ) const;
00321 
00322         // difference between a date and some seconds is a date
00323         LookDateTime& operator-=( long theChange );
00324         LookDateTime operator-( long theChange ) const;
00325 
00326         // get/set date/time
00327         void SetTime( const LookTime& theTime );
00328         void SetDate( const LookDate& theDate );
00329 
00330         LookTime GetTime( void ) const;
00331         LookDate GetDate( void ) const;
00332 
00333         bool Set( const LookString& theDateTimeString );
00334 
00335         // see GetString for lookDate for first 4 params
00336         // timeprecision is 3 for hms, 2 for hm and 1 for h
00337         LookString GetString( const LookString& theDateSeparator = "/",
00338                 const LookString& theOrder = "dmy", 
00339                 bool monthIsNumber = true,
00340                 int yearDigits = 4,             
00341                 const LookString& theTimeSeparator = ":",
00342                 int theTimePrecision = 3,
00343                 bool hours24 = true ) const;
00344         
00345         // get current date/time
00346         static LookDateTime Now( void );
00347 
00348 protected:
00349 
00350         void Normalise( void );
00351 
00353         LookDate itsDate;
00355         LookTime itsTime;
00356 };
00357 
00358 
00359 } // end of "look" namespace
00360 #endif 

Generated at Thu Jan 17 12:53:06 2002 for liblookdb by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001