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

PDateTime.cc

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 00003 // PDateTime 00004 // Copyright (c) 2002 by Nicolas Chapados 00005 00006 // Redistribution and use in source and binary forms, with or without 00007 // modification, are permitted provided that the following conditions are met: 00008 // 00009 // 1. Redistributions of source code must retain the above copyright 00010 // notice, this list of conditions and the following disclaimer. 00011 // 00012 // 2. Redistributions in binary form must reproduce the above copyright 00013 // notice, this list of conditions and the following disclaimer in the 00014 // documentation and/or other materials provided with the distribution. 00015 // 00016 // 3. The name of the authors may not be used to endorse or promote 00017 // products derived from this software without specific prior written 00018 // permission. 00019 // 00020 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00021 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00022 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00023 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00024 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00025 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00026 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00027 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00028 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00029 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00030 // 00031 // This file is part of the PLearn library. For more information on the PLearn 00032 // library, go to the PLearn Web site at www.plearn.org 00033 00034 /* ******************************************************* 00035 * $Id: PDateTime.cc,v 1.7 2004/07/21 19:55:18 chapados Exp $ 00036 * This file is part of the PLearn library. 00037 ******************************************************* */ 00038 00039 //#include <limits.h> // from stdc 00040 #include "stringutils.h" // For tostring. 00041 #include "PDateTime.h" 00042 #include "general.h" // for MISSING_VALUE 00043 00044 namespace PLearn { 00045 using namespace std; 00046 00047 PDateTime::PDateTime() 00048 { 00049 setMissing(); 00050 } 00051 00052 PDateTime::PDateTime(double julian_day) 00053 { 00054 int jw = (int)((julian_day - 1867216.25)/36524.25); 00055 int jx = (int)(jw/4); 00056 int ja = (int)(julian_day + 1 + jw - jx); 00057 int jb = ja + 1524; 00058 int jc = (int)((jb - 122.1)/365.25); 00059 int jd = (int)(365.25*jc); 00060 int je = (int)((jb - jd)/30.6001); 00061 int jf = (int)(30.6001*je); 00062 00063 day = jb - jd - jf; 00064 month = (je>13) ? je-13 : je-1; 00065 year = (month>2) ? jc-4716 : jc-4715; 00066 00067 double fraction = julian_day - floor(julian_day); 00068 int hh,mm,ss; 00069 double_to_hhmmss(fraction,hh,mm,ss); 00070 hour = hh; 00071 min = mm; 00072 sec = ss; 00073 } 00074 00075 PDateTime::PDateTime(string date) 00076 { 00077 // Parse either "YYYY/MM/DD" or "YYYY/MM/DD hh:mm:ss" format 00078 if ((date.size() == 10 || date.size() == 19) && 00079 date[4] == '/' && date[7] == '/' && 00080 (date.size() == 19? date[13] == ':' && date[16] == ':' : true)) { 00081 year = toint(date.substr(0,4)); 00082 month = toint(date.substr(5,2)); 00083 day = toint(date.substr(8,2)); 00084 if (date.size() == 10) 00085 hour = min = sec = 0; 00086 else { 00087 hour = toint(date.substr(11,2)); 00088 min = toint(date.substr(14,2)); 00089 sec = toint(date.substr(17,2)); 00090 } 00091 } 00092 else 00093 PLERROR("PDateTime::PDateTime: the passed date-time string is not in " 00094 "\"YYYY/MM/DD\" or \"YYYY/MM/DD hh:mm:ss\" format"); 00095 } 00096 00097 bool PDateTime::isMissing() const 00098 { 00099 return year == SHRT_MIN && month == 0 && day == 0; 00100 } 00101 00102 void PDateTime::setMissing() 00103 { 00104 year = SHRT_MIN; 00105 month = 0; 00106 day = 0; 00107 } 00108 00109 string PDateTime::info() const 00110 { 00111 return tostring(year)+ slash+ 00112 right(tostring(int(month)), 2, '0') + slash+ 00113 right(tostring(int(day)), 2, '0') + " "+ 00114 right(tostring(int(hour)), 2, '0') + ":"+ 00115 right(tostring(int(min)), 2, '0') + ":"+ 00116 right(tostring(int(sec)), 2, '0'); 00117 } 00118 00119 double PDateTime::toJulianDay() const 00120 { 00121 if (year < 1582) 00122 PLERROR("toJulianDay works safely only for year > 1581 (%d)", year); 00123 00124 double fraction = hhmmss_to_double(hour,min,sec); 00125 00126 int jy = (month>2) ? year : year-1; 00127 int jm = (month>2) ? month : month+12; 00128 int ja = (int)(jy/100); 00129 int jb = (int)(ja/4); 00130 int jc = 2 - ja + jb; 00131 int je = (int)(365.25*(jy + 4716)); 00132 int jf = (int)(30.6001*(jm + 1)); 00133 00134 return jc + day + je + jf - 1524 + fraction; 00135 } 00136 00137 double datetime_to_double(const PDateTime& t) 00138 { 00139 if (t.isMissing()) 00140 return MISSING_VALUE; 00141 else 00142 return double((t.year-1900)*10000 + t.month*100 + t.day) + 00143 hhmmss_to_double(t.hour,t.min,t.sec); 00144 } 00145 00146 PDateTime double_to_datetime(double f) 00147 { 00148 PDateTime date; // missing by default 00149 if (! is_missing(f)) { 00150 long d = long(f); 00151 double fraction = f-d; 00152 date.year = 1900 + d/10000; 00153 d %= 10000; 00154 date.month = d/100; 00155 date.day = d%100; 00156 00157 int hh,mm,ss; 00158 double_to_hhmmss(fraction,hh,mm,ss); 00159 date.hour = hh; 00160 date.min = mm; 00161 date.sec = ss; 00162 } 00163 return date; 00164 } 00165 00166 double hhmmss_to_double(int hh, int mm, int ss) 00167 { 00168 // There are 1440 minutes in a day. 00169 // There are 86400 seconds in a day. 00170 return double(hh)/24. + double(mm)/1440. + double(ss)/86400; 00171 } 00172 00173 void double_to_hhmmss(double fraction, int& hh, int& mm, int& ss) 00174 { 00175 hh = int(fraction *= 24); 00176 fraction -= hh; 00177 mm = int(fraction *= 60); 00178 fraction -= mm; 00179 ss = int(fraction * 60); 00180 } 00181 00182 00183 } // end of namespace PLearn 00184

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