source: XIOS/trunk/src/date.cpp @ 501

Last change on this file since 501 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: 9.7 KB
Line 
1#include "date.hpp"
2#include "calendar.hpp"
3#include "calendar_type.hpp"
4#include <boost/date_time/gregorian/gregorian.hpp>
5#include <boost/date_time/posix_time/posix_time.hpp>
6
7using namespace boost::posix_time ;
8using namespace boost::gregorian ;
9
10namespace xios
11{
12      /// ////////////////////// Définitions ////////////////////// ///
13      CDate::CDate(const CCalendar& calendar)
14         : relCalendar(calendar)
15         , year(0), month(1) , day(1)
16         , hour(0), minute(0), second(0)
17      {   }
18     
19      CDate::CDate(const CCalendar& calendar,
20                   int yr, int mth, int d,
21                   int hr, int min, int sec)
22         : relCalendar(calendar)
23         , year(yr), month(mth) , day(d)
24         , hour(hr), minute(min), second(sec)
25      {
26         if(!this->checkDate())
27         {
28            DEBUG(<< "La date initialisée a été modifiée "
29                  << "car elle était incorrecte par rapport au calendrier souhaité.");
30         }
31      }
32
33      CDate::CDate(const CDate & date)
34            : relCalendar(date.getRelCalendar()),
35              year(date.year), month(date.month)  , day(date.day),
36              hour(date.hour), minute(date.minute), second(date.second)
37      {
38         if(!this->checkDate())
39         {
40            DEBUG(<< "La date initialisée a été modifiée "
41                  << "car elle était incorrecte par rapport au calendrier souhaité.");
42         }
43      }
44
45      CDate::~CDate(void)
46      { /* Ne rien faire de plus */ }
47
48      ///---------------------------------------------------------------
49
50      CDate & CDate::operator=(const CDate & date)
51      {
52         // relCalendar = d.getRelCalendar(); << inutile si fonction bien utilisée
53         year = date.year; month  = date.month ; day    = date.day;
54         hour = date.hour; minute = date.minute; second = date.second;
55         return (*this);
56      }
57
58      StdOStream & operator<<(StdOStream & out, const CDate & date)
59      {
60         std::streamsize s ; 
61         char c ;
62
63
64         int width=4 ;
65         double maxSize=10000 ;
66         while(date.year>=maxSize)
67         {
68           maxSize*=10 ;
69           width++ ;
70         }
71         s = out.width (width);  c = out.fill ('0') ; out << date.year << '-';
72
73         s = out.width (2);  c = out.fill ('0') ; out << date.month << '-';
74         s = out.width (2);  c = out.fill ('0') ; out << date.day << ' ';
75         s = out.width (2);  c = out.fill ('0') ; out << date.hour << ':';
76         s = out.width (2);  c = out.fill ('0') ; out << date.minute << ':';
77         s = out.width (2);  c = out.fill ('0') ; out << date.second ;
78
79         return (out);
80      }
81
82      StdIStream & operator>>(StdIStream & in, CDate & date) // Non testée.
83      {
84        char sep = '-'; // Le caractÚre c est utilisé pour "recueillir" les séparateurs "/" et ":".
85        char c ;
86         
87        in >> date.year >> c ;
88        if (c==sep)
89        {
90          in >> date.month >> c ;
91          if (c==sep)
92          {
93            in >> date.day  ;
94            c=in.get();
95            sep=' ' ;
96            if (c==sep)
97            {
98              in >> date.hour >> c ;
99              sep=':' ;
100              if (c==sep) 
101              {
102                in>>date.minute >> c;
103                if (c==sep)
104                {
105                  in>>date.second ;
106                  if(!date.checkDate())
107                    ERROR("StdIStream & operator >> (StdIStream & in, CDate & date)",<<"Bad date format or not conform to calendar" );
108                    return (in);
109                }
110              }
111            }
112          }
113        }
114        ERROR("StdIStream & operator >> (StdIStream & in, CDate & date)",<<"Bad date format or not conform to calendar" );
115        return (in);
116      }
117
118      CDate::operator Time(void) const // Non vérifiée, pas optimisée ...
119      {
120         // Todo : Tester si la date courante est supérieure à la date initiale.
121         Time retvalue = - relCalendar.getNbSecond(relCalendar.getInitDate())
122                         + relCalendar.getNbSecond(*this);
123
124         if ((relCalendar.getId().compare("D360")    == 0) ||
125             (relCalendar.getId().compare("AllLeap") == 0) ||
126             (relCalendar.getId().compare("NoLeap")  == 0))
127         return (retvalue + (getYear() - relCalendar.getTimeOrigin().getYear())
128                                       * relCalendar.getYearTotalLength(*this));
129
130         for(CDate _d(relCalendar.getTimeOrigin());
131            _d.getYear() < getYear(); _d.setYear(_d.getYear()+1))
132            retvalue += relCalendar.getYearTotalLength(_d);
133         return (retvalue);
134      }
135
136      //----------------------------------------------------------------
137
138      bool CDate::checkDate(void)
139      {
140         bool retValue = true;
141
142         // Vérificatio de la valeur du mois.
143         if (month  < 1) { retValue = false; month  = 1; }
144         if (month  > relCalendar.getYearLength())
145         { retValue = false; month = relCalendar.getYearLength(); }
146
147         // Vérification de la valeur du jour.
148         if (day    < 1) { retValue = false; month  = 1; }
149         if (day    > (&relCalendar)->getMonthLength(*this))
150         { retValue = false; day = (&relCalendar)->getMonthLength(*this); }
151
152         // Vérification de la valeur de l'heure.
153         if (hour   < 0) { retValue = false; hour  = 0; }
154         if (hour   >= relCalendar.getDayLength())
155         { retValue = false; hour = relCalendar.getDayLength()-1; }
156
157         // Vérification de la valeur des minutes.
158         if (minute < 0) { retValue = false; minute = 0; }
159         if (minute >= relCalendar.getHourLength())
160         { retValue = false; minute = relCalendar.getHourLength()-1; }
161
162         // Vérification de la valeur des secondes.
163         if (second < 0) { retValue = false; month  = 0; }
164         if (second >= relCalendar.getMinuteLength())
165         { retValue = false; second = relCalendar.getMinuteLength()-1; }
166
167         return retValue;
168      }
169
170      //----------------------------------------------------------------
171
172      int CDate::getYear  (void) const { return (this->year  ); }
173      int CDate::getMonth (void) const { return (this->month ); }
174      int CDate::getDay   (void) const { return (this->day   ); }
175      int CDate::getHour  (void) const { return (this->hour  ); }
176      int CDate::getMinute(void) const { return (this->minute); }
177      int CDate::getSecond(void) const { return (this->second); }
178
179      //----------------------------------------------------------------
180
181      const CCalendar & CDate::getRelCalendar(void) const
182      { return (this->relCalendar); }
183
184      //----------------------------------------------------------------
185
186      void CDate::setYear  (int newyear)  { this->year  = newyear; }
187      void CDate::setMonth (int newmonth) { this->month = newmonth; }
188      void CDate::setDay (int newday) { this->day = newday; }
189
190      //----------------------------------------------------------------
191
192      void CDate::addMonth (int value)
193      {// Value doit être égale à 1 ou -1.
194         this->month += value;
195         if (this->month == 13) { year++; this->month = 1 ; }
196         if (this->month == 0 ) { year--; this->month = 12; }
197      }
198
199      //----------------------------------------------------------------
200
201      CDate CDate::FromString(const StdString & str, const CCalendar & calendar)
202      {
203         CDate dt(calendar);
204         StdIStringStream iss(str);
205         iss >> dt;
206         return dt;
207      }
208     
209      //----------------------------------------------------------------
210     
211      StdString CDate::getStryyyymmdd(void) const
212      { 
213         std::streamsize s ; 
214         char c ;
215
216         ostringstream oss ;
217
218         s = oss.width (4);  c = oss.fill ('0') ; oss << year ;
219         s = oss.width (2);  c = oss.fill ('0') ; oss << month;
220         s = oss.width (2);  c = oss.fill ('0') ; oss << day ;
221
222         return oss.str();
223      }
224     
225
226      string CDate::getStr(const string& str) const
227      {
228        ostringstream oss ;
229        int level;
230       
231        level=0 ;
232        for(string::const_iterator it=str.begin();it!=str.end();++it)
233        {
234          if (level==0)
235          {
236            if (*it=='%') level++ ;
237            else oss<<*it ;
238          }
239          else if (level==1)
240          {
241            switch (*it)
242            {
243              case 'y' :
244                oss.width (4);  oss.fill ('0') ; oss << year ;
245                level=0 ;
246                break ;
247              case 'm' : // month or minute
248                level++ ;
249                break ;
250              case 'd' :
251                oss.width (2);  oss.fill ('0') ; oss << day ;
252                level=0;
253                break ;
254              case 'h' :
255                oss.width (2);  oss.fill ('0') ; oss << hour ;
256                level=0;
257                break ;
258              case 's' :
259                oss.width (2);  oss.fill ('0') ; oss << second ;
260                level=0 ;
261                break;
262              default :
263                oss<<'%'<<*it ;
264                level=0 ;
265            }
266          }
267          else if (level==2)
268          {
269            switch (*it)
270            {
271              case 'o' : // month
272                oss.width (2);  oss.fill ('0') ; oss << month ;
273                level=0 ;
274                break ;
275              case 'i' : //minute
276                oss.width (2);  oss.fill ('0') ; oss << minute ;
277                level=0 ;
278                break ;
279              default :
280                oss<<"%m"<<*it ;
281                level=0 ;
282            }
283          }
284        }
285        return oss.str();
286      }
287     
288      StdString CDate::toString(void) const
289      { 
290         StdOStringStream oss;
291         oss << (*this);
292         return (oss.str()); 
293      }
294
295      ///---------------------------------------------------------------
296
297} // namespace xios
Note: See TracBrowser for help on using the repository browser.