source: XIOS/dev/dev_olga/src/interface/c/icdate.cpp @ 1612

Last change on this file since 1612 was 1612, checked in by oabramkina, 5 years ago

Dev: adding exception handling.

To activate it, compilation flag -DXIOS_EXCEPTION should be added.

  • Property copyright set to
    Software name : XIOS (Xml I/O Server)
    http://forge.ipsl.jussieu.fr/ioserver
    Creation date : January 2009
    Licence : CeCCIL version2
    see license file in root directory : Licence_CeCILL_V2-en.txt
    or http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
    Holder : CEA/LSCE (Laboratoire des Sciences du CLimat et de l'Environnement)
    CNRS/IPSL (Institut Pierre Simon Laplace)
    Project Manager : Yann Meurdesoif
    yann.meurdesoif@cea.fr
  • Property svn:eol-style set to native
File size: 10.3 KB
RevLine 
[325]1/* ************************************************************************** *
[335]2 *      Copyright © IPSL/LSCE, xios, Avril 2010 - Octobre 2011         *
[325]3 * ************************************************************************** */
4
5#include <boost/multi_array.hpp>
6
[591]7#include "xios.hpp"
[325]8
[352]9#include "attribute_template.hpp"
10#include "object_template.hpp"
11#include "group_template.hpp"
[325]12
13#include "calendar_type.hpp"
14
15#include "icutil.hpp"
[545]16#include "icdate.hpp"
17#include "exception.hpp"
18#include "calendar_util.hpp"
[347]19#include "timer.hpp"
[352]20#include "context.hpp"
[403]21#include "context_client.hpp"
[325]22
[545]23static 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.");
[1542]28  const std::shared_ptr<xios::CCalendar> cal = context->getCalendar();
[545]29  if (!cal)
30    ERROR(idFunc, << "Impossible to do calendar operations: no calendar was defined.");
31  return *cal;
32}
33
[325]34extern "C"
35{
[545]36  long long int cxios_date_convert_to_seconds(cxios_date date_c)
[1612]37  TRY
[545]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  }
[1612]44  CATCH_DUMP_STACK
[325]45
[794]46  void cxios_date_convert_to_string(cxios_date date_c, char* str, int str_size)
[1612]47  TRY
[794]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  }
[1612]56  CATCH_DUMP_STACK
[794]57
[801]58  cxios_date cxios_date_convert_from_string(const char* str, int str_size)
[1612]59  TRY
[801]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  }
[1612]70  CATCH_DUMP_STACK
[801]71
[545]72  cxios_date cxios_date_add_duration(cxios_date date_c, cxios_duration dur_c)
[1612]73  TRY
[545]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);
[1472]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 );
[545]79    xios::CDate res = date + dur;
80    return { res.getYear(), res.getMonth(), res.getDay(), res.getHour(), res.getMinute(), res.getSecond() };
81  }
[1612]82  CATCH_DUMP_STACK
[325]83
[545]84  cxios_date cxios_date_sub_duration(cxios_date date_c, cxios_duration dur_c)
[1612]85  TRY
[545]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);
[1472]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
[545]92    xios::CDate res = date - dur;
93    return { res.getYear(), res.getMonth(), res.getDay(), res.getHour(), res.getMinute(), res.getSecond() };
94  }
[1612]95  CATCH_DUMP_STACK
[545]96
97  cxios_duration cxios_date_sub(cxios_date date1_c, cxios_date date2_c)
[1612]98  TRY
[545]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  }
[1612]109  CATCH_DUMP_STACK
[545]110
111  bool cxios_date_eq(cxios_date date1_c, cxios_date date2_c)
[1612]112  TRY
[545]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  }
[1612]122  CATCH_DUMP_STACK
[545]123
124  bool cxios_date_neq(cxios_date date1_c, cxios_date date2_c)
[1612]125  TRY
[545]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  }
[1612]135  CATCH_DUMP_STACK
[545]136
137  bool cxios_date_lt(cxios_date date1_c, cxios_date date2_c)
[1612]138  TRY
[545]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  }
[1612]148  CATCH_DUMP_STACK
[545]149
150  bool cxios_date_le(cxios_date date1_c, cxios_date date2_c)
[1612]151  TRY
[545]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  }
[1612]161  CATCH_DUMP_STACK
[545]162
163  bool cxios_date_gt(cxios_date date1_c, cxios_date date2_c)
[1612]164  TRY
[545]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  }
[1612]174  CATCH_DUMP_STACK
[545]175
176  bool cxios_date_ge(cxios_date date1_c, cxios_date date2_c)
[1612]177  TRY
[545]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  }
[1612]187  CATCH_DUMP_STACK
[549]188
189  int cxios_date_get_second_of_year(cxios_date date_c)
[1612]190  TRY
[549]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  }
[1612]197  CATCH_DUMP_STACK
[549]198
199  double cxios_date_get_day_of_year(cxios_date date_c)
[1612]200  TRY
[549]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  }
[1612]207  CATCH_DUMP_STACK
[549]208
209  double cxios_date_get_fraction_of_year(cxios_date date_c)
[1612]210  TRY
[549]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  }
[1612]217  CATCH_DUMP_STACK
[549]218
219  int cxios_date_get_second_of_day(cxios_date date_c)
[1612]220  TRY
[549]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  }
[1612]227  CATCH_DUMP_STACK
[549]228
229  double cxios_date_get_fraction_of_day(cxios_date date_c)
[1612]230  TRY
[549]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  }
[1612]237  CATCH_DUMP_STACK
[325]238} // extern "C"
Note: See TracBrowser for help on using the repository browser.