Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

Date.h

Go to the documentation of this file.
00001 
00002 //
00003 // This file is part of the MADELINE 2 program 
00004 // written by Edward H. Trager and Ritu Khanna
00005 // Copyright (c) 2005 by the
00006 // Regents of the University of Michigan.
00007 // All Rights Reserved.
00008 // 
00009 // The latest version of this program is available from:
00010 // 
00011 //   http://eyegene.ophthy.med.umich.edu/madeline/
00012 //   
00013 // Released under the GNU General Public License.
00014 // A copy of the GPL is included in the distribution
00015 // package of this software, or see:
00016 // 
00017 //   http://www.gnu.org/copyleft/
00018 //   
00019 // ... for licensing details.
00020 // 
00022 //
00023 // 2005.03.27.ET
00024 //
00025 
00026 //
00027 // Date.h
00028 //
00029 
00051 
00052 #ifndef DATE_INCLUDED
00053 #define DATE_INCLUDED
00054 
00055 #include <time.h>
00056 #include <math.h>
00057 #include <stdarg.h>
00058 #include <stdio.h>
00059 #include <limits.h>
00060 #include <string.h>
00061 #include <stdlib.h>
00062 #include <iostream>
00063 
00064 #include "Data.h"
00065 #include "Number.h"
00066 
00067 //
00068 // Date Class:
00069 //
00070 class Date : public Data {
00071 public:
00072         
00073         //
00074         // We use BUDDHIST_GREGORIAN here to emphasize that this
00075         // is not the lunar calendar but just the Gregorian with
00076         // years counted using the Buddhist Era convention:
00077         // 
00078         enum CALENDAR{ GREGORIAN, BUDDHIST_GREGORIAN, HIJRI };
00079         
00080         const static Date MISSING;
00081         
00082 private:
00083         
00084         const static int _daysInMonth[12];
00085         
00086         static bool _displayRangeMeans;
00087         static bool _displayDelimiters;
00088         
00089         // Private constants needed by algorithms:
00090         const static long _IGREG=(15+31L*(10+12L*1582)); // Gregorian calendar adopted Oct. 15, 1582 
00091         const static long _IGREGJULIAN = 2299161;        // Julian day number of Oct. 15m 1582
00092         //
00093         // The start (day 1 of Muharram, 1 AH) of the Hijri Islamic calendar  was 
00094         // Friday July 16, 622 CE, julianDay=1,948,440.  
00095         // As the calendar depends on Lunar sightings by a human (rather than 
00096         // astronomical calculations), it can go up or down a day.
00097         // By using two days before here, the algorithm based on Reingold and 
00098         // Dershowitz (see reference below) gives the correct dates and days of 
00099         // week for:
00100         // 
00101         //   Friday   0632.03.06 CE == 0010.12.09 AH
00102         //   Tuesday  1992.12.08 CE == 1413.06.13 AH
00103         //   Saturday 2005.03.26 CE == 1426.02.16 AH
00104         //   
00105         const static long _IHIJRIJULIAN = 1948438;
00106         
00107         //
00108         // Length of date string in yyyy.mm.dd or dd.mm.yyyy format
00109         // or in ~-yyyy.mm.dd - yyyy.mm.dd format, plus some extra for
00110         // good measure:
00111         // 
00112         // -> _DATESTRINGLENGTH=72 would allow, for example, a date string
00113         //   like "二〇〇五-〇五-一七 -  二〇〇五-一〇-三〇" in UTF-8:
00114         // 
00115         const static unsigned int _DATESTRINGLENGTH=72;  
00116         const static double _DAYSINYEAR;      // Current (in year 2005) average no. of days per year
00117         static std::set<std::string> _dateMissingValue;
00118         
00119         // Private data:
00120         bool     _isMissing;                             // Boolean for missing
00121         bool     _isApproximate;
00122         bool     _isRange;
00123         long     _julian;                                // julian day number
00124         long     _highEndJulian;
00125         
00126         // STATIC MEMBER:
00127         static CALENDAR _calendarDisplayType;            // Default calendar
00128         
00130         //
00131         // Private methods:
00132         // 
00134         
00135         //
00136         // Converts Gregorian civil year yyyy, month mm, day dd 
00137         // to Julian day number:
00138         //
00139         long _yearMonthDayToJulian(int yyyy,int mm, int dd);
00140         
00141         //
00142         // Determines if a Gregorian calendar year is a leap year: 
00143         //
00144         bool _isLeapYear(int year);
00145         // 
00146         // Returns last day of a month.
00147         //
00148         int _getLastDayOfMonth(int year, int month);
00149         //
00150         // Obtain the Gregorian civil year yyyy, month mm, and day dd:
00151         //
00152         void _getYearMonthDay(const long julian, int *yyyy,int *mm,int *dd) const;
00153         
00155         //
00156         // Islamic calendar stuff:
00157         //
00158         // Reference: 
00159         // "Calendrical Calculations'' by Nachum Dershowitz and
00160         // Edward M. Reingold, Software---Practice & Experience,
00161         // vol. 20, no. 9 (September, 1990), pp. 899--928.
00162         //
00164         
00165         //
00166         // Obtain the Julian day number from an Islamic Hijri date:
00167         //
00168         long _islamicYearMonthDayToJulian( int yyyy, int mm, int dd) const ;
00169         
00170         //
00171         // Returns true if it is an Islamic Hijri leap year:
00172         //
00173         bool _isIslamicLeapYear(int year) const;
00174         
00175         //
00176         // Returns the number of days in the Islamic month:
00177         //
00178         int _lastDayOfIslamicMonth(int year, int month) const ;
00179         
00180         //
00181         // _getIslamicYearMonthDay
00182         //
00183         // --> Only use if date is > IHIJRIJULIAN (start of Islamic calendar)
00184         //
00185         void _getIslamicYearMonthDay(const long julian, int *yyyy, int *mm, int *dd) const;
00186         
00187         bool _intersectionIsNotNull(const Date& b) const;
00188         
00190         //
00191         // New Date range parsing methods:
00192         //
00195         //
00196         // Level 0 Methods:
00197         //
00199         bool _hasASCIIDateDelimiter(const char **position);
00200         bool _hasApproximationIndicator(const char **position);
00201         bool _hasCJKCharacterForYear(const char **position);
00202         bool _hasCJKCharacterForMonth(const char **position);
00203         bool _hasCJKCharacterForDay(const char **position);
00204         bool _hasCJKCharacterForArrive(const char **position);
00205         bool _hasCJKCharacterForFullStop(const char **position);
00206         bool _hasCJKCharacterForForwardSlash(const char **position);
00207         bool _hasCJKCharacterForDash(const char **position);
00208         bool _hasCJKCharacterForOpeningSquareBracket(const char **position);
00209         bool _hasCJKCharacterForClosingSquareBracket(const char **position);
00210         bool _hasUnicodeRightArrow(const char **position);
00211         bool _hasKoreanHangulForYear(const char **position);
00212         bool _hasKoreanHangulForMonth(const char **position);
00213         bool _hasKoreanHangulForDay(const char **position);
00214         //
00215         // Use this for any additional characters:
00216         //
00217         bool _hasUTF8Character(const char **position,const char *UTF8Character);
00219         //
00220         // Level 1 Methods:
00221         //
00223         bool _hasRangeDelimiter(const char **position,bool startBracketFound);
00224         bool _hasCJKVersionOfGenericDateDelimiter(const char **position);
00225         bool _hasYearDelimiter(const char **position, bool *isProperCJKDate);
00226         bool _hasMonthDelimiter(const char **position);
00227         bool _hasDayDelimiter(const char **position);
00228         bool _hasOpeningBracket(const char **position);
00229         bool _hasClosingBracket(const char **position);
00230         int _getValue(const char **position);
00231         void _skipWhiteSpace(const char **position);
00232         
00234         //
00235         // Utility Methods:
00236         //
00238         // void _warning(const char *message);
00239         // void _showDate(int year, int month, int day);
00240         
00242         //
00243         // Level 2 Methods:
00244         //
00246         void _readDateString(const char *s);
00247         
00249         //
00250         // REVISED AND ADDED PRIVATE METHODS:
00251         //
00253         bool _yearIsValid(int year , const char *dateString );
00254         bool _monthIsValid(int month , const char *dateString );
00255         bool _dayIsValid(int year, int month, int day , const char *dateString );
00256 
00257 public:
00258         //
00259         // Static methods:
00260         //
00261         static void addDateMissingValue(std::string value){ _dateMissingValue.insert(value); }
00262         static bool isMissingValue(std::string value);
00263         // Constructors:
00264         Date() { setMissing(); }           // defaults to missing
00265         Date(const char *d ) { set(d); }
00266         Date(const std::string &d){ set(d.c_str()); }
00267         Date(const Date *d);
00268         
00269         // Methods required by virtual base class Variable:
00270         bool isMissing( void ) const;
00271         void setMissing( void );
00272         void set(const char *dateString);
00273         void set(const std::string &dateString);
00274         const std::string get( void ) const;
00275         
00276         // Operators:
00277         Date& operator++();
00278         Date operator++( int );
00279         
00280         Date& operator--();
00281         Date operator--( int );
00282         
00283         Date& operator+=( const Number & b );
00284         Date& operator-=( const Number & b );
00285         
00286         Date operator+( const Number & b ) const;
00287         Date operator-( const Number & b ) const;
00288         Number operator-( const Date & b ) const;
00289         bool operator<(const Data& b) const;
00290         bool operator==(const Data& b) const;
00291         bool isApproximate( void ) const { return _isApproximate; }
00292         const DATATYPE getDataType( void ) const { return DATE; } 
00293         virtual Date* clone() const;
00294         
00295         // Determines whether a string is a valid Date string
00296         static bool isA(const std::string& s);
00297         static double getDaysInYear(void) { return _DAYSINYEAR; }
00298         
00299         static void displayRangeMeans(bool displayThem);
00300         static void displayDelimiters(bool displayThem); 
00301 };
00302 
00303 //
00304 // Free Functions:
00305 //
00306 std::ostream& operator<<(std::ostream& s,const Date& d);
00307 
00308 #endif
00309 

Generated on Fri Nov 18 16:24:39 2011 for MADELINE by  doxygen 1.4.4