00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
#include "stringutils.h"
00043
#include "PDate.h"
00044
00045
#include <plearn/math/pl_math.h>
00046
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
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
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
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;
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 }