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

PDate.cc

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 * $Id: PDate.cc,v 1.10 2004/08/04 13:17:35 tihocan Exp $ 00038 * This file is part of the PLearn library. 00039 ******************************************************* */ 00040 00041 //#include <limits.h> // from stdc 00042 #include "stringutils.h" 00043 #include "PDate.h" 00044 //#include "general.h" // for MISSING_VALUE 00045 #include <plearn/math/pl_math.h> 00046 //#include <ctype.h> 00047 00048 namespace PLearn { 00049 using namespace std; 00050 00051 PDate::PDate() 00052 { 00053 setMissing(); 00054 } 00055 00056 PDate::PDate(int julian_day) 00057 { 00058 int jw = (int)((julian_day - 1867216.25)/36524.25); 00059 int jx = (int)(jw/4); 00060 int ja = julian_day + 1 + jw - jx; 00061 int jb = ja + 1524; 00062 int jc = (int)((jb - 122.1)/365.25); 00063 int jd = (int)(365.25*jc); 00064 int je = (int)((jb - jd)/30.6001); 00065 int jf = (int)(30.6001*je); 00066 00067 day = jb - jd - jf; 00068 month = (je>13) ? je-13 : je-1; 00069 year = (month>2) ? jc-4716 : jc-4715; 00070 } 00071 00072 PDate::PDate(string date) 00073 { 00074 // Format "2003/01/27" 00075 if (date.size() == 10 && date[4] == '/' && date[7] == '/') 00076 { 00077 year = toint(date.substr(0,4)); 00078 month = toint(date.substr(5,2)); 00079 day = toint(date.substr(8,2)); 00080 } 00081 // Format "27JAN2003" 00082 else if(date.size()==9 && isupper(date[2]) && isupper(date[3]) && isupper(date[4])) 00083 { 00084 year = toint(date.substr(5,4)); 00085 day = toint(date.substr(0,2)); 00086 string mo = date.substr(2,3); 00087 if(mo=="JAN") 00088 month = 1; 00089 else if(mo=="FEB") 00090 month = 2; 00091 else if(mo=="MAR") 00092 month = 3; 00093 else if(mo=="APR") 00094 month = 4; 00095 else if(mo=="MAY") 00096 month = 5; 00097 else if(mo=="JUN") 00098 month = 6; 00099 else if(mo=="JUL") 00100 month = 7; 00101 else if(mo=="AUG") 00102 month = 8; 00103 else if(mo=="SEP") 00104 month = 9; 00105 else if(mo=="OCT") 00106 month = 10; 00107 else if(mo=="NOV") 00108 month = 11; 00109 else if(mo=="DEC") 00110 month = 12; 00111 else 00112 PLERROR("Invalid month string: %s",mo.c_str()); 00113 } 00114 00115 // Format "20020917" 00116 else if (date.size() == 8 && pl_isnumber(date)) 00117 { 00118 year = toint(date.substr(0,4)); 00119 month = toint(date.substr(4,2)); 00120 day = toint(date.substr(6,2)); 00121 if(year<1970 || year>3000 || month<1 || month>12 || day<1 || day>31) 00122 PLERROR("Invalid date string: %s",date.c_str()); 00123 } 00124 else 00125 PLERROR("PDate::PDate: the passed date string is not in a known format: %s", date.c_str()); 00126 } 00127 00128 bool PDate::isMissing() const 00129 { 00130 return year == SHRT_MIN && month == 0 && day == 0; 00131 } 00132 00133 void PDate::setMissing() 00134 { 00135 year = SHRT_MIN; 00136 month = 0; 00137 day = 0; 00138 } 00139 00140 string PDate::info() const 00141 { 00142 return tostring(year)+slash+ 00143 right(tostring(int(month)), 2, '0') +slash+ 00144 right(tostring(int(day)), 2, '0'); 00145 } 00146 00147 int PDate::toJulianDay() const 00148 { 00149 if (year < 1582) 00150 PLERROR("toJulianDay works safely only for year > 1581 (%d)", year); 00151 int jy = (month>2) ? year : year-1; 00152 int jm = (month>2) ? month : month+12; 00153 int ja = (int)(jy/100); 00154 int jb = (int)(ja/4); 00155 int jc = 2 - ja + jb; 00156 int je = (int)(365.25*(jy + 4716)); 00157 int jf = (int)(30.6001*(jm + 1)); 00158 00159 return jc + day + je + jf - 1524; 00160 } 00161 00162 float date_to_float(const PDate& t) 00163 { 00164 if (t.isMissing()) 00165 return MISSING_VALUE; 00166 else 00167 return float((t.year-1900)*10000 + t.month*100 + t.day); 00168 } 00169 00170 PDate float_to_date(float f) 00171 { 00172 PDate date; // missing by default 00173 if (! is_missing(f)) { 00174 long d = long(f); 00175 date.year = 1900 + d/10000; 00176 d %= 10000; 00177 date.month = d/100; 00178 date.day = d%100; 00179 } 00180 return date; 00181 } 00182 00183 } // end of namespace PLearn

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