37static char rcsid[] not_used =
"$Id$";
47#include <libdap/Error.h>
48#include "DODS_Date_Time.h"
50#include <libdap/debug.h>
54#define seconds_per_day 86400.0
59extract_argument(BaseType *arg)
62 if (arg->type() != dods_str_c)
63 throw Error(malformed_expr,
64 "The Projection function requires a DODS string-type argument.");
69 arg->buf2val((
void **)&sp);
70 string s = sp->c_str();
73 DBG(cerr <<
"s: " << s << endl);
86 return _time.OK() && _date.OK();
111 set(y, m, d, hh, mm, ss,
gmt);
117 set(y, yd, hh, mm, ss,
gmt);
132DODS_Date_Time::parse_fractional_time(
string dec_year)
135 double d_year_day, d_hr_day, d_min_day, d_sec_day;
136 int i_year, i_year_day, i_hr_day, i_min_day, i_sec_day;
140 double d_year = strtod(dec_year.c_str(), 0);
142 i_year = (
int)d_year;
143 double year_fraction = d_year - i_year;
145 secs_in_year = days_in_year(i_year) * seconds_per_day;
150 d_year_day = (secs_in_year * year_fraction)/seconds_per_day + 1;
151 i_year_day = (
int)d_year_day;
156 d_hr_day = ((d_year_day - i_year_day)*seconds_per_day) / seconds_per_hour;
157 i_hr_day = (
int)d_hr_day;
162 d_min_day = ((d_hr_day - i_hr_day)*seconds_per_hour) / seconds_per_minute;
163 i_min_day = (
int)d_min_day;
168 d_sec_day = (d_min_day - i_min_day)*seconds_per_minute;
169 i_sec_day = (
int)d_sec_day;
175 if ((d_sec_day - i_sec_day) >= .5) i_sec_day++;
177 if ( i_sec_day == 60 ) {
180 if ( i_min_day == 60 ) {
183 if ( i_hr_day == 24 ) {
186 if ( i_year_day == (days_in_year(i_year) + 1)) {
194 _date.set((
int)i_year, (
int)i_year_day);
195 _time.set((
int)i_hr_day, (
int)i_min_day, (
double)i_sec_day);
206 if (date_time.find(
".") != string::npos) {
207 parse_fractional_time(date_time);
211 size_t i = date_time.find(
":");
212 string date_part = date_time.substr(0, i);
213 string time_part = date_time.substr(i+1, date_time.size());
215 _date.set(date_part);
216 _time.set(time_part);
225 set(extract_argument(date_time));
232 _time.set(hh, mm, ss,
gmt);
241 _time.set(hh, mm, ss,
gmt);
255 return _date.month();
267 return _date.day_number();
273 return _time.hours();
279 return _time.minutes();
285 return _time.seconds();
299 return _date.get() +
":" + _time.get(
gmt);
301 return _date.get(yd) +
":" + _time.get(
gmt);
306 double decday = (_date.fraction()
307 + _time.fraction()/days_in_year(_date.year()));
315 throw Error(unknown_error,
"Invalid date format");
317 assert(
"Invalid date format" &&
false);
325 return _date.julian_day() + _time.seconds_since_midnight()/seconds_per_day;
332 tm_rec.tm_mday = _date.day();
333 tm_rec.tm_mon = _date.month() - 1;
334 tm_rec.tm_year = _date.year() - 1900;
335 tm_rec.tm_hour = _time.hours();
336 tm_rec.tm_min = _time.minutes();
337 tm_rec.tm_sec = (
int)_time.seconds();
338 tm_rec.tm_isdst = -1;
340 return mktime(&tm_rec);
346 return _time.get_epsilon();
352 _time.set_epsilon(eps);
358 return t1._date == t2._date && t1._time == t2._time;
364 return t1._date != t2._date || t1._time != t2._time;
370 return t1._date < t2._date
371 || (t1._date == t2._date && t1._time < t2._time);
377 return t1._date > t2._date
378 || (t1._date == t2._date && t1._time > t2._time);
384 return t1 == t2 || t1 < t2;
390 return t1 == t2 || t1 > t2;
406main(
int argc,
char *argv[])
414 dt.set_date_time(argv[1]);
417 dt.set_date_time(argv[1]);
418 dt2.set_date_time(argv[2]);
421 dt.set_date_time(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]),
422 atoi(argv[4]), atof(argv[5]));
425 dt.set_date_time(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]),
426 atoi(argv[4]), atoi(argv[5]), atof(argv[6]));
429 cerr <<
"Wrong number of arguments!" << endl;
434 cout <<
"True: dt < dt2" << endl;
436 cout <<
"False: dt < dt2" << endl;
439 cout <<
"True: dt > dt2" << endl;
441 cout <<
"False: dt > dt2" << endl;
444 cout <<
"True: dt <= dt2" << endl;
446 cout <<
"False: dt <= dt2" << endl;
449 cout <<
"True: dt >= dt2" << endl;
451 cout <<
"False: dt >= dt2" << endl;
454 cout <<
"True: dt == dt2" << endl;
456 cout <<
"False: dt == dt2" << endl;
459 cout <<
"True: dt != dt2" << endl;
461 cout <<
"False: dt != dt2" << endl;
463 cout <<
"YMD: " << dt.ymd_date_time() << endl;
464 cout <<
"YD: " << dt.yd_date_time() << endl;
465 cout <<
"Julian day: " << dt.
julian_day() << endl;
466 cout <<
"Seconds: " << dt.
unix_time() << endl;
double julian_day() const
friend int operator>=(DODS_Date_Time &t1, DODS_Date_Time &t2)
Greater-than or Equal-to.
string get(date_format format=ymd, bool gmt=true) const
friend int operator==(DODS_Date_Time &t1, DODS_Date_Time &t2)
Equality.
double get_epsilon() const
void set_epsilon(double eps)
void set(DODS_Date d, DODS_Time t)
friend int operator<(DODS_Date_Time &t1, DODS_Date_Time &t2)
Less-than.
friend int operator!=(DODS_Date_Time &t1, DODS_Date_Time &t2)
Inequality.
friend int operator<=(DODS_Date_Time &t1, DODS_Date_Time &t2)
Less-than or Equal-to.
friend int operator>(DODS_Date_Time &t1, DODS_Date_Time &t2)
Greater-than.