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

PDate.h

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 00003 // PLearn (A C++ Machine Learning Library) 00004 // Copyright (C) 1998 Pascal Vincent 00005 // Copyright (C) 1999-2002 Pascal Vincent, Yoshua Bengio and University of Montreal 00006 // 00007 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions are met: 00010 // 00011 // 1. Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // 00014 // 2. Redistributions in binary form must reproduce the above copyright 00015 // notice, this list of conditions and the following disclaimer in the 00016 // documentation and/or other materials provided with the distribution. 00017 // 00018 // 3. The name of the authors may not be used to endorse or promote 00019 // products derived from this software without specific prior written 00020 // permission. 00021 // 00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00033 // This file is part of the PLearn library. For more information on the PLearn 00034 // library, go to the PLearn Web site at www.plearn.org 00035 00036 00037 00038 00039 /* ******************************************************* 00040 * $Id: PDate.h,v 1.6 2004/02/28 17:53:01 tihocan Exp $ 00041 * This file is part of the PLearn library. 00042 ******************************************************* */ 00043 00044 00047 #ifndef PDate_INC 00048 #define PDate_INC 00049 00050 //#include <iostream> 00051 #include <string> 00052 #include <ctime> 00053 00054 namespace PLearn { 00055 using namespace std; 00056 00062 class PDate 00063 { 00064 public: 00065 short year; 00066 unsigned char month; 00067 unsigned char day; 00068 00070 PDate(); 00071 00072 PDate(short the_year, unsigned char the_month, unsigned char the_day) 00073 :year(the_year), month(the_month), day(the_day) {} 00074 00075 PDate(int the_year, int the_month, int the_day) 00076 :year(the_year), month(int(the_month)), day(int(the_day)) {} 00077 00080 PDate(int julian_day); 00081 00084 PDate(string date); 00085 00087 bool isMissing() const; 00088 void setMissing(); 00089 00090 string info() const; 00091 00093 bool operator==(const PDate& rhs) const { 00094 return year == rhs.year && month == rhs.month && day == rhs.day; 00095 } 00096 00097 bool operator!=(const PDate& rhs) const { 00098 return ! (*this == rhs); 00099 } 00100 00101 bool operator<(const PDate& rhs) const { 00102 return year < rhs.year || 00103 (year == rhs.year && month < rhs.month) || 00104 (year == rhs.year && month == rhs.month && day < rhs.day); 00105 } 00106 00107 bool operator<=(const PDate& rhs) const { 00108 return *this == rhs || *this < rhs; 00109 } 00110 00111 bool operator>(const PDate& rhs) const { 00112 return ! (*this <= rhs); 00113 } 00114 00115 bool operator>=(const PDate& rhs) const { 00116 return ! (*this < rhs); 00117 } 00118 00123 int toJulianDay() const; 00124 00125 //0=monday ... 6=sunday 00126 int dayOfWeek() const 00127 { return toJulianDay()%7; } 00128 00129 static PDate today() 00130 { 00131 time_t now(time(0)); 00132 tm* snow = localtime(&now); 00133 return PDate(1900+snow->tm_year, 1+snow->tm_mon, snow->tm_mday); 00134 } 00135 00136 }; 00137 00139 inline int operator-(const PDate& to_date, const PDate& from_date) 00140 { 00141 return to_date.toJulianDay() - from_date.toJulianDay(); 00142 } 00143 00145 inline PDate operator+(const PDate& pdate, int ndays) 00146 { 00147 return PDate(pdate.toJulianDay()+ndays); 00148 } 00149 00152 inline PDate operator-(const PDate& pdate, int ndays) 00153 { 00154 return PDate(pdate.toJulianDay()-ndays); 00155 } 00156 00157 00158 inline ostream& operator<<(ostream& os, const PDate& date) 00159 { 00160 os << date.info(); 00161 return os; 00162 } 00163 00166 float date_to_float(const PDate& t); 00167 00168 PDate float_to_date(float f); 00169 00170 inline PDate float_to_date(double d) 00171 { return float_to_date(float(d)); } 00172 00173 } // end of namespace PLearn 00174 00175 #endif

Generated on Tue Aug 17 16:01:03 2004 for PLearn by doxygen 1.3.7