Commit | Line | Data |
---|---|---|
e93545dd GE |
1 | /*************************************************************************** |
2 | timecvt.cpp - description | |
3 | ------------------- | |
4 | begin : Fri May 11 2001 | |
5 | copyright : (C) 2001 by STYLETEC | |
6 | email : info@styletec.de | |
7 | ***************************************************************************/ | |
8 | ||
9 | #include <string> | |
10 | #include <sstream> | |
11 | #include <iostream> | |
12 | #include <iomanip> | |
f1499910 GE |
13 | #include <bitset> |
14 | #include <stdexcept> | |
e93545dd GE |
15 | |
16 | #include <time.h> | |
17 | #include <sys/timeb.h> | |
18 | ||
19 | #include <timefunc.hxx> | |
20 | #include <i18n.h> | |
21 | ||
22 | using namespace std; | |
23 | ||
24 | double prec_time(void) | |
25 | { | |
26 | struct timeb tb; | |
27 | double ret; | |
28 | ||
29 | ftime(&tb); | |
30 | ||
31 | ret=tb.time+(static_cast<float>(tb.millitm)/1000); | |
32 | ||
33 | return ret; | |
34 | } | |
35 | ||
36 | // converts ISO-DATE: 2003-06-13 | |
37 | int date_to_seconds(const std::string &date) | |
38 | { | |
39 | int rtn = -1, year = -1, month = -1, day = -1; | |
40 | ||
41 | string::size_type pos = date.find("-"); | |
42 | if (pos == string::npos) | |
43 | return rtn; | |
44 | ||
45 | istringstream in(string(date,0,pos)); | |
46 | in >> year; | |
47 | year -= 1900; | |
48 | ||
49 | string dstr(date, pos+1); | |
50 | if ((pos = dstr.find("-")) == string::npos) | |
51 | return rtn; | |
52 | ||
53 | in.clear(); | |
54 | in.str(string(dstr, 0, pos)); | |
55 | in >> month; | |
56 | month -= 1; | |
57 | ||
58 | in.clear(); | |
59 | in.str(string(dstr, pos+1)); | |
60 | in >> day; | |
61 | ||
62 | if (year < 0 || month == -1 || day == -1) | |
63 | return rtn; | |
64 | ||
65 | struct tm tm_struct; | |
66 | bzero (&tm_struct, sizeof(struct tm)); | |
67 | tm_struct.tm_year = year; | |
68 | tm_struct.tm_mon = month; | |
69 | tm_struct.tm_mday = day; | |
70 | tm_struct.tm_isdst = -1; | |
71 | ||
72 | rtn = mktime (&tm_struct); | |
73 | return rtn; | |
74 | } | |
75 | ||
76 | string make_nice_time(int seconds) | |
77 | { | |
78 | ostringstream out; | |
79 | ||
80 | int days=seconds/86400; | |
81 | seconds%=86400; | |
82 | ||
83 | int hours=seconds/3600; | |
84 | seconds%=3600; | |
85 | ||
86 | int minutes=seconds/60; | |
87 | seconds%=60; | |
88 | ||
89 | if (days==1) | |
90 | out << i18n("1 day") << ", "; | |
91 | else if (days>1) | |
92 | out << days << ' ' << i18n("days") << ", "; | |
93 | ||
94 | out << setfill('0'); | |
95 | out << setw(2) << hours << ':' << setw(2) << minutes << ':' << setw(2) << seconds; | |
96 | ||
97 | return out.str(); | |
98 | } | |
99 | ||
100 | string format_full_time(int seconds) | |
101 | { | |
102 | char buf[50]; | |
103 | memset (buf, 0, 50); | |
104 | struct tm *ta = localtime ((time_t *)&seconds); | |
105 | ||
106 | strftime (buf, 49, "%d.%m.%Y %H:%M", ta); | |
107 | return string(buf); | |
108 | } | |
f1499910 | 109 | |
87869870 GE |
110 | void seconds_to_hour_minute(int seconds, int *hour, int *minute) |
111 | { | |
112 | if (hour != NULL) { | |
113 | *hour = 0; | |
114 | while (seconds >= 3600) { | |
115 | seconds-=3600; | |
116 | (*hour)++; | |
117 | } | |
118 | } | |
119 | ||
120 | if (minute != NULL) { | |
121 | *minute = 0; | |
122 | while (seconds >= 60) { | |
123 | seconds-=60; | |
124 | (*minute)++; | |
125 | } | |
126 | } | |
127 | } | |
128 | ||
1344894d | 129 | std::string output_hour_minute(int hour, int minute, bool h_for_00) |
2c66f490 GE |
130 | { |
131 | ostringstream out; | |
132 | ||
133 | if (hour >= 0 && hour < 10) | |
134 | out << '0'; | |
135 | out << hour; | |
136 | ||
1344894d | 137 | if (!h_for_00 || minute != 0) |
2c66f490 GE |
138 | { |
139 | out << ':'; | |
fe223928 | 140 | if (minute >= 0 && minute < 10) |
2c66f490 GE |
141 | out << '0'; |
142 | out << minute; | |
143 | } | |
144 | else | |
145 | out << 'h'; | |
146 | ||
147 | return out.str(); | |
148 | } | |
149 | ||
f1499910 GE |
150 | WEEK::WEEK(const std::string& daystring) |
151 | { | |
152 | int len=daystring.length(); | |
153 | for (int p=0; p < len; p++) | |
154 | { | |
865bdeef GE |
155 | char nr[2]; |
156 | nr[0]=daystring[p]; | |
157 | nr[1]=0; | |
158 | istringstream c(nr); | |
f1499910 GE |
159 | int wnr=-1; |
160 | if (!(c >> wnr) || wnr<0 || wnr >6) | |
865bdeef | 161 | throw range_error("illegal weekday >"+string(nr)+"< in "+daystring); |
f1499910 GE |
162 | |
163 | days.set(wnr); | |
164 | } | |
165 | } | |
166 | ||
167 | std::string WEEK::get_daystring() const | |
168 | { | |
169 | ostringstream out; | |
170 | for (int i = 0; i < 7; i++) | |
171 | if (days[i]) | |
172 | out << i; | |
173 | ||
174 | return out.str(); | |
175 | } | |
176 | ||
177 | std::string WEEK::get_displaystring() const | |
178 | { | |
179 | string weekdays_str; | |
180 | ||
181 | // From Monday to Saturday | |
182 | int j; | |
183 | for (int i = 1; i < 7; i++) | |
184 | { | |
185 | if (days[i]) | |
186 | { | |
187 | if (!weekdays_str.empty()) | |
188 | weekdays_str += ", "; | |
189 | ||
190 | weekdays_str += get_day_display(static_cast<WEEKDAY>(i)); | |
191 | ||
192 | // check if we can group two or more days | |
193 | j = i; | |
194 | while (days[j] && j < 7) | |
195 | j++; | |
196 | j--; | |
197 | ||
198 | // Sunday end of week? j -> 7 | |
199 | if (j-i > 0 && j == 6 && days[0]) | |
200 | j++; | |
201 | ||
202 | if (j-i > 1) | |
203 | { | |
204 | if (j == 7) | |
205 | weekdays_str += "-" + get_day_display(SU); | |
206 | else | |
207 | weekdays_str += "-" + get_day_display(static_cast<WEEKDAY>(j)); | |
208 | ||
209 | i = j; | |
210 | } | |
211 | } | |
212 | } | |
213 | ||
214 | // special: sunday | |
215 | if (days[0] && j != 7) | |
216 | { | |
217 | if (!weekdays_str.empty()) | |
218 | weekdays_str += ", "; | |
219 | ||
220 | weekdays_str += get_day_display(SU); | |
221 | } | |
222 | ||
223 | return weekdays_str; | |
224 | } | |
225 | ||
226 | std::string WEEK::get_netfilterstring() const | |
227 | { | |
228 | string out; | |
229 | for (int i = 0; i < 7; i++) | |
230 | if (days[i]) | |
231 | { | |
232 | if (!out.empty()) | |
233 | out+=","; | |
234 | out+=get_english_display(static_cast<WEEKDAY>(i));; | |
235 | } | |
236 | ||
237 | return out; | |
238 | } | |
239 | ||
240 | std::string WEEK::get_day_display(WEEKDAY day) | |
241 | { | |
242 | string weekday_str; | |
243 | ||
244 | switch (day) { | |
245 | case MO: | |
246 | weekday_str = i18n("Mon"); | |
247 | break; | |
248 | case TU: | |
249 | weekday_str = i18n("Tue"); | |
250 | break; | |
251 | case WE: | |
252 | weekday_str = i18n("Wed"); | |
253 | break; | |
254 | case TH: | |
255 | weekday_str = i18n("Thu"); | |
256 | break; | |
257 | case FR: | |
258 | weekday_str = i18n("Fri"); | |
259 | break; | |
260 | case SA: | |
261 | weekday_str = i18n("Sat"); | |
262 | break; | |
263 | case SU: | |
264 | weekday_str = i18n("Sun"); | |
265 | break; | |
266 | default: | |
267 | break; | |
268 | } | |
269 | ||
270 | return weekday_str; | |
271 | } | |
272 | ||
273 | std::string WEEK::get_english_display(WEEKDAY day) | |
274 | { | |
275 | string weekday_str; | |
276 | ||
277 | switch (day) { | |
278 | case MO: | |
279 | weekday_str = "Mon"; | |
280 | break; | |
281 | case TU: | |
282 | weekday_str = "Tue"; | |
283 | break; | |
284 | case WE: | |
285 | weekday_str = "Wed"; | |
286 | break; | |
287 | case TH: | |
288 | weekday_str = "Thu"; | |
289 | break; | |
290 | case FR: | |
291 | weekday_str = "Fri"; | |
292 | break; | |
293 | case SA: | |
294 | weekday_str = "Sat"; | |
295 | break; | |
296 | case SU: | |
297 | weekday_str = "Sun"; | |
298 | break; | |
299 | default: | |
300 | break; | |
301 | } | |
302 | ||
303 | return weekday_str; | |
304 | } | |
4e157d1d TJ |
305 | |
306 | string get_month_name(unsigned char month) | |
307 | { | |
308 | string rtn; | |
309 | switch(month) { | |
310 | case 1: | |
311 | rtn = i18n("January"); | |
312 | break; | |
313 | case 2: | |
314 | rtn = i18n("February"); | |
315 | break; | |
316 | case 3: | |
317 | rtn = i18n("March"); | |
318 | break; | |
319 | case 4: | |
320 | rtn = i18n("April"); | |
321 | break; | |
322 | case 5: | |
323 | rtn = i18n("May"); | |
324 | break; | |
325 | case 6: | |
326 | rtn = i18n("June"); | |
327 | break; | |
328 | case 7: | |
329 | rtn = i18n("July"); | |
330 | break; | |
331 | case 8: | |
332 | rtn = i18n("August"); | |
333 | break; | |
334 | case 9: | |
335 | rtn = i18n("September"); | |
336 | break; | |
337 | case 10: | |
338 | rtn = i18n("October"); | |
339 | break; | |
340 | case 11: | |
341 | rtn = i18n("November"); | |
342 | break; | |
343 | case 12: | |
344 | rtn = i18n("December"); | |
345 | break; | |
346 | default: | |
347 | { | |
348 | ostringstream out; | |
349 | out << i18n("Illegal month:") << " " << month; | |
350 | rtn = out.str(); | |
351 | } | |
352 | } | |
353 | ||
354 | return rtn; | |
355 | } |