![]() |
Main Page Class Hierarchy Alphabetical List Compound List File List Compound Members
![]() |
00001 /******************************************************************************** 00002 * * 00003 * D a t e C l a s s * 00004 * * 00005 ********************************************************************************* 00006 * Copyright (C) 2005,2009 by Jeroen van der Zijp. All Rights Reserved. * 00007 ********************************************************************************* 00008 * This library is free software; you can redistribute it and/or modify * 00009 * it under the terms of the GNU Lesser General Public License as published by * 00010 * the Free Software Foundation; either version 3 of the License, or * 00011 * (at your option) any later version. * 00012 * * 00013 * This library is distributed in the hope that it will be useful, * 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00016 * GNU Lesser General Public License for more details. * 00017 * * 00018 * You should have received a copy of the GNU Lesser General Public License * 00019 * along with this program. If not, see <http://www.gnu.org/licenses/> * 00020 ********************************************************************************* 00021 * $Id: FXDate.h,v 1.29 2009/01/06 13:07:22 fox Exp $ * 00022 ********************************************************************************/ 00023 #ifndef FXDATE_H 00024 #define FXDATE_H 00025 00026 namespace FX { 00027 00028 00029 00030 /** 00031 * Gregorian date class, which is useful for calendrical calculations. 00032 */ 00033 class FXAPI FXDate { 00034 private: 00035 FXuint julian; 00036 private: 00037 static const FXchar shortMonthName[12][4]; 00038 static const FXchar longMonthName[12][10]; 00039 static const FXchar shortWeekDay[7][4]; 00040 static const FXchar longWeekDay[7][10]; 00041 public: 00042 00043 /// Names for the months 00044 enum { 00045 Jan=1,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec 00046 }; 00047 00048 /// Names for the weekdays 00049 enum { 00050 Sun=0,Mon,Tue,Wed,Thu,Fri,Sat 00051 }; 00052 00053 public: 00054 00055 /// Default constructor 00056 FXDate(){} 00057 00058 /// Copy constructor 00059 FXDate(const FXDate& date):julian(date.julian){} 00060 00061 /// Initialize with julian day number 00062 FXDate(FXuint jd):julian(jd){} 00063 00064 /// Initialize with year and day of year 00065 FXDate(FXint yr,FXint dy); 00066 00067 /// Initialize with year, month, and day of month 00068 FXDate(FXint yr,FXint mo,FXint dy); 00069 00070 /// Set julian day number 00071 void setJulian(FXuint jd){ julian=jd; } 00072 00073 /// Get julian day number 00074 FXuint getJulian() const { return julian; } 00075 00076 /// Set date to year and day of year 00077 void setDate(FXint yr,FXint dy); 00078 00079 /// Get year and day of year from date 00080 void getDate(FXint& yr,FXint& dy) const; 00081 00082 /// Set date to year, month, and day of month 00083 void setDate(FXint yr,FXint mo,FXint dy); 00084 00085 /// Get year, month, and day of month from date 00086 void getDate(FXint& yr,FXint& mo,FXint& dy) const; 00087 00088 /// Set date from nanoseconds since 1/1/1970 00089 void setTime(FXTime ns); 00090 00091 /// Get nanoseconds since 1/1/1970 from date 00092 FXTime getTime() const; 00093 00094 /// Return day of the month 00095 FXint day() const; 00096 00097 /// Return month 00098 FXint month() const; 00099 00100 /// Return year 00101 FXint year() const; 00102 00103 /// Return day of the week 00104 FXint dayOfWeek() const; 00105 00106 /// Return day of year 00107 FXint dayOfYear() const; 00108 00109 /// Return ISO8601 week number of this date 00110 FXint weekOfYear() const; 00111 00112 /// Return true if this is a leap year 00113 FXbool leapYear() const; 00114 00115 /// Return number of days in this year 00116 FXint daysInYear() const; 00117 00118 /// Return days in this month 00119 FXint daysInMonth() const; 00120 00121 /// Add d days to this date 00122 FXDate& addDays(FXint d); 00123 00124 /// Add m months to this date; day of month is adjusted for leap-years 00125 FXDate& addMonths(FXint m); 00126 00127 /// Add y years to this date; day of month is adjusted for leap-years 00128 FXDate& addYears(FXint y); 00129 00130 /// Is the value a leap year 00131 static FXbool leapYear(FXint yr); 00132 00133 /// Return number of days in a given year 00134 static FXint daysInYear(FXint yr); 00135 00136 /// Return number of days in the month in given year, month 00137 static FXint daysInMonth(FXint yr,FXint mo); 00138 00139 /// Get the name of the month 00140 static const FXchar *monthName(FXint mo){ return longMonthName[mo-1]; } 00141 00142 /// Get the abbreviated name of the month 00143 static const FXchar *monthNameShort(FXint mo){ return shortMonthName[mo-1]; } 00144 00145 /// Get the name of the day 00146 static const FXchar *dayName(FXint dy){ return longWeekDay[dy]; } 00147 00148 /// Get the abbreviated name of the day 00149 static const FXchar *dayNameShort(FXint dy){ return shortWeekDay[dy]; } 00150 00151 /// Return current local date 00152 static FXDate localDate(); 00153 00154 /// Return current universal (UTC) date 00155 static FXDate universalDate(); 00156 00157 /// Assignment 00158 FXDate& operator=(const FXDate& date){julian=date.julian;return *this;} 00159 00160 /// Assignment operators 00161 FXDate& operator+=(FXint x){ julian+=x; return *this; } 00162 FXDate& operator-=(FXint x){ julian-=x; return *this; } 00163 00164 /// Pre-Increment and decrement 00165 FXDate& operator++(){ ++julian; return *this; } 00166 FXDate& operator--(){ --julian; return *this; } 00167 00168 /// Post-Increment and decrement 00169 FXDate operator++(int){ FXDate t(julian++); return t; } 00170 FXDate operator--(int){ FXDate t(julian--); return t; } 00171 00172 /// Equality tests 00173 FXbool operator==(const FXDate& date) const { return julian==date.julian;} 00174 FXbool operator!=(const FXDate& date) const { return julian!=date.julian;} 00175 00176 /// Inequality tests 00177 FXbool operator<(const FXDate& date) const { return julian<date.julian;} 00178 FXbool operator<=(const FXDate& date) const { return julian<=date.julian;} 00179 FXbool operator>(const FXDate& date) const { return julian>date.julian;} 00180 FXbool operator>=(const FXDate& date) const { return julian>=date.julian;} 00181 00182 /// Add days to date yielding another date 00183 friend inline FXDate operator+(const FXDate& d,FXint x); 00184 friend inline FXDate operator+(FXint x,const FXDate& d); 00185 00186 /// Substract dates yielding days 00187 friend inline FXint operator-(const FXDate& a,const FXDate& b); 00188 00189 /// save to stream 00190 friend FXAPI FXStream& operator<<(FXStream& store,const FXDate& d); 00191 00192 /// load from stream 00193 friend FXAPI FXStream& operator>>(FXStream& store,FXDate& d); 00194 }; 00195 00196 00197 inline FXDate operator+(const FXDate& d,FXint x){ return FXDate(d.julian+x); } 00198 inline FXDate operator+(FXint x,const FXDate& d){ return FXDate(x+d.julian); } 00199 inline FXint operator-(const FXDate& a,const FXDate& b){return a.julian-b.julian; } 00200 00201 extern FXAPI FXStream& operator<<(FXStream& store,const FXDate& d); 00202 extern FXAPI FXStream& operator>>(FXStream& store,FXDate& d); 00203 00204 } 00205 00206 #endif
![]() |