source: XIOS/trunk/src/interface/c/icdate.cpp @ 794

Last change on this file since 794 was 794, checked in by rlacroix, 8 years ago

Fortran interface: Add functions to convert date and duration to string.

  • 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: 9.6 KB
Line 
1/* ************************************************************************** *
2 *      Copyright © IPSL/LSCE, xios, Avril 2010 - Octobre 2011         *
3 * ************************************************************************** */
4
5#include <boost/multi_array.hpp>
6#include <boost/shared_ptr.hpp>
7
8#include "xios.hpp"
9
10#include "attribute_template.hpp"
11#include "object_template.hpp"
12#include "group_template.hpp"
13
14#include "calendar_type.hpp"
15
16#include "icutil.hpp"
17#include "icdate.hpp"
18#include "exception.hpp"
19#include "calendar_util.hpp"
20#include "timer.hpp"
21#include "context.hpp"
22#include "context_client.hpp"
23
24static 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
35extern "C"
36{
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  }
44
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
55  cxios_date cxios_date_add_duration(cxios_date date_c, cxios_duration dur_c)
56  {
57    xios::CDate date = xios::CDate(getCalendar("cxios_date cxios_date_add_duration(cxios_date date_c, cxios_duration dur_c)"),
58                                   date_c.year, date_c.month, date_c.day,
59                                   date_c.hour, date_c.minute, date_c.second);
60    xios::CDuration dur = { dur_c.year, dur_c.month, dur_c.day,
61                            dur_c.hour, dur_c.minute, dur_c.second,
62                            dur_c.timestep };
63    xios::CDate res = date + dur;
64    return { res.getYear(), res.getMonth(), res.getDay(), res.getHour(), res.getMinute(), res.getSecond() };
65  }
66
67  cxios_date cxios_date_sub_duration(cxios_date date_c, cxios_duration dur_c)
68  {
69    xios::CDate date = xios::CDate(getCalendar("cxios_date cxios_date_sub_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  }
78
79  cxios_duration cxios_date_sub(cxios_date date1_c, cxios_date date2_c)
80  {
81    xios::CDate date1 = xios::CDate(getCalendar("cxios_duration cxios_date_sub(cxios_date date1_c, cxios_date date2_c)"),
82                                    date1_c.year, date1_c.month, date1_c.day,
83                                    date1_c.hour, date1_c.minute, date1_c.second);
84    xios::CDate date2 = xios::CDate(getCalendar("cxios_duration cxios_date_sub(cxios_date date1_c, cxios_date date2_c)"),
85                                    date2_c.year, date2_c.month, date2_c.day,
86                                    date2_c.hour, date2_c.minute, date2_c.second);
87    xios::CDuration res = date1 - date2;
88    return { res.year, res.month, res.day, res.hour, res.minute, res.second, res.timestep };
89  }
90
91  bool cxios_date_eq(cxios_date date1_c, cxios_date date2_c)
92  {
93    xios::CDate date1 = xios::CDate(getCalendar("bool cxios_date_eq(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("bool cxios_date_eq(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    return (date1 == date2);
100  }
101
102  bool cxios_date_neq(cxios_date date1_c, cxios_date date2_c)
103  {
104    xios::CDate date1 = xios::CDate(getCalendar("bool cxios_date_eq(cxios_date date1_c, cxios_date date2_c)"),
105                                    date1_c.year, date1_c.month, date1_c.day,
106                                    date1_c.hour, date1_c.minute, date1_c.second);
107    xios::CDate date2 = xios::CDate(getCalendar("bool cxios_date_eq(cxios_date date1_c, cxios_date date2_c)"),
108                                    date2_c.year, date2_c.month, date2_c.day,
109                                    date2_c.hour, date2_c.minute, date2_c.second);
110    return (date1 != date2);
111  }
112
113  bool cxios_date_lt(cxios_date date1_c, cxios_date date2_c)
114  {
115    xios::CDate date1 = xios::CDate(getCalendar("bool cxios_date_eq(cxios_date date1_c, cxios_date date2_c)"),
116                                    date1_c.year, date1_c.month, date1_c.day,
117                                    date1_c.hour, date1_c.minute, date1_c.second);
118    xios::CDate date2 = xios::CDate(getCalendar("bool cxios_date_eq(cxios_date date1_c, cxios_date date2_c)"),
119                                    date2_c.year, date2_c.month, date2_c.day,
120                                    date2_c.hour, date2_c.minute, date2_c.second);
121    return (date1 < date2);
122  }
123
124  bool cxios_date_le(cxios_date date1_c, cxios_date date2_c)
125  {
126    xios::CDate date1 = xios::CDate(getCalendar("bool cxios_date_eq(cxios_date date1_c, cxios_date date2_c)"),
127                                    date1_c.year, date1_c.month, date1_c.day,
128                                    date1_c.hour, date1_c.minute, date1_c.second);
129    xios::CDate date2 = xios::CDate(getCalendar("bool cxios_date_eq(cxios_date date1_c, cxios_date date2_c)"),
130                                    date2_c.year, date2_c.month, date2_c.day,
131                                    date2_c.hour, date2_c.minute, date2_c.second);
132    return (date1 <= date2);
133  }
134
135  bool cxios_date_gt(cxios_date date1_c, cxios_date date2_c)
136  {
137    xios::CDate date1 = xios::CDate(getCalendar("bool cxios_date_eq(cxios_date date1_c, cxios_date date2_c)"),
138                                    date1_c.year, date1_c.month, date1_c.day,
139                                    date1_c.hour, date1_c.minute, date1_c.second);
140    xios::CDate date2 = xios::CDate(getCalendar("bool cxios_date_eq(cxios_date date1_c, cxios_date date2_c)"),
141                                    date2_c.year, date2_c.month, date2_c.day,
142                                    date2_c.hour, date2_c.minute, date2_c.second);
143    return (date1 > date2);
144  }
145
146  bool cxios_date_ge(cxios_date date1_c, cxios_date date2_c)
147  {
148    xios::CDate date1 = xios::CDate(getCalendar("bool cxios_date_eq(cxios_date date1_c, cxios_date date2_c)"),
149                                    date1_c.year, date1_c.month, date1_c.day,
150                                    date1_c.hour, date1_c.minute, date1_c.second);
151    xios::CDate date2 = xios::CDate(getCalendar("bool cxios_date_eq(cxios_date date1_c, cxios_date date2_c)"),
152                                    date2_c.year, date2_c.month, date2_c.day,
153                                    date2_c.hour, date2_c.minute, date2_c.second);
154    return (date1 >= date2);
155  }
156
157  int cxios_date_get_second_of_year(cxios_date date_c)
158  {
159    xios::CDate date = xios::CDate(getCalendar("int cxios_date_get_second_of_year(cxios_date date_c)"),
160                                   date_c.year, date_c.month, date_c.day,
161                                   date_c.hour, date_c.minute, date_c.second);
162    return date.getSecondOfYear();
163  }
164
165  double cxios_date_get_day_of_year(cxios_date date_c)
166  {
167    xios::CDate date = xios::CDate(getCalendar("int cxios_date_get_day_of_year(cxios_date date_c)"),
168                                   date_c.year, date_c.month, date_c.day,
169                                   date_c.hour, date_c.minute, date_c.second);
170    return date.getDayOfYear();
171  }
172
173  double cxios_date_get_fraction_of_year(cxios_date date_c)
174  {
175    xios::CDate date = xios::CDate(getCalendar("int cxios_date_get_fraction_of_year(cxios_date date_c)"),
176                                   date_c.year, date_c.month, date_c.day,
177                                   date_c.hour, date_c.minute, date_c.second);
178    return date.getFractionOfYear();
179  }
180
181  int cxios_date_get_second_of_day(cxios_date date_c)
182  {
183    xios::CDate date = xios::CDate(getCalendar("int cxios_date_get_second_of_day(cxios_date date_c)"),
184                                   date_c.year, date_c.month, date_c.day,
185                                   date_c.hour, date_c.minute, date_c.second);
186    return date.getSecondOfDay();
187  }
188
189  double cxios_date_get_fraction_of_day(cxios_date date_c)
190  {
191    xios::CDate date = xios::CDate(getCalendar("int cxios_date_get_fraction_of_day(cxios_date date_c)"),
192                                   date_c.year, date_c.month, date_c.day,
193                                   date_c.hour, date_c.minute, date_c.second);
194    return date.getFractionOfDay();
195  }
196} // extern "C"
Note: See TracBrowser for help on using the repository browser.