source: XIOS/trunk/src/calendar_util.cpp @ 545

Last change on this file since 545 was 545, checked in by rlacroix, 9 years ago

Expose the calendar operations through the Fortran interface.

  • Add arithmetic operations on the xios_duration and xios_date types:
    • xios_duration + xios_duration = xios_duration
    • xios_duration - xios_duration = xios_duration
    • scalar * xios_duration = xios_duration * scalar = xios_duration
    • - xios_duration = xios_duration
    • xios_date + xios_duration = xios_date
    • xios_date - xios_duration = xios_date
    • xios_date - xios_date = xios_duration
  • Add comparison operations on the xios_duration and xios_date types:
    • xios_duration: ==, /=
    • xios_date: ==, /=, <, <=, >, >=
  • Add a new function "xios_date_convert_to_seconds" to convert a date into the number of seconds since the time origin of the calendar
  • Define some constant durations "xios_second", "xios_minute", "xios_hour", "xios_day", "xios_month", "xios_year" et "xios_timestep" to ease the definition of new durations (for example, 10h is just 10 * xios_hour)
  • Add a new function "xios_set_calendar" so that one can manually create the calendar attached to the current context and thus use the calendar operations before calling "xios_close_context_definition". This function can accept optional parameters so that the calendar attributes (calendar_type, start_date, time_origin and timestep) can be easily overwritten. Note that you cannot define a new calendar after one was already created (either because "xios_set_calendar" or "xios_close_context_definition" was used)
  • Readd the function "xios_set_timestep" as a simplified alias of "xios_set_context_attr(context, timestep)" for the current context
  • 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
File size: 7.6 KB
Line 
1#include "calendar_util.hpp"
2
3namespace xios
4{
5      /// ////////////////////// Définitions ////////////////////// ///
6
7      CDuration operator*(const double & scal, const CDuration & ddr)
8      { return (ddr * scal); }
9
10      CDuration operator-(const CDuration & ddr , const CDuration & dr)
11      {
12         CDuration dur(ddr);
13         dur.year -= dr.year;  dur.month  -= dr.month ; dur.day    -= dr.day;
14         dur.hour -= dr.hour;  dur.minute -= dr.minute; dur.second -= dr.second; dur.timestep -= dr.timestep;
15         return (dur);
16      }
17
18      CDuration operator+(const CDuration & ddr , const CDuration & dr)
19      {
20         CDuration dur(ddr);
21         dur.year += dr.year;  dur.month  += dr.month ; dur.day    += dr.day;
22         dur.hour += dr.hour;  dur.minute += dr.minute; dur.second += dr.second; dur.timestep += dr.timestep;
23         return (dur);
24      }
25
26      CDuration operator*(const CDuration & ddr , const double & scal)
27      {
28         CDuration dur(ddr);
29         dur.year *= scal;  dur.month  *= scal; dur.day    *= scal;
30         dur.hour *= scal;  dur.minute *= scal; dur.second *= scal; dur.timestep *= scal;
31         return (dur);
32      }
33
34      CDuration operator-(const CDuration & ddr)
35      {
36         CDuration dur(ddr);
37         dur.year   = -dur.year;
38         dur.month  = -dur.month;
39         dur.day    = -dur.day;
40         dur.hour   = -dur.hour;
41         dur.minute = -dur.minute;
42         dur.second = -dur.second;
43         dur.timestep = -dur.timestep;
44         return (dur);
45      }
46
47      //-----------------------------------------------------------------
48
49      CDate operator+(const CDate & dt, const CDuration & dr)
50      {
51         CDuration drr (dr);
52         int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0;
53         const CCalendar & c = dt.getRelCalendar();
54         
55         drr.timestep=0 ;       
56         drr=drr+dr.timestep*dt.getRelCalendar().getTimeStep() ;
57         
58         drr.resolve(dt.getRelCalendar());
59
60         // Ajustement des minutes par rapport aux secondes.
61         second += dt.getSecond() + drr.second;
62         if (second <  0) { minute --; second += c.getMinuteLength(); }
63         if (second >= c.getMinuteLength()) { minute ++; second -= c.getMinuteLength(); }
64
65         // Ajustement des heures en fonction des minutes.
66         minute += dt.getMinute() + drr.minute;
67         if (minute < 0) { hour --; minute += c.getHourLength(); }
68         if (minute >= c.getHourLength()) { hour ++; minute -= c.getHourLength(); }
69
70         // Ajustement des jours en fonction des heures.
71         hour += dt.getHour() + drr.hour;
72         if (hour <  0) { drr.day --; hour += c.getDayLength(); }
73         if (hour >= c.getDayLength()) { drr.day ++; hour -= c.getDayLength(); }
74
75         // Ajustement des mois en fonction des jours.
76         CDate dtt(dt); 
77         drr.day+=dtt.getDay()-1 ;
78         dtt.setDay(1) ;         
79
80         if ( drr.day >= 0 )
81         {
82           for(; c.getMonthLength(dtt) <= drr.day; dtt.addMonth (1))
83           { drr.day -=  c.getMonthLength(dtt); drr.month += 1 ; }
84
85           day = drr.day+1 ;
86         }
87         else 
88         {
89           dtt.addMonth(-1) ;
90           drr.month-=1 ;
91           for(; c.getMonthLength(dtt) < -drr.day; dtt.addMonth (-1))
92           { drr.day+=c.getMonthLength(dtt) ; drr.month-=1 ; }
93           day=c.getMonthLength(dtt)+drr.day+1 ;
94         }
95           
96/*
97         if (day <  0) { drr.month --; day += c.getMonthLength(dtt); }
98         if (day > c.getMonthLength(dtt)) { drr.month ++; day -= c.getMonthLength(dtt); } // << ProblÚme ici
99         if (day == 0){ day = c.getMonthLength(dtt); drr.month --; }
100*/         
101         drr.resolve(dt.getRelCalendar());
102
103         // Ajustement des années en fonction des mois.
104         month += dt.getMonth() + drr.month;
105         if (month <  0) { drr.year --; month += c.getYearLength(); }
106         if (month >  c.getYearLength()) { drr.year ++; month -= c.getYearLength(); }
107         if (month == 0){ month = c.getYearLength(); drr.year--; }
108
109         year += dt.getYear() + drr.year;
110
111         return (CDate(dt.getRelCalendar(), year, month, day, hour, minute, second));
112      }
113
114      CDate operator-(const CDate & dt, const CDuration & dr) { return (dt + (-dr)); }
115
116      //-----------------------------------------------------------------
117
118      CDuration operator-(const CDate & dt0, const CDate & dt1)
119      {
120         // TODO :: Vérifier que les deux dates (dt0 et dt1) ont une référence vers le même calendrier.
121         CDuration dur =
122         { dt0.getYear() - dt1.getYear(), dt0.getMonth()  - dt1.getMonth() , dt0.getDay()    - dt1.getDay(),
123           dt0.getHour() - dt1.getHour(), dt0.getMinute() - dt1.getMinute(), dt0.getSecond() - dt1.getSecond() };
124         return (dur.resolve(dt0.getRelCalendar()));
125      }
126
127      //-----------------------------------------------------------------
128
129      /// Les opérateurs de comparaison. (Non testés pour le moment)
130
131      bool operator==(const CDuration& ddr, const CDuration& dr)
132      {
133         return ((ddr.year == dr.year) && (ddr.month  == dr.month)  && (dr.day    == ddr.day) &&
134                 (ddr.hour == dr.hour) && (ddr.minute == dr.minute) && (dr.second == ddr.second) &&
135                 (ddr.timestep == dr.timestep));
136      }
137
138      bool operator!=(const CDuration& ddr, const CDuration& dr)
139      {
140         return !(ddr == dr);
141      }
142
143      bool operator==(const CDate& dt0, const CDate& dt1)
144      {
145         // TODO :: Vérifier que les deux dates (dt0 et dt1) ont une référence vers le même calendrier.
146         return ((dt0.getYear() == dt1.getYear()) && (dt0.getMonth()  == dt1.getMonth())  && (dt1.getDay()    == dt0.getDay()) &&
147                 (dt0.getHour() == dt1.getHour()) && (dt0.getMinute() == dt1.getMinute()) && (dt1.getSecond() == dt0.getSecond()));
148      }
149
150      bool operator< (const CDate& dt0, const CDate& dt1)
151      {
152         // TODO :: Vérifier que les deux dates (dt0 et dt1) ont une référence vers le même calendrier.
153         if   (dt0.getYear()  < dt1.getYear())
154         { 
155            return true;
156         }
157         else if (dt0.getYear() == dt1.getYear())
158         { 
159            if   (dt0.getMonth()  < dt1.getMonth())
160            {
161               return true;
162            }
163            else if (dt0.getMonth() == dt1.getMonth())
164            {
165               if   (dt0.getDay()  < dt1.getDay())
166               {
167                   return true;
168               }
169               else if (dt0.getDay() == dt1.getDay())
170               {
171                  if    (dt0.getHour()  < dt1.getHour())
172                  {
173                     return true;
174                  }
175                  else if (dt0.getHour() == dt1.getHour())
176                  { 
177                     if   (dt0.getMinute()  < dt1.getMinute())
178                     {
179                        return true;
180                     }
181                     else if (dt0.getMinute() == dt1.getMinute())
182                     {
183                        if (dt0.getSecond() < dt1.getSecond())
184                           return true;
185                     }
186                  }
187               }
188            }
189         }
190         return false;
191      }
192
193      //-----------------------------------------------------------------
194
195      bool operator!=(const CDate & dt0, const CDate & dt1){ return !(dt1 == dt0); }
196      bool operator> (const CDate & dt0, const CDate & dt1){ return (dt1 < dt0); }
197      bool operator>=(const CDate & dt0, const CDate & dt1){ return ((dt0 > dt1) || (dt1 == dt0)); }
198      bool operator<=(const CDate & dt0, const CDate & dt1){ return ((dt0 < dt1) || (dt1 == dt0)); }
199
200      ///----------------------------------------------------------------
201
202} // namespace xios
203
204
205
206
207
208
Note: See TracBrowser for help on using the repository browser.