comparison base/RealTime.cpp @ 439:beb2948baa77

* Merge revisions 1041 to 1130 from sv-rdf-import branch
author Chris Cannam
date Thu, 18 Sep 2008 12:09:32 +0000
parents 72ec275e458b
children 81963c51b488
comparison
equal deleted inserted replaced
438:32c399d06374 439:beb2948baa77
77 RealTime::fromTimeval(const struct timeval &tv) 77 RealTime::fromTimeval(const struct timeval &tv)
78 { 78 {
79 return RealTime(tv.tv_sec, tv.tv_usec * 1000); 79 return RealTime(tv.tv_sec, tv.tv_usec * 1000);
80 } 80 }
81 81
82 RealTime
83 RealTime::fromXsdDuration(std::string xsdd)
84 {
85 RealTime t;
86
87 int year = 0, month = 0, day = 0, hour = 0, minute = 0;
88 double second = 0.0;
89
90 int i = 0;
91
92 const char *s = xsdd.c_str();
93 int len = xsdd.length();
94
95 bool negative = false, afterT = false;
96
97 int valstart = 0;
98
99 while (i < len) {
100
101 if (s[i] == '-') {
102 if (i == 0) negative = true;
103 ++i;
104 continue;
105 }
106
107 double value = 0.0;
108 char *eptr = 0;
109
110 if (isdigit(s[i]) || s[i] == '.') {
111 valstart = i;
112 value = strtod(&s[i], &eptr);
113 i = eptr - s;
114 }
115
116 if (i == len) break;
117
118 switch (s[i]) {
119 case 'Y': year = int(value + 0.1); break;
120 case 'D': day = int(value + 0.1); break;
121 case 'H': hour = int(value + 0.1); break;
122 case 'M':
123 if (afterT) minute = int(value + 0.1);
124 else month = int(value + 0.1);
125 break;
126 case 'S':
127 second = value;
128 break;
129 case 'T': afterT = true; break;
130 };
131
132 ++i;
133 }
134
135 if (year > 0) {
136 std::cerr << "WARNING: This xsd:duration (\"" << xsdd << "\") contains a non-zero year.\nWith no origin and a limited data size, I will treat a year as exactly 31556952\nseconds and you should expect overflow and/or poor results." << std::endl;
137 t = t + RealTime(year * 31556952, 0);
138 }
139
140 if (month > 0) {
141 std::cerr << "WARNING: This xsd:duration (\"" << xsdd << "\") contains a non-zero month.\nWith no origin and a limited data size, I will treat a month as exactly 2629746\nseconds and you should expect overflow and/or poor results." << std::endl;
142 t = t + RealTime(month * 2629746, 0);
143 }
144
145 if (day > 0) {
146 t = t + RealTime(day * 86400, 0);
147 }
148
149 if (hour > 0) {
150 t = t + RealTime(hour * 3600, 0);
151 }
152
153 if (minute > 0) {
154 t = t + RealTime(minute * 60, 0);
155 }
156
157 t = t + fromSeconds(second);
158
159 return t;
160 }
161
162 double
163 RealTime::toDouble() const
164 {
165 double d = sec;
166 d += double(nsec) / double(ONE_BILLION);
167 return d;
168 }
169
82 std::ostream &operator<<(std::ostream &out, const RealTime &rt) 170 std::ostream &operator<<(std::ostream &out, const RealTime &rt)
83 { 171 {
84 if (rt < RealTime::zeroTime) { 172 if (rt < RealTime::zeroTime) {
85 out << "-"; 173 out << "-";
86 } else { 174 } else {
126 214
127 RealTime 215 RealTime
128 RealTime::fromString(std::string s) 216 RealTime::fromString(std::string s)
129 { 217 {
130 bool negative = false; 218 bool negative = false;
131 bool faulty = false;
132 bool section = 0; 219 bool section = 0;
133 std::string ssec, snsec; 220 std::string ssec, snsec;
134 221
135 for (size_t i = 0; i < s.length(); ++i) { 222 for (size_t i = 0; i < s.length(); ++i) {
136 223