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