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

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

Préparation nouvelle arborescence

File size: 5.1 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               in >> v >> c;
53               switch (c)
54               {
55                  case 'y': duration.year   = v; continue;
56                  case 'd': duration.day    = v; continue;
57                  case 'h': duration.hour   = v; continue;
58                  case 's': duration.second = v; continue;
59                  case 'm':
60                     in >> c;
61                     if     (c == 'i') duration.minute = v;
62                     else if(c == 'o') duration.month  = v;
63                     else
64                     {
65                        StdString valc("m"); valc.append(1, c);
66                        DEBUG("La chaine \"" << valc << "\" ne permet pas de définir une unité de durée.");
67                        continue;
68                     }
69                     continue;
70                  default:
71                     StdString valc; valc.append(1, c);
72                     DEBUG("La chaine \"" << valc << "\" ne permet pas de définir une unité de durée.");
73                     continue;
74               }
75            }
76            return (in);
77      }
78
79      //-----------------------------------------------------------------
80
81      bool CDuration::isNone(void) const
82      {
83         if ((year == 0) && (month  == 0) && (day    == 0) &&
84             (hour == 0) && (minute == 0) && (second == 0))
85            return (true);
86         return (false);
87      }
88
89      //-----------------------------------------------------------------
90
91      CDuration & CDuration::resolve(const CCalendar & c)
92      {
93         // Simplification de l'écriture des minutes.
94         second += modf(minute, &minute) * (float)c.getMinuteLength();
95         minute += int(second)/c.getMinuteLength(); second = int(second)%c.getMinuteLength();
96
97         // Simplification de l'écriture des heures.
98         minute += modf(hour , &hour) * (float)c.getHourLength();
99         hour   += int(minute)/c.getHourLength(); minute = int(minute)%c.getHourLength();
100
101         // Simplification de l'écriture des jours.
102         hour   += modf(day, &day) * (float)c.getDayLength();
103         day    += int(hour)  /c.getDayLength(); hour   = int(hour)%c.getDayLength();
104
105         // > Aucune équivalence jour - mois fixée par avance. //
106
107         // Simplification de l'écriture des années.
108         month  += modf(year, &year) * (float)c.getYearLength();
109         year   += int(month) /c.getYearLength(); month  = int(month)%c.getYearLength();
110         return (*this);
111      }
112
113      //-----------------------------------------------------------------
114
115      StdString CDuration::toString(void) const
116      {
117         const  CDuration & own = *this;
118         StdOStringStream oss; oss << own;
119         return (oss.str());
120      }
121
122      CDuration CDuration::FromString(const StdString & str)
123      {
124         CDuration dr = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
125         StdIStringStream iss(str); iss >> dr;
126         return (dr);
127      }
128
129      ///---------------------------------------------------------------
130
131
132   } // namespace date
133} // namespace xmlioserver
134
Note: See TracBrowser for help on using the repository browser.