libi2ncommon: (gerd) add WEEK class to handle weekday-bitsets
authorGerd v. Egidy <gerd.von.egidy@intra2net.com>
Tue, 31 Aug 2004 09:59:57 +0000 (09:59 +0000)
committerGerd v. Egidy <gerd.von.egidy@intra2net.com>
Tue, 31 Aug 2004 09:59:57 +0000 (09:59 +0000)
src/timefunc.cpp
src/timefunc.hxx

index 155411a..30c56ac 100644 (file)
@@ -10,6 +10,8 @@
 #include <sstream>
 #include <iostream>
 #include <iomanip>
+#include <bitset>
+#include <stdexcept>
 
 #include <time.h>
 #include <sys/timeb.h>
@@ -104,3 +106,157 @@ string format_full_time(int seconds)
     strftime (buf, 49, "%d.%m.%Y %H:%M", ta);
     return string(buf);
 }
+
+WEEK::WEEK(const std::string& daystring)
+{
+    int len=daystring.length();
+    for (int p=0; p < len; p++)
+    {
+        char nr=daystring[p];
+        istringstream c(&nr);
+        int wnr=-1;
+        if (!(c >> wnr) || wnr<0 || wnr >6)
+            throw out_of_range("illegal weekday in "+daystring);
+            
+        days.set(wnr);
+    }
+}
+
+std::string WEEK::get_daystring() const
+{
+    ostringstream out;
+    for (int i = 0; i < 7; i++)
+        if (days[i])
+            out << i;
+
+    return out.str();
+}
+
+std::string WEEK::get_displaystring() const
+{
+    string weekdays_str;
+
+    // From Monday to Saturday
+    int j;
+    for (int i = 1; i < 7; i++)
+    {
+        if (days[i])
+        {
+            if (!weekdays_str.empty())
+                weekdays_str += ", ";
+
+            weekdays_str += get_day_display(static_cast<WEEKDAY>(i));
+
+            // check if we can group two or more days
+            j = i;
+            while (days[j] && j < 7)
+                j++;
+            j--;
+
+            // Sunday end of week? j -> 7
+            if (j-i > 0 && j == 6 && days[0])
+                j++;
+
+            if (j-i > 1) 
+            {
+                if (j == 7)
+                    weekdays_str += "-" + get_day_display(SU);
+                else
+                    weekdays_str += "-" + get_day_display(static_cast<WEEKDAY>(j));
+
+                i = j;
+            }
+        }
+    }
+
+    // special: sunday
+    if (days[0] && j != 7) 
+    {
+        if (!weekdays_str.empty())
+            weekdays_str += ", ";
+
+        weekdays_str += get_day_display(SU);
+    }
+
+    return weekdays_str;
+}
+
+std::string WEEK::get_netfilterstring() const
+{
+    string out;
+    for (int i = 0; i < 7; i++)
+        if (days[i])
+        {
+            if (!out.empty())
+                out+=","; 
+            out+=get_english_display(static_cast<WEEKDAY>(i));;
+        }
+            
+    return out;
+}
+
+std::string WEEK::get_day_display(WEEKDAY day)
+{
+    string weekday_str;
+
+    switch (day) {
+        case MO:
+            weekday_str = i18n("Mon");
+            break;
+        case TU:
+            weekday_str = i18n("Tue");
+            break;
+        case WE:
+            weekday_str = i18n("Wed");
+            break;
+        case TH:
+            weekday_str = i18n("Thu");
+            break;
+        case FR:
+            weekday_str = i18n("Fri");
+            break;
+        case SA:
+            weekday_str = i18n("Sat");
+            break;
+        case SU:
+            weekday_str = i18n("Sun");
+            break;
+        default:
+            break;
+    }
+
+    return weekday_str;
+}
+
+std::string WEEK::get_english_display(WEEKDAY day)
+{
+    string weekday_str;
+
+    switch (day) {
+        case MO:
+            weekday_str = "Mon";
+            break;
+        case TU:
+            weekday_str = "Tue";
+            break;
+        case WE:
+            weekday_str = "Wed";
+            break;
+        case TH:
+            weekday_str = "Thu";
+            break;
+        case FR:
+            weekday_str = "Fri";
+            break;
+        case SA:
+            weekday_str = "Sat";
+            break;
+        case SU:
+            weekday_str = "Sun";
+            break;
+        default:
+            break;
+    }
+
+    return weekday_str;
+}
index 97e1b15..88a5d0d 100644 (file)
@@ -9,6 +9,8 @@
 #ifndef __TIMEFUNC_HXX
 #define __TIMEFUNC_HXX
 
+#include <bitset>
+
 double prec_time(void);
 
 int date_to_seconds(const std::string &date);
@@ -16,4 +18,33 @@ int date_to_seconds(const std::string &date);
 std::string make_nice_time(int seconds);
 std::string format_full_time(int seconds);
 
+class WEEK
+{
+    private:
+        std::bitset<7> days;
+
+    public:
+        enum WEEKDAY { SU=0, MO=1, TU=2, WE=3, TH=4, FR=5, SA=6 };
+        
+        // throws out_of_range if illegal week
+        WEEK(const std::string& daystring);
+        
+        WEEK(const std::bitset<7> &days)
+            : days(days)
+            { }
+            
+        void clear()
+            { days.reset(); }
+            
+        operator std::bitset<7>() const
+            { return days; }
+        
+        std::string get_daystring() const;
+        std::string get_displaystring() const;
+        std::string get_netfilterstring() const;
+        
+        static std::string get_day_display(WEEKDAY day);
+        static std::string get_english_display(WEEKDAY day);
+};
+
 #endif