[118] | 1 | #ifndef __XMLIO_DURATION__ |
---|
| 2 | #define __XMLIO_DURATION__ |
---|
| 3 | |
---|
| 4 | namespace XMLIOSERVER |
---|
| 5 | { |
---|
| 6 | class AbstractCalendar; |
---|
| 7 | |
---|
| 8 | typedef long long int Time; |
---|
| 9 | |
---|
| 10 | typedef struct _duration |
---|
| 11 | { |
---|
[119] | 12 | public : |
---|
[118] | 13 | |
---|
[128] | 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 | |
---|
[130] | 21 | friend std::ostream& operator<<(std::ostream& _out, const struct _duration& d) |
---|
[119] | 22 | { |
---|
[130] | 23 | std::ostringstream out; |
---|
[119] | 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 " ; } |
---|
[130] | 31 | |
---|
| 32 | _out << (out.str().substr(0, out.str().size()-1)); // << suppression de l'espace en fin de chaîne. |
---|
| 33 | return (_out); |
---|
[119] | 34 | } |
---|
[118] | 35 | |
---|
[119] | 36 | friend std::istream& operator>>(std::istream& in, struct _duration& d) |
---|
| 37 | { |
---|
[123] | 38 | d.year = d.month = d.day = d.hour = d.minute = d.second = 0.0; |
---|
[119] | 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 | } |
---|
[118] | 69 | |
---|
[128] | 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; return (oss.str()); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | public: /* static */ |
---|
| 87 | |
---|
[119] | 88 | static struct _duration FromString(const string& str) |
---|
| 89 | { |
---|
| 90 | struct _duration dr = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; |
---|
| 91 | std::istringstream iss(str); |
---|
| 92 | iss >> dr; return dr; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | double year, month, day, hour, minute, second; |
---|
| 96 | |
---|
[118] | 97 | } Duration; |
---|
| 98 | |
---|
| 99 | // Les principales durées constantes : année, mois, jour, ... |
---|
| 100 | static const Duration Year = {1.0, 0.0, 0.0, 0.0, 0.0, 0.0}, |
---|
| 101 | Month = {0.0, 1.0, 0.0, 0.0, 0.0, 0.0}, |
---|
[119] | 102 | Week = {0.0, 0.0, 7.0, 0.0, 0.0, 0.0}, |
---|
[118] | 103 | Day = {0.0, 0.0, 1.0, 0.0, 0.0, 0.0}, |
---|
| 104 | Hour = {0.0, 0.0, 0.0, 1.0, 0.0, 0.0}, |
---|
| 105 | Minute = {0.0, 0.0, 0.0, 0.0, 1.0, 0.0}, |
---|
[123] | 106 | Second = {0.0, 0.0, 0.0, 0.0, 0.0, 1.0}, |
---|
| 107 | NoneDu = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; |
---|
[118] | 108 | |
---|
| 109 | } // namespace XMLIOSERVER |
---|
| 110 | |
---|
| 111 | #endif // __XMLIO_DURATION__ |
---|