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

Last change on this file since 528 was 501, checked in by ymipsl, 10 years ago

Add licence copyright to all file ond directory src using the command :
svn propset -R copyright -F header_licence src

XIOS is now officialy under CeCILL licence

YM

  • 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.2 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      bool operator==(const CDate& dt0, const CDate& dt1)
131      {
132         // TODO :: Vérifier que les deux dates (dt0 et dt1) ont une référence vers le même calendrier.
133         return ((dt0.getYear() == dt1.getYear()) && (dt0.getMonth()  == dt1.getMonth())  && (dt1.getDay()    == dt0.getDay()) &&
134                 (dt0.getHour() == dt1.getHour()) && (dt0.getMinute() == dt1.getMinute()) && (dt1.getSecond() == dt0.getSecond()));
135      }
136
137      bool operator< (const CDate& dt0, const CDate& dt1)
138      {
139         // TODO :: Vérifier que les deux dates (dt0 et dt1) ont une référence vers le même calendrier.
140         if   (dt0.getYear()  < dt1.getYear())
141         { 
142            return true;
143         }
144         else if (dt0.getYear() == dt1.getYear())
145         { 
146            if   (dt0.getMonth()  < dt1.getMonth())
147            {
148               return true;
149            }
150            else if (dt0.getMonth() == dt1.getMonth())
151            {
152               if   (dt0.getDay()  < dt1.getDay())
153               {
154                   return true;
155               }
156               else if (dt0.getDay() == dt1.getDay())
157               {
158                  if    (dt0.getHour()  < dt1.getHour())
159                  {
160                     return true;
161                  }
162                  else if (dt0.getHour() == dt1.getHour())
163                  { 
164                     if   (dt0.getMinute()  < dt1.getMinute())
165                     {
166                        return true;
167                     }
168                     else if (dt0.getMinute() == dt1.getMinute())
169                     {
170                        if (dt0.getSecond() < dt1.getSecond())
171                           return true;
172                     }
173                  }
174               }
175            }
176         }
177         return false;
178      }
179
180      //-----------------------------------------------------------------
181
182      bool operator!=(const CDate & dt0, const CDate & dt1){ return !(dt1 == dt0); }
183      bool operator> (const CDate & dt0, const CDate & dt1){ return (dt1 < dt0); }
184      bool operator>=(const CDate & dt0, const CDate & dt1){ return ((dt0 > dt1) || (dt1 == dt0)); }
185      bool operator<=(const CDate & dt0, const CDate & dt1){ return ((dt0 < dt1) || (dt1 == dt0)); }
186
187      ///----------------------------------------------------------------
188
189} // namespace xios
190
191
192
193
194
195
Note: See TracBrowser for help on using the repository browser.