Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

FXDate.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *                            D a t e   C l a s s                                *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 2005,2006 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or                 *
00009 * modify it under the terms of the GNU Lesser General Public                    *
00010 * License as published by the Free Software Foundation; either                  *
00011 * version 2.1 of the License, or (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 GNU             *
00016 * Lesser General Public License for more details.                               *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public              *
00019 * License along with this library; if not, write to the Free Software           *
00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.    *
00021 *********************************************************************************
00022 * $Id: FXDate.h,v 1.10 2006/01/22 17:58:00 fox Exp $                            *
00023 ********************************************************************************/
00024 #ifndef FXDATE_H
00025 #define FXDATE_H
00026 
00027 namespace FX {
00028 
00029 
00030 
00031 /**
00032 * Gregorian date object.
00033 */
00034 class FXAPI FXDate {
00035 private:
00036   FXuint julian;
00037 private:
00038   static const FXchar shortMonthName[12][4];
00039   static const FXchar longMonthName[12][10];
00040   static const FXchar shortWeekDay[7][4];
00041   static const FXchar longWeekDay[7][10];
00042 protected:
00043   static void greg2jul(FXuint& jd,FXint y,FXint m,FXint d);
00044   static void jul2greg(FXuint jd,FXint& y,FXint& m,FXint& d);
00045 public:
00046 
00047   /// Names for the months
00048   enum {
00049     Jan=1,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec
00050     };
00051 
00052   /// Names for the weekdays
00053   enum {
00054     Sun=0,Mon,Tue,Wed,Thu,Fri,Sat
00055     };
00056 
00057 public:
00058 
00059   /// Default constructor
00060   FXDate(){}
00061 
00062   /// Copy constructor
00063   FXDate(const FXDate& date):julian(date.julian){}
00064 
00065   /// Initialize with year, month, and day
00066   FXDate(FXint y,FXint m,FXint d);
00067 
00068   /// Initialize with julian day number
00069   FXDate(FXuint j):julian(j){}
00070 
00071   /// Set julian day number
00072   void setJulian(FXuint day){ julian=day; }
00073 
00074   /// Get julian day number
00075   FXuint getJulian() const { return julian; }
00076 
00077   /// Set to year, month, and day
00078   void setDate(FXint y,FXint m,FXint d);
00079 
00080   /// Get year, month, and day
00081   void getDate(FXint& y,FXint& m,FXint& d) const;
00082 
00083   /// Return day of the month
00084   FXint day() const;
00085 
00086   /// Return month
00087   FXint month() const;
00088 
00089   /// Return year
00090   FXint year() const;
00091 
00092   /// Return day of the week
00093   FXint dayOfWeek() const;
00094 
00095   /// Return day of year
00096   FXint dayOfYear() const;
00097 
00098   /// Return days in this month
00099   FXint daysInMonth() const;
00100 
00101   /// Return true if leap year
00102   bool leapYear() const;
00103 
00104   /// Is the value a leap year
00105   static bool leapYear(FXint y);
00106 
00107   /// Get the name of the month
00108   static const FXchar *monthName(FXint month){ return longMonthName[month-1]; }
00109 
00110   /// Get the abbreviated name of the month
00111   static const FXchar *monthNameShort(FXint month){ return shortMonthName[month-1]; }
00112 
00113   /// Get the name of the day
00114   static const FXchar *dayName(FXint day){ return longWeekDay[day]; }
00115 
00116   /// Get the abbreviated name of the day
00117   static const FXchar *dayNameShort(FXint day){ return shortWeekDay[day]; }
00118 
00119   /// Return current local date
00120   static FXDate localDate();
00121 
00122   /// Return current UTC (Zulu) date
00123   static FXDate zuluDate();
00124 
00125   /// Assignment
00126   FXDate& operator=(const FXDate& date){julian=date.julian;return *this;}
00127 
00128   /// Assignment operators
00129   FXDate& operator+=(FXint x){ julian+=x; return *this; }
00130   FXDate& operator-=(FXint x){ julian-=x; return *this; }
00131 
00132   /// Increment and decrement
00133   FXDate& operator++(){ julian++; return *this; }
00134   FXDate& operator--(){ julian--; return *this; }
00135 
00136   /// Equality tests
00137   bool operator==(const FXDate& date) const { return julian==date.julian;}
00138   bool operator!=(const FXDate& date) const { return julian!=date.julian;}
00139 
00140   /// Inequality tests
00141   bool operator<(const FXDate& date) const { return julian<date.julian;}
00142   bool operator<=(const FXDate& date) const { return julian<=date.julian;}
00143   bool operator>(const FXDate& date) const { return julian>date.julian;}
00144   bool operator>=(const FXDate& date) const { return julian>=date.julian;}
00145 
00146   /// Add days to date yielding another date
00147   friend inline FXDate operator+(const FXDate& d,FXint x);
00148   friend inline FXDate operator+(FXint x,const FXDate& d);
00149 
00150   /// Substract dates yielding days
00151   friend inline FXint operator-(const FXDate& a,const FXDate& b);
00152 
00153   /// save to stream
00154   friend FXAPI FXStream& operator<<(FXStream& store,const FXDate& d);
00155 
00156   /// load from stream
00157   friend FXAPI FXStream& operator>>(FXStream& store,FXDate& d);
00158   };
00159 
00160 
00161 inline FXDate operator+(const FXDate& d,FXint x){ return FXDate(d.julian+x); }
00162 inline FXDate operator+(FXint x,const FXDate& d){ return FXDate(x+d.julian); }
00163 inline FXint operator-(const FXDate& a,const FXDate& b){return a.julian-b.julian; }
00164 
00165 extern FXAPI FXStream& operator<<(FXStream& store,const FXDate& d);
00166 extern FXAPI FXStream& operator>>(FXStream& store,FXDate& d);
00167 
00168 }
00169 
00170 #endif

Copyright © 1997-2005 Jeroen van der Zijp