source: XMLIO_V2/dev/common/src/xmlio/duration.cpp @ 266

Last change on this file since 266 was 266, checked in by hozdoba, 13 years ago

Corrections après tests sur titane

File size: 5.6 KB
Line 
1#include "duration.hpp"
2#include "date.hpp"
3#include "calendar.hpp"
4
5namespace xmlioserver
6{
7   namespace date
8   {
9      /// ////////////////////// Définitions ////////////////////// ///
10      const CDuration Year   = {1.0, 0.0, 0.0, 0.0, 0.0, 0.0},
11                      Month  = {0.0, 1.0, 0.0, 0.0, 0.0, 0.0},
12                      Week   = {0.0, 0.0, 7.0, 0.0, 0.0, 0.0},
13                      Day    = {0.0, 0.0, 1.0, 0.0, 0.0, 0.0},
14                      Hour   = {0.0, 0.0, 0.0, 1.0, 0.0, 0.0},
15                      Minute = {0.0, 0.0, 0.0, 0.0, 1.0, 0.0},
16                      Second = {0.0, 0.0, 0.0, 0.0, 0.0, 1.0},
17                      NoneDu = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
18
19      ///---------------------------------------------------------------
20
21      CDuration & CDuration::operator=(const CDuration & duration)
22      {
23         year = duration.year;  month  = duration.month ; day    = duration.day;
24         hour = duration.hour;  minute = duration.minute; second = duration.second;
25         return (*this);
26      }
27
28      StdOStream & operator<<(StdOStream & out, const CDuration & duration)
29      {
30         StdOStringStream sout;
31         bool testValue = true;
32         if(duration.year   != 0.0) { testValue = false; sout << duration.year   << "y " ; }
33         if(duration.month  != 0.0) { testValue = false; sout << duration.month  << "mo "; }
34         if(duration.day    != 0.0) { testValue = false; sout << duration.day    << "d " ; }
35         if(duration.hour   != 0.0) { testValue = false; sout << duration.hour   << "h " ; }
36         if(duration.minute != 0.0) { testValue = false; sout << duration.minute << "mi "; }
37         if(duration.second != 0.0 || testValue)       { sout << duration.second << "s " ; }
38
39         // << suppression de l'espace en fin de chaîne.
40         out << (sout.str().substr(0, sout.str().size()-1));
41         return (out);
42      }
43
44      StdIStream & operator>>(StdIStream & in , CDuration & duration)
45      {
46         duration.year = duration.month  = duration.day    =
47         duration.hour = duration.minute = duration.second = 0.0;
48         double v = 1.0;
49         char   c = '/';
50         while (!in.eof())
51         {
52               if (!(in >> v >> c)) 
53               {
54                 //DEBUG("----> Pb StdIStream & operator>>(StdIStream & in , CDuration & duration)") ;
55                 //if (in.eof())  DEBUG("----> Fin de fichier StdIStream & operator>>(StdIStream & in , CDuration & duration)") ;
56               }
57               if (in.eof()) 
58               {
59                 //DEBUG("----> Fin de fichier StdIStream & operator>>(StdIStream & in , CDuration & duration)") ;
60                 break ;
61               }
62               switch (c)
63               {
64                  case 'y': duration.year   = v; break;
65                  case 'd': duration.day    = v; break;
66                  case 'h': duration.hour   = v; break;
67                  case 's': duration.second = v; break;
68                  case 'm':
69                  {
70                     in >> c;
71                     if     (c == 'i') duration.minute = v;
72                     else if(c == 'o') duration.month  = v;
73                     else
74                     {
75                        StdString valc("m"); valc.append(1, c);
76                        //DEBUG("La chaine \"" << valc << "\" ne permet pas de définir une unité de durée.");
77                        break;
78                     }
79                     break;
80                  }
81                  default:
82                     StdString valc; valc.append(1, c);
83                     //DEBUG("La chaine \"" << valc << "\" ne permet pas de définir une unité de durée.");
84                     break;
85               }
86            }
87            return (in);
88      }
89
90      //-----------------------------------------------------------------
91
92      bool CDuration::isNone(void) const
93      {
94         if ((year == 0) && (month  == 0) && (day    == 0) &&
95             (hour == 0) && (minute == 0) && (second == 0))
96            return (true);
97         return (false);
98      }
99
100      //-----------------------------------------------------------------
101
102      CDuration & CDuration::resolve(const CCalendar & c)
103      {
104         // Simplification de l'écriture des minutes.
105         second += modf(minute, &minute) * (float)c.getMinuteLength();
106         minute += int(second)/c.getMinuteLength(); second = int(second)%c.getMinuteLength();
107
108         // Simplification de l'écriture des heures.
109         minute += modf(hour , &hour) * (float)c.getHourLength();
110         hour   += int(minute)/c.getHourLength(); minute = int(minute)%c.getHourLength();
111
112         // Simplification de l'écriture des jours.
113         hour   += modf(day, &day) * (float)c.getDayLength();
114         day    += int(hour)  /c.getDayLength(); hour   = int(hour)%c.getDayLength();
115
116         // > Aucune équivalence jour - mois fixée par avance. //
117
118         // Simplification de l'écriture des années.
119         month  += modf(year, &year) * (float)c.getYearLength();
120         year   += int(month) /c.getYearLength(); month  = int(month)%c.getYearLength();
121         return (*this);
122      }
123
124      //-----------------------------------------------------------------
125
126      StdString CDuration::toString(void) const
127      {
128         const  CDuration & own = *this;
129         StdOStringStream oss; oss << own;
130         return (oss.str());
131      }
132
133      CDuration CDuration::FromString(const StdString & str)
134      {
135         CDuration dr = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
136         StdIStringStream iss(str); iss >> dr;
137         return (dr);
138      }
139
140      ///---------------------------------------------------------------
141
142
143   } // namespace date
144} // namespace xmlioserver
145
Note: See TracBrowser for help on using the repository browser.