1 | /* ************************************************************************** * |
---|
2 | * Copyright © IPSL/LSCE, xios, Avril 2010 - Octobre 2011 * |
---|
3 | * ************************************************************************** */ |
---|
4 | |
---|
5 | #include <boost/multi_array.hpp> |
---|
6 | |
---|
7 | #include "xios.hpp" |
---|
8 | |
---|
9 | #include "attribute_template.hpp" |
---|
10 | #include "object_template.hpp" |
---|
11 | #include "group_template.hpp" |
---|
12 | |
---|
13 | #include "calendar_type.hpp" |
---|
14 | |
---|
15 | #include "icutil.hpp" |
---|
16 | #include "icdate.hpp" |
---|
17 | #include "exception.hpp" |
---|
18 | #include "calendar_util.hpp" |
---|
19 | #include "timer.hpp" |
---|
20 | #include "context.hpp" |
---|
21 | #include "context_client.hpp" |
---|
22 | |
---|
23 | static const xios::CCalendar& getCalendar(const std::string& idFunc) |
---|
24 | { |
---|
25 | const xios::CContext* context = CContext::getCurrent(); |
---|
26 | if (!context) |
---|
27 | ERROR(idFunc, << "Impossible to do calendar operations: no current context available."); |
---|
28 | const std::shared_ptr<xios::CCalendar> cal = context->getCalendar(); |
---|
29 | if (!cal) |
---|
30 | ERROR(idFunc, << "Impossible to do calendar operations: no calendar was defined."); |
---|
31 | return *cal; |
---|
32 | } |
---|
33 | |
---|
34 | extern "C" |
---|
35 | { |
---|
36 | long long int cxios_date_convert_to_seconds(cxios_date date_c) |
---|
37 | TRY |
---|
38 | { |
---|
39 | xios::CDate date = xios::CDate(getCalendar("long long int cxios_date_convert_to_seconds(cxios_date date_c)"), |
---|
40 | date_c.year, date_c.month, date_c.day, |
---|
41 | date_c.hour, date_c.minute, date_c.second); |
---|
42 | return date; |
---|
43 | } |
---|
44 | CATCH_DUMP_STACK |
---|
45 | |
---|
46 | void cxios_date_convert_to_string(cxios_date date_c, char* str, int str_size) |
---|
47 | TRY |
---|
48 | { |
---|
49 | xios::CDate date = xios::CDate(getCalendar("void cxios_date_convert_to_string(cxios_date date_c, char* str, int str_size)"), |
---|
50 | date_c.year, date_c.month, date_c.day, |
---|
51 | date_c.hour, date_c.minute, date_c.second); |
---|
52 | |
---|
53 | if (!string_copy(date.toString(), str, str_size)) |
---|
54 | ERROR("void cxios_date_convert_to_string(cxios_date date_c, char* str, int str_size)", << "Input string is too short"); |
---|
55 | } |
---|
56 | CATCH_DUMP_STACK |
---|
57 | |
---|
58 | cxios_date cxios_date_convert_from_string(const char* str, int str_size) |
---|
59 | TRY |
---|
60 | { |
---|
61 | std::string date_str; |
---|
62 | xios::CDate date; |
---|
63 | |
---|
64 | if (cstr2string(str, str_size, date_str)) |
---|
65 | date = xios::CDate::FromString(date_str, |
---|
66 | getCalendar("cxios_date cxios_date_convert_from_string(const char* str, int str_size)")); |
---|
67 | |
---|
68 | return { date.getYear(), date.getMonth(), date.getDay(), date.getHour(), date.getMinute(), date.getSecond() }; |
---|
69 | } |
---|
70 | CATCH_DUMP_STACK |
---|
71 | |
---|
72 | cxios_date cxios_date_add_duration(cxios_date date_c, cxios_duration dur_c) |
---|
73 | TRY |
---|
74 | { |
---|
75 | xios::CDate date = xios::CDate(getCalendar("cxios_date cxios_date_add_duration(cxios_date date_c, cxios_duration dur_c)"), |
---|
76 | date_c.year, date_c.month, date_c.day, |
---|
77 | date_c.hour, date_c.minute, date_c.second); |
---|
78 | xios::CDuration dur( dur_c.year, dur_c.month, dur_c.day, dur_c.hour, dur_c.minute, dur_c.second, dur_c.timestep ); |
---|
79 | xios::CDate res = date + dur; |
---|
80 | return { res.getYear(), res.getMonth(), res.getDay(), res.getHour(), res.getMinute(), res.getSecond() }; |
---|
81 | } |
---|
82 | CATCH_DUMP_STACK |
---|
83 | |
---|
84 | cxios_date cxios_date_sub_duration(cxios_date date_c, cxios_duration dur_c) |
---|
85 | TRY |
---|
86 | { |
---|
87 | xios::CDate date = xios::CDate(getCalendar("cxios_date cxios_date_sub_duration(cxios_date date_c, cxios_duration dur_c)"), |
---|
88 | date_c.year, date_c.month, date_c.day, |
---|
89 | date_c.hour, date_c.minute, date_c.second); |
---|
90 | xios::CDuration dur( dur_c.year, dur_c.month, dur_c.day, dur_c.hour, dur_c.minute, dur_c.second, dur_c.timestep ); |
---|
91 | |
---|
92 | xios::CDate res = date - dur; |
---|
93 | return { res.getYear(), res.getMonth(), res.getDay(), res.getHour(), res.getMinute(), res.getSecond() }; |
---|
94 | } |
---|
95 | CATCH_DUMP_STACK |
---|
96 | |
---|
97 | cxios_duration cxios_date_sub(cxios_date date1_c, cxios_date date2_c) |
---|
98 | TRY |
---|
99 | { |
---|
100 | xios::CDate date1 = xios::CDate(getCalendar("cxios_duration cxios_date_sub(cxios_date date1_c, cxios_date date2_c)"), |
---|
101 | date1_c.year, date1_c.month, date1_c.day, |
---|
102 | date1_c.hour, date1_c.minute, date1_c.second); |
---|
103 | xios::CDate date2 = xios::CDate(getCalendar("cxios_duration cxios_date_sub(cxios_date date1_c, cxios_date date2_c)"), |
---|
104 | date2_c.year, date2_c.month, date2_c.day, |
---|
105 | date2_c.hour, date2_c.minute, date2_c.second); |
---|
106 | xios::CDuration res = date1 - date2; |
---|
107 | return { res.year, res.month, res.day, res.hour, res.minute, res.second, res.timestep }; |
---|
108 | } |
---|
109 | CATCH_DUMP_STACK |
---|
110 | |
---|
111 | bool cxios_date_eq(cxios_date date1_c, cxios_date date2_c) |
---|
112 | TRY |
---|
113 | { |
---|
114 | xios::CDate date1 = xios::CDate(getCalendar("bool cxios_date_eq(cxios_date date1_c, cxios_date date2_c)"), |
---|
115 | date1_c.year, date1_c.month, date1_c.day, |
---|
116 | date1_c.hour, date1_c.minute, date1_c.second); |
---|
117 | xios::CDate date2 = xios::CDate(getCalendar("bool cxios_date_eq(cxios_date date1_c, cxios_date date2_c)"), |
---|
118 | date2_c.year, date2_c.month, date2_c.day, |
---|
119 | date2_c.hour, date2_c.minute, date2_c.second); |
---|
120 | return (date1 == date2); |
---|
121 | } |
---|
122 | CATCH_DUMP_STACK |
---|
123 | |
---|
124 | bool cxios_date_neq(cxios_date date1_c, cxios_date date2_c) |
---|
125 | TRY |
---|
126 | { |
---|
127 | xios::CDate date1 = xios::CDate(getCalendar("bool cxios_date_eq(cxios_date date1_c, cxios_date date2_c)"), |
---|
128 | date1_c.year, date1_c.month, date1_c.day, |
---|
129 | date1_c.hour, date1_c.minute, date1_c.second); |
---|
130 | xios::CDate date2 = xios::CDate(getCalendar("bool cxios_date_eq(cxios_date date1_c, cxios_date date2_c)"), |
---|
131 | date2_c.year, date2_c.month, date2_c.day, |
---|
132 | date2_c.hour, date2_c.minute, date2_c.second); |
---|
133 | return (date1 != date2); |
---|
134 | } |
---|
135 | CATCH_DUMP_STACK |
---|
136 | |
---|
137 | bool cxios_date_lt(cxios_date date1_c, cxios_date date2_c) |
---|
138 | TRY |
---|
139 | { |
---|
140 | xios::CDate date1 = xios::CDate(getCalendar("bool cxios_date_eq(cxios_date date1_c, cxios_date date2_c)"), |
---|
141 | date1_c.year, date1_c.month, date1_c.day, |
---|
142 | date1_c.hour, date1_c.minute, date1_c.second); |
---|
143 | xios::CDate date2 = xios::CDate(getCalendar("bool cxios_date_eq(cxios_date date1_c, cxios_date date2_c)"), |
---|
144 | date2_c.year, date2_c.month, date2_c.day, |
---|
145 | date2_c.hour, date2_c.minute, date2_c.second); |
---|
146 | return (date1 < date2); |
---|
147 | } |
---|
148 | CATCH_DUMP_STACK |
---|
149 | |
---|
150 | bool cxios_date_le(cxios_date date1_c, cxios_date date2_c) |
---|
151 | TRY |
---|
152 | { |
---|
153 | xios::CDate date1 = xios::CDate(getCalendar("bool cxios_date_eq(cxios_date date1_c, cxios_date date2_c)"), |
---|
154 | date1_c.year, date1_c.month, date1_c.day, |
---|
155 | date1_c.hour, date1_c.minute, date1_c.second); |
---|
156 | xios::CDate date2 = xios::CDate(getCalendar("bool cxios_date_eq(cxios_date date1_c, cxios_date date2_c)"), |
---|
157 | date2_c.year, date2_c.month, date2_c.day, |
---|
158 | date2_c.hour, date2_c.minute, date2_c.second); |
---|
159 | return (date1 <= date2); |
---|
160 | } |
---|
161 | CATCH_DUMP_STACK |
---|
162 | |
---|
163 | bool cxios_date_gt(cxios_date date1_c, cxios_date date2_c) |
---|
164 | TRY |
---|
165 | { |
---|
166 | xios::CDate date1 = xios::CDate(getCalendar("bool cxios_date_eq(cxios_date date1_c, cxios_date date2_c)"), |
---|
167 | date1_c.year, date1_c.month, date1_c.day, |
---|
168 | date1_c.hour, date1_c.minute, date1_c.second); |
---|
169 | xios::CDate date2 = xios::CDate(getCalendar("bool cxios_date_eq(cxios_date date1_c, cxios_date date2_c)"), |
---|
170 | date2_c.year, date2_c.month, date2_c.day, |
---|
171 | date2_c.hour, date2_c.minute, date2_c.second); |
---|
172 | return (date1 > date2); |
---|
173 | } |
---|
174 | CATCH_DUMP_STACK |
---|
175 | |
---|
176 | bool cxios_date_ge(cxios_date date1_c, cxios_date date2_c) |
---|
177 | TRY |
---|
178 | { |
---|
179 | xios::CDate date1 = xios::CDate(getCalendar("bool cxios_date_eq(cxios_date date1_c, cxios_date date2_c)"), |
---|
180 | date1_c.year, date1_c.month, date1_c.day, |
---|
181 | date1_c.hour, date1_c.minute, date1_c.second); |
---|
182 | xios::CDate date2 = xios::CDate(getCalendar("bool cxios_date_eq(cxios_date date1_c, cxios_date date2_c)"), |
---|
183 | date2_c.year, date2_c.month, date2_c.day, |
---|
184 | date2_c.hour, date2_c.minute, date2_c.second); |
---|
185 | return (date1 >= date2); |
---|
186 | } |
---|
187 | CATCH_DUMP_STACK |
---|
188 | |
---|
189 | int cxios_date_get_second_of_year(cxios_date date_c) |
---|
190 | TRY |
---|
191 | { |
---|
192 | xios::CDate date = xios::CDate(getCalendar("int cxios_date_get_second_of_year(cxios_date date_c)"), |
---|
193 | date_c.year, date_c.month, date_c.day, |
---|
194 | date_c.hour, date_c.minute, date_c.second); |
---|
195 | return date.getSecondOfYear(); |
---|
196 | } |
---|
197 | CATCH_DUMP_STACK |
---|
198 | |
---|
199 | double cxios_date_get_day_of_year(cxios_date date_c) |
---|
200 | TRY |
---|
201 | { |
---|
202 | xios::CDate date = xios::CDate(getCalendar("int cxios_date_get_day_of_year(cxios_date date_c)"), |
---|
203 | date_c.year, date_c.month, date_c.day, |
---|
204 | date_c.hour, date_c.minute, date_c.second); |
---|
205 | return date.getDayOfYear(); |
---|
206 | } |
---|
207 | CATCH_DUMP_STACK |
---|
208 | |
---|
209 | double cxios_date_get_fraction_of_year(cxios_date date_c) |
---|
210 | TRY |
---|
211 | { |
---|
212 | xios::CDate date = xios::CDate(getCalendar("int cxios_date_get_fraction_of_year(cxios_date date_c)"), |
---|
213 | date_c.year, date_c.month, date_c.day, |
---|
214 | date_c.hour, date_c.minute, date_c.second); |
---|
215 | return date.getFractionOfYear(); |
---|
216 | } |
---|
217 | CATCH_DUMP_STACK |
---|
218 | |
---|
219 | int cxios_date_get_second_of_day(cxios_date date_c) |
---|
220 | TRY |
---|
221 | { |
---|
222 | xios::CDate date = xios::CDate(getCalendar("int cxios_date_get_second_of_day(cxios_date date_c)"), |
---|
223 | date_c.year, date_c.month, date_c.day, |
---|
224 | date_c.hour, date_c.minute, date_c.second); |
---|
225 | return date.getSecondOfDay(); |
---|
226 | } |
---|
227 | CATCH_DUMP_STACK |
---|
228 | |
---|
229 | double cxios_date_get_fraction_of_day(cxios_date date_c) |
---|
230 | TRY |
---|
231 | { |
---|
232 | xios::CDate date = xios::CDate(getCalendar("int cxios_date_get_fraction_of_day(cxios_date date_c)"), |
---|
233 | date_c.year, date_c.month, date_c.day, |
---|
234 | date_c.hour, date_c.minute, date_c.second); |
---|
235 | return date.getFractionOfDay(); |
---|
236 | } |
---|
237 | CATCH_DUMP_STACK |
---|
238 | } // extern "C" |
---|