source: XMLIO_V2/dev/dev_rv/src/XMLIO/duration.hpp @ 131

Last change on this file since 131 was 131, checked in by hozdoba, 14 years ago

Début Interface c<->fortran

File size: 3.9 KB
Line 
1#ifndef __XMLIO_DURATION__
2#define __XMLIO_DURATION__
3
4namespace XMLIOSERVER
5{
6   class AbstractCalendar;
7
8   typedef long long int Time;
9
10   typedef struct _duration
11   {
12      public :
13
14         struct _duration& operator=(const struct _duration& ddr)
15         {
16            year = ddr.year;  month  = ddr.month ; day    = ddr.day;
17            hour = ddr.hour;  minute = ddr.minute; second = ddr.second;
18            return (*this);
19         }
20
21         friend std::ostream& operator<<(std::ostream& _out, const struct _duration& d)
22         {
23            std::ostringstream out;
24            bool testValue = true;
25            if(d.year   != 0.0) { testValue = false; out << d.year   << "y " ; }
26            if(d.month  != 0.0) { testValue = false; out << d.month  << "mo "; }
27            if(d.day    != 0.0) { testValue = false; out << d.day    << "d " ; }
28            if(d.hour   != 0.0) { testValue = false; out << d.hour   << "h " ; }
29            if(d.minute != 0.0) { testValue = false; out << d.minute << "mi "; }
30            if(d.second != 0.0 || testValue) { out << d.second << "s " ; }
31
32            _out << (out.str().substr(0, out.str().size()-1)); // << suppression de l'espace en fin de chaîne.
33            return (_out);
34         }
35
36         friend std::istream& operator>>(std::istream& in, struct _duration& d)
37         {
38            d.year = d.month = d.day = d.hour = d.minute = d.second = 0.0;
39            double v = 1.0;
40            char   c = '/';
41            while (!in.eof())
42            {
43               in >> v >> c;
44               switch (c)
45               {
46                  case 'y': d.year   = v; continue;
47                  case 'd': d.day    = v; continue;
48                  case 'h': d.hour   = v; continue;
49                  case 's': d.second = v; continue;
50                  case 'm':
51                     in >> c;
52                     if (c == 'i') d.minute   = v;
53                     else if(c == 'o') d.month   = v;
54                     else
55                     {
56                        string valc("m"); valc.append(1, c);
57                        WARNING("La chaine \""+valc+"\" ne permet pas de définir une unité de durée.");
58                        continue;
59                     }
60                     continue;
61                  default:
62                     string valc; valc.append(1, c);
63                     WARNING("La chaine \""+valc+"\" ne permet pas de définir une unité de durée.");
64                     continue;
65               }
66            }
67            return (in);
68         }
69
70         bool isNone(void) const
71         {
72            if ((year == 0) && (month  == 0) && (day    == 0) &&
73                (hour == 0) && (minute == 0) && (second == 0))
74               return (true);
75            return (false);
76         }
77
78         struct _duration& resolve(const AbstractCalendar&);
79
80         std::string toString(void) const
81         {
82            const struct _duration& own = *this;
83            std::ostringstream oss; oss << own;
84            return (oss.str());
85         }
86
87      public: /* static */
88
89         static struct _duration FromString(const string& str)
90         {
91            struct _duration dr = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
92            std::istringstream iss(str);
93            iss >> dr; return dr;
94         }
95
96         double year, month, day, hour, minute, second;
97
98   } Duration;
99
100   // Les principales durées constantes : année, mois, jour, ...
101   static const Duration Year   = {1.0, 0.0, 0.0, 0.0, 0.0, 0.0},
102                         Month  = {0.0, 1.0, 0.0, 0.0, 0.0, 0.0},
103                         Week   = {0.0, 0.0, 7.0, 0.0, 0.0, 0.0},
104                         Day    = {0.0, 0.0, 1.0, 0.0, 0.0, 0.0},
105                         Hour   = {0.0, 0.0, 0.0, 1.0, 0.0, 0.0},
106                         Minute = {0.0, 0.0, 0.0, 0.0, 1.0, 0.0},
107                         Second = {0.0, 0.0, 0.0, 0.0, 0.0, 1.0},
108                         NoneDu = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
109
110} // namespace XMLIOSERVER
111
112#endif // __XMLIO_DURATION__
Note: See TracBrowser for help on using the repository browser.