Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

DateParse.hpp

Go to the documentation of this file.
00001 /*==========================================================================
00002  * Copyright (c) 2004 University of Massachusetts.  All Rights Reserved.
00003  *
00004  * Use of the Lemur Toolkit for Language Modeling and Information Retrieval
00005  * is subject to the terms of the software license set forth in the LICENSE
00006  * file included with this software, and also available at
00007  * http://www.lemurproject.org/license.html
00008  *
00009  *==========================================================================
00010 */
00011 
00012 
00013 //
00014 // DateParse
00015 //
00016 // 13 April 2004 -- tds
00017 //
00018 
00019 #ifndef INDRI_DATEPARSE_HPP
00020 #define INDRI_DATEPARSE_HPP
00021 
00022 class DateParse {
00023 private:
00024   static int _parseYear( const std::string& year ) {
00025     return atoi( year.c_str() );
00026   }
00027 
00028   static int _parseDay( const std::string& day ) {
00029     return atoi( day.c_str() );
00030   }
00031 
00032   static int _parseMonth( const std::string& month ) {
00033     if( month[0] >= '0' && month[0] <= '9' )
00034       return atoi( month.c_str() );
00035 
00036     char prefix[4];
00037     memset( prefix, 0, 4 );
00038 
00039     for( unsigned int i=0; i<4 && i<month.size(); i++ ) {
00040       prefix[i] = tolower( month[i] );
00041     }
00042     
00043     // j f m a m j j a s o n d
00044     if( prefix[0] == 'j' ) {
00045       if( prefix[1] == 'a' ) return 1; // january
00046       if( prefix[2] == 'n' ) return 6; // june
00047       if( prefix[2] == 'l' ) return 7; // july
00048       return 0;
00049     } else if( prefix[0] == 'f' ) {
00050       return 2; // february
00051     } else if( prefix[0] == 'a' ) {
00052       if( prefix[1] == 'p' ) return 4; // april
00053       if( prefix[1] == 'u' ) return 8; // august
00054       return 0;
00055     } else if( prefix[0] == 'm' ) {
00056       if( prefix[2] == 'r' ) return 3; // march
00057       if( prefix[2] == 'y' ) return 5; // may
00058       return 0;
00059     } else if( prefix[0] == 's' ) {
00060       return 9; // september
00061     } else if( prefix[0] == 'o' ) {
00062       return 10; // october
00063     } else if( prefix[0] == 'n' ) {
00064       return 11; // november
00065     } else if( prefix[0] == 'd' ) {
00066       return 12;
00067     }
00068 
00069     return 0;
00070   }
00071 
00072 public:
00073   // converts year, month, and day from parser to the number of days since 1600
00074   static UINT64 convertDate( const std::string& year, const std::string& month, const std::string& day ) {
00075     int numYear = _parseYear( year );
00076     int numMonth = _parseMonth( month );
00077     int numDay = _parseDay( day );
00078 
00079     int monthCumulativeDays[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
00080     if( numMonth == 0 || numYear < 1601 || numDay == 0 )
00081       return 0;
00082 
00083     // let's do days since 1600 here
00084     UINT64 totalDays = 0;
00085     UINT64 yearsSince = numYear - 1600;
00086     
00087     // every 4 years is a leap year, except every 100 years is not, except every 400 years actually is...
00088     UINT64 leapDays = yearsSince / 4 -
00089                       yearsSince / 100 +
00090                       yearsSince / 400 + 
00091                       1; // 1 leap day in 1600
00092 
00093     // if this year is a leap year and it's past febuary, add an extra year on
00094     if( numMonth > 2 && (numYear % 4) == 0 && ( ((numYear % 100) != 0) || (numYear % 400) == 0) )
00095       leapDays++;
00096 
00097     return (numDay-1) + monthCumulativeDays[numMonth-1] + yearsSince*365 + leapDays;
00098   }
00099 };
00100 
00101 #endif // INDRI_DATEPARSE_HPP
00102 

Generated on Wed Nov 3 12:58:53 2004 for Lemur Toolkit by doxygen1.2.18