libi2ncommon: (gerd) add seconds_to_hour_minute function (from ui)
[libi2ncommon] / src / timefunc.cpp
CommitLineData
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
22using namespace std;
23
24double 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
37int 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
76string 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
100string 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
110void 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
f1499910
GE
129WEEK::WEEK(const std::string& daystring)
130{
131 int len=daystring.length();
132 for (int p=0; p < len; p++)
133 {
134 char nr=daystring[p];
135 istringstream c(&nr);
136 int wnr=-1;
137 if (!(c >> wnr) || wnr<0 || wnr >6)
138 throw out_of_range("illegal weekday in "+daystring);
139
140 days.set(wnr);
141 }
142}
143
144std::string WEEK::get_daystring() const
145{
146 ostringstream out;
147 for (int i = 0; i < 7; i++)
148 if (days[i])
149 out << i;
150
151 return out.str();
152}
153
154std::string WEEK::get_displaystring() const
155{
156 string weekdays_str;
157
158 // From Monday to Saturday
159 int j;
160 for (int i = 1; i < 7; i++)
161 {
162 if (days[i])
163 {
164 if (!weekdays_str.empty())
165 weekdays_str += ", ";
166
167 weekdays_str += get_day_display(static_cast<WEEKDAY>(i));
168
169 // check if we can group two or more days
170 j = i;
171 while (days[j] && j < 7)
172 j++;
173 j--;
174
175 // Sunday end of week? j -> 7
176 if (j-i > 0 && j == 6 && days[0])
177 j++;
178
179 if (j-i > 1)
180 {
181 if (j == 7)
182 weekdays_str += "-" + get_day_display(SU);
183 else
184 weekdays_str += "-" + get_day_display(static_cast<WEEKDAY>(j));
185
186 i = j;
187 }
188 }
189 }
190
191 // special: sunday
192 if (days[0] && j != 7)
193 {
194 if (!weekdays_str.empty())
195 weekdays_str += ", ";
196
197 weekdays_str += get_day_display(SU);
198 }
199
200 return weekdays_str;
201}
202
203std::string WEEK::get_netfilterstring() const
204{
205 string out;
206 for (int i = 0; i < 7; i++)
207 if (days[i])
208 {
209 if (!out.empty())
210 out+=",";
211 out+=get_english_display(static_cast<WEEKDAY>(i));;
212 }
213
214 return out;
215}
216
217std::string WEEK::get_day_display(WEEKDAY day)
218{
219 string weekday_str;
220
221 switch (day) {
222 case MO:
223 weekday_str = i18n("Mon");
224 break;
225 case TU:
226 weekday_str = i18n("Tue");
227 break;
228 case WE:
229 weekday_str = i18n("Wed");
230 break;
231 case TH:
232 weekday_str = i18n("Thu");
233 break;
234 case FR:
235 weekday_str = i18n("Fri");
236 break;
237 case SA:
238 weekday_str = i18n("Sat");
239 break;
240 case SU:
241 weekday_str = i18n("Sun");
242 break;
243 default:
244 break;
245 }
246
247 return weekday_str;
248}
249
250std::string WEEK::get_english_display(WEEKDAY day)
251{
252 string weekday_str;
253
254 switch (day) {
255 case MO:
256 weekday_str = "Mon";
257 break;
258 case TU:
259 weekday_str = "Tue";
260 break;
261 case WE:
262 weekday_str = "Wed";
263 break;
264 case TH:
265 weekday_str = "Thu";
266 break;
267 case FR:
268 weekday_str = "Fri";
269 break;
270 case SA:
271 weekday_str = "Sat";
272 break;
273 case SU:
274 weekday_str = "Sun";
275 break;
276 default:
277 break;
278 }
279
280 return weekday_str;
281}