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

Last change on this file since 316 was 316, checked in by ymipsl, 12 years ago
  • Change date format :

dd/mm/yyyy-hh:mm:ss becomes yyyy-mm-dd hh:mm:ss

  • add boost date_time functionality

YM

File size: 7.3 KB
Line 
1#include "date.hpp"
2#include "calendar.hpp"
3#include <boost/date_time/gregorian/gregorian.hpp>
4#include <boost/date_time/posix_time/posix_time.hpp>
5
6using namespace boost::posix_time ;
7using namespace boost::gregorian ;
8
9namespace xmlioserver
10{
11   namespace date
12   {
13      /// ////////////////////// Définitions ////////////////////// ///
14      CDate::CDate(const CCalendar& calendar,
15                   int yr, int mth, int d,
16                   int hr, int min, int sec)
17         : relCalendar(calendar)
18         , year(yr), month(mth) , day(d)
19         , hour(hr), minute(min), second(sec)
20      {
21         if(!this->checkDate())
22         {
23            DEBUG(<< "La date initialisée a été modifiée "
24                  << "car elle était incorrecte par rapport au calendrier souhaité.");
25         }
26      }
27
28      CDate::CDate(const CDate & date)
29            : relCalendar(date.getRelCalendar()),
30              year(date.year), month(date.month)  , day(date.day),
31              hour(date.hour), minute(date.minute), second(date.second)
32      {
33         if(!this->checkDate())
34         {
35            DEBUG(<< "La date initialisée a été modifiée "
36                  << "car elle était incorrecte par rapport au calendrier souhaité.");
37         }
38      }
39
40      CDate::~CDate(void)
41      { /* Ne rien faire de plus */ }
42
43      ///---------------------------------------------------------------
44
45      CDate & CDate::operator=(const CDate & date)
46      {
47         // relCalendar = d.getRelCalendar(); << inutile si fonction bien utilisée
48         year = date.year; month  = date.month ; day    = date.day;
49         hour = date.hour; minute = date.minute; second = date.second;
50         return (*this);
51      }
52
53      StdOStream & operator<<(StdOStream & out, const CDate & date)
54      {
55         std::streamsize s ; 
56         char c ;
57         
58         s = out.width (4);  c = out.fill ('0') ; out << date.year << '-';
59         s = out.width (2);  c = out.fill ('0') ; out << date.month << '-';
60         s = out.width (2);  c = out.fill ('0') ; out << date.day << ' ';
61         s = out.width (2);  c = out.fill ('0') ; out << date.hour << ':';
62         s = out.width (2);  c = out.fill ('0') ; out << date.minute << ':';
63         s = out.width (2);  c = out.fill ('0') ; out << date.second ;
64
65         return (out);
66      }
67
68      StdIStream & operator>>(StdIStream & in, CDate & date) // Non testée.
69      {
70        char sep = '-'; // Le caractÚre c est utilisé pour "recueillir" les séparateurs "/" et ":".
71        char c ;
72         
73        in >> date.year >> c ;
74        if (c==sep)
75        {
76          in >> date.month >> c ;
77          if (c==sep)
78          {
79            in >> date.day  ;
80            c=in.get();
81            sep=' ' ;
82            if (c==sep)
83            {
84              in >> date.hour >> c ;
85              sep=':' ;
86              if (c==sep) 
87              {
88                in>>date.minute >> c;
89                if (c==sep)
90                {
91                  in>>date.second ;
92                  if(!date.checkDate())
93                    ERROR("StdIStream & operator >> (StdIStream & in, CDate & date)",<<"Bad date format or not conform to calendar" );
94                    return (in);
95                }
96              }
97            }
98          }
99        }
100        ERROR("StdIStream & operator >> (StdIStream & in, CDate & date)",<<"Bad date format or not conform to calendar" );
101        return (in);
102      }
103
104      CDate::operator Time(void) // Non vérifiée, pas optimisée ...
105      {
106         // Todo : Tester si la date courante est supérieure à la date initiale.
107         Time retvalue = - relCalendar.getNbSecond(relCalendar.getInitDate())
108                         + relCalendar.getNbSecond(*this);
109
110         if ((relCalendar.getId().compare("D360")    == 0) ||
111             (relCalendar.getId().compare("AllLeap") == 0) ||
112             (relCalendar.getId().compare("NoLeap")  == 0))
113         return (retvalue + (getYear() - relCalendar.getInitDate().getYear())
114                                       * relCalendar.getYearTotalLength(*this));
115
116         for(CDate _d(relCalendar.getInitDate());
117            _d.getYear() < getYear(); _d.setYear(_d.getYear()+1))
118            retvalue += relCalendar.getYearTotalLength(_d);
119         return (retvalue);
120      }
121
122      //----------------------------------------------------------------
123
124      bool CDate::checkDate(void)
125      {
126         bool retValue = true;
127
128         // Vérificatio de la valeur du mois.
129         if (month  < 1) { retValue = false; month  = 1; }
130         if (month  > relCalendar.getYearLength())
131         { retValue = false; month = relCalendar.getYearLength(); }
132
133         // Vérification de la valeur du jour.
134         if (day    < 1) { retValue = false; month  = 1; }
135         if (day    > relCalendar.getMonthLength(*this))
136         { retValue = false; day = relCalendar.getMonthLength(*this); }
137
138         // Vérification de la valeur de l'heure.
139         if (hour   < 0) { retValue = false; hour  = 0; }
140         if (hour   >= relCalendar.getDayLength())
141         { retValue = false; hour = relCalendar.getDayLength()-1; }
142
143         // Vérification de la valeur des minutes.
144         if (minute < 0) { retValue = false; minute = 0; }
145         if (minute >= relCalendar.getHourLength())
146         { retValue = false; minute = relCalendar.getHourLength()-1; }
147
148         // Vérification de la valeur des secondes.
149         if (second < 0) { retValue = false; month  = 0; }
150         if (second >= relCalendar.getMinuteLength())
151         { retValue = false; second = relCalendar.getMinuteLength()-1; }
152
153         return retValue;
154      }
155
156      //----------------------------------------------------------------
157
158      int CDate::getYear  (void) const { return (this->year  ); }
159      int CDate::getMonth (void) const { return (this->month ); }
160      int CDate::getDay   (void) const { return (this->day   ); }
161      int CDate::getHour  (void) const { return (this->hour  ); }
162      int CDate::getMinute(void) const { return (this->minute); }
163      int CDate::getSecond(void) const { return (this->second); }
164
165      //----------------------------------------------------------------
166
167      const CCalendar & CDate::getRelCalendar(void) const
168      { return (this->relCalendar); }
169
170      //----------------------------------------------------------------
171
172      void CDate::setYear  (int newyear)  { this->year  = newyear; }
173      void CDate::setMonth (int newmonth) { this->month = newmonth; }
174
175      //----------------------------------------------------------------
176
177      void CDate::addMonth (int value)
178      {// Value doit être égale à 1 ou -1.
179         this->month += value;
180         if (this->month == 13) { year++; this->month = 1 ; }
181         if (this->month == 0 ) { year--; this->month = 12; }
182      }
183
184      //----------------------------------------------------------------
185
186      CDate CDate::FromString(const StdString & str, const CCalendar & calendar)
187      {
188         CDate dt(calendar);
189         StdIStringStream iss(str);
190         iss >> dt;
191         return dt;
192      }
193     
194      //----------------------------------------------------------------
195     
196       StdString CDate::toString(void) const
197      { 
198         StdOStringStream oss;
199         oss << (*this);
200         return (oss.str()); 
201      }
202
203      ///---------------------------------------------------------------
204
205   } // namespace date
206} // namespace xmlioserver
Note: See TracBrowser for help on using the repository browser.