Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class 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 namespace indri
00022 {
00023   namespace parse
00024   {
00025     
00026     class DateParse {
00027     private:
00028       static int _parseYear( const std::string& year ) {
00029         return atoi( year.c_str() );
00030       }
00031 
00032       static int _parseDay( const std::string& day ) {
00033         return atoi( day.c_str() );
00034       }
00035 
00036       static int _parseMonth( const std::string& month ) {
00037         if( month[0] >= '0' && month[0] <= '9' )
00038           return atoi( month.c_str() );
00039 
00040         char prefix[4];
00041         memset( prefix, 0, 4 );
00042 
00043         for( unsigned int i=0; i<4 && i<month.size(); i++ ) {
00044           prefix[i] = tolower( month[i] );
00045         }
00046     
00047         // j f m a m j j a s o n d
00048         if( prefix[0] == 'j' ) {
00049           if( prefix[1] == 'a' ) return 1; // january
00050           if( prefix[2] == 'n' ) return 6; // june
00051           if( prefix[2] == 'l' ) return 7; // july
00052           return 0;
00053         } else if( prefix[0] == 'f' ) {
00054           return 2; // february
00055         } else if( prefix[0] == 'a' ) {
00056           if( prefix[1] == 'p' ) return 4; // april
00057           if( prefix[1] == 'u' ) return 8; // august
00058           return 0;
00059         } else if( prefix[0] == 'm' ) {
00060           if( prefix[2] == 'r' ) return 3; // march
00061           if( prefix[2] == 'y' ) return 5; // may
00062           return 0;
00063         } else if( prefix[0] == 's' ) {
00064           return 9; // september
00065         } else if( prefix[0] == 'o' ) {
00066           return 10; // october
00067         } else if( prefix[0] == 'n' ) {
00068           return 11; // november
00069         } else if( prefix[0] == 'd' ) {
00070           return 12;
00071         }
00072 
00073         return 0;
00074       }
00075 
00076     public:
00077       // converts year, month, and day from parser to the number of days since 1600
00078       static UINT64 convertDate( const std::string& year, const std::string& month, const std::string& day ) {
00079         int numYear = _parseYear( year );
00080         int numMonth = _parseMonth( month );
00081         int numDay = _parseDay( day );
00082 
00083         int monthCumulativeDays[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
00084         if( numMonth == 0 || numYear < 1601 || numDay == 0 )
00085           return 0;
00086 
00087         // let's do days since 1600 here
00088         UINT64 totalDays = 0;
00089         UINT64 yearsSince = numYear - 1600;
00090     
00091         // every 4 years is a leap year, except every 100 years is not, except every 400 years actually is...
00092         UINT64 leapDays = yearsSince / 4 -
00093           yearsSince / 100 +
00094           yearsSince / 400 + 
00095           1; // 1 leap day in 1600
00096 
00097         // if this year is a leap year and it's past febuary, add an extra year on
00098         if( numMonth > 2 && (numYear % 4) == 0 && ( ((numYear % 100) != 0) || (numYear % 400) == 0) )
00099           leapDays++;
00100 
00101         return (numDay-1) + monthCumulativeDays[numMonth-1] + yearsSince*365 + leapDays;
00102       }
00103     };
00104   }
00105 }
00106 
00107 #endif // INDRI_DATEPARSE_HPP
00108 

Generated on Tue Jun 15 11:02:53 2010 for Lemur by doxygen 1.3.4