PLearn 0.1
|
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 10319 2010-02-01 19:48:01Z ducharme $ 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 #include <plearn/io/PStream.h> 00054 #include <plearn/base/TypeTraits.h> 00055 00056 namespace PLearn { 00057 using namespace std; 00058 00064 class PDate 00065 { 00066 public: 00067 static PDate lastDateOfMonth(int year, int month); // Builds a PDate 00068 static unsigned char lastDayOfMonth(int year, int month); 00069 00070 public: 00071 short year; 00072 unsigned char month; 00073 unsigned char day; 00074 00076 PDate(); 00077 00078 PDate(short the_year, unsigned char the_month, unsigned char the_day) 00079 :year(the_year), month(the_month), day(the_day) {} 00080 00081 PDate(int the_year, int the_month, int the_day) 00082 :year(the_year), month(int(the_month)), day(int(the_day)) {} 00083 00086 PDate(int julian_day); 00087 00091 PDate(string date,bool invalid_value_as_missing=false); 00092 00094 bool isMissing() const; 00095 void setMissing(); 00096 00098 bool isValid() const; 00099 PDate lastDateOfMonth() const { return PDate::lastDateOfMonth(year, month); } 00100 unsigned char lastDayOfMonth() const { return PDate::lastDayOfMonth(year, month); } 00101 00102 string info() const; 00103 00105 bool operator==(const PDate& rhs) const { 00106 return year == rhs.year && month == rhs.month && day == rhs.day; 00107 } 00108 00109 bool operator!=(const PDate& rhs) const { 00110 return ! (*this == rhs); 00111 } 00112 00113 bool operator<(const PDate& rhs) const { 00114 return year < rhs.year || 00115 (year == rhs.year && month < rhs.month) || 00116 (year == rhs.year && month == rhs.month && day < rhs.day); 00117 } 00118 00119 bool operator<=(const PDate& rhs) const { 00120 return *this == rhs || *this < rhs; 00121 } 00122 00123 bool operator>(const PDate& rhs) const { 00124 return ! (*this <= rhs); 00125 } 00126 00127 bool operator>=(const PDate& rhs) const { 00128 return ! (*this < rhs); 00129 } 00130 00135 int toJulianDay() const; 00136 00137 //0=monday ... 6=sunday 00138 int dayOfWeek() const 00139 { return toJulianDay()%7; } 00140 00141 // 0 = january 1 of the year 00142 int dayOfYear() const; 00143 00145 int weekNumber() const; 00146 00147 static PDate today() 00148 { 00149 time_t now(time(0)); 00150 tm* snow = localtime(&now); 00151 return PDate(1900+snow->tm_year, 1+snow->tm_mon, snow->tm_mday); 00152 } 00153 00154 }; 00155 00157 inline int operator-(const PDate& to_date, const PDate& from_date) 00158 { 00159 return to_date.toJulianDay() - from_date.toJulianDay(); 00160 } 00161 00163 inline PDate operator+(const PDate& pdate, int ndays) 00164 { 00165 return PDate(pdate.toJulianDay()+ndays); 00166 } 00167 00170 inline PDate operator-(const PDate& pdate, int ndays) 00171 { 00172 return PDate(pdate.toJulianDay()-ndays); 00173 } 00174 00175 00176 inline ostream& operator<<(ostream& os, const PDate& date) 00177 { 00178 os << date.info(); 00179 return os; 00180 } 00181 00183 PStream& operator<<(PStream& out, const PDate& date); 00184 00186 PStream& operator>>(PStream& in, PDate& date); 00187 00188 00191 inline int add_months_to_date(int xyymmdd, int nmonths) // x=c or x=yy 00192 { 00193 int xyy = xyymmdd/10000; 00194 int mmdd = xyymmdd%10000; 00195 int mm = mmdd/100; 00196 int dd = mmdd%100; 00197 00198 int monthpos = xyy*12+(mm-1)+nmonths; 00199 xyy = monthpos/12; 00200 mm = 1+monthpos%12; 00201 return xyy*10000+mm*100+dd; 00202 } 00203 00207 float date_to_float(const PDate& t); 00208 00209 PDate float_to_date(float f); 00210 00211 inline PDate float_to_date(double d) 00212 { return float_to_date(float(d)); } 00213 00214 00218 double date_to_double(const PDate& t); 00219 00220 PDate double_to_date(double d); 00221 00222 DECLARE_TYPE_TRAITS(PDate); 00223 00224 } // end of namespace PLearn 00225 00226 #endif 00227 00228 00229 /* 00230 Local Variables: 00231 mode:c++ 00232 c-basic-offset:4 00233 c-file-style:"stroustrup" 00234 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00235 indent-tabs-mode:nil 00236 fill-column:79 00237 End: 00238 */ 00239 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :