Ignore:
Timestamp:
12/10/14 14:27:09 (9 years ago)
Author:
rlacroix
Message:

Add a new attribute type for dates and use it for the context's start_date and time_origin.

The "xios_date" type should now be used to get/set date attributes through the Fortran interface. This avoids using strings to manipulate dates.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • XIOS/trunk/src/date.cpp

    r501 r532  
    1111{ 
    1212      /// ////////////////////// Définitions ////////////////////// /// 
    13       CDate::CDate(const CCalendar& calendar) 
    14          : relCalendar(calendar) 
     13      CDate::CDate(void) 
     14         : relCalendar(NULL) 
    1515         , year(0), month(1) , day(1) 
    1616         , hour(0), minute(0), second(0) 
    17       {   } 
     17      {} 
     18 
     19      CDate::CDate(const CCalendar& calendar) 
     20         : relCalendar(&calendar) 
     21         , year(0), month(1) , day(1) 
     22         , hour(0), minute(0), second(0) 
     23      {} 
    1824       
    1925      CDate::CDate(const CCalendar& calendar, 
    2026                   int yr, int mth, int d, 
    2127                   int hr, int min, int sec) 
    22          : relCalendar(calendar) 
     28         : relCalendar(&calendar) 
    2329         , year(yr), month(mth) , day(d) 
    2430         , hour(hr), minute(min), second(sec) 
     
    3137      } 
    3238 
    33       CDate::CDate(const CDate & date) 
    34             : relCalendar(date.getRelCalendar()), 
     39      CDate::CDate(const CDate& date) 
     40            : relCalendar(date.relCalendar), 
    3541              year(date.year), month(date.month)  , day(date.day), 
    3642              hour(date.hour), minute(date.minute), second(date.second) 
    3743      { 
    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          } 
     44        // Delay the verification until we get a calendar we can compare the date to 
     45        if (relCalendar && !checkDate()) 
     46        { 
     47          DEBUG(<< "La date initialisée a été modifiée " 
     48                << "car elle était incorrecte par rapport au calendrier souhaité."); 
     49        } 
    4350      } 
    4451 
     
    5057      CDate & CDate::operator=(const CDate & date) 
    5158      { 
    52          // relCalendar = d.getRelCalendar(); << inutile si fonction bien utilisée 
     59         relCalendar = date.relCalendar; 
    5360         year = date.year; month  = date.month ; day    = date.day; 
    5461         hour = date.hour; minute = date.minute; second = date.second; 
     
    103110                if (c==sep) 
    104111                { 
    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); 
     112                  in >> date.second; 
     113                  // Delay the verification until we get a calendar we can compare the date to 
     114                  if (date.relCalendar && !date.checkDate()) 
     115                    ERROR("StdIStream & operator >> (StdIStream & in, CDate & date)", 
     116                          << "Bad date format or not conform to calendar"); 
     117                  return (in); 
    109118                } 
    110119              } 
     
    118127      CDate::operator Time(void) const // Non vérifiée, pas optimisée ... 
    119128      { 
     129         // This will check that a calendar was correctly associated to the date 
     130         const CCalendar& c = getRelCalendar(); 
     131 
    120132         // 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()); 
     133         Time retvalue = - c.getNbSecond(c.getInitDate()) 
     134                         + c.getNbSecond(*this); 
     135 
     136         if ((c.getId().compare("D360")    == 0) || 
     137             (c.getId().compare("AllLeap") == 0) || 
     138             (c.getId().compare("NoLeap")  == 0)) 
     139         return (retvalue + (getYear() - c.getTimeOrigin().getYear()) 
     140                                       * c.getYearTotalLength(*this)); 
     141 
     142         for(CDate _d(c.getTimeOrigin()); 
    131143            _d.getYear() < getYear(); _d.setYear(_d.getYear()+1)) 
    132             retvalue += relCalendar.getYearTotalLength(_d); 
     144            retvalue += c.getYearTotalLength(_d); 
    133145         return (retvalue); 
    134146      } 
     
    140152         bool retValue = true; 
    141153 
    142          // Vérificatio de la valeur du mois. 
     154         // This will check that a calendar was correctly associated to the date 
     155         const CCalendar& c = getRelCalendar(); 
     156 
     157         // Vérification de la valeur du mois. 
    143158         if (month  < 1) { retValue = false; month  = 1; } 
    144          if (month  > relCalendar.getYearLength()) 
    145          { retValue = false; month = relCalendar.getYearLength(); } 
     159         if (month  > c.getYearLength()) 
     160         { retValue = false; month = c.getYearLength(); } 
    146161 
    147162         // 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); } 
     163         if (day    < 1) { retValue = false; day  = 1; } 
     164         if (day    > c.getMonthLength(*this)) 
     165         { retValue = false; day = c.getMonthLength(*this); } 
    151166 
    152167         // Vérification de la valeur de l'heure. 
    153168         if (hour   < 0) { retValue = false; hour  = 0; } 
    154          if (hour   >= relCalendar.getDayLength()) 
    155          { retValue = false; hour = relCalendar.getDayLength()-1; } 
     169         if (hour   >= c.getDayLength()) 
     170         { retValue = false; hour = c.getDayLength() - 1; } 
    156171 
    157172         // Vérification de la valeur des minutes. 
    158173         if (minute < 0) { retValue = false; minute = 0; } 
    159          if (minute >= relCalendar.getHourLength()) 
    160          { retValue = false; minute = relCalendar.getHourLength()-1; } 
     174         if (minute >= c.getHourLength()) 
     175         { retValue = false; minute = c.getHourLength() - 1; } 
    161176 
    162177         // 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; } 
     178         if (second < 0) { retValue = false; second = 0; } 
     179         if (second >= c.getMinuteLength()) 
     180         { retValue = false; second = c.getMinuteLength() - 1; } 
    166181 
    167182         return retValue; 
     
    179194      //---------------------------------------------------------------- 
    180195 
    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; } 
     196      const CCalendar& CDate::getRelCalendar(void) const 
     197      { 
     198        if (!this->relCalendar) 
     199          ERROR("const CCalendar& CDate::getRelCalendar(void) const", 
     200                "Invalid state: The date is not associated with any calendar."); 
     201        return (*this->relCalendar); 
     202      } 
     203 
     204      bool CDate::hasRelCalendar(void) const 
     205      { return (this->relCalendar != NULL); } 
     206 
     207      //---------------------------------------------------------------- 
     208 
     209      void CDate::setYear  (int newyear)   { this->year   = newyear; } 
     210      void CDate::setMonth (int newmonth)  { this->month  = newmonth; } 
     211      void CDate::setDay   (int newday)    { this->day    = newday; } 
     212      void CDate::setHour  (int newhour)   { this->hour   = newhour; } 
     213      void CDate::setMinute(int newminute) { this->minute = newminute; } 
     214      void CDate::setSecond(int newsecond) { this->second = newsecond; } 
     215 
     216      void CDate::setDate(int yr, int mth, int d, int hr, int min, int sec) 
     217      { 
     218        this->year   = yr; 
     219        this->month  = mth; 
     220        this->day    = d; 
     221        this->hour   = hr; 
     222        this->minute = min; 
     223        this->second = sec; 
     224      } 
    189225 
    190226      //---------------------------------------------------------------- 
     
    195231         if (this->month == 13) { year++; this->month = 1 ; } 
    196232         if (this->month == 0 ) { year--; this->month = 12; } 
     233      } 
     234 
     235      //---------------------------------------------------------------- 
     236 
     237      bool CDate::setRelCalendar(const CCalendar& relCalendar) 
     238      { 
     239        this->relCalendar = &relCalendar; 
     240        return this->checkDate(); 
    197241      } 
    198242 
Note: See TracChangeset for help on using the changeset viewer.