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
#include "stringutils.h"
00041
#include "PDateTime.h"
00042
#include "general.h"
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
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;
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
00169
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 }
00184