Virtual U.org
Get Personal Training on VU Today
    
Top shadow
 
 register/help
User Name:

Password:

ODATE.CPP Source File
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

ODATE.CPP

Go to the documentation of this file.
00001 //Filename    : ODATE.CPP
00002 //Description : Date Information Object
00003 
00004 #include <stdlib.h>
00005 #include <string.h>
00006 #include <windowsx.h>
00007 
00008 #include <OSTR.H>
00009 #include <OMISC.H>
00010 #include <ODATE.H>
00011 
00012 //--------- Define static member variables -----------//
00013 
00014 #define  JULIAN_ADJUSTMENT    1721425L
00015 
00016 static char month_str_array[][10] = {
00017     "January",
00018     "February",
00019     "March",
00020     "April",
00021     "May",
00022     "June",
00023     "July",
00024     "August",
00025     "September",
00026     "October",
00027     "November",
00028     "December"
00029 } ;
00030 
00031 static int month_tot[]=
00032 { 0, 0,  31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }
00033 ;
00034 
00035 // Jan Feb Mar  Apr  May  Jun   Jul  Aug  Sep  Oct  Nov  Dec
00036 // 31  28  31   30   31   30     31   31   30   31   30   31
00037 
00038 //------------ Begin of function DateInfo::julain ----------//
00050 long DateInfo::julian( int year, int month, int day ) {
00051     long  total, dayYear ;
00052 
00053     dayYear    =  day_year( year, month, day) ;
00054 
00055     if (dayYear < 1)
00056         return( -1) ;                                 /* Illegal Date */
00057 
00058     total =  ytoj(year) ;
00059     total+=  dayYear ;
00060     total+=  JULIAN_ADJUSTMENT ;
00061 
00062     return total;
00063 }
00064 
00065 //-------------- End of function DateInfo::julian ------------//
00066 
00067 //--------- Begin of function DateInfo::julian ---------//
00078 long DateInfo::julian( char *dateStr ) {
00079     return julian( m.atoi( dateStr,4 ), m.atoi( dateStr+4,2 ),
00080                    m.atoi( dateStr+6,2 ) );
00081 }
00082 
00083 //---------- End of function DateInfo::julian ----------//
00084 
00085 //------------ Begin of function DateInfo::get_date ---------//
00095 int DateInfo::get_date( long julianDate, char returnType ) {
00096     int   year, month, day, nDays, maxDays ;
00097     long   totalDays ;
00098 
00099     //JULIAN_ADJUSTMENT = 1721425
00100     if ( julianDate > 5373484 || julianDate < JULIAN_ADJUSTMENT )
00101         return -1;
00102 
00103     totalDays  =  (long) (julianDate) - JULIAN_ADJUSTMENT ;
00104     year       =  (int) ((double)totalDays/365.2425) + 1 ;
00105     nDays      =  (int) (totalDays -  ytoj(year)) ;
00106 
00107     if ( nDays <= 0 ) {
00108         year-- ;
00109         nDays   =  (int) (totalDays - ytoj(year)) ;
00110     }
00111 
00112     if (year%4 == 0 && year%100 != 0 || year%400 == 0)
00113         maxDays =  366 ;
00114     else
00115         maxDays =  365 ;
00116 
00117     if ( nDays > maxDays ) {
00118         year++ ;
00119         nDays -= maxDays ;
00120     }
00121 
00122     if ( month_day( year, nDays, month, day ) < 0 )
00123         return -1;
00124 
00125     //............................................//
00126 
00127     switch( returnType ) {
00128     case 'Y':
00129         return year;
00130 
00131     case 'M':                                     // return the month
00132         return month;
00133 
00134     case 'D':
00135         return day;
00136     }
00137 
00138     return 0;
00139 }
00140 
00141 //------------- End of function DateInfo::get_date --------//
00142 
00143 //------------ Begin of function DateInfo::      ---------//
00159 char* DateInfo::date_str( long julianDate, int shortMonthStr, int showDate) {
00160     int    year, month, day, nDays, maxDays ;
00161     long   totalDays ;
00162     static char strBuf[10];
00163 
00164     if ( julianDate > 5373484 || julianDate < JULIAN_ADJUSTMENT ) {
00165         strBuf[0]=NULL;
00166         return strBuf;
00167     }
00168 
00169     totalDays  =  (long) (julianDate) - JULIAN_ADJUSTMENT ;
00170     year       =  (int) ((double)totalDays/365.2425) + 1 ;
00171     nDays      =  (int) (totalDays -  ytoj(year)) ;
00172 
00173     if ( nDays <= 0 ) {
00174         year-- ;
00175         nDays   =  (int) (totalDays - ytoj(year)) ;
00176     }
00177 
00178     if (year%4 == 0 && year%100 != 0 || year%400 == 0)
00179         maxDays =  366 ;
00180     else
00181         maxDays =  365 ;
00182 
00183     if ( nDays > maxDays ) {
00184         year++ ;
00185         nDays -= maxDays ;
00186     }
00187 
00188     if ( month_day( year, nDays, month, day ) < 0 ) {
00189         strBuf[0]=NULL;
00190         return strBuf;
00191     }
00192 
00193     //--------------------------------------------//
00194 
00195     static String str;
00196 
00197     str = month_str_array[month-1];
00198 
00199     if( shortMonthStr )
00200         str = str.left(3);
00201 
00202     if( showDate ) {
00203         str += " ";
00204         str += itoa(day,strBuf,10);
00205         str += ",";
00206     }
00207     str += " Yr.";
00208     str += itoa(year,strBuf,10);
00209 
00210     return str;
00211 }
00212 
00213 //------------- End of function DateInfo::date_str --------//
00214 
00215 //------------ Begin of function DateInfo::month_str ---------//
00221 char* DateInfo::month_str(int monthNo) {
00222     return month_str_array[monthNo-1];
00223 }
00224 
00225 //------------- End of function DateInfo::month_str --------//
00226 
00227 //---------- Begin of function DateInfo::day_year ----------//
00234 
00235 int DateInfo::day_year( int year, int month, int day ) {
00236     int  isLeap, monthDays ;
00237 
00238     isLeap =   ( year%4 == 0 && year%100 != 0 || year%400 == 0 ) ?  1 : 0 ;
00239 
00240     monthDays =  month_tot[ month+1 ] -  month_tot[ month] ;
00241     if ( month == 2 )  monthDays += isLeap ;
00242 
00243     if ( year  < 0  ||
00244          month < 1  ||  month > 12  ||
00245          day   < 1  ||  day   > monthDays )
00246         return( -1 ) ;                                /* Illegal Date */
00247 
00248     if ( month <= 2 )  isLeap = 0 ;
00249 
00250     return(  month_tot[month] + day + isLeap ) ;
00251 }
00252 
00253 //------------- End of function DateInfo::day_year -----------//
00254 
00255 //----------- Begin of function DateInfo::ytoj ----------//
00266 
00267 long DateInfo::ytoj( int yr ) {
00268     yr-- ;
00269     return( yr*365L +  yr/4L - yr/100L + yr/400L ) ;
00270 }
00271 
00272 //--------------- End of function DateInfo::ytoj -----------//
00273 
00274 //--------- Begin of function DateInfo::month_day ---------//
00279 int DateInfo::month_day( int year, int days,  int &monthRef,  int &dayRef ) {
00280     int isLeap, i ;
00281 
00282     isLeap =  ( year%4 == 0 && year%100 != 0 || year%400 == 0 ) ?  1 : 0 ;
00283     if ( days <= 59 )  isLeap = 0 ;
00284 
00285     for( i = 2; i <= 13; i++) {
00286         if ( days <=  month_tot[i] + isLeap ) {
00287             monthRef =  --i ;
00288             if ( i <= 2) isLeap = 0 ;
00289 
00290             dayRef   =  days - month_tot[ i] - isLeap ;
00291             return( 0) ;
00292         }
00293     }
00294     dayRef   =  0 ;
00295     monthRef =  0 ;
00296 
00297     return( -1 ) ;
00298 }
00299 
00300 //----------- End of function DateInfo::month_day -----------//
00301 
00302 //--------- Begin of function DateInfo::time_str ---------//
00308 char* DateInfo::time_str(int inTime) {
00309     // #### begin Gilbert 18/8 ####//
00310     static char strBuf[6] = "00:00";
00311     //itoa( inTime/100, strBuf, 10 );
00312     //itoa( inTime%100, strBuf+3, 10 );
00313     strBuf[4] = '0' + inTime % 10;
00314     strBuf[3] = '0' + (inTime/10) % 10;
00315     strBuf[1] = '0' + (inTime/100) % 10;
00316     strBuf[0] = '0' + (inTime/1000) % 10;
00317     // #### end Gilbert 18/8 ####//
00318 
00319     return strBuf;
00320 }
00321 
00322 //---------- End of function DateInfo::time_str ----------//
00323 
00324 //-------- Begin of function DateInfo::days_in_month ------//
00332 int DateInfo::days_in_month(int inMonth) {
00333     return month_tot[inMonth+1] - month_tot[inMonth];
00334 }
00335 
00336 //---------- End of function DateInfo::days_in_month ------//
00337 
00338 //-------- Begin of function DateInfo::add_months ------//
00347 int DateInfo::add_months(int inDate, int addMonth) {
00348     int inYear  = year(inDate);
00349     int inMonth = month(inDate);
00350     int inDay   = day(inDate);
00351 
00352     inMonth += addMonth;
00353 
00354     if( inMonth > 12 ) {
00355         inMonth -= 12;
00356         inYear++;
00357     }
00358 
00359     if( inDay > days_in_month(inMonth) )
00360         inDay = days_in_month(inMonth);
00361 
00362     return julian( inYear, inMonth, inDay );
00363 }
00364 
00365 //---------- End of function DateInfo::add_months ------//
00366 
00367 //-------- Begin of function DateInfo::file_time_to_julian ------//
00369 int DateInfo::file_time_to_julian(FILETIME& fileTime) {
00370     /*
00371       WORD dosTime, dosDate;
00372 
00373       CoFileTimeToDosDateTime( &fileTime, &dosDate, &dosTime );
00374 
00375       //--------------------------------------------//
00376       // Bits   Contents
00377       // 0-4    Days of the month (1-31).
00378       // 5-8    Months (1 = January, 2 = February, and so forth).
00379       // 9-15   Year offset from 1980 (add 1980 to get actual year).
00380       //--------------------------------------------//
00381 
00382       return julian( 1980 + (dosDate>>9), (dosDate>>5) & 0x0F, dosDate & 0x0F );
00383     */
00384     return 0;
00385 }
00386 
00387 //---------- End of function DateInfo::file_time_to_julian ------//

Generated on Fri Aug 23 01:37:20 2002 for VirtualU by doxygen1.2.17