1 /***************************************************************************
2 timecvt.cpp - description
4 begin : Fri May 11 2001
5 copyright : (C) 2001 by STYLETEC
6 email : info@styletec.de
7 ***************************************************************************/
17 #include <sys/timeb.h>
19 #include <timefunc.hxx>
24 double prec_time(void)
31 ret=tb.time+(static_cast<float>(tb.millitm)/1000);
36 // converts ISO-DATE: 2003-06-13
37 int date_to_seconds(const std::string &date)
39 int rtn = -1, year = -1, month = -1, day = -1;
41 string::size_type pos = date.find("-");
42 if (pos == string::npos)
45 istringstream in(string(date,0,pos));
49 string dstr(date, pos+1);
50 if ((pos = dstr.find("-")) == string::npos)
54 in.str(string(dstr, 0, pos));
59 in.str(string(dstr, pos+1));
62 if (year < 0 || month == -1 || day == -1)
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;
72 rtn = mktime (&tm_struct);
76 string make_nice_time(int seconds)
80 int days=seconds/86400;
83 int hours=seconds/3600;
86 int minutes=seconds/60;
90 out << i18n("1 day") << ", ";
92 out << days << ' ' << i18n("days") << ", ";
95 out << setw(2) << hours << ':' << setw(2) << minutes << ':' << setw(2) << seconds;
100 string format_full_time(int seconds)
104 struct tm *ta = localtime ((time_t *)&seconds);
106 strftime (buf, 49, "%d.%m.%Y %H:%M", ta);
110 void seconds_to_hour_minute(int seconds, int *hour, int *minute)
114 while (seconds >= 3600) {
120 if (minute != NULL) {
122 while (seconds >= 60) {
129 std::string output_hour_minute(int hour, int minute, bool h_for_00)
133 if (hour >= 0 && hour < 10)
137 if (!h_for_00 || minute != 0)
140 if (minute > 0 && minute < 10)
150 WEEK::WEEK(const std::string& daystring)
152 int len=daystring.length();
153 for (int p=0; p < len; p++)
155 char nr=daystring[p];
156 istringstream c(&nr);
158 if (!(c >> wnr) || wnr<0 || wnr >6)
159 throw out_of_range("illegal weekday in "+daystring);
165 std::string WEEK::get_daystring() const
168 for (int i = 0; i < 7; i++)
175 std::string WEEK::get_displaystring() const
179 // From Monday to Saturday
181 for (int i = 1; i < 7; i++)
185 if (!weekdays_str.empty())
186 weekdays_str += ", ";
188 weekdays_str += get_day_display(static_cast<WEEKDAY>(i));
190 // check if we can group two or more days
192 while (days[j] && j < 7)
196 // Sunday end of week? j -> 7
197 if (j-i > 0 && j == 6 && days[0])
203 weekdays_str += "-" + get_day_display(SU);
205 weekdays_str += "-" + get_day_display(static_cast<WEEKDAY>(j));
213 if (days[0] && j != 7)
215 if (!weekdays_str.empty())
216 weekdays_str += ", ";
218 weekdays_str += get_day_display(SU);
224 std::string WEEK::get_netfilterstring() const
227 for (int i = 0; i < 7; i++)
232 out+=get_english_display(static_cast<WEEKDAY>(i));;
238 std::string WEEK::get_day_display(WEEKDAY day)
244 weekday_str = i18n("Mon");
247 weekday_str = i18n("Tue");
250 weekday_str = i18n("Wed");
253 weekday_str = i18n("Thu");
256 weekday_str = i18n("Fri");
259 weekday_str = i18n("Sat");
262 weekday_str = i18n("Sun");
271 std::string WEEK::get_english_display(WEEKDAY day)