51DODS_Date_Factory::DODS_Date_Factory(DDS &dds,
const string &attribute_name)
57 AttrTable *at = dds.get_attr_table().find_container(attribute_name);
59 throw Error(unknown_error,
60 string(
"DODS_Date_Factory requires that the ")
62 +
string(
"DODS_Date attribute be present."));
64 string year_name = at->get_attr(
"year_variable");
65 string year_base = at->get_attr(
"year_base");
66 string month_name = at->get_attr(
"month_variable");
67 string day_name = at->get_attr(
"day_variable");
68 string year_day_name = at->get_attr(
"year_day_variable");
69 string month_day_name = at->get_attr(
"month_day_const");
73 if (year_day_name ==
"" && day_name !=
"")
75 else if (year_day_name !=
"" && day_name ==
"")
77 else if (year_day_name ==
"" && day_name ==
"")
80 throw Error(unknown_error,
81"DODS_Date_Factory requires that one, and only one, of the attributes\n\
82day_variable or year_day_variable be present.");
89 const char *c = year_base.c_str();
91 _year_base = strtol(c, &c2, 0);
92 if (c == c2 || _year_base == DODS_LONG_MAX || _year_base == DODS_LONG_MIN)
93 throw Error(unknown_error,
94"The year_base attribute value cannot be converted to a valid integer.");
100 if (_format == ym && month_day_name !=
"")
102 const char *c = month_day_name.c_str();
104 _month_day = strtol(c, &c2, 0);
105 if (c == c2 || _month_day == DODS_LONG_MAX || _month_day == DODS_LONG_MIN)
106 throw Error(unknown_error,
107"The month_day attribute value cannot be converted to a valid integer.");
113 _year = dds.var(year_name);
114 if ((_year->type() != dods_int16_c) && (_year->type() != dods_uint16_c) &&
115 (_year->type() != dods_int32_c) && (_year->type() != dods_uint32_c))
116 throw Error(unknown_error,
"DODS_Date_Factory: The variable used for the year must be an integer.");
120 _month = dds.var(month_name);
121 if (!is_integer_type(_month))
122 throw Error(unknown_error,
123"DODS_Date_Factory: The variable used for the month must be an integer.");
125 _day = dds.var(day_name);
126 if (!is_integer_type(_day))
127 throw Error(unknown_error,
128"DODS_Date_Factory: The variable used for days must be an integer.");
136 _year_day = dds.var(year_day_name);
137 if (!is_integer_type(_year))
138 throw Error(unknown_error,
139"DODS_Date_Factory: The variable used for the year-day must be an integer.");
143 _month = dds.var(month_name);
144 if (!is_integer_type(_month))
145 throw Error(unknown_error,
146 "DODS_Date_Factory: The variable used for the month must be an integer.");
153 throw Error(unknown_error,
154"DODS_Date_Factory: Not able to figure out the date format.");
162 dods_uint32 year = get_integer_value(_year);
166 dods_uint32 month = get_integer_value(_month);
167 dods_uint32 day = get_integer_value(_day);
169 return DODS_Date(year + _year_base, month, day);
174 dods_uint32 year_day = get_integer_value(_year_day);
176 return DODS_Date(year + _year_base, year_day);
181 dods_uint32 month = get_integer_value(_month);
183 int day = _month_day;
184 date_format fmt = ym;
186 return DODS_Date(year + _year_base, month, day, fmt);
191 throw Error(unknown_error,
192"DODS_Date_Factory: Unknown date format, should never get here!");