37 static char rcsid[]
not_used =
"$Id$";
54 #define seconds_per_day 86400.0
59 extract_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);
132 DODS_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;
151 i_year_day = (int)d_year_day;
157 i_hr_day = (int)d_hr_day;
163 i_min_day = (int)d_min_day;
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 ) {
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();
273 return _time.
hours();
299 return _date.
get() +
":" + _time.
get(gmt);
301 return _date.
get(
yd) +
":" + _time.
get(gmt);
315 throw Error(unknown_error,
"Invalid date format");
317 assert(
"Invalid date format" &&
false);
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);
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;
393 #ifdef DATE_TIME_TEST
406 main(
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;
int operator<(DODS_Date_Time &t1, DODS_Date_Time &t2)
time_t unix_time() const
Return the number of seconds since 00:00:00 UTC 1 Jan 1970.
string get(bool gmt=true) const
Get the string representation of time.
double days_in_year(int year)
How many days are in the given Gregorian year?
int operator<=(DODS_Date_Time &t1, DODS_Date_Time &t2)
int operator>(DODS_Date_Time &t1, DODS_Date_Time &t2)
void set(string date)
Parse the string and assign the value to this object.
double seconds_since_midnight() const
Get the number of seconds since midnight.
double get_epsilon() const
Get the value of epsilon used for equality tests of time.
bool OK() const
Class invariant.
void set(DODS_Date d, DODS_Time t)
Set an instance using DODS_Date and DODS_Time objects.
string get(date_format format=ymd, bool gmt=true) const
Get the string representation of the date/time.
double get_epsilon() const
Get the value of epsilon used for equality tests.
double fraction() const
Get the time as a fraction of a day.
DODS_Date_Time()
Create an empty instance.
const double seconds_per_hour
int operator!=(DODS_Date_Time &t1, DODS_Date_Time &t2)
int operator==(DODS_Date_Time &t1, DODS_Date_Time &t2)
void set_epsilon(double eps)
Set the value of epsilon used for equality tests of time.
const double seconds_per_minute
int main(int argc, char **argv)
date_format
Constants used to denote different supported date formats.
void set(string time)
Set the value by parsing the string #time#.
The DODS Date/Time class is used to represent and compute with combined date and time values...
int operator>=(DODS_Date_Time &t1, DODS_Date_Time &t2)
string get(date_format format=ymd) const
Get the string representation for this date.
void set_epsilon(double eps)
Set the value of epsilon used for equality tests.
double julian_day() const